diff --git a/build.gradle.kts b/build.gradle.kts index d29963509..c43a20a97 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -67,6 +67,7 @@ dependencies { testRuntimeOnly("net.datafaker:datafaker:latest.release") { exclude(group = "org.yaml", module = "snakeyaml") } + testRuntimeOnly("net.sourceforge.htmlunit:htmlunit:2.+") testRuntimeOnly("org.mockito.kotlin:mockito-kotlin:latest.release") testRuntimeOnly("org.testcontainers:testcontainers:latest.release") testRuntimeOnly("org.testcontainers:nginx:latest.release") diff --git a/src/main/resources/META-INF/rewrite/htmlunit.yml b/src/main/resources/META-INF/rewrite/htmlunit.yml new file mode 100644 index 000000000..3995efa2f --- /dev/null +++ b/src/main/resources/META-INF/rewrite/htmlunit.yml @@ -0,0 +1,45 @@ +# +# Copyright 2024 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. +# +--- +type: specs.openrewrite.org/v1beta/recipe +name: org.openrewrite.java.testing.htmlunit.UpgradeHtmlUnit_3 +displayName: Migrate to HtmlUnit 3.x +description: >- + Automates the HtmlUnit [migration guide](https://htmlunit.sourceforge.io/migration.html) from 2.x to 3.x. +recipeList: + - org.openrewrite.java.dependencies.ChangeDependency: + oldGroupId: net.sourceforge.htmlunit + oldArtifactId: '*' + newGroupId: org.htmlunit + newVersion: 3.x + - org.openrewrite.java.dependencies.ChangeDependency: + oldGroupId: com.gargoylesoftware + oldArtifactId: HTMLUnit + newGroupId: org.htmlunit + newArtifactId: htmlunit + newVersion: 3.x + - org.openrewrite.java.ChangePackage: + oldPackageName: com.gargoylesoftware.htmlunit + newPackageName: org.htmlunit + recursive: true + - org.openrewrite.java.ChangeMethodName: + methodPattern: org.htmlunit.html.HtmlInput getValueAttribute() + newMethodName: getValue + matchOverrides: true + - org.openrewrite.java.ChangeMethodName: + methodPattern: org.htmlunit.html.HtmlInput setValueAttribute(String) + newMethodName: setValue + matchOverrides: true diff --git a/src/test/java/org/openrewrite/java/testing/htmlunit/UpgradeHtmlUnit3Test.java b/src/test/java/org/openrewrite/java/testing/htmlunit/UpgradeHtmlUnit3Test.java new file mode 100644 index 000000000..f4de2f45d --- /dev/null +++ b/src/test/java/org/openrewrite/java/testing/htmlunit/UpgradeHtmlUnit3Test.java @@ -0,0 +1,90 @@ +/* + * 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.testing.htmlunit; + +import org.junit.jupiter.api.Test; +import org.openrewrite.DocumentExample; +import org.openrewrite.java.JavaParser; +import org.openrewrite.test.RecipeSpec; +import org.openrewrite.test.RewriteTest; + +import static org.openrewrite.java.Assertions.java; + +class UpgradeHtmlUnit3Test implements RewriteTest { + + @Override + public void defaults(RecipeSpec spec) { + spec + .parser(JavaParser.fromJavaVersion().classpath("htmlunit")) + .recipeFromResource("/META-INF/rewrite/htmlunit.yml", "org.openrewrite.java.testing.htmlunit.UpgradeHtmlUnit_3"); + } + + @Test + @DocumentExample + void shouldUpdateHtmlUnit() { + rewriteRun( + //language=java + java( + """ + import com.gargoylesoftware.htmlunit.WebClient; + import com.gargoylesoftware.htmlunit.html.HtmlForm; + import com.gargoylesoftware.htmlunit.html.HtmlInput; + import com.gargoylesoftware.htmlunit.html.HtmlPage; + + import java.io.IOException; + + public class HtmlUnitUse { + void run() throws IOException { + try (WebClient webClient = new WebClient()) { + HtmlPage page = webClient.getPage("https://htmlunit.sourceforge.io/"); + HtmlForm form = page.getFormByName("config"); + HtmlInput a = form.getInputByName("a"); + String value = a.getValueAttribute(); + assert "".equals(value); + a.setAttribute("value", "up2"); + a.setAttribute("value2", "leave"); + a.setValueAttribute("updated"); + } + } + } + """, + """ + import org.htmlunit.WebClient; + import org.htmlunit.html.HtmlForm; + import org.htmlunit.html.HtmlInput; + import org.htmlunit.html.HtmlPage; + + import java.io.IOException; + + public class HtmlUnitUse { + void run() throws IOException { + try (WebClient webClient = new WebClient()) { + HtmlPage page = webClient.getPage("https://htmlunit.sourceforge.io/"); + HtmlForm form = page.getFormByName("config"); + HtmlInput a = form.getInputByName("a"); + String value = a.getValue(); + assert "".equals(value); + a.setAttribute("value", "up2"); + a.setAttribute("value2", "leave"); + a.setValue("updated"); + } + } + } + """ + ) + ); + } +}