Skip to content

Commit 257f3f6

Browse files
Added tests
1 parent 398a737 commit 257f3f6

File tree

2 files changed

+198
-16
lines changed

2 files changed

+198
-16
lines changed

spring-data-eclipse-store-migration/src/main/java/software/xdev/spring/data/eclipse/store/AddAnnotationToOtherAnnotation.java

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
package software.xdev.spring.data.eclipse.store;
1717

1818
import java.util.Comparator;
19+
import java.util.Objects;
1920

21+
import org.jetbrains.annotations.NotNull;
2022
import org.openrewrite.ExecutionContext;
2123
import org.openrewrite.Option;
2224
import org.openrewrite.Recipe;
@@ -30,7 +32,6 @@
3032

3133
public class AddAnnotationToOtherAnnotation extends Recipe
3234
{
33-
3435
@Option(displayName = "Existing annotation type",
3536
description = "Annotation type that is already existing. Recipe is looking for this annotation to add the new "
3637
+ "annotation.",
@@ -70,43 +71,39 @@ public AddAnnotationToOtherAnnotation()
7071
}
7172

7273
@Override
73-
public String getDisplayName()
74+
public @NotNull String getDisplayName()
7475
{
7576
return "AddAnnotationToOtherAnnotation";
7677
}
7778

7879
@Override
79-
public String getDescription()
80+
public @NotNull String getDescription()
8081
{
8182
return "Add the a new annotation to an existing annotation.";
8283
}
8384

8485
@Override
85-
public TreeVisitor<?, ExecutionContext> getVisitor()
86+
public @NotNull TreeVisitor<?, ExecutionContext> getVisitor()
8687
{
8788
final AnnotationMatcher existingAnnotationMatcher = new AnnotationMatcher(this.existingAnnotationType);
8889
final AnnotationMatcher newAnnotationMatcher = new AnnotationMatcher(this.annotationTypeToAdd);
8990
return new JavaIsoVisitor<>()
9091
{
9192
@Override
92-
public J.ClassDeclaration visitClassDeclaration(
93-
final J.ClassDeclaration classDecl,
94-
final ExecutionContext executionContext)
93+
public J.@NotNull ClassDeclaration visitClassDeclaration(
94+
final J.@NotNull ClassDeclaration classDecl,
95+
final @NotNull ExecutionContext executionContext)
9596
{
9697
final J.ClassDeclaration cd = super.visitClassDeclaration(classDecl, executionContext);
9798

9899
if(
99-
cd.getAnnotations() == null
100-
|| cd.getLeadingAnnotations()
100+
cd.getLeadingAnnotations()
101101
.stream()
102-
.filter(annotation -> existingAnnotationMatcher.matches(annotation))
102+
.filter(existingAnnotationMatcher::matches)
103103
.findAny()
104-
.isEmpty()
105-
|| cd.getLeadingAnnotations()
104+
.isEmpty() || cd.getLeadingAnnotations()
106105
.stream()
107-
.filter(annotation -> newAnnotationMatcher.matches(annotation))
108-
.findAny()
109-
.isPresent()
106+
.anyMatch(newAnnotationMatcher::matches)
110107
)
111108
{
112109
return cd;
@@ -150,4 +147,38 @@ public String getAnnotationTypeToAddSimpleName()
150147
{
151148
return this.annotationTypeToAddSimpleName;
152149
}
150+
151+
@Override
152+
public boolean equals(final Object o)
153+
{
154+
if(this == o)
155+
{
156+
return true;
157+
}
158+
if(o == null || this.getClass() != o.getClass())
159+
{
160+
return false;
161+
}
162+
if(!super.equals(o))
163+
{
164+
return false;
165+
}
166+
final AddAnnotationToOtherAnnotation that = (AddAnnotationToOtherAnnotation)o;
167+
return Objects.equals(this.existingAnnotationType, that.existingAnnotationType) && Objects.equals(
168+
this.annotationTypeToAdd,
169+
that.annotationTypeToAdd) && Objects.equals(this.classPath, that.classPath) && Objects.equals(
170+
this.annotationTypeToAddSimpleName,
171+
that.annotationTypeToAddSimpleName);
172+
}
173+
174+
@Override
175+
public int hashCode()
176+
{
177+
return Objects.hash(
178+
super.hashCode(),
179+
this.existingAnnotationType,
180+
this.annotationTypeToAdd,
181+
this.classPath,
182+
this.annotationTypeToAddSimpleName);
183+
}
153184
}

spring-data-eclipse-store-migration/src/test/java/software/xdev/spring/data/eclipse/store/AddAnnotationToOtherAnnotationTest.java

Lines changed: 152 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public void defaults(final RecipeSpec recipeSpec)
4444
}
4545

4646
@Test
47-
void testAddingAnnotation()
47+
void testSimpleSingle()
4848
{
4949
this.rewriteRun
5050
(
@@ -77,4 +77,155 @@ public A()
7777
)
7878
);
7979
}
80+
81+
@Test
82+
void testSimpleMultiple()
83+
{
84+
this.rewriteRun
85+
(
86+
java
87+
(
88+
"""
89+
import org.springframework.boot.autoconfigure.SpringBootApplication;
90+
91+
@SpringBootApplication
92+
class A
93+
{
94+
public A()
95+
{
96+
}
97+
}
98+
""",
99+
"""
100+
import org.springframework.boot.autoconfigure.SpringBootApplication;
101+
import software.xdev.spring.data.eclipse.store.repository.config.EnableEclipseStoreRepositories;
102+
103+
@EnableEclipseStoreRepositories
104+
@SpringBootApplication
105+
class A
106+
{
107+
public A()
108+
{
109+
}
110+
}
111+
"""
112+
),
113+
java
114+
(
115+
"""
116+
import org.springframework.boot.autoconfigure.SpringBootApplication;
117+
118+
@SpringBootApplication
119+
class B
120+
{
121+
public B()
122+
{
123+
}
124+
}
125+
""",
126+
"""
127+
import org.springframework.boot.autoconfigure.SpringBootApplication;
128+
import software.xdev.spring.data.eclipse.store.repository.config.EnableEclipseStoreRepositories;
129+
130+
@EnableEclipseStoreRepositories
131+
@SpringBootApplication
132+
class B
133+
{
134+
public B()
135+
{
136+
}
137+
}
138+
"""
139+
)
140+
);
141+
}
142+
143+
@Test
144+
void testAlreadyAdded()
145+
{
146+
this.rewriteRun
147+
(
148+
java
149+
(
150+
"""
151+
import org.springframework.boot.autoconfigure.SpringBootApplication;
152+
import software.xdev.spring.data.eclipse.store.repository.config.EnableEclipseStoreRepositories;
153+
154+
@EnableEclipseStoreRepositories
155+
@SpringBootApplication
156+
class A
157+
{
158+
public A()
159+
{
160+
}
161+
}
162+
"""
163+
)
164+
);
165+
}
166+
167+
@Test
168+
void testSimpleNoAnnotation()
169+
{
170+
this.rewriteRun
171+
(
172+
java
173+
(
174+
"""
175+
class A
176+
{
177+
public A()
178+
{
179+
}
180+
}
181+
"""
182+
)
183+
);
184+
}
185+
186+
@Test
187+
void testSimpleNoAnnotationAndAnnotation()
188+
{
189+
this.rewriteRun
190+
(
191+
java
192+
(
193+
"""
194+
class B
195+
{
196+
public B()
197+
{
198+
}
199+
}
200+
"""
201+
),
202+
java
203+
(
204+
"""
205+
import org.springframework.boot.autoconfigure.SpringBootApplication;
206+
207+
@SpringBootApplication
208+
class A
209+
{
210+
public A()
211+
{
212+
}
213+
}
214+
""",
215+
"""
216+
import org.springframework.boot.autoconfigure.SpringBootApplication;
217+
import software.xdev.spring.data.eclipse.store.repository.config.EnableEclipseStoreRepositories;
218+
219+
@EnableEclipseStoreRepositories
220+
@SpringBootApplication
221+
class A
222+
{
223+
public A()
224+
{
225+
}
226+
}
227+
"""
228+
)
229+
);
230+
}
80231
}

0 commit comments

Comments
 (0)