Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix AddOrUpdateAnnotationAttribute for values of type Class #4342

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading