Skip to content

Commit

Permalink
Move HtmlUnit migrations to rewrite-testing-frameworks (#643)
Browse files Browse the repository at this point in the history
* Move HtmlUnit migrations to rewrite-testing-frameworks

Coming from rewrite-jenkins

* Add explicit new 3.x versions
  • Loading branch information
timtebeek authored Nov 23, 2024
1 parent a530dbe commit 0f8f838
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 0 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
45 changes: 45 additions & 0 deletions src/main/resources/META-INF/rewrite/htmlunit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#
# Copyright 2024 the original author or authors.
# <p>
# 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
# <p>
# https://www.apache.org/licenses/LICENSE-2.0
# <p>
# 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Copyright 2023 the original author or authors.
* <p>
* 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
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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");
}
}
}
"""
)
);
}
}

0 comments on commit 0f8f838

Please sign in to comment.