diff --git a/.gitignore b/.gitignore index 261a2e0..8d9638e 100644 --- a/.gitignore +++ b/.gitignore @@ -46,4 +46,6 @@ gradle-app.setting .classpath .idea/ -out/ \ No newline at end of file +out/ +bin/ +.vscode/ diff --git a/build.gradle.kts b/build.gradle.kts index 18e9ddd..202e55e 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -3,7 +3,7 @@ plugins { } group = "org.openrewrite.recipe" -description = "Open Liberty Migration" +description = "Recipes to migrate to the IBM WebSphere Liberty. Automatically." val rewriteVersion = rewriteRecipe.rewriteVersion.get() dependencies { @@ -12,6 +12,7 @@ dependencies { implementation("org.openrewrite:rewrite-java") implementation("org.openrewrite.recipe:rewrite-java-dependencies") implementation("org.openrewrite.recipe:rewrite-migrate-java") + implementation("org.openrewrite.recipe:rewrite-static-analysis") testImplementation("org.openrewrite:rewrite-java-17") testImplementation("org.openrewrite:rewrite-test") diff --git a/src/main/java/org/openrewrite/java/liberty/ChangeMethodInvocation.java b/src/main/java/org/openrewrite/java/liberty/ChangeMethodInvocation.java new file mode 100644 index 0000000..4a8dea1 --- /dev/null +++ b/src/main/java/org/openrewrite/java/liberty/ChangeMethodInvocation.java @@ -0,0 +1,189 @@ +/* + * Copyright 2023 the original author or authors. + *
+ * 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 + *
+ * https://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 org.openrewrite.java.liberty;
+
+import lombok.EqualsAndHashCode;
+import lombok.Value;
+
+import java.util.Collections;
+import java.util.List;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.openrewrite.*;
+import org.openrewrite.internal.lang.NonNull;
+import org.openrewrite.internal.lang.Nullable;
+import org.openrewrite.java.JavaTemplate;
+import org.openrewrite.java.JavaVisitor;
+import org.openrewrite.java.MethodMatcher;
+import org.openrewrite.java.tree.*;
+import org.openrewrite.java.tree.JavaType.FullyQualified;
+import org.openrewrite.marker.Markers;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+
+/*
+ Development in this class is not complete
+*/
+@Value
+@EqualsAndHashCode(callSuper = true)
+public class ChangeMethodInvocation extends Recipe {
+
+ @Option(displayName = "Method pattern",
+ description = "A method pattern that is used to find matching method invocations.",
+ example = "org.mockito.Matchers anyVararg()")
+ @NonNull
+ String methodPattern;
+
+ @Option(displayName = "New method name",
+ description = "The method name that will replace the existing name.",
+ example = "org.mockito.Matchers any()")
+ @NonNull
+ String newMethodPattern;
+
+ @Option(displayName = "Perform static methd call",
+ description = "When set to `true` the method invocation will be performed statically.",
+ example = "true",
+ required = false)
+ @Nullable
+ Boolean performStaticCall;
+
+ @JsonCreator
+ public ChangeMethodInvocation(@NonNull @JsonProperty("methodPattern") String methodPattern,
+ @NonNull @JsonProperty("newMethodPattern") String newMethodPattern, @Nullable @JsonProperty("performStaticCall") Boolean performStaticCall) {
+ this.methodPattern = methodPattern;
+ this.newMethodPattern = newMethodPattern;
+ this.performStaticCall = performStaticCall;
+ }
+
+
+ @Override
+ public String getDisplayName() {
+ return "Change method invocation";
+ }
+
+ @Override
+ public String getDescription() {
+ return "Rename a method invocation.";
+ }
+
+ @Override
+ public boolean causesAnotherCycle() {
+ return true;
+ }
+
+ @Override
+ public TreeVisitor, ExecutionContext> getVisitor() {
+ return new ChangeMethodInvocationVisitor(new MethodMatcher(methodPattern, false), newMethodPattern);
+ }
+
+ private class ChangeMethodInvocationVisitor extends JavaVisitor
+ * 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
+ *
+ * https://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 org.openrewrite.java.liberty;
+
+import lombok.EqualsAndHashCode;
+import lombok.Value;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import org.openrewrite.ExecutionContext;
+import org.openrewrite.Option;
+import org.openrewrite.Recipe;
+import org.openrewrite.TreeVisitor;
+import org.openrewrite.internal.lang.NonNull;
+import org.openrewrite.java.JavaIsoVisitor;
+import org.openrewrite.java.tree.*;
+
+import com.fasterxml.jackson.annotation.JsonCreator;
+import com.fasterxml.jackson.annotation.JsonProperty;
+
+@Value
+@EqualsAndHashCode(callSuper = true)
+public class ChangeStringLiteral extends Recipe {
+
+ @Option(displayName = "Value pattern",
+ description = "A pattern that is used to find matching string literal.",
+ example = "java:(.*)")
+ @NonNull
+ String valuePattern;
+
+ @Option(displayName = "New value template",
+ description = "The template that will replace the existing value.",
+ example = "org:$1")
+ @NonNull
+ String newValueTemplate;
+
+ @JsonCreator
+ public ChangeStringLiteral(@NonNull @JsonProperty("valuePattern") String valuePattern, @NonNull @JsonProperty("newValueTemplate") String newValueTemplate) {
+ this.valuePattern = valuePattern;
+ this.newValueTemplate = newValueTemplate;
+ }
+
+
+ @Override
+ public String getDisplayName() {
+ return "Change string literal";
+ }
+
+ @Override
+ public String getDescription() {
+ return "Changes the value of a string literal.";
+ }
+
+ @Override
+ public TreeVisitor, ExecutionContext> getVisitor() {
+ return new ChangeStringLiteralVisitor<>(Pattern.compile(valuePattern), newValueTemplate);
+ }
+
+ @Value
+ @EqualsAndHashCode(callSuper = true)
+ public class ChangeStringLiteralVisitor extends JavaIsoVisitor {
+ Pattern valuePattern;
+ String newValueTemplate;
+
+ @Override
+ public J.Literal visitLiteral(J.Literal literal, P p) {
+ String literalValue = literal.getValue().toString();
+ Matcher m = valuePattern.matcher(literalValue);
+ if (m.find()) {
+ literalValue = m.replaceFirst(newValueTemplate);
+ literal = literal.withValue(literalValue).withValueSource(literalValue);
+ }
+ return literal;
+ }
+ }
+
+}
\ No newline at end of file
diff --git a/src/main/java/org/openrewrite/java/liberty/RemoveWas2LibertyNonPortableJndiLookup.java b/src/main/java/org/openrewrite/java/liberty/RemoveWas2LibertyNonPortableJndiLookup.java
new file mode 100644
index 0000000..7d9270b
--- /dev/null
+++ b/src/main/java/org/openrewrite/java/liberty/RemoveWas2LibertyNonPortableJndiLookup.java
@@ -0,0 +1,86 @@
+/*
+ * Copyright 2023 the original author or authors.
+ *
+ * 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
+ *
+ * https://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 org.openrewrite.java.liberty;
+
+import java.util.function.Supplier;
+
+import org.openrewrite.internal.lang.Nullable;
+import org.openrewrite.ExecutionContext;
+import org.openrewrite.Recipe;
+import org.openrewrite.TreeVisitor;
+import org.openrewrite.java.JavaVisitor;
+import org.openrewrite.java.MethodMatcher;
+import org.openrewrite.java.tree.Expression;
+import org.openrewrite.java.tree.J;
+import org.openrewrite.java.tree.MethodCall;
+
+public class RemoveWas2LibertyNonPortableJndiLookup extends Recipe {
+
+ @Override
+ public String getDisplayName() {
+ return "Removes invalid JNDI properties";
+ }
+
+ @Override
+ public String getDescription() {
+ return "Remove the use of invalid JNDI properties from Hashtable.";
+ }
+
+ @Override
+ public TreeVisitor, ExecutionContext> getVisitor() {
+ return new MethodInvocationVisitor();
+ }
+
+ private class MethodInvocationVisitor extends JavaVisitor
+ * 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
+ *
+ * https://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 org.openrewrite.xml.liberty;
+
+import org.openrewrite.internal.lang.Nullable;
+import org.openrewrite.java.marker.JavaProject;
+
+import org.openrewrite.ExecutionContext;
+import org.openrewrite.Recipe;
+import org.openrewrite.SourceFile;
+import org.openrewrite.Tree;
+import org.openrewrite.TreeVisitor;
+
+import java.io.File;
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.List;
+
+public class PersistenceXmlLocationRule extends Recipe {
+
+ @Override
+ public String getDisplayName() {
+ return "Move persistence.xml file.";
+ }
+
+ @Override
+ public String getDescription() {
+ return "This recipes moves persistence.xml files into the root META-INF directory in source folder.";
+ }
+
+ public File getProjectDirectory(File sourceFile, String projectName) {
+ File parent = sourceFile.getParentFile();
+ while (parent != null && !parent.getName().equals(projectName)) {
+ parent = parent.getParentFile();
+ }
+
+ return parent;
+ }
+
+ public List
-# 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
-#
-# https://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.
-#
-
----
diff --git a/src/main/resources/META-INF/rewrite/was-to-liberty.yml b/src/main/resources/META-INF/rewrite/was-to-liberty.yml
new file mode 100644
index 0000000..4887967
--- /dev/null
+++ b/src/main/resources/META-INF/rewrite/was-to-liberty.yml
@@ -0,0 +1,131 @@
+#
+# Copyright 2023 the original author or authors.
+#
+# 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
+#
+# https://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.
+#
+
+########################################################################################################################
+# WebSphere traditional to Liberty
+---
+type: specs.openrewrite.org/v1beta/recipe
+name: org.openrewrite.java.liberty
+displayName: Migrate from WebSphere traditional to Liberty
+description: >
+ Use this category of rules to identify code changes needed when migrating
+ from WebSphere Application Server traditional to Liberty.
+recipeList:
+ - org.openrewrite.java.liberty.RemoveWas2LibertyNonPortableJndiLookup
+ - org.openrewrite.java.liberty.ServerName
+ - org.openrewrite.xml.liberty.AppDDNamespaceRule
+ - org.openrewrite.xml.liberty.ConnectorDDNamespaceRule
+ - org.openrewrite.xml.liberty.EJBDDNamespaceRule
+ - org.openrewrite.xml.liberty.PersistenceXmlLocationRule
+ - org.openrewrite.xml.liberty.WebDDNamespaceRule
+---
+type: specs.openrewrite.org/v1beta/recipe
+name: org.openrewrite.java.liberty.RemoveWas2LibertyNonPortableJndiLookup
+recipeList:
+ - org.openrewrite.java.liberty.RemoveWas2LibertyNonPortableJndiLookup
+---
+type: specs.openrewrite.org/v1beta/recipe
+name: org.openrewrite.java.liberty.ServerName
+recipeList:
+ - org.openrewrite.java.liberty.ChangeMethodInvocation:
+ methodPattern: com.ibm.websphere.runtime.ServerName getDisplayName()
+ newMethodPattern: java.lang.System getProperty("wlp.server.name")
+ performStaticCall: true
+ - org.openrewrite.java.liberty.ChangeMethodInvocation:
+ methodPattern: com.ibm.websphere.runtime.ServerName getFullName()
+ newMethodPattern: java.lang.System getProperty("wlp.server.name")
+ performStaticCall: true
+---
+type: specs.openrewrite.org/v1beta/recipe
+name: org.openrewrite.xml.liberty.AppDDNamespaceRule
+recipeList:
+ - org.openrewrite.xml.ChangeNamespaceValue:
+ elementName: application
+ newValue: http://java.sun.com/xml/ns/j2ee
+ versionMatcher: 1.4
+ searchAllNamespaces: false
+ - org.openrewrite.xml.ChangeNamespaceValue:
+ elementName: application
+ newValue: http://java.sun.com/xml/ns/javaee
+ versionMatcher: 5,6
+ searchAllNamespaces: false
+ - org.openrewrite.xml.ChangeNamespaceValue:
+ elementName: application
+ newValue: http://xmlns.jcp.org/xml/ns/javaee
+ versionMatcher: 7+
+ searchAllNamespaces: false
+---
+type: specs.openrewrite.org/v1beta/recipe
+name: org.openrewrite.xml.liberty.ConnectorDDNamespaceRule
+recipeList:
+ - org.openrewrite.xml.ChangeNamespaceValue:
+ elementName: connector
+ newValue: http://java.sun.com/xml/ns/j2ee
+ versionMatcher: 1.5
+ searchAllNamespaces: false
+ - org.openrewrite.xml.ChangeNamespaceValue:
+ elementName: connector
+ newValue: http://java.sun.com/xml/ns/javaee
+ versionMatcher: 1.6
+ searchAllNamespaces: false
+ - org.openrewrite.xml.ChangeNamespaceValue:
+ elementName: connector
+ newValue: http://xmlns.jcp.org/xml/ns/javaee
+ versionMatcher: 1.7+
+ searchAllNamespaces: false
+---
+type: specs.openrewrite.org/v1beta/recipe
+name: org.openrewrite.xml.liberty.EJBDDNamespaceRule
+recipeList:
+ - org.openrewrite.xml.ChangeNamespaceValue:
+ elementName: ejb-jar
+ newValue: http://java.sun.com/xml/ns/j2ee
+ versionMatcher: 2.1
+ searchAllNamespaces: false
+ - org.openrewrite.xml.ChangeNamespaceValue:
+ elementName: ejb-jar
+ newValue: http://java.sun.com/xml/ns/javaee
+ versionMatcher: 3.0,3.1
+ searchAllNamespaces: false
+ - org.openrewrite.xml.ChangeNamespaceValue:
+ elementName: ejb-jar
+ newValue: http://xmlns.jcp.org/xml/ns/javaee
+ versionMatcher: 3.2+
+ searchAllNamespaces: false
+---
+type: specs.openrewrite.org/v1beta/recipe
+name: org.openrewrite.xml.liberty.PersistenceXmlLocationRule
+recipeList:
+ - org.openrewrite.xml.liberty.PersistenceXmlLocationRule
+---
+type: specs.openrewrite.org/v1beta/recipe
+name: org.openrewrite.xml.liberty.WebDDNamespaceRule
+recipeList:
+ - org.openrewrite.xml.ChangeNamespaceValue:
+ elementName: web-app
+ newValue: http://java.sun.com/xml/ns/j2ee
+ versionMatcher: 2.4
+ searchAllNamespaces: false
+ - org.openrewrite.xml.ChangeNamespaceValue:
+ elementName: web-app
+ newValue: http://java.sun.com/xml/ns/java
+ versionMatcher: 2.5,3.0
+ searchAllNamespaces: false
+ - org.openrewrite.xml.ChangeNamespaceValue:
+ elementName: web-app
+ newValue: http://xmlns.jcp.org/xml/ns/javaee
+ versionMatcher: 3.1+
+ searchAllNamespaces: false
diff --git a/src/test/java/org/openrewrite/.editorconfig b/src/test/java/org/openrewrite/.editorconfig
new file mode 100644
index 0000000..a482493
--- /dev/null
+++ b/src/test/java/org/openrewrite/.editorconfig
@@ -0,0 +1,5 @@
+root = true
+
+[*.java]
+indent_size = 4
+ij_continuation_indent_size = 2
diff --git a/src/test/java/org/openrewrite/java/liberty/InvalidInitialContextTest.java b/src/test/java/org/openrewrite/java/liberty/InvalidInitialContextTest.java
new file mode 100644
index 0000000..b99a4e6
--- /dev/null
+++ b/src/test/java/org/openrewrite/java/liberty/InvalidInitialContextTest.java
@@ -0,0 +1,86 @@
+/*
+ * Copyright 2023 the original author or authors.
+ *
+ * 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
+ *
+ * https://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 org.openrewrite.java.liberty;
+
+import org.junit.jupiter.api.Test;
+import org.openrewrite.test.RewriteTest;
+
+import static org.openrewrite.java.Assertions.java;
+
+public class InvalidInitialContextTest implements RewriteTest {
+
+ String initialContextClass = """
+ package javax.naming;
+
+ public class InitialContext {
+
+ public InitialContext() {
+ }
+
+ public void lookup() {
+ }
+
+ }
+ """;
+
+ @Test
+ void replaceTimeoutTest() {
+ rewriteRun(
+ spec -> spec.recipe(new ChangeStringLiteral("^java:/comp(.*)$", "java:comp$1")),
+ java(initialContextClass),
+ java(
+ """
+ package com.test;
+
+ import javax.naming.InitialContext;
+ import javax.naming.NamingException;
+
+ public class TestDetectInvalidInitialContext {
+ public static final String BAD_ENV = "java:/comp";
+ public static final String GOOD_ENV = "java:comp";
+
+ private void doX(){
+ InitialContext ic = new InitialContext();
+ ic.lookup("java:/comp");
+ ic.lookup("java:/comp/BadEntry");
+ ic.lookup("java:comp/GoodEntry");
+ }
+ }
+ """,
+ """
+ package com.test;
+
+ import javax.naming.InitialContext;
+ import javax.naming.NamingException;
+
+ public class TestDetectInvalidInitialContext {
+ public static final String BAD_ENV = java:comp;
+ public static final String GOOD_ENV = "java:comp";
+
+ private void doX(){
+ InitialContext ic = new InitialContext();
+ ic.lookup(java:comp);
+ ic.lookup(java:comp/BadEntry);
+ ic.lookup("java:comp/GoodEntry");
+ }
+ }
+ """
+ )
+ );
+ }
+
+}
diff --git a/src/test/java/org/openrewrite/java/liberty/RemoveWas2LibertyNonPortableJndiLookupTest.java b/src/test/java/org/openrewrite/java/liberty/RemoveWas2LibertyNonPortableJndiLookupTest.java
new file mode 100644
index 0000000..992e802
--- /dev/null
+++ b/src/test/java/org/openrewrite/java/liberty/RemoveWas2LibertyNonPortableJndiLookupTest.java
@@ -0,0 +1,70 @@
+/*
+ * Copyright 2023 the original author or authors.
+ *
+ * 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
+ *
+ * https://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 org.openrewrite.java.liberty;
+
+import org.junit.jupiter.api.Test;
+import org.openrewrite.config.Environment;
+import org.openrewrite.test.RewriteTest;
+
+import static org.openrewrite.java.Assertions.java;
+
+public class RemoveWas2LibertyNonPortableJndiLookupTest implements RewriteTest {
+
+ @Test
+ void removeInvalidPropertiesTest() {
+ rewriteRun(
+ spec -> spec.recipe(Environment.builder().scanRuntimeClasspath("org.openrewrite.java.liberty").build().activateRecipes("org.openrewrite.java.liberty.RemoveWas2LibertyNonPortableJndiLookup")),
+ java(
+ """
+ package com.ibm;
+
+ import java.util.Hashtable;
+ import javax.naming.InitialContext;
+
+ public class ServerNameUsage {
+
+ public void doX() {
+ Hashtable ht = new Hashtable();
+ ht.put("java.naming.factory.initial", "com.ibm.websphere.naming.WsnInitialContextFactory");
+ ht.put("java.naming.provider.url", "corbaloc:iiop:localhost:2809");
+
+ InitialContext ctx = new InitialContext(ht);
+ }
+
+ }
+ """,
+ """
+ package com.ibm;
+
+ import java.util.Hashtable;
+ import javax.naming.InitialContext;
+
+ public class ServerNameUsage {
+
+ public void doX() {
+ Hashtable ht = new Hashtable();
+
+ InitialContext ctx = new InitialContext(ht);
+ }
+
+ }
+ """
+ )
+ );
+ }
+
+}
diff --git a/src/test/java/org/openrewrite/java/liberty/ServerNameTest.java b/src/test/java/org/openrewrite/java/liberty/ServerNameTest.java
new file mode 100644
index 0000000..1402cde
--- /dev/null
+++ b/src/test/java/org/openrewrite/java/liberty/ServerNameTest.java
@@ -0,0 +1,110 @@
+/*
+ * Copyright 2023 the original author or authors.
+ *
+ * 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
+ *
+ * https://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 org.openrewrite.java.liberty;
+
+import org.junit.jupiter.api.Test;
+import org.openrewrite.config.Environment;
+import org.openrewrite.test.RewriteTest;
+
+import static org.openrewrite.java.Assertions.java;
+
+public class ServerNameTest implements RewriteTest {
+
+ String serverNameClass = """
+ package com.ibm.websphere.runtime;
+
+ public class ServerName {
+
+ public static String getDisplayName() {
+ return "";
+ }
+
+ public static String getFullName() {
+ return "";
+ }
+
+ }
+ """;
+
+ @Test
+ void replaceGetFullNameTest() {
+ rewriteRun(
+ spec -> spec.recipe(Environment.builder().scanRuntimeClasspath("org.openrewrite.java.liberty").build().activateRecipes("org.openrewrite.java.liberty.ServerName")),
+ java(serverNameClass),
+ java(
+ """
+ package com.ibm;
+
+ import com.ibm.websphere.runtime.ServerName;
+
+ public class ServerNameUsage {
+
+ public void doX() {
+ ServerName.getFullName();
+ }
+
+ }
+ """,
+ """
+ package com.ibm;
+
+ public class ServerNameUsage {
+
+ public void doX() {
+ System.getProperty("wlp.server.name");
+ }
+
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void replaceGetDisplayNameTest() {
+ rewriteRun(
+ spec -> spec.recipe(Environment.builder().scanRuntimeClasspath("org.openrewrite.java.liberty").build().activateRecipes("org.openrewrite.java.liberty.ServerName")),
+ java(serverNameClass),
+ java(
+ """
+ package com.ibm;
+
+ import com.ibm.websphere.runtime.ServerName;
+
+ public class ServerNameUsage {
+
+ public void doX() {
+ ServerName.getDisplayName();
+ }
+
+ }
+ """,
+ """
+ package com.ibm;
+
+ public class ServerNameUsage {
+
+ public void doX() {
+ System.getProperty("wlp.server.name");
+ }
+
+ }
+ """
+ )
+ );
+ }
+}
diff --git a/src/test/java/org/openrewrite/xml/liberty/AppDDNamespaceTest.java b/src/test/java/org/openrewrite/xml/liberty/AppDDNamespaceTest.java
new file mode 100644
index 0000000..1aac361
--- /dev/null
+++ b/src/test/java/org/openrewrite/xml/liberty/AppDDNamespaceTest.java
@@ -0,0 +1,295 @@
+/*
+ * Copyright 2023 the original author or authors.
+ *
+ * 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
+ *
+ * https://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 org.openrewrite.xml.liberty;
+
+import org.junit.jupiter.api.Test;
+import org.openrewrite.config.Environment;
+import org.openrewrite.test.RecipeSpec;
+import org.openrewrite.test.RewriteTest;
+
+import static org.openrewrite.xml.Assertions.xml;
+
+class AppDDNamespaceTest implements RewriteTest {
+ @Override
+ public void defaults(RecipeSpec spec) {
+ spec.recipe(
+ Environment.builder()
+ .scanRuntimeClasspath("org.openrewrite.java.liberty")
+ .build()
+ .activateRecipes("org.openrewrite.xml.liberty.AppDDNamespaceRule"));
+ }
+
+ @Test
+ void replaceVersion14Test() {
+ rewriteRun(
+ //language=xml
+ xml(
+ """
+
+ * 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
+ *
+ * https://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 org.openrewrite.xml.liberty;
+
+import org.junit.jupiter.api.Test;
+import org.openrewrite.config.Environment;
+import org.openrewrite.test.RecipeSpec;
+import org.openrewrite.test.RewriteTest;
+
+import static org.openrewrite.xml.Assertions.xml;
+
+ class ConnectorDDNamespaceTest implements RewriteTest {
+ @Override
+ public void defaults(RecipeSpec spec) {
+ spec.recipe(
+ Environment.builder()
+ .scanRuntimeClasspath("org.openrewrite.java.liberty")
+ .build()
+ .activateRecipes("org.openrewrite.xml.liberty.ConnectorDDNamespaceRule"));
+ }
+
+ @Test
+ void replaceVersion15Test() {
+ rewriteRun(
+ //language=xml
+ xml(
+ """
+
+ * 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
+ *
+ * https://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 org.openrewrite.xml.liberty;
+
+import org.junit.jupiter.api.Test;
+import org.openrewrite.config.Environment;
+import org.openrewrite.test.RecipeSpec;
+import org.openrewrite.test.RewriteTest;
+
+import static org.openrewrite.xml.Assertions.xml;
+
+class EJBDDNamespaceTest implements RewriteTest {
+ @Override
+ public void defaults(RecipeSpec spec) {
+ spec.recipe(
+ Environment.builder()
+ .scanRuntimeClasspath("org.openrewrite.java.liberty")
+ .build()
+ .activateRecipes("org.openrewrite.xml.liberty.EJBDDNamespaceRule"));
+ }
+
+ @Test
+ void replaceVersion21Test() {
+ rewriteRun(
+ //language=xml
+ xml(
+ """
+
+ * 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
+ *
+ * https://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 org.openrewrite.xml.liberty;
+
+import org.junit.jupiter.api.Test;
+import org.openrewrite.DocumentExample;
+import org.openrewrite.config.Environment;
+import org.openrewrite.test.RecipeSpec;
+import org.openrewrite.test.RewriteTest;
+
+import java.nio.file.Paths;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.openrewrite.java.Assertions.project;
+import static org.openrewrite.test.SourceSpecs.text;
+
+class PersistenceXmlLocationRuleTest implements RewriteTest {
+ @Override
+ public void defaults(RecipeSpec spec) {
+ spec.recipe(
+ Environment.builder()
+ .scanRuntimeClasspath("org.openrewrite.java.liberty")
+ .build()
+ .activateRecipes("org.openrewrite.xml.liberty.PersistenceXmlLocationRule"));
+ }
+
+ @DocumentExample
+ @Test
+ void movePersistenceXMLFileTest() {
+ rewriteRun(
+ text(
+ //language=xml
+ """
+
+ * 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
+ *
+ * https://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 org.openrewrite.xml.liberty;
+
+import org.junit.jupiter.api.Test;
+import org.openrewrite.config.Environment;
+import org.openrewrite.test.RecipeSpec;
+import org.openrewrite.test.RewriteTest;
+
+import static org.openrewrite.xml.Assertions.xml;
+
+class WebDDNamespaceTest implements RewriteTest {
+ @Override
+ public void defaults(RecipeSpec spec) {
+ spec.recipe(
+ Environment.builder()
+ .scanRuntimeClasspath("org.openrewrite.java.liberty")
+ .build()
+ .activateRecipes("org.openrewrite.xml.liberty.WebDDNamespaceRule"));
+ }
+
+ @Test
+ void replaceVersion24Test() {
+ rewriteRun(
+ //language=xml
+ xml(
+ """
+