Skip to content

Commit

Permalink
AddsProperty to exclude JPA now
Browse files Browse the repository at this point in the history
  • Loading branch information
JohannesRabauer committed Feb 6, 2024
1 parent 95c5969 commit 502c071
Show file tree
Hide file tree
Showing 4 changed files with 268 additions and 5 deletions.
6 changes: 2 additions & 4 deletions spring-data-eclipse-store-migration/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@

<org.openrewrite.recipe.version>2.6.3</org.openrewrite.recipe.version>
<org.springframework.boot.version>3.2.2</org.springframework.boot.version>
<software.xdev.spring.data.eclipse.store.version>1.0.0</software.xdev.spring.data.eclipse.store.version>
<software.xdev.spring.data.eclipse.store.version>1.0.1</software.xdev.spring.data.eclipse.store.version>
<lombok.version>1.18.30</lombok.version>
</properties>

Expand Down Expand Up @@ -115,7 +115,7 @@
</dependency>
<dependency>
<groupId>org.openrewrite</groupId>
<artifactId>rewrite-maven</artifactId>
<artifactId>rewrite-properties</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
Expand All @@ -136,7 +136,6 @@
<version>${software.xdev.spring.data.eclipse.store.version}</version>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>org.openrewrite</groupId>
<artifactId>rewrite-java-17</artifactId>
Expand All @@ -152,7 +151,6 @@
<artifactId>slf4j-simple</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* Copyright © 2023 XDEV Software (https://xdev.software)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package software.xdev.spring.data.eclipse.store;

import org.jetbrains.annotations.NotNull;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Option;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.properties.AddProperty;

import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;


@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class AddPropertyIfClassExists extends Recipe
{
@Option(displayName = "Class that must exist in the classpath to add the property",
description = "Name of the class that must exist in the classpath to execute the recipe to add a property in the properties file.",
example = "software.xdev")
private String className;

@Option(
displayName = "Property key",
description = "The property key to add.",
example = "management.metrics.enable.process.files"
)
private String property;
@Option(
displayName = "Property value",
description = "The value of the new property key."
)
private String value;
@Option(
displayName = "Optional comment to be prepended to the property",
description = "A comment that will be added to the new property.",
required = false,
example = "This is a comment"
)
private @Nullable String comment;
@Option(
displayName = "Optional delimiter",
description = "Property entries support different delimiters (`=`, `:`, or whitespace). The default value is "
+ "`=` unless provided the delimiter of the new property entry.",
required = false,
example = ":"
)
private @Nullable String delimiter;

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

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

@Override
public @NotNull TreeVisitor<?, ExecutionContext> getVisitor()
{
if(!this.doesClasspathContainClass(this.className))
{
return TreeVisitor.noop();
}
return new AddProperty(
this.property, this.value, this.comment, this.delimiter
).getVisitor();
}

private boolean doesClasspathContainClass(final String classToCheck)
{
try
{
Class.forName(classToCheck, false, this.getClass().getClassLoader());
return true;
}
catch(final ClassNotFoundException e)
{
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,17 @@ recipeList:
classPath: 'spring-data-eclipse-store'
annotationTypeToAddSimpleName: 'EnableEclipseStoreRepositories'

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

- org.openrewrite.maven.AddDependency:
groupId: software.xdev
artifactId: spring-data-eclipse-store
version: 1.0.1
version: 1.0.2
acceptTransitive: true

- org.openrewrite.maven.AddPlugin:
Expand All @@ -36,3 +43,34 @@ recipeList:
- org.openrewrite.java.RemoveAnnotation:
# https://docs.openrewrite.org/reference/method-patterns
annotationPattern: '@org.springframework.data.jpa.repository.Query *(..)'

- org.openrewrite.java.RemoveAnnotation:
annotationPattern: '@org.springframework.data.jpa.repository.config.EnableJpaRepositories *(..)'

# Change all the spring framework repositories to specifically use EclipseStore repositories.

- org.openrewrite.java.ChangeType:
oldFullyQualifiedTypeName: org.springframework.data.repository.Repository
newFullyQualifiedTypeName: software.xdev.spring.data.eclipse.store.repository.interfaces.EclipseStoreCustomRepository

- org.openrewrite.java.ChangeType:
oldFullyQualifiedTypeName: org.springframework.data.repository.CrudRepository
newFullyQualifiedTypeName: software.xdev.spring.data.eclipse.store.repository.interfaces.EclipseStoreCrudRepository

- org.openrewrite.java.ChangeType:
oldFullyQualifiedTypeName: org.springframework.data.repository.ListCrudRepository
newFullyQualifiedTypeName: software.xdev.spring.data.eclipse.store.repository.interfaces.EclipseStoreListCrudRepository

- org.openrewrite.java.ChangeType:
oldFullyQualifiedTypeName: org.springframework.data.repository.PagingAndSortingRepositoryRepository
newFullyQualifiedTypeName: software.xdev.spring.data.eclipse.store.repository.interfaces.EclipseStorePagingAndSortingRepositoryRepository

- org.openrewrite.java.ChangeType:
oldFullyQualifiedTypeName: org.springframework.data.repository.ListPagingAndSortingRepositoryRepository
newFullyQualifiedTypeName: software.xdev.spring.data.eclipse.store.repository.interfaces.EclipseStoreListPagingAndSortingRepositoryRepository

# Change all the JPA repositories to specifically use EclipseStore repositories.

- org.openrewrite.java.ChangeType:
oldFullyQualifiedTypeName: org.springframework.data.jpa.repository.JpaRepository
newFullyQualifiedTypeName: software.xdev.spring.data.eclipse.store.repository.interfaces.EclipseStoreRepository
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* Copyright © 2023 XDEV Software (https://xdev.software)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package software.xdev.spring.data.eclipse.store;

import static org.openrewrite.properties.Assertions.properties;

import org.junit.jupiter.api.Test;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;


class AddPropertyIfClassExistsTest implements RewriteTest
{

@Override
public void defaults(final RecipeSpec recipeSpec)
{
recipeSpec
.recipe(new AddPropertyIfClassExists(
HibernateJpaAutoConfiguration.class.getName(),
"spring.autoconfigure.exclude",
DataSourceAutoConfiguration.class.getName()
+ ","
+ DataSourceTransactionManagerAutoConfiguration.class.getName()
+ ","
+ HibernateJpaAutoConfiguration.class.getName(),
"",
"="));
}

@Test
void testSimple()
{
this.rewriteRun
(
properties(
"",
"""
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration
"""
)
);
}

/**
* It's not clear if this is a desired behavior, but since the Open Rewrite Recipe
* {@link org.openrewrite.properties.AddProperty} is doing this, and we are only using this recipe, this is how it
* works now.
* <p>
* Might change in the future.
* </p>
*/
@Test
void testMultipleProperties()
{
this.rewriteRun
(
properties(
"",
"""
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration
"""
),
properties(
"",
"""
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration
"""
)
);
}

@Test
void testExisting()
{
this.rewriteRun
(
properties(
"""
spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration,org.springframework.boot.autoconfigure.jdbc.DataSourceTransactionManagerAutoConfiguration,org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration
"""
)
);
}

@Test
void testClassNotExisting()
{
this.rewriteRun
(
recipeSpec ->
recipeSpec.recipe(new AddPropertyIfClassExists(
"not.existing.Class",
"spring.autoconfigure.exclude",
"DummyValue",
"",
"=")),
properties(
""
)
);
}
}

0 comments on commit 502c071

Please sign in to comment.