Skip to content

Commit

Permalink
Tests only
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsdebruin committed Oct 28, 2024
1 parent 6e795b0 commit bb67416
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.openrewrite.java.spring;

import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.JavaIsoVisitor;

public class RequiredFieldIntoConstructorParameter extends Recipe {

@Override
public String getDisplayName() {
return "Required FieldInto Constructor Parameter";
}

@Override
public String getDescription() {
return "Moves attributes for setters maker with @Required annotation to constructor parameter.";
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new JavaIsoVisitor<ExecutionContext>() {

};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package org.openrewrite.java.spring;

import org.junit.jupiter.api.Test;
import org.openrewrite.java.JavaParser;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.java.Assertions.java;

class RequiredFieldIntoConstructorParameterTest implements RewriteTest {
@Override
public void defaults(RecipeSpec spec) {
spec.recipe(new RequiredFieldIntoConstructorParameter())
.parser(JavaParser.fromJavaVersion().classpath("spring-beans"));
}

@Test
void fieldIntoExistingSingleConstructor() {
//language=java
rewriteRun(
java(
"""
package demo;
import org.springframework.beans.factory.annotation.Required;
public class Test {
private String a;
@Required
void setA(String a) {
this.a = a;
}
}
""",
"""
package demo;
public class Test {
private final String a;
Test(String a) {
this.a = a;
}
}
"""
)
);
}

@Test
public void requiredFieldIntoConstructorParameter() {
rewriteRun(
//language=java
java(
"""
package demo;
import org.springframework.beans.factory.annotation.Required;
public class Test {
private String first;
private String a;
Test(String first, String a) {
this.first = first;
}
@Required
void setA(String a) {
this.a = a;
}
}
""",
"""
package demo;
public class Test {
private String first;
private final String a;
Test(String first, String a) {
this.first = first;
this.a = a;
}
}
"""
)
);
}
}

0 comments on commit bb67416

Please sign in to comment.