Skip to content

Commit

Permalink
Fix AddOrUpdateAnnotationAttribute for values of type Class (#4342)
Browse files Browse the repository at this point in the history
* Fix AddOrUpdateAnnotationAttribute for values of type Class

* Correct type information on new attribute & add FQN test

* Remove need for second cycle

* Document two branches of the nested conditionals

---------

Co-authored-by: Tim te Beek <[email protected]>
  • Loading branch information
froque and timtebeek authored Jul 25, 2024
1 parent 3965da6 commit 14aef40
Show file tree
Hide file tree
Showing 2 changed files with 226 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@ void addValueAttribute() {
java(
"""
import org.example.Foo;
@Foo
public class A {
}
""",
"""
import org.example.Foo;
@Foo("hello")
public class A {
}
Expand All @@ -53,6 +53,68 @@ public class A {
);
}

@Test
void addValueAttributeClass() {
rewriteRun(
spec -> spec.recipe(new AddOrUpdateAnnotationAttribute("org.example.Foo", null, "Integer.class", null)),
java(
"""
package org.example;
public @interface Foo {
Class<? extends Number> value();
}
"""
),
java(
"""
import org.example.Foo;
@Foo
public class A {
}
""",
"""
import org.example.Foo;
@Foo(Integer.class)
public class A {
}
"""
)
);
}

@Test
void addValueAttributeFullyQualifiedClass() {
rewriteRun(
spec -> spec.recipe(new AddOrUpdateAnnotationAttribute("org.example.Foo", null, "java.math.BigDecimal.class", null)),
java(
"""
package org.example;
public @interface Foo {
Class<? extends Number> value();
}
"""
),
java(
"""
import org.example.Foo;
@Foo
public class A {
}
""",
"""
import org.example.Foo;
@Foo(java.math.BigDecimal.class)
public class A {
}
"""
)
);
}

@Test
void updateValueAttribute() {
rewriteRun(
Expand All @@ -69,14 +131,14 @@ void updateValueAttribute() {
java(
"""
import org.example.Foo;
@Foo("goodbye")
public class A {
}
""",
"""
import org.example.Foo;
@Foo("hello")
public class A {
}
Expand All @@ -85,6 +147,38 @@ public class A {
);
}

@Test
void updateValueAttributeClass() {
rewriteRun(
spec -> spec.recipe(new AddOrUpdateAnnotationAttribute("org.example.Foo", null, "Integer.class", null)),
java(
"""
package org.example;
public @interface Foo {
Class<? extends Number> value();
}
"""
),

java(
"""
import org.example.Foo;
@Foo(Long.class)
public class A {
}
""",
"""
import org.example.Foo;
@Foo(Integer.class)
public class A {
}
"""
)
);
}

@Test
void removeValueAttribute() {
rewriteRun(
Expand Down Expand Up @@ -117,6 +211,38 @@ public class A {
);
}

@Test
void removeValueAttributeClass() {
rewriteRun(
spec -> spec.recipe(new AddOrUpdateAnnotationAttribute("org.example.Foo", null, null, null)),
java(
"""
package org.example;
public @interface Foo {
Class<? extends Number> value();
}
"""
),

java(
"""
import org.example.Foo;
@Foo(Long.class)
public class A {
}
""",
"""
import org.example.Foo;
@Foo
public class A {
}
"""
)
);
}

@Test
void addNamedAttribute() {
rewriteRun(spec -> spec.recipe(new AddOrUpdateAnnotationAttribute("org.junit.Test", "timeout", "500", null)),
Expand All @@ -131,19 +257,19 @@ void addNamedAttribute() {
java(
"""
import org.junit.Test;
class SomeTest {
@Test
void foo() {
}
}
""",
"""
import org.junit.Test;
class SomeTest {
@Test(timeout = 500)
void foo() {
}
Expand All @@ -168,19 +294,19 @@ void replaceAttribute() {
java(
"""
import org.junit.Test;
class SomeTest {
@Test(timeout = 1)
void foo() {
}
}
""",
"""
import org.junit.Test;
class SomeTest {
@Test(timeout = 500)
void foo() {
}
Expand All @@ -205,19 +331,19 @@ void removeAttribute() {
java(
"""
import org.junit.Test;
class SomeTest {
@Test(timeout = 1)
void foo() {
}
}
""",
"""
import org.junit.Test;
class SomeTest {
@Test
void foo() {
}
Expand All @@ -244,19 +370,19 @@ void preserveExistingAttributes() {
java(
"""
import org.junit.Test;
class SomeTest {
@Test(foo = "")
void foo() {
}
}
""",
"""
import org.junit.Test;
class SomeTest {
@Test(timeout = 500, foo = "")
void foo() {
}
Expand All @@ -268,8 +394,7 @@ void foo() {

@Test
void implicitValueToExplicitValue() {
rewriteRun(spec -> spec.recipe(new AddOrUpdateAnnotationAttribute("org.junit.Test", "other", "1", null))
.expectedCyclesThatMakeChanges(2),
rewriteRun(spec -> spec.recipe(new AddOrUpdateAnnotationAttribute("org.junit.Test", "other", "1", null)),
java(
"""
package org.junit;
Expand All @@ -282,19 +407,19 @@ void implicitValueToExplicitValue() {
java(
"""
import org.junit.Test;
class SomeTest {
@Test("foo")
void foo() {
}
}
""",
"""
import org.junit.Test;
class SomeTest {
@Test(other = 1, value = "foo")
void foo() {
}
Expand All @@ -304,6 +429,43 @@ void foo() {
);
}

@Test
void implicitValueToExplicitValueClass() {
rewriteRun(spec -> spec.recipe(new AddOrUpdateAnnotationAttribute("org.junit.Test", "other", "1", null)),
java(
"""
package org.junit;
public @interface Test {
long other() default 0L;
Class<? extends Number> value();
}
"""
),
java(
"""
import org.junit.Test;
class SomeTest {
@Test(Integer.class)
void foo() {
}
}
""",
"""
import org.junit.Test;
class SomeTest {
@Test(other = 1, value = Integer.class)
void foo() {
}
}
"""
)
);
}

@Test
void dontChangeWhenSetToAddOnly() {
rewriteRun(
Expand All @@ -320,7 +482,7 @@ void dontChangeWhenSetToAddOnly() {
java(
"""
import org.junit.Test;
class SomeTest {
@Test(other = 0)
void foo() {
Expand Down
Loading

0 comments on commit 14aef40

Please sign in to comment.