-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
95c5969
commit 502c071
Showing
4 changed files
with
268 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
...ation/src/main/java/software/xdev/spring/data/eclipse/store/AddPropertyIfClassExists.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
119 changes: 119 additions & 0 deletions
119
...n/src/test/java/software/xdev/spring/data/eclipse/store/AddPropertyIfClassExistsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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( | ||
"" | ||
) | ||
); | ||
} | ||
} |