Skip to content

Commit

Permalink
Add test for annotation default value instrumentation (#174)
Browse files Browse the repository at this point in the history
  • Loading branch information
IlyaMuravjov authored Mar 1, 2024
1 parent e8faa82 commit aca7b5d
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 13 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package example;

public class AnnotationsEx {
@MyAnnotation
public static int getSelfAnnotationCount() throws NoSuchMethodException {
return AnnotationsEx.class.getMethod("getSelfAnnotationCount").getAnnotations().length;
}

@MyAnnotation
public static String getAnnotationDefaultValue() throws NoSuchMethodException {
return AnnotationsEx.class
.getMethod("getAnnotationDefaultValue")
.getAnnotation(MyAnnotation.class)
.x();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@

@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String x() default "MyAnnotation default value";
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class ConcreteExecutorTests: UTestConcreteExecutorTest() {

@Test
fun `annotated method test`() = executeTest {
val uTest = UTestCreator.AnnotatedMethodClass.getClassAnnotationCount(jcClasspath)
val uTest = UTestCreator.AnnotationsEx.getSelfAnnotationCount(jcClasspath)
val res = uTestConcreteExecutor.executeAsync(uTest)
assertIs<UTestExecutionSuccessResult>(res)
val result = res.result
Expand All @@ -83,6 +83,18 @@ class ConcreteExecutorTests: UTestConcreteExecutorTest() {
assertEquals(1, result.value)
}

@Test
@Disabled
fun `annotation default value test`() = executeTest {
val uTest = UTestCreator.AnnotationsEx.getAnnotationDefaultValue(jcClasspath)
val res = uTestConcreteExecutor.executeAsync(uTest)
assertIs<UTestExecutionSuccessResult>(res)
val result = res.result
assertNotNull(result)
assertIs<UTestConstantDescriptor.String>(result)
assertEquals("MyAnnotation default value", result.value)
}

@Test
fun `static fields test`() = executeTest {
repeat(3) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,11 +365,20 @@ object UTestCreator {
}
}

object AnnotatedMethodClass {
fun getClassAnnotationCount(jcClasspath: JcClasspath): UTest {
val jcClass = jcClasspath.findClass<example.AnnotatedMethodClass>()
object AnnotationsEx {
fun getSelfAnnotationCount(jcClasspath: JcClasspath): UTest {
val jcClass = jcClasspath.findClass<example.AnnotationsEx>()
val jcMethod = jcClass.findDeclaredMethodOrNull("getSelfAnnotationCount")
?: error("Cant find method getSelfAnnotationCount in class AnnotatedMethodClass")
?: error("Cant find method getSelfAnnotationCount in class AnnotationsEx")
val instance = UTestNullExpression(jcClass.toType())
val statements = emptyList<UTestInst>()
return UTest(statements, UTestMethodCall(instance, jcMethod, emptyList()))
}

fun getAnnotationDefaultValue(jcClasspath: JcClasspath): UTest {
val jcClass = jcClasspath.findClass<example.AnnotationsEx>()
val jcMethod = jcClass.findDeclaredMethodOrNull("getAnnotationDefaultValue")
?: error("Cant find method getAnnotationDefaultValue in class AnnotationsEx")
val instance = UTestNullExpression(jcClass.toType())
val statements = emptyList<UTestInst>()
return UTest(statements, UTestMethodCall(instance, jcMethod, emptyList()))
Expand Down

0 comments on commit aca7b5d

Please sign in to comment.