Skip to content

Commit 502c071

Browse files
AddsProperty to exclude JPA now
1 parent 95c5969 commit 502c071

File tree

4 files changed

+268
-5
lines changed

4 files changed

+268
-5
lines changed

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050

5151
<org.openrewrite.recipe.version>2.6.3</org.openrewrite.recipe.version>
5252
<org.springframework.boot.version>3.2.2</org.springframework.boot.version>
53-
<software.xdev.spring.data.eclipse.store.version>1.0.0</software.xdev.spring.data.eclipse.store.version>
53+
<software.xdev.spring.data.eclipse.store.version>1.0.1</software.xdev.spring.data.eclipse.store.version>
5454
<lombok.version>1.18.30</lombok.version>
5555
</properties>
5656

@@ -115,7 +115,7 @@
115115
</dependency>
116116
<dependency>
117117
<groupId>org.openrewrite</groupId>
118-
<artifactId>rewrite-maven</artifactId>
118+
<artifactId>rewrite-properties</artifactId>
119119
</dependency>
120120
<dependency>
121121
<groupId>org.projectlombok</groupId>
@@ -136,7 +136,6 @@
136136
<version>${software.xdev.spring.data.eclipse.store.version}</version>
137137
<scope>runtime</scope>
138138
</dependency>
139-
140139
<dependency>
141140
<groupId>org.openrewrite</groupId>
142141
<artifactId>rewrite-java-17</artifactId>
@@ -152,7 +151,6 @@
152151
<artifactId>slf4j-simple</artifactId>
153152
<scope>test</scope>
154153
</dependency>
155-
156154
</dependencies>
157155

158156
<build>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* Copyright © 2023 XDEV Software (https://xdev.software)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package software.xdev.spring.data.eclipse.store;
17+
18+
import org.jetbrains.annotations.NotNull;
19+
import org.openrewrite.ExecutionContext;
20+
import org.openrewrite.Option;
21+
import org.openrewrite.Recipe;
22+
import org.openrewrite.TreeVisitor;
23+
import org.openrewrite.internal.lang.Nullable;
24+
import org.openrewrite.properties.AddProperty;
25+
26+
import lombok.AllArgsConstructor;
27+
import lombok.EqualsAndHashCode;
28+
import lombok.Getter;
29+
import lombok.NoArgsConstructor;
30+
import lombok.Setter;
31+
32+
33+
@Getter
34+
@Setter
35+
@AllArgsConstructor
36+
@NoArgsConstructor
37+
@EqualsAndHashCode(callSuper = true)
38+
public class AddPropertyIfClassExists extends Recipe
39+
{
40+
@Option(displayName = "Class that must exist in the classpath to add the property",
41+
description = "Name of the class that must exist in the classpath to execute the recipe to add a property in the properties file.",
42+
example = "software.xdev")
43+
private String className;
44+
45+
@Option(
46+
displayName = "Property key",
47+
description = "The property key to add.",
48+
example = "management.metrics.enable.process.files"
49+
)
50+
private String property;
51+
@Option(
52+
displayName = "Property value",
53+
description = "The value of the new property key."
54+
)
55+
private String value;
56+
@Option(
57+
displayName = "Optional comment to be prepended to the property",
58+
description = "A comment that will be added to the new property.",
59+
required = false,
60+
example = "This is a comment"
61+
)
62+
private @Nullable String comment;
63+
@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+
required = false,
68+
example = ":"
69+
)
70+
private @Nullable String delimiter;
71+
72+
@Override
73+
public @NotNull String getDisplayName()
74+
{
75+
return "AddValueToAnnotationIfDependencyExists";
76+
}
77+
78+
@Override
79+
public @NotNull String getDescription()
80+
{
81+
return "::::TODO:::Add the a new annotation to an existing annotation.";
82+
}
83+
84+
@Override
85+
public @NotNull TreeVisitor<?, ExecutionContext> getVisitor()
86+
{
87+
if(!this.doesClasspathContainClass(this.className))
88+
{
89+
return TreeVisitor.noop();
90+
}
91+
return new AddProperty(
92+
this.property, this.value, this.comment, this.delimiter
93+
).getVisitor();
94+
}
95+
96+
private boolean doesClasspathContainClass(final String classToCheck)
97+
{
98+
try
99+
{
100+
Class.forName(classToCheck, false, this.getClass().getClassLoader());
101+
return true;
102+
}
103+
catch(final ClassNotFoundException e)
104+
{
105+
return false;
106+
}
107+
}
108+
}

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

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,17 @@ recipeList:
1616
classPath: 'spring-data-eclipse-store'
1717
annotationTypeToAddSimpleName: 'EnableEclipseStoreRepositories'
1818

19+
- software.xdev.spring.data.eclipse.store.AddPropertyIfClassExists:
20+
className: 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration'
21+
property: 'spring.autoconfigure.exclude'
22+
value: 'org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration'
23+
comment: 'This suppresses the use of JPA in the project. It is needed to let EclipseStore coexist with JPA.'
24+
delimiter: =
25+
1926
- org.openrewrite.maven.AddDependency:
2027
groupId: software.xdev
2128
artifactId: spring-data-eclipse-store
22-
version: 1.0.1
29+
version: 1.0.2
2330
acceptTransitive: true
2431

