|
| 1 | +/** |
| 2 | + * Copyright (C) 2010-2015 eBusiness Information, Excilys Group |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed To in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | +package org.androidannotations.helper; |
| 17 | + |
| 18 | +import static org.junit.Assert.assertFalse; |
| 19 | +import static org.junit.Assert.assertTrue; |
| 20 | +import static org.mockito.Matchers.any; |
| 21 | +import static org.mockito.Mockito.doAnswer; |
| 22 | +import static org.mockito.Mockito.doReturn; |
| 23 | +import static org.mockito.Mockito.mock; |
| 24 | + |
| 25 | +import java.lang.annotation.Annotation; |
| 26 | +import java.util.ArrayList; |
| 27 | +import java.util.List; |
| 28 | + |
| 29 | +import javax.lang.model.element.ExecutableElement; |
| 30 | +import javax.lang.model.element.TypeElement; |
| 31 | +import javax.lang.model.element.VariableElement; |
| 32 | +import javax.lang.model.type.TypeKind; |
| 33 | +import javax.lang.model.type.TypeMirror; |
| 34 | + |
| 35 | +import org.androidannotations.ElementValidation; |
| 36 | +import org.junit.Before; |
| 37 | +import org.junit.Test; |
| 38 | +import org.mockito.invocation.InvocationOnMock; |
| 39 | +import org.mockito.stubbing.Answer; |
| 40 | + |
| 41 | +import android.os.Bundle; |
| 42 | +import android.widget.AdapterView; |
| 43 | +import android.widget.TextView; |
| 44 | + |
| 45 | +public class ValidatorParameterHelperTest { |
| 46 | + |
| 47 | + private ValidatorParameterHelper validator; |
| 48 | + |
| 49 | + @Before |
| 50 | + public void setup() { |
| 51 | + TargetAnnotationHelper targetAnnotationHelper = mock(TargetAnnotationHelper.class); |
| 52 | + |
| 53 | + doAnswer(new IsSubtypeAnswer()).when(targetAnnotationHelper).isSubtype(any(TypeMirror.class), any(TypeMirror.class)); |
| 54 | + doAnswer(new TypeElementFromQualifiedNameAnswer()).when(targetAnnotationHelper).typeElementFromQualifiedName(any(String.class)); |
| 55 | + |
| 56 | + validator = new ValidatorParameterHelper(targetAnnotationHelper); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void anyOfTypes() throws Exception { |
| 61 | + ExecutableElement executableElement = createMethod(Integer.class, Long.class); |
| 62 | + |
| 63 | + ElementValidation valid = new ElementValidation("", executableElement); |
| 64 | + validator.anyOfTypes(CanonicalNameConstants.INTEGER, CanonicalNameConstants.LONG).multiple().validate(executableElement, valid); |
| 65 | + assertTrue(valid.isValid()); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void extendsViewType() throws Exception { |
| 70 | + ExecutableElement executableElement = createMethod(TextView.class); |
| 71 | + |
| 72 | + ElementValidation valid = new ElementValidation("", executableElement); |
| 73 | + validator.extendsType(CanonicalNameConstants.VIEW).validate(executableElement, valid); |
| 74 | + assertTrue(valid.isValid()); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void primitiveParam() throws Exception { |
| 79 | + ExecutableElement executableElement = createMethod(int.class, Integer.class); |
| 80 | + |
| 81 | + ElementValidation valid = new ElementValidation("", executableElement); |
| 82 | + validator.primitiveOrWrapper(TypeKind.INT).multiple().validate(executableElement, valid); |
| 83 | + assertTrue(valid.isValid()); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void annotatedParam() throws Exception { |
| 88 | + ExecutableElement executableElement = createMethod(p(String.class, TestAnnotation.class)); |
| 89 | + |
| 90 | + ElementValidation valid = new ElementValidation("", executableElement); |
| 91 | + validator.annotatedWith(TestAnnotation.class).validate(executableElement, valid); |
| 92 | + assertTrue(valid.isValid()); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + public void optionalStringParam() throws Exception { |
| 97 | + ExecutableElement executableElement = createMethod(String.class); |
| 98 | + |
| 99 | + ElementValidation valid = new ElementValidation("", executableElement); |
| 100 | + validator.type(CanonicalNameConstants.STRING).optional().validate(executableElement, valid); |
| 101 | + assertTrue(valid.isValid()); |
| 102 | + |
| 103 | + ExecutableElement withoutArgument = createMethod(); |
| 104 | + |
| 105 | + ElementValidation valid2 = new ElementValidation("", executableElement); |
| 106 | + validator.type(CanonicalNameConstants.STRING).optional().validate(withoutArgument, valid2); |
| 107 | + assertTrue(valid2.isValid()); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + public void stringParam() throws Exception { |
| 112 | + ExecutableElement executableElement = createMethod(String.class); |
| 113 | + |
| 114 | + ElementValidation valid = new ElementValidation("", executableElement); |
| 115 | + validator.type(CanonicalNameConstants.STRING).validate(executableElement, valid); |
| 116 | + assertTrue(valid.isValid()); |
| 117 | + |
| 118 | + executableElement = createMethod(int.class); |
| 119 | + valid = new ElementValidation("", executableElement); |
| 120 | + validator.noParam().validate(executableElement, valid); |
| 121 | + assertFalse(valid.isValid()); |
| 122 | + } |
| 123 | + |
| 124 | + @Test |
| 125 | + public void anyParam() throws Exception { |
| 126 | + ExecutableElement executableElement = createMethod(String.class); |
| 127 | + |
| 128 | + ElementValidation valid = new ElementValidation("", executableElement); |
| 129 | + validator.anyType().validate(executableElement, valid); |
| 130 | + assertTrue(valid.isValid()); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + public void noParam() throws Exception { |
| 135 | + ExecutableElement executableElement = createMethod(); |
| 136 | + |
| 137 | + ElementValidation valid = new ElementValidation("", executableElement); |
| 138 | + validator.noParam().validate(executableElement, valid); |
| 139 | + assertTrue(valid.isValid()); |
| 140 | + |
| 141 | + executableElement = createMethod(int.class); |
| 142 | + valid = new ElementValidation("", executableElement); |
| 143 | + validator.noParam().validate(executableElement, valid); |
| 144 | + assertFalse(valid.isValid()); |
| 145 | + } |
| 146 | + |
| 147 | + @Test |
| 148 | + public void inOrderSuccess() throws Exception { |
| 149 | + ExecutableElement executableElement = createMethod(boolean.class, int.class); |
| 150 | + |
| 151 | + ElementValidation valid = new ElementValidation("", executableElement); |
| 152 | + validator.inOrder() // |
| 153 | + .type(boolean.class.getName()) // |
| 154 | + .type(long.class.getName()).optional() // |
| 155 | + .type(int.class.getName()) // |
| 156 | + .validate(executableElement, valid); |
| 157 | + assertTrue(valid.isValid()); |
| 158 | + } |
| 159 | + |
| 160 | + @Test |
| 161 | + public void inOrderSuccess2() throws Exception { |
| 162 | + ExecutableElement executableElement = createMethod(boolean.class, boolean.class, int.class); |
| 163 | + |
| 164 | + ElementValidation valid = new ElementValidation("", executableElement); |
| 165 | + validator.inOrder() // |
| 166 | + .type(boolean.class.getName()).multiple() // |
| 167 | + .type(long.class.getName()).optional() // |
| 168 | + .type(int.class.getName()) // |
| 169 | + .validate(executableElement, valid); |
| 170 | + assertTrue(valid.isValid()); |
| 171 | + } |
| 172 | + |
| 173 | + @Test |
| 174 | + public void inOrderFail() throws Exception { |
| 175 | + ExecutableElement executableElement = createMethod(AdapterView.class, long.class, boolean.class, Bundle.class); |
| 176 | + |
| 177 | + ElementValidation valid = new ElementValidation("", executableElement); |
| 178 | + validator.inOrder() // |
| 179 | + .type(boolean.class.getName()).optional() // |
| 180 | + .type(long.class.getName()).optional() // |
| 181 | + .type(int.class.getName()).optional() // |
| 182 | + .validate(executableElement, valid); |
| 183 | + assertFalse(valid.isValid()); |
| 184 | + } |
| 185 | + |
| 186 | + @Test |
| 187 | + public void inOrderFail2() throws Exception { |
| 188 | + ExecutableElement executableElement = createMethod(long.class, boolean.class); |
| 189 | + |
| 190 | + ElementValidation valid = new ElementValidation("", executableElement); |
| 191 | + validator.inOrder() // |
| 192 | + .type(boolean.class.getName()).multiple() // |
| 193 | + .type(long.class.getName()).optional() // |
| 194 | + .type(int.class.getName()).optional() // |
| 195 | + .validate(executableElement, valid); |
| 196 | + assertFalse(valid.isValid()); |
| 197 | + } |
| 198 | + |
| 199 | + @Test |
| 200 | + public void inOrderFail3() throws Exception { |
| 201 | + ExecutableElement executableElement = createMethod(boolean.class, boolean.class, long.class, Bundle.class); |
| 202 | + |
| 203 | + ElementValidation valid = new ElementValidation("", executableElement); |
| 204 | + validator.inOrder() // |
| 205 | + .type(boolean.class.getName()).multiple() // |
| 206 | + .type(long.class.getName()).optional() // |
| 207 | + .type(int.class.getName()).optional() // |
| 208 | + .validate(executableElement, valid); |
| 209 | + assertFalse(valid.isValid()); |
| 210 | + } |
| 211 | + |
| 212 | + @Test |
| 213 | + public void inOrderFail4() throws Exception { |
| 214 | + ExecutableElement executableElement = createMethod(int.class, long.class, boolean.class); |
| 215 | + |
| 216 | + ElementValidation valid = new ElementValidation("", executableElement); |
| 217 | + validator.inOrder() // |
| 218 | + .type(boolean.class.getName()).optional() // |
| 219 | + .type(long.class.getName()).optional() // |
| 220 | + .type(int.class.getName()).optional() // |
| 221 | + .validate(executableElement, valid); |
| 222 | + assertFalse(valid.isValid()); |
| 223 | + } |
| 224 | + |
| 225 | + @Test |
| 226 | + public void itemSelect() { |
| 227 | + ExecutableElement executableElement = createMethod(boolean.class, int.class); |
| 228 | + |
| 229 | + ElementValidation valid = new ElementValidation("", executableElement); |
| 230 | + validator.inOrder() // |
| 231 | + .primitiveOrWrapper(TypeKind.BOOLEAN) // |
| 232 | + .anyType().optional() // |
| 233 | + .validate(executableElement, valid); |
| 234 | + assertTrue(valid.isValid()); |
| 235 | + } |
| 236 | + |
| 237 | + private static ExecutableElement createMethod(Class<?> firstParamClass, Class<?>... paramClasses) { |
| 238 | + Param[] params = new Param[paramClasses.length + 1]; |
| 239 | + params[0] = new Param(firstParamClass, null); |
| 240 | + for (int i = 0; i < paramClasses.length; i++) { |
| 241 | + params[i + 1] = new Param(paramClasses[i], null); |
| 242 | + } |
| 243 | + return createMethod(params); |
| 244 | + } |
| 245 | + |
| 246 | + private static ExecutableElement createMethod(Param... params) { |
| 247 | + List<VariableElement> paramList = new ArrayList<>(); |
| 248 | + for (Param param : params) { |
| 249 | + VariableElement variableElement = mock(VariableElement.class); |
| 250 | + doReturn(param.type.getName()).when(variableElement).toString(); |
| 251 | + if (param.annotationType != null) { |
| 252 | + doReturn(mock(param.annotationType)).when(variableElement).getAnnotation(param.annotationType); |
| 253 | + } |
| 254 | + |
| 255 | + doReturn(mockTypeMirror(param.type)).when(variableElement).asType(); |
| 256 | + |
| 257 | + paramList.add(variableElement); |
| 258 | + } |
| 259 | + |
| 260 | + ExecutableElement executableElement = mock(ExecutableElement.class); |
| 261 | + doReturn(paramList).when(executableElement).getParameters(); |
| 262 | + return executableElement; |
| 263 | + } |
| 264 | + |
| 265 | + private static Param p(Class<?> type, Class<? extends Annotation> annotationType) { |
| 266 | + return new Param(type, annotationType); |
| 267 | + } |
| 268 | + |
| 269 | + private static TypeMirror mockTypeMirror(Class<?> type) { |
| 270 | + ClassAwareTypeMirror typeMirror = mock(ClassAwareTypeMirror.class); |
| 271 | + doReturn(type.getCanonicalName()).when(typeMirror).toString(); |
| 272 | + doReturn(getKindOfType(type)).when(typeMirror).getKind(); |
| 273 | + doReturn(type).when(typeMirror).getMirrorClass(); |
| 274 | + return typeMirror; |
| 275 | + } |
| 276 | + |
| 277 | + private static TypeKind getKindOfType(Class<?> type) { |
| 278 | + if (!type.isPrimitive()) { |
| 279 | + return TypeKind.DECLARED; |
| 280 | + } |
| 281 | + return TypeKind.valueOf(type.getName().toUpperCase()); |
| 282 | + } |
| 283 | + |
| 284 | + private @interface TestAnnotation { |
| 285 | + } |
| 286 | + |
| 287 | + private interface ClassAwareTypeMirror extends TypeMirror { |
| 288 | + Class<?> getMirrorClass(); |
| 289 | + } |
| 290 | + |
| 291 | + private static final class IsSubtypeAnswer implements Answer<Object> { |
| 292 | + @Override |
| 293 | + public Boolean answer(InvocationOnMock invocation) throws Throwable { |
| 294 | + Object[] args = invocation.getArguments(); |
| 295 | + ensureValidArgs(args); |
| 296 | + |
| 297 | + Class<?> subClass = ((ClassAwareTypeMirror) args[0]).getClass(); |
| 298 | + Class<?> superClass = ((ClassAwareTypeMirror) args[1]).getClass(); |
| 299 | + |
| 300 | + return superClass.isAssignableFrom(subClass); |
| 301 | + } |
| 302 | + |
| 303 | + private void ensureValidArgs(Object[] args) { |
| 304 | + if (args.length != 2) { |
| 305 | + throw new IllegalArgumentException("invalid argument count"); |
| 306 | + } |
| 307 | + if (!(args[0] instanceof ClassAwareTypeMirror)) { |
| 308 | + throw new IllegalArgumentException("first argument has to be an instance of " + ClassAwareTypeMirror.class); |
| 309 | + } |
| 310 | + if (!(args[1] instanceof ClassAwareTypeMirror)) { |
| 311 | + throw new IllegalArgumentException("second argument has to be an instance of " + ClassAwareTypeMirror.class); |
| 312 | + } |
| 313 | + } |
| 314 | + } |
| 315 | + |
| 316 | + private static final class TypeElementFromQualifiedNameAnswer implements Answer<TypeElement> { |
| 317 | + @Override |
| 318 | + public TypeElement answer(InvocationOnMock invocation) throws Throwable { |
| 319 | + Object[] args = invocation.getArguments(); |
| 320 | + ensureValidArgs(args); |
| 321 | + |
| 322 | + TypeElement typeElement = mock(TypeElement.class); |
| 323 | + Class<?> typeClass = Class.forName((String) args[0]); |
| 324 | + doReturn(mockTypeMirror(typeClass)).when(typeElement).asType(); |
| 325 | + |
| 326 | + return typeElement; |
| 327 | + } |
| 328 | + |
| 329 | + private void ensureValidArgs(Object[] args) { |
| 330 | + if (args.length != 1) { |
| 331 | + throw new IllegalArgumentException("invalid argument count"); |
| 332 | + } |
| 333 | + if (!(args[0] instanceof String)) { |
| 334 | + throw new IllegalArgumentException("first argument has to be an instance of " + String.class); |
| 335 | + } |
| 336 | + } |
| 337 | + } |
| 338 | + |
| 339 | + private static final class Param { |
| 340 | + final Class<?> type; |
| 341 | + final Class<? extends Annotation> annotationType; |
| 342 | + |
| 343 | + Param(Class<?> type, Class<? extends Annotation> annotationType) { |
| 344 | + this.type = type; |
| 345 | + this.annotationType = annotationType; |
| 346 | + } |
| 347 | + } |
| 348 | +} |
0 commit comments