Permalink
Please sign in to comment.
Browse files
Add ability to set different uris for insert, update and delete
- Loading branch information...
Showing
with
693 additions
and 25 deletions.
- +65 −0 ...pushtorefresh/storio/common/annotations/processor/introspection/AnnotatedFieldValidationTest.java
- +78 −0 .../com/pushtorefresh/storio/common/annotations/processor/introspection/AnnotationProcessorStub.java
- +54 −7 ...om/pushtorefresh/storio/contentresolver/annotations/processor/StorIOContentResolverProcessor.java
- +6 −1 .../pushtorefresh/storio/contentresolver/annotations/processor/generate/DeleteResolverGenerator.java
- +12 −2 ...com/pushtorefresh/storio/contentresolver/annotations/processor/generate/PutResolverGenerator.java
- +73 −0 .../com/pushtorefresh/storio/contentresolver/annotations/processor/ContentResolverProcessorStub.java
- +61 −0 ...c/test/java/com/pushtorefresh/storio/contentresolver/annotations/processor/UriValidationTest.java
- +118 −6 ...htorefresh/storio/contentresolver/annotations/processor/generate/DeleteResolverGeneratorTest.java
- +199 −7 ...pushtorefresh/storio/contentresolver/annotations/processor/generate/PutResolverGeneratorTest.java
- +27 −2 ...src/main/java/com/pushtorefresh/storio/contentresolver/annotations/StorIOContentResolverType.java
65
...fresh/storio/common/annotations/processor/introspection/AnnotatedFieldValidationTest.java
@@ -0,0 +1,65 @@ | ||
+package com.pushtorefresh.storio.common.annotations.processor.introspection; | ||
+ | ||
+import com.pushtorefresh.storio.common.annotations.processor.ProcessingException; | ||
+ | ||
+import org.junit.Rule; | ||
+import org.junit.Test; | ||
+import org.junit.rules.ExpectedException; | ||
+ | ||
+import java.util.HashSet; | ||
+import java.util.Set; | ||
+ | ||
+import javax.lang.model.element.Modifier; | ||
+ | ||
+import static javax.lang.model.element.ElementKind.METHOD; | ||
+import static org.mockito.Mockito.when; | ||
+ | ||
+public class AnnotatedFieldValidationTest { | ||
+ | ||
+ @Rule | ||
+ public ExpectedException expectedException = ExpectedException.none(); | ||
+ | ||
+ @Test | ||
+ public void failIfEnclosingElementIsNotClass() { | ||
+ AnnotationProcessorStub stub = AnnotationProcessorStub.newInstance(); | ||
+ when(stub.enclosingElement.getKind()).thenReturn(METHOD); | ||
+ | ||
+ expectedException.expect(ProcessingException.class); | ||
+ expectedException.expectMessage("Please apply TestClassAnnotation to fields of class: TestField"); | ||
+ stub.processor.validateAnnotatedField(stub.field); | ||
+ } | ||
+ | ||
+ @Test | ||
+ public void failIfThereIsNoAnnotation() { | ||
+ AnnotationProcessorStub stub = AnnotationProcessorStub.newInstance(); | ||
+ when(stub.enclosingElement.getAnnotation(AnnotationProcessorStub.TestClassAnnotation.class)).thenReturn(null); | ||
+ | ||
+ expectedException.expect(ProcessingException.class); | ||
+ expectedException.expectMessage("Please annotate class TestClass with TestClassAnnotation"); | ||
+ stub.processor.validateAnnotatedField(stub.field); | ||
+ } | ||
+ | ||
+ @Test | ||
+ public void failIfFieldPrivate() { | ||
+ AnnotationProcessorStub stub = AnnotationProcessorStub.newInstance(); | ||
+ Set<Modifier> modifiers = new HashSet<Modifier>(); | ||
+ modifiers.add(Modifier.PRIVATE); | ||
+ when(stub.field.getModifiers()).thenReturn(modifiers); | ||
+ | ||
+ expectedException.expect(ProcessingException.class); | ||
+ expectedException.expectMessage("TestFieldAnnotation can not be applied to private field: TestField"); | ||
+ stub.processor.validateAnnotatedField(stub.field); | ||
+ } | ||
+ | ||
+ @Test | ||
+ public void failIfFieldFinal() { | ||
+ AnnotationProcessorStub stub = AnnotationProcessorStub.newInstance(); | ||
+ Set<Modifier> modifiers = new HashSet<Modifier>(); | ||
+ modifiers.add(Modifier.FINAL); | ||
+ when(stub.field.getModifiers()).thenReturn(modifiers); | ||
+ | ||
+ expectedException.expect(ProcessingException.class); | ||
+ expectedException.expectMessage("TestFieldAnnotation can not be applied to final field: TestField"); | ||
+ stub.processor.validateAnnotatedField(stub.field); | ||
+ } | ||
+} |
78
...htorefresh/storio/common/annotations/processor/introspection/AnnotationProcessorStub.java
@@ -0,0 +1,78 @@ | ||
+package com.pushtorefresh.storio.common.annotations.processor.introspection; | ||
+ | ||
+import com.pushtorefresh.storio.common.annotations.processor.StorIOAnnotationsProcessor; | ||
+ | ||
+import org.jetbrains.annotations.NotNull; | ||
+import org.mockito.Mockito; | ||
+ | ||
+import java.lang.annotation.Annotation; | ||
+ | ||
+import javax.lang.model.element.Element; | ||
+import javax.lang.model.element.Name; | ||
+ | ||
+import static javax.lang.model.element.ElementKind.CLASS; | ||
+import static org.mockito.Mockito.mock; | ||
+import static org.mockito.Mockito.when; | ||
+ | ||
+public class AnnotationProcessorStub { | ||
+ | ||
+ final TestStorIOAnnotationsProcessor processor; | ||
+ final Element field; | ||
+ final Element enclosingElement; | ||
+ final TestClassAnnotation classAnnotation; | ||
+ final TestFieldAnnotation fieldAnnotation; | ||
+ | ||
+ @NotNull | ||
+ static AnnotationProcessorStub newInstance() { | ||
+ return new AnnotationProcessorStub(); | ||
+ } | ||
+ | ||
+ public AnnotationProcessorStub() { | ||
+ processor = mock(TestStorIOAnnotationsProcessor.class, Mockito.CALLS_REAL_METHODS); | ||
+ | ||
+ field = mock(Element.class); | ||
+ | ||
+ Name name = mock(Name.class); | ||
+ when(name.toString()).thenReturn("TestField"); | ||
+ when(field.getSimpleName()).thenReturn(name); | ||
+ | ||
+ enclosingElement = mock(Element.class); | ||
+ Name className = mock(Name.class); | ||
+ when(className.toString()).thenReturn("TestClass"); | ||
+ when(enclosingElement.getSimpleName()).thenReturn(className); | ||
+ when(enclosingElement.getKind()).thenReturn(CLASS); | ||
+ | ||
+ when(field.getEnclosingElement()).thenReturn(enclosingElement); | ||
+ | ||
+ classAnnotation = mock(TestClassAnnotation.class); | ||
+ fieldAnnotation = mock(TestFieldAnnotation.class); | ||
+ | ||
+ when(enclosingElement.getAnnotation(TestClassAnnotation.class)).thenReturn(classAnnotation); | ||
+ } | ||
+ | ||
+ protected static abstract class TestStorIOAnnotationsProcessor extends StorIOAnnotationsProcessor { | ||
+ | ||
+ @Override | ||
+ public void validateAnnotatedField(@NotNull Element annotatedField) { | ||
+ super.validateAnnotatedField(annotatedField); | ||
+ } | ||
+ | ||
+ @NotNull | ||
+ @Override | ||
+ public Class<? extends Annotation> getTypeAnnotationClass() { | ||
+ return TestClassAnnotation.class; | ||
+ } | ||
+ | ||
+ @NotNull | ||
+ @Override | ||
+ protected Class<? extends Annotation> getColumnAnnotationClass() { | ||
+ return TestFieldAnnotation.class; | ||
+ } | ||
+ } | ||
+ | ||
+ static abstract class TestClassAnnotation implements Annotation { | ||
+ } | ||
+ | ||
+ static abstract class TestFieldAnnotation implements Annotation { | ||
+ } | ||
+} |
61
...orefresh/storio/contentresolver/annotations/processor/StorIOContentResolverProcessor.java
7
...efresh/storio/contentresolver/annotations/processor/generate/DeleteResolverGenerator.java
14
...torefresh/storio/contentresolver/annotations/processor/generate/PutResolverGenerator.java
73
...htorefresh/storio/contentresolver/annotations/processor/ContentResolverProcessorStub.java
@@ -0,0 +1,73 @@ | ||
+package com.pushtorefresh.storio.contentresolver.annotations.processor; | ||
+ | ||
+import org.jetbrains.annotations.NotNull; | ||
+import org.jetbrains.annotations.Nullable; | ||
+import org.mockito.Mockito; | ||
+ | ||
+import java.lang.annotation.Annotation; | ||
+import java.util.Map; | ||
+ | ||
+import javax.lang.model.element.Element; | ||
+import javax.lang.model.element.Name; | ||
+import javax.lang.model.element.TypeElement; | ||
+ | ||
+import static org.mockito.Mockito.mock; | ||
+import static org.mockito.Mockito.when; | ||
+ | ||
+public class ContentResolverProcessorStub { | ||
+ | ||
+ final TestStorIOContentResolverProcessor processor; | ||
+ final TypeElement classElement; | ||
+ final TestClassAnnotation classAnnotation; | ||
+ | ||
+ @NotNull | ||
+ static ContentResolverProcessorStub newInstance() { | ||
+ return new ContentResolverProcessorStub(); | ||
+ } | ||
+ | ||
+ public ContentResolverProcessorStub() { | ||
+ processor = mock(TestStorIOContentResolverProcessor.class, Mockito.CALLS_REAL_METHODS); | ||
+ classAnnotation = mock(TestClassAnnotation.class); | ||
+ | ||
+ classElement = mock(TypeElement.class); | ||
+ Name qualifiedName = mock(Name.class); | ||
+ when(qualifiedName.toString()).thenReturn("ClassElementName"); | ||
+ when(classElement.getQualifiedName()).thenReturn(qualifiedName); | ||
+ } | ||
+ | ||
+ protected static abstract class TestStorIOContentResolverProcessor extends StorIOContentResolverProcessor { | ||
+ | ||
+ // region Public Morozov | ||
+ @Override | ||
+ public void validateAnnotatedField(@NotNull Element annotatedField) { | ||
+ super.validateAnnotatedField(annotatedField); | ||
+ } | ||
+ | ||
+ public void validateUris( | ||
+ @NotNull TypeElement classElement, | ||
+ @Nullable String commonUri, | ||
+ @NotNull Map operationUriMap) { | ||
+ //noinspection unchecked | ||
+ super.validateUris(classElement, commonUri, operationUriMap); | ||
+ } | ||
+ // endregion | ||
+ | ||
+ @NotNull | ||
+ @Override | ||
+ public Class<? extends Annotation> getTypeAnnotationClass() { | ||
+ return TestClassAnnotation.class; | ||
+ } | ||
+ | ||
+ @NotNull | ||
+ @Override | ||
+ protected Class<? extends Annotation> getColumnAnnotationClass() { | ||
+ return TestFieldAnnotation.class; | ||
+ } | ||
+ } | ||
+ | ||
+ static abstract class TestClassAnnotation implements Annotation { | ||
+ } | ||
+ | ||
+ static abstract class TestFieldAnnotation implements Annotation { | ||
+ } | ||
+} |
61
...ava/com/pushtorefresh/storio/contentresolver/annotations/processor/UriValidationTest.java
@@ -0,0 +1,61 @@ | ||
+package com.pushtorefresh.storio.contentresolver.annotations.processor; | ||
+ | ||
+import com.pushtorefresh.storio.common.annotations.processor.ProcessingException; | ||
+ | ||
+import org.junit.Rule; | ||
+import org.junit.Test; | ||
+import org.junit.rules.ExpectedException; | ||
+ | ||
+import java.util.Collections; | ||
+import java.util.HashMap; | ||
+import java.util.Map; | ||
+ | ||
+public class UriValidationTest { | ||
+ | ||
+ @Rule | ||
+ public ExpectedException expectedException = ExpectedException.none(); | ||
+ | ||
+ @Test | ||
+ public void successIfCommonNameExist() { | ||
+ ContentResolverProcessorStub stub = ContentResolverProcessorStub.newInstance(); | ||
+ stub.processor.validateUris(stub.classElement, "content://testUri", Collections.emptyMap()); | ||
+ } | ||
+ | ||
+ @Test | ||
+ public void successIfNamesForOperationsExist() { | ||
+ ContentResolverProcessorStub stub = ContentResolverProcessorStub.newInstance(); | ||
+ stub.processor.validateUris(stub.classElement, "", Collections.singletonMap("operation", "content://testUri")); | ||
+ } | ||
+ | ||
+ @Test | ||
+ public void failIfNameForOperationIsEmpty() { | ||
+ ContentResolverProcessorStub stub = ContentResolverProcessorStub.newInstance(); | ||
+ | ||
+ Map<String, String> operationNameMap = new HashMap<String, String>(3); | ||
+ operationNameMap.put("insert", "content://insertUri"); | ||
+ operationNameMap.put("update", ""); | ||
+ operationNameMap.put("delete", "content://deleteUri"); | ||
+ | ||
+ expectedException.expect(ProcessingException.class); | ||
+ expectedException.expectMessage("Uri of ClassElementName annotated " + | ||
+ "with TestClassAnnotation is null or empty for operation update"); | ||
+ | ||
+ stub.processor.validateUris(stub.classElement, "", operationNameMap); | ||
+ } | ||
+ | ||
+ @Test | ||
+ public void failIfAllNamesAreEmpty() { | ||
+ ContentResolverProcessorStub stub = ContentResolverProcessorStub.newInstance(); | ||
+ | ||
+ Map<String, String> operationNameMap = new HashMap<String, String>(3); | ||
+ operationNameMap.put("insert", ""); | ||
+ operationNameMap.put("update", ""); | ||
+ operationNameMap.put("delete", ""); | ||
+ | ||
+ expectedException.expect(ProcessingException.class); | ||
+ expectedException.expectMessage("Uri of ClassElementName annotated " + | ||
+ "with TestClassAnnotation is null or empty"); // Without operation marker | ||
+ | ||
+ stub.processor.validateUris(stub.classElement, "", operationNameMap); | ||
+ } | ||
+} |
124
...sh/storio/contentresolver/annotations/processor/generate/DeleteResolverGeneratorTest.java
206
...fresh/storio/contentresolver/annotations/processor/generate/PutResolverGeneratorTest.java
29
.../java/com/pushtorefresh/storio/contentresolver/annotations/StorIOContentResolverType.java
0 comments on commit
e832ae4