This is an automated email from the ASF dual-hosted git repository. sunlan pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/groovy.git
commit 37aeeb8637dcdd5d3e8b27465a1dfe69b6cb742b Author: aalmiray <[email protected]> AuthorDate: Thu Jun 6 21:23:29 2019 +0200 Add first batch of tests for @Generated. Relates to GROOVY-9164 --- .../generated/AbstractGeneratedAstTestCase.groovy | 60 +++++++++++++++++ .../groovy/generated/AutoCloneGeneratedTest.groovy | 37 +++++++++++ .../generated/AutoExternalizeGeneratedTest.groovy | 44 +++++++++++++ .../groovy/generated/CanonicalGeneratedTest.groovy | 77 ++++++++++++++++++++++ .../generated/ConstructorsGeneratedTest.groovy | 28 ++++++++ .../generated/GroovyObjectGeneratedTest.groovy | 38 +++++++++++ .../groovy/generated/ImmutableGeneratedTest.groovy | 76 +++++++++++++++++++++ .../generated/PropertiesGeneratedTest.groovy | 41 ++++++++++++ .../groovy/generated/SortableGeneratedTest.groovy | 58 ++++++++++++++++ 9 files changed, 459 insertions(+) diff --git a/src/test/groovy/generated/AbstractGeneratedAstTestCase.groovy b/src/test/groovy/generated/AbstractGeneratedAstTestCase.groovy new file mode 100644 index 0000000..f96d2f0 --- /dev/null +++ b/src/test/groovy/generated/AbstractGeneratedAstTestCase.groovy @@ -0,0 +1,60 @@ +package groovy.generated + +import groovy.transform.CompileStatic +import groovy.transform.Generated + +import java.lang.reflect.Constructor +import java.lang.reflect.Method + +/** + * @author Dmitry Vyazelenko + * @author Andres Almiray + */ +@CompileStatic +abstract class AbstractGeneratedAstTestCase { + protected final void assertConstructorIsAnnotated(Class<?> classUnderTest, Class... paramTypes) { + assert findConstructor(classUnderTest, paramTypes).getAnnotation(Generated) + } + + protected final void assertConstructorIsNotAnnotated(Class<?> classUnderTest, Class... paramTypes) { + assert !findConstructor(classUnderTest, paramTypes).getAnnotation(Generated) + } + + protected final void assertMethodIsAnnotated(Class<?> classUnderTest, String methodName, Class... paramTypes) { + assert findMethod(classUnderTest, methodName, paramTypes).getAnnotation(Generated) + } + + protected final void assertMethodIsNotAnnotated(Class<?> classUnderTest, String methodName, Class... paramTypes) { + assert !findMethod(classUnderTest, methodName, paramTypes).getAnnotation(Generated) + } + + protected final void assertExactMethodIsAnnotated(Class<?> classUnderTest, String methodName, Class returnType, Class... paramTypes) { + assert findExactMethod(classUnderTest, methodName, returnType, paramTypes).getAnnotation(Generated) + } + + protected final void assertExactMethodIsNotAnnotated(Class<?> classUnderTest, String methodName, Class returnType, Class... paramTypes) { + assert !findExactMethod(classUnderTest, methodName, returnType, paramTypes).getAnnotation(Generated) + } + + private Method findMethod(Class<?> classUnderTest, String methodName, Class... paramTypes) { + Method target = classUnderTest.getMethod(methodName, paramTypes) + assert target + target + } + + private Method findExactMethod(Class<?> classUnderTest, String methodName, Class returnType, Class... paramTypes) { + Method target = classUnderTest.methods.find { m -> + m.name == methodName && + m.returnType == returnType && + Arrays.equals(m.parameterTypes, paramTypes) + } + assert target + target + } + + private Constructor findConstructor(Class<?> classUnderTest, Class... paramTypes) { + Constructor target = classUnderTest.getConstructor(paramTypes) + assert target + target + } +} diff --git a/src/test/groovy/generated/AutoCloneGeneratedTest.groovy b/src/test/groovy/generated/AutoCloneGeneratedTest.groovy new file mode 100644 index 0000000..a8aaab2 --- /dev/null +++ b/src/test/groovy/generated/AutoCloneGeneratedTest.groovy @@ -0,0 +1,37 @@ +package groovy.generated + +import groovy.transform.CompileStatic +import org.junit.Ignore +import org.junit.Test + +/** + * @author Dmitry Vyazelenko + * @author Andres Almiray + */ +@CompileStatic +class AutoCloneGeneratedTest extends AbstractGeneratedAstTestCase { + final Class<?> implicitAutoClone = new GroovyClassLoader().parseClass('''@groovy.transform.AutoClone + |class ClassUnderTest { + |}'''.stripMargin()) + + final Class<?> explicitAutoClone = new GroovyClassLoader().parseClass('''@groovy.transform.AutoClone + |class ClassUnderTest { + | Object clone() throws java.lang.CloneNotSupportedException { null } + |}'''.stripMargin()) + + @Test + void test_clone_is_annotated() { + assertExactMethodIsAnnotated(implicitAutoClone, 'clone', Object) + } + + @Test + void test_clone_with_exact_type_is_annotated() { + assertExactMethodIsAnnotated(implicitAutoClone, 'clone', implicitAutoClone) + } + + @Ignore('https://issues.apache.org/jira/browse/GROOVY-9162') + @Test + void test_clone_is_not_annotated() { + assertExactMethodIsNotAnnotated(explicitAutoClone, 'clone', Object) + } +} \ No newline at end of file diff --git a/src/test/groovy/generated/AutoExternalizeGeneratedTest.groovy b/src/test/groovy/generated/AutoExternalizeGeneratedTest.groovy new file mode 100644 index 0000000..a9a90c0 --- /dev/null +++ b/src/test/groovy/generated/AutoExternalizeGeneratedTest.groovy @@ -0,0 +1,44 @@ +package groovy.generated + +import groovy.transform.CompileStatic +import org.junit.Ignore +import org.junit.Test + +/** + * @author Dmitry Vyazelenko + * @author Andres Almiray + */ +@CompileStatic +class AutoExternalizeGeneratedTest extends AbstractGeneratedAstTestCase { + final Class<?> implicitAutoExternalize = new GroovyClassLoader().parseClass('''@groovy.transform.AutoExternalize + |class ClassUnderTest { + |}'''.stripMargin()) + + final Class<?> explicitAutoExternalize = new GroovyClassLoader().parseClass('''@groovy.transform.AutoExternalize + |class ClassUnderTest { + | void writeExternal(ObjectOutput out) throws IOException { } + | void readExternal(ObjectInput oin) { } + |}'''.stripMargin()) + + @Test + void test_writeExternal_is_annotated() { + assertMethodIsAnnotated(implicitAutoExternalize, 'writeExternal', ObjectOutput) + } + + @Test + void test_readExternal_is_annotated() { + assertMethodIsAnnotated(implicitAutoExternalize, 'readExternal', ObjectInput) + } + + @Ignore('https://issues.apache.org/jira/browse/GROOVY-9163') + @Test + void test_writeExternal_is_not_annotated() { + assertMethodIsNotAnnotated(explicitAutoExternalize, 'writeExternal', ObjectOutput) + } + + @Ignore('https://issues.apache.org/jira/browse/GROOVY-9163') + @Test + void test_readExternal_is_not_annotated() { + assertMethodIsNotAnnotated(explicitAutoExternalize, 'readExternal', ObjectInput) + } +} \ No newline at end of file diff --git a/src/test/groovy/generated/CanonicalGeneratedTest.groovy b/src/test/groovy/generated/CanonicalGeneratedTest.groovy new file mode 100644 index 0000000..6d20d3c --- /dev/null +++ b/src/test/groovy/generated/CanonicalGeneratedTest.groovy @@ -0,0 +1,77 @@ +package groovy.generated + +import groovy.transform.CompileStatic +import org.junit.Test + +/** + * @author Dmitry Vyazelenko + * @author Andres Almiray + */ +@CompileStatic +class CanonicalGeneratedTest extends AbstractGeneratedAstTestCase { + final Class<?> implicitCanonical = new GroovyClassLoader().parseClass('''@groovy.transform.Canonical + |class ClassUnderTest { + | String name + | int age + |}'''.stripMargin()) + + final Class<?> explicitCanonical = new GroovyClassLoader().parseClass('''@groovy.transform.Canonical + |class ClassUnderTest { + | String name + | int age + | ClassUnderTest(String n, int a) { } + | boolean equals(Object o) { false } + | int hashCode() { 42 } + | String toString() { '' } + |}'''.stripMargin()) + + @Test + void test_noArg_constructor_is_annotated() { + assertConstructorIsAnnotated(implicitCanonical) + } + + @Test + void test_constructor_is_annotated() { + assertConstructorIsAnnotated(implicitCanonical, String, int.class) + } + + @Test + void test_equals_is_annotated() { + assertMethodIsAnnotated(implicitCanonical, 'equals', Object) + } + + @Test + void test_canEqual_is_annotated() { + assertMethodIsAnnotated(implicitCanonical, 'canEqual', Object) + } + + @Test + void test_hashCode_is_annotated() { + assertMethodIsAnnotated(implicitCanonical, 'hashCode') + } + + @Test + void test_toString_is_annotated() { + assertMethodIsAnnotated(implicitCanonical, 'toString') + } + + @Test + void test_constructor_is_not_annotated() { + assertConstructorIsNotAnnotated(explicitCanonical, String, int.class) + } + + @Test + void test_equals_is_not_annotated() { + assertMethodIsNotAnnotated(explicitCanonical, 'equals', Object) + } + + @Test + void test_hashCode_is_not_annotated() { + assertMethodIsNotAnnotated(explicitCanonical, 'hashCode') + } + + @Test + void test_toString_is_not_annotated() { + assertMethodIsNotAnnotated(explicitCanonical, 'toString') + } +} \ No newline at end of file diff --git a/src/test/groovy/generated/ConstructorsGeneratedTest.groovy b/src/test/groovy/generated/ConstructorsGeneratedTest.groovy new file mode 100644 index 0000000..7d350ea --- /dev/null +++ b/src/test/groovy/generated/ConstructorsGeneratedTest.groovy @@ -0,0 +1,28 @@ +package groovy.generated + +import groovy.transform.CompileStatic +import org.junit.Test + +/** + * @author Dmitry Vyazelenko + * @author Andres Almiray + */ +@CompileStatic +class ConstructorsGeneratedTest extends AbstractGeneratedAstTestCase { + final Class<?> noExplicitConstructors = new GroovyClassLoader().parseClass('''class ClassUnderTest { + |}'''.stripMargin()) + + final Class<?> explicitNoArgConstructor = new GroovyClassLoader().parseClass('''class ClassUnderTest { + | ClassUnderTest() { } + |}'''.stripMargin()) + + @Test + void test_default_constructors_are_annotated() { + assertConstructorIsAnnotated(noExplicitConstructors) + } + + @Test + void test_explicit_notArg_constructor_is_not_annotated() { + assertConstructorIsNotAnnotated(explicitNoArgConstructor) + } +} \ No newline at end of file diff --git a/src/test/groovy/generated/GroovyObjectGeneratedTest.groovy b/src/test/groovy/generated/GroovyObjectGeneratedTest.groovy new file mode 100644 index 0000000..7c473ef --- /dev/null +++ b/src/test/groovy/generated/GroovyObjectGeneratedTest.groovy @@ -0,0 +1,38 @@ +package groovy.generated + +import groovy.transform.CompileStatic +import org.junit.Test + +/** + * @author Dmitry Vyazelenko + * @author Andres Almiray + */ +@CompileStatic +class GroovyObjectGeneratedTest extends AbstractGeneratedAstTestCase { + final Class<?> classUnderTest = new GroovyClassLoader().parseClass('class MyClass { }') + + @Test + void test_invokeMethod_is_annotated() { + assertMethodIsAnnotated(classUnderTest, 'invokeMethod', String, Object) + } + + @Test + void test_getProperty_is_annotated() { + assertMethodIsAnnotated(classUnderTest, 'getProperty', String) + } + + @Test + void test_setProperty_is_annotated() { + assertMethodIsAnnotated(classUnderTest, 'setProperty', String, Object) + } + + @Test + void test_getMetaClass_is_annotated() { + assertMethodIsAnnotated(classUnderTest, 'getMetaClass') + } + + @Test + void test_setMetaClass_is_annotated() { + assertMethodIsAnnotated(classUnderTest, 'setMetaClass', MetaClass) + } +} \ No newline at end of file diff --git a/src/test/groovy/generated/ImmutableGeneratedTest.groovy b/src/test/groovy/generated/ImmutableGeneratedTest.groovy new file mode 100644 index 0000000..09ae409 --- /dev/null +++ b/src/test/groovy/generated/ImmutableGeneratedTest.groovy @@ -0,0 +1,76 @@ +package groovy.generated + +import groovy.transform.CompileStatic +import org.junit.Test + +/** + * @author Dmitry Vyazelenko + * @author Andres Almiray + */ +@CompileStatic +class ImmutableGeneratedTest extends AbstractGeneratedAstTestCase { + final Class<?> implicitImmutable = new GroovyClassLoader().parseClass('''@groovy.transform.Immutable + |class ClassUnderTest { + | String name + | int age + |}'''.stripMargin()) + + final Class<?> explicitImmutable = new GroovyClassLoader().parseClass('''@groovy.transform.Immutable + |class ClassUnderTest { + | String name + | int age + | boolean equals(Object o) { false } + | int hashCode() { 42 } + | String toString() { '' } + |}'''.stripMargin()) + + @Test + void test_noArg_constructor_is_annotated() { + assertConstructorIsAnnotated(implicitImmutable) + } + + @Test + void test_Map_constructor_is_annotated() { + assertConstructorIsAnnotated(implicitImmutable, Map) + } + + @Test + void test_constructor_is_annotated() { + assertConstructorIsAnnotated(implicitImmutable, String, int.class) + } + + @Test + void test_equals_is_annotated() { + assertMethodIsAnnotated(implicitImmutable, 'equals', Object) + } + + @Test + void test_canEqual_is_annotated() { + assertMethodIsAnnotated(implicitImmutable, 'canEqual', Object) + } + + @Test + void test_hashCode_is_annotated() { + assertMethodIsAnnotated(implicitImmutable, 'hashCode') + } + + @Test + void test_toString_is_annotated() { + assertMethodIsAnnotated(implicitImmutable, 'toString') + } + + @Test + void test_equals_is_not_annotated() { + assertMethodIsNotAnnotated(explicitImmutable, 'equals', Object) + } + + @Test + void test_hashCode_is_not_annotated() { + assertMethodIsNotAnnotated(explicitImmutable, 'hashCode') + } + + @Test + void test_toString_is_not_annotated() { + assertMethodIsNotAnnotated(explicitImmutable, 'toString') + } +} \ No newline at end of file diff --git a/src/test/groovy/generated/PropertiesGeneratedTest.groovy b/src/test/groovy/generated/PropertiesGeneratedTest.groovy new file mode 100644 index 0000000..07e90e8 --- /dev/null +++ b/src/test/groovy/generated/PropertiesGeneratedTest.groovy @@ -0,0 +1,41 @@ +package groovy.generated + +import groovy.transform.CompileStatic +import org.junit.Test + +/** + * @author Dmitry Vyazelenko + * @author Andres Almiray + */ +@CompileStatic +class PropertiesGeneratedTest extends AbstractGeneratedAstTestCase { + final Class<?> withProps = new GroovyClassLoader().parseClass('''class WithProps { + | String name + |}'''.stripMargin()) + + final Class<?> withExplicitProps = new GroovyClassLoader().parseClass('''class WithExplicitProps { + | private String name + | String getName() { name } + | void setName(String n) { name = n } + |}'''.stripMargin()) + + @Test + void test_implicit_getName_is_annotated() { + assertMethodIsAnnotated(withProps, 'getName') + } + + @Test + void test_implicit_setName_is_annotated() { + assertMethodIsAnnotated(withProps, 'setName', String) + } + + @Test + void test_explicit_getName_is_annotated() { + assertMethodIsNotAnnotated(withExplicitProps, 'getName') + } + + @Test + void test_explicit_setName_is_annotated() { + assertMethodIsNotAnnotated(withExplicitProps, 'setName', String) + } +} \ No newline at end of file diff --git a/src/test/groovy/generated/SortableGeneratedTest.groovy b/src/test/groovy/generated/SortableGeneratedTest.groovy new file mode 100644 index 0000000..5019c15 --- /dev/null +++ b/src/test/groovy/generated/SortableGeneratedTest.groovy @@ -0,0 +1,58 @@ +package groovy.generated + +import groovy.transform.CompileStatic +import org.junit.Ignore +import org.junit.Test + +/** + * @author Dmitry Vyazelenko + * @author Andres Almiray + */ +@CompileStatic +class SortableGeneratedTest extends AbstractGeneratedAstTestCase { + final Class<?> implicitSortable = new GroovyClassLoader().parseClass('''@groovy.transform.Sortable + |class ClassUnderTest { + | String name + | int age + |}'''.stripMargin()) + + final Class<?> explicitSortable = new GroovyClassLoader().parseClass('''@groovy.transform.Sortable + |class ClassUnderTest { + | String name + | int age + | int compareTo(Object o) { 42 } + | int compareTo(ClassUnderTest o) { 42 } + |}'''.stripMargin()) + + @Test + void test_compareTo_is_annotated() { + assertMethodIsAnnotated(implicitSortable, 'compareTo', Object) + } + + @Test + void test_compareTo_with_exact_type_is_annotated() { + assertMethodIsAnnotated(implicitSortable, 'compareTo', implicitSortable) + } + + @Test + void test_static_comparatorByName_is_annotated() { + assertMethodIsAnnotated(implicitSortable, 'comparatorByName') + } + + @Test + void test_static_comparatorByAge_is_annotated() { + assertMethodIsAnnotated(implicitSortable, 'comparatorByAge') + } + + + @Test + void test_explicit_compareTo_is_not_annotated() { + assertMethodIsNotAnnotated(explicitSortable, 'compareTo', Object) + } + + @Ignore('https://issues.apache.org/jira/browse/GROOVY-9161') + @Test + void test_explicit_compareTo_with_exact_type_is_not_annotated() { + assertMethodIsNotAnnotated(explicitSortable, 'compareTo', explicitSortable) + } +} \ No newline at end of file