2532
- org.openrewrite.maven.AddPlugin:
@@ -36,3 +43,34 @@ recipeList:
3643
- org.openrewrite.java.RemoveAnnotation:
3744
# https://docs.openrewrite.org/reference/method-patterns
3845
annotationPattern: '@org.springframework.data.jpa.repository.Query *(..)'
46+
47+
- org.openrewrite.java.RemoveAnnotation:
48+
annotationPattern: '@org.springframework.data.jpa.repository.config.EnableJpaRepositories *(..)'
49+
50+
# Change all the spring framework repositories to specifically use EclipseStore repositories.
51+
52+
- org.openrewrite.java.ChangeType:
53+
oldFullyQualifiedTypeName: org.springframework.data.repository.Repository
54+
newFullyQualifiedTypeName: software.xdev.spring.data.eclipse.store.repository.interfaces.EclipseStoreCustomRepository
55+
56+
- org.openrewrite.java.ChangeType:
57+
oldFullyQualifiedTypeName: org.springframework.data.repository.CrudRepository
58+
newFullyQualifiedTypeName: software.xdev.spring.data.eclipse.store.repository.interfaces.EclipseStoreCrudRepository
59+
60+
- org.openrewrite.java.ChangeType:
61+
oldFullyQualifiedTypeName: org.springframework.data.repository.ListCrudRepository
62+
newFullyQualifiedTypeName: software.xdev.spring.data.eclipse.store.repository.interfaces.EclipseStoreListCrudRepository
63+
64+
- org.openrewrite.java.ChangeType:
65+
oldFullyQualifiedTypeName: org.springframework.data.repository.PagingAndSortingRepositoryRepository
66+
newFullyQualifiedTypeName: software.xdev.spring.data.eclipse.store.repository.interfaces.EclipseStorePagingAndSortingRepositoryRepository
67+
68+
- org.openrewrite.java.ChangeType:
69+
oldFullyQualifiedTypeName: org.springframework.data.repository.ListPagingAndSortingRepositoryRepository
70+
newFullyQualifiedTypeName: software.xdev.spring.data.eclipse.store.repository.interfaces.EclipseStoreListPagingAndSortingRepositoryRepository
71+
72+
# Change all the JPA repositories to specifically use EclipseStore repositories.
73+
74+
- org.openrewrite.java.ChangeType:
75+
oldFullyQualifiedTypeName: org.springframework.data.jpa.repository.JpaRepository
76+
newFullyQualifiedTypeName: software.xdev.spring.data.eclipse.store.repository.interfaces.EclipseStoreRepository
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
* Copyright © 2023 XDEV Software (https://xdev.software)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package software.xdev.spring.data.eclipse.store;
17+
18+
import static org.openrewrite.properties.Assertions.properties;
19+
20+
import org.junit.jupiter.api.Test;
21+
import org.openrewrite.test.RecipeSpec;
22+
import org.openrewrite.test.RewriteTest;
23+
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
24+
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
25+
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
26+
27+
28+
class AddPropertyIfClassExistsTest implements RewriteTest
29+
{
30+
31+
@Override
32+
public void defaults(final RecipeSpec recipeSpec)
33+
{
34+
recipeSpec
35+
.recipe(new AddPropertyIfClassExists(
36+
HibernateJpaAutoConfiguration.class.getName(),
37+
"spring.autoconfigure.exclude",
38+
DataSourceAutoConfiguration.class.getName()
39+
+ ","
40+
+ DataSourceTransactionManagerAutoConfiguration.class.getName()
41+
+ ","
42+
+ HibernateJpaAutoConfiguration.class.getName(),
43+
"",
44+
"="));
45+
}
46+
47+
@Test
48+
void testSimple()
49+
{
50+
this.rewriteRun
51+
(
52+
properties(
53+
"",
54+
"""
55+
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration
56+
"""
57+
)
58+
);
59+
}
60+
61+
/**
62+
* 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
64+
* works now.
65+
* <p>
66+
* Might change in the future.
67+
* </p>
68+
*/
69+
@Test
70+
void testMultipleProperties()
71+
{
72+
this.rewriteRun
73+
(
74+
properties(
75+
"",
76+
"""
77+
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration
78+
"""
79+
),
80+
properties(
81+
"",
82+
"""
83+
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration
84+
"""
85+
)
86+
);
87+
}
88+
89+
@Test
90+
void testExisting()
91+
{
92+
this.rewriteRun
93+
(
94+
properties(
95+
"""
96+
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration
97+
"""
98+
)
99+
);
100+
}
101+
102+
@Test
103+
void testClassNotExisting()
104+
{
105+
this.rewriteRun
106+
(
107+
recipeSpec ->
108+
recipeSpec.recipe(new AddPropertyIfClassExists(
109+
"not.existing.Class",
110+
"spring.autoconfigure.exclude",
111+
"DummyValue",
112+
"",
113+
"=")),
114+
properties(
115+
""
116+
)
117+
);
118+
}
119+
}

0 commit comments

Comments
 (0)