Skip to content

Commit

Permalink
Added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannesRabauer committed Feb 2, 2024
1 parent 398a737 commit 257f3f6
Show file tree
Hide file tree
Showing 2 changed files with 198 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
package software.xdev.spring.data.eclipse.store;

import java.util.Comparator;
import java.util.Objects;

import org.jetbrains.annotations.NotNull;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Option;
import org.openrewrite.Recipe;
Expand All @@ -30,7 +32,6 @@

public class AddAnnotationToOtherAnnotation extends Recipe
{

@Option(displayName = "Existing annotation type",
description = "Annotation type that is already existing. Recipe is looking for this annotation to add the new "
+ "annotation.",
Expand Down Expand Up @@ -70,43 +71,39 @@ public AddAnnotationToOtherAnnotation()
}

@Override
public String getDisplayName()
public @NotNull String getDisplayName()
{
return "AddAnnotationToOtherAnnotation";
}

@Override
public String getDescription()
public @NotNull String getDescription()
{
return "Add the a new annotation to an existing annotation.";
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor()
public @NotNull TreeVisitor<?, ExecutionContext> getVisitor()
{
final AnnotationMatcher existingAnnotationMatcher = new AnnotationMatcher(this.existingAnnotationType);
final AnnotationMatcher newAnnotationMatcher = new AnnotationMatcher(this.annotationTypeToAdd);
return new JavaIsoVisitor<>()
{
@Override
public J.ClassDeclaration visitClassDeclaration(
final J.ClassDeclaration classDecl,
final ExecutionContext executionContext)
public J.@NotNull ClassDeclaration visitClassDeclaration(
final J.@NotNull ClassDeclaration classDecl,
final @NotNull ExecutionContext executionContext)
{
final J.ClassDeclaration cd = super.visitClassDeclaration(classDecl, executionContext);

if(
cd.getAnnotations() == null
|| cd.getLeadingAnnotations()
cd.getLeadingAnnotations()
.stream()
.filter(annotation -> existingAnnotationMatcher.matches(annotation))
.filter(existingAnnotationMatcher::matches)
.findAny()
.isEmpty()
|| cd.getLeadingAnnotations()
.isEmpty() || cd.getLeadingAnnotations()
.stream()
.filter(annotation -> newAnnotationMatcher.matches(annotation))
.findAny()
.isPresent()
.anyMatch(newAnnotationMatcher::matches)
)
{
return cd;
Expand Down Expand Up @@ -150,4 +147,38 @@ public String getAnnotationTypeToAddSimpleName()
{
return this.annotationTypeToAddSimpleName;
}

@Override
public boolean equals(final Object o)
{
if(this == o)
{
return true;
}
if(o == null || this.getClass() != o.getClass())
{
return false;
}
if(!super.equals(o))
{
return false;
}
final AddAnnotationToOtherAnnotation that = (AddAnnotationToOtherAnnotation)o;
return Objects.equals(this.existingAnnotationType, that.existingAnnotationType) && Objects.equals(
this.annotationTypeToAdd,
that.annotationTypeToAdd) && Objects.equals(this.classPath, that.classPath) && Objects.equals(
this.annotationTypeToAddSimpleName,
that.annotationTypeToAddSimpleName);
}

@Override
public int hashCode()
{
return Objects.hash(
super.hashCode(),
this.existingAnnotationType,
this.annotationTypeToAdd,
this.classPath,
this.annotationTypeToAddSimpleName);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void defaults(final RecipeSpec recipeSpec)
}

@Test
void testAddingAnnotation()
void testSimpleSingle()
{
this.rewriteRun
(
Expand Down Expand Up @@ -77,4 +77,155 @@ public A()
)
);
}

@Test
void testSimpleMultiple()
{
this.rewriteRun
(
java
(
"""
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
class A
{
public A()
{
}
}
""",
"""
import org.springframework.boot.autoconfigure.SpringBootApplication;
import software.xdev.spring.data.eclipse.store.repository.config.EnableEclipseStoreRepositories;
@EnableEclipseStoreRepositories
@SpringBootApplication
class A
{
public A()
{
}
}
"""
),
java
(
"""
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
class B
{
public B()
{
}
}
""",
"""
import org.springframework.boot.autoconfigure.SpringBootApplication;
import software.xdev.spring.data.eclipse.store.repository.config.EnableEclipseStoreRepositories;
@EnableEclipseStoreRepositories
@SpringBootApplication
class B
{
public B()
{
}
}
"""
)
);
}

@Test
void testAlreadyAdded()
{
this.rewriteRun
(
java
(
"""
import org.springframework.boot.autoconfigure.SpringBootApplication;
import software.xdev.spring.data.eclipse.store.repository.config.EnableEclipseStoreRepositories;
@EnableEclipseStoreRepositories
@SpringBootApplication
class A
{
public A()
{
}
}
"""
)
);
}

@Test
void testSimpleNoAnnotation()
{
this.rewriteRun
(
java
(
"""
class A
{
public A()
{
}
}
"""
)
);
}

@Test
void testSimpleNoAnnotationAndAnnotation()
{
this.rewriteRun
(
java
(
"""
class B
{
public B()
{
}
}
"""
),
java
(
"""
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
class A
{
public A()
{
}
}
""",
"""
import org.springframework.boot.autoconfigure.SpringBootApplication;
import software.xdev.spring.data.eclipse.store.repository.config.EnableEclipseStoreRepositories;
@EnableEclipseStoreRepositories
@SpringBootApplication
class A
{
public A()
{
}
}
"""
)
);
}
}

0 comments on commit 257f3f6

Please sign in to comment.