Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move HtmlUnit migrations to rewrite-testing-frameworks #643

Merged
merged 2 commits into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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");
}
}
}
"""
)
);
}
}