Skip to content

Commit da2121d

Browse files
Changed AddPropertyIfClassExists to AddSpringPropertyIfClassExists
1 parent bbaad19 commit da2121d

File tree

4 files changed

+81
-30
lines changed

4 files changed

+81
-30
lines changed

spring-data-eclipse-store-migration/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@
114114
<artifactId>rewrite-java</artifactId>
115115
</dependency>
116116
<dependency>
117-
<groupId>org.openrewrite</groupId>
118-
<artifactId>rewrite-properties</artifactId>
117+
<groupId>org.openrewrite.recipe</groupId>
118+
<artifactId>rewrite-spring</artifactId>
119119
</dependency>
120120
<dependency>
121121
<groupId>org.projectlombok</groupId>
Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@
1515
*/
1616
package software.xdev.spring.data.eclipse.store;
1717

18+
import java.util.List;
19+
1820
import org.jetbrains.annotations.NotNull;
1921
import org.openrewrite.ExecutionContext;
2022
import org.openrewrite.Option;
2123
import org.openrewrite.Recipe;
2224
import org.openrewrite.TreeVisitor;
2325
import org.openrewrite.internal.lang.Nullable;
24-
import org.openrewrite.properties.AddProperty;
26+
import org.openrewrite.java.spring.AddSpringProperty;
2527

