From 779c1282d6ebff4829610a3d549b99718df12c52 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Wed, 27 Nov 2024 16:45:25 +0100 Subject: [PATCH] Restore HtmlUnit to prevent circular dependency --- build.gradle.kts | 2 +- .../resources/META-INF/rewrite/htmlunit.yml | 45 ++++++++++ .../htmlunit/UpgradeHtmlUnit3Test.java | 90 +++++++++++++++++++ 3 files changed, 136 insertions(+), 1 deletion(-) create mode 100644 src/main/resources/META-INF/rewrite/htmlunit.yml create mode 100644 src/test/java/org/openrewrite/java/testing/htmlunit/UpgradeHtmlUnit3Test.java diff --git a/build.gradle.kts b/build.gradle.kts index 418d8ec..d9dfcab 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -18,7 +18,6 @@ dependencies { implementation("org.openrewrite:rewrite-maven") implementation("org.openrewrite:rewrite-yaml") implementation("org.openrewrite.recipe:rewrite-java-dependencies:$rewriteVersion") - implementation("org.openrewrite.recipe:rewrite-testing-frameworks:$rewriteVersion") runtimeOnly("org.openrewrite:rewrite-java-11") testImplementation("org.ow2.asm:asm:latest.release") @@ -36,5 +35,6 @@ dependencies { testRuntimeOnly("com.github.spotbugs:spotbugs-annotations:4.7.0") testRuntimeOnly("com.google.code.findbugs:jsr305:3.0.2") + testRuntimeOnly("net.sourceforge.htmlunit:htmlunit:2.+") testRuntimeOnly("org.slf4j:slf4j-simple:1.7.36") } 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 0000000..3995efa --- /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 0000000..f4de2f4 --- /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"); + } + } + } + """ + ) + ); + } +}