2628
import lombok.AllArgsConstructor;
2729
import lombok.EqualsAndHashCode;
@@ -35,7 +37,7 @@
3537
@AllArgsConstructor
3638
@NoArgsConstructor
3739
@EqualsAndHashCode(callSuper = true)
38-
public class AddPropertyIfClassExists extends Recipe
40+
public class AddSpringPropertyIfClassExists extends Recipe
3941
{
4042
@Option(displayName = "Class that must exist in the classpath to add the property",
4143
description = "Name of the class that must exist in the classpath to execute the recipe to add a property in the properties file.",
@@ -50,7 +52,8 @@ public class AddPropertyIfClassExists extends Recipe
5052
private String property;
5153
@Option(
5254
displayName = "Property value",
53-
description = "The value of the new property key."
55+
description = "The value of the new property key.",
56+
example = "true"
5457
)
5558
private String value;
5659
@Option(
@@ -61,24 +64,25 @@ public class AddPropertyIfClassExists extends Recipe
6164
)
6265
private @Nullable String comment;
6366
@Option(
64-
displayName = "Optional delimiter",
65-
description = "Property entries support different delimiters (`=`, `:`, or whitespace). The default value is "
66-
+ "`=` unless provided the delimiter of the new property entry.",
67+
displayName = "Optional list of file path matcher",
68+
description = "Each value in this list represents a glob expression that is used to match which files will be "
69+
+ "modified. If this value is not present, this recipe will query the execution context for reasonable "
70+
+ "defaults. (\"**/application.yml\", \"**/application.yml\", and \"**/application.properties\".",
6771
required = false,
68-
example = ":"
72+
example = "[\"**/application.yml\"]"
6973
)
70-
private @Nullable String delimiter;
74+
private @Nullable List<String> pathExpressions;
7175

7276
@Override
7377
public @NotNull String getDisplayName()
7478
{
75-
return "AddPropertyIfClassExists";
79+
return "AddSpringPropertyIfClassExists";
7680
}
7781

7882
@Override
7983
public @NotNull String getDescription()
8084
{
81-
return "Add a property to the properties file if a specific class exists.";
85+
return "Add a spring property to the properties file if a specific class exists.";
8286
}
8387

8488
@Override
@@ -88,8 +92,8 @@ public class AddPropertyIfClassExists extends Recipe
8892
{
8993
return TreeVisitor.noop();
9094
}
91-
return new AddProperty(
92-
this.property, this.value, this.comment, this.delimiter
95+
return new AddSpringProperty(
96+
this.property, this.value, this.comment, this.pathExpressions
9397
).getVisitor();
9498
}
9599

spring-data-eclipse-store-migration/src/main/resources/META-INF/rewrite/rewrite.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,11 @@ recipeList:
1616
classPath: 'spring-data-eclipse-store'
1717
annotationTypeToAddSimpleName: 'EnableEclipseStoreRepositories'
1818

19-
- software.xdev.spring.data.eclipse.store.AddPropertyIfClassExists:
19+
- software.xdev.spring.data.eclipse.store.AddSpringPropertyIfClassExists:
2020
className: 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration'
2121
property: 'spring.autoconfigure.exclude'
2222
value: 'org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration'
2323
comment: 'This suppresses the use of JPA in the project. It is needed to let EclipseStore coexist with JPA.'
24-
delimiter: =
2524

2625
- org.openrewrite.maven.AddDependency:
2726
groupId: software.xdev
Lines changed: 62 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package software.xdev.spring.data.eclipse.store;
1717

1818
import static org.openrewrite.properties.Assertions.properties;
19+
import static org.openrewrite.yaml.Assertions.yaml;
1920

2021
import org.junit.jupiter.api.Test;
2122
import org.openrewrite.test.RecipeSpec;
@@ -25,42 +26,84 @@
2526
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
2627

2728

28-
class AddPropertyIfClassExistsTest implements RewriteTest
29+
class AddSpringPropertyIfClassExistsTest implements RewriteTest
2930
{
3031

3132
@Override
3233
public void defaults(final RecipeSpec recipeSpec)
3334
{
3435
recipeSpec
35-
.recipe(new AddPropertyIfClassExists(
36+
.recipe(new AddSpringPropertyIfClassExists(
3637
HibernateJpaAutoConfiguration.class.getName(),
3738
"spring.autoconfigure.exclude",
3839
DataSourceAutoConfiguration.class.getName()
3940
+ ","
4041
+ DataSourceTransactionManagerAutoConfiguration.class.getName()
4142
+ ","
4243
+ HibernateJpaAutoConfiguration.class.getName(),
43-
"",
44-
"="));
44+
"", null));
4545
}
4646

4747
@Test
48-
void testSimple()
48+
void testSimpleProperties()
4949
{
5050
this.rewriteRun
5151
(
5252
properties(
5353
"",
5454
"""
5555
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration
56-
"""
56+
""",
57+
s -> s.path("application.properties")
58+
)
59+
);
60+
}
61+
62+
@Test
63+
void testSimpleYaml()
64+
{
65+
this.rewriteRun
66+
(
67+
yaml(
68+
"",
69+
"""
70+
spring:
71+
autoconfigure:
72+
exclude: org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration
73+
""",
74+
s -> s.path("application.yml")
75+
)
76+
);
77+
}
78+
79+
@Test
80+
void testWrongFileNameYaml()
81+
{
82+
this.rewriteRun
83+
(
84+
yaml(
85+
"",
86+
s -> s.path("random.yml")
87+
)
88+
);
89+
}
90+
91+
@Test
92+
void testWrongFileNameProperties()
93+
{
94+
this.rewriteRun
95+
(
96+
properties(
97+
"",
98+
s -> s.path("random.properties")
5799
)
58100
);
59101
}
60102

61103
/**
62104
* It's not clear if this is a desired behavior, but since the Open Rewrite Recipe
63-
* {@link org.openrewrite.properties.AddProperty} is doing this, and we are only using this recipe, this is how it
105+
* {@link org.openrewrite.java.spring.AddSpringProperty} is doing this, and we are only using this recipe, this is
106+
* how it
64107
* works now.
65108
* <p>
66109
* Might change in the future.
@@ -75,13 +118,17 @@ void testMultipleProperties()
75118
"",
76119
"""
77120
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration
78-
"""
121+
""",
122+
s -> s.path("application.properties")
79123
),
80-
properties(
124+
yaml(
81125
"",
82126
"""
83-
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration
84-
"""
127+
spring:
128+
autoconfigure:
129+
exclude: org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration
130+
""",
131+
s -> s.path("application.yml")
85132
)
86133
);
87134
}
@@ -105,14 +152,15 @@ void testClassNotExisting()
105152
this.rewriteRun
106153
(
107154
recipeSpec ->
108-
recipeSpec.recipe(new AddPropertyIfClassExists(
155+
recipeSpec.recipe(new AddSpringPropertyIfClassExists(
109156
"not.existing.Class",
110157
"spring.autoconfigure.exclude",
111158
"DummyValue",
112159
"",
113-
"=")),
160+
null)),
114161
properties(
115-
""
162+
"",
163+
s -> s.path("application.properties")
116164
)
117165
);
118166
}

0 commit comments

Comments
 (0)