Skip to content

Commit

Permalink
Relocate UpgradeHtmlUnit_3_3_0, JavaxAnnotationsToSpotbugs
Browse files Browse the repository at this point in the history
Rewrite test in java. Issue #4
  • Loading branch information
sghill committed Jun 29, 2023
1 parent f20ce76 commit 4ad665a
Show file tree
Hide file tree
Showing 5 changed files with 282 additions and 281 deletions.
4 changes: 2 additions & 2 deletions src/main/resources/META-INF/rewrite/rewrite.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

---
type: specs.openrewrite.org/v1beta/recipe
name: net.sghill.jenkins.rewrite.UpgradeHtmlUnit_3_3_0
name: org.openrewrite.jenkins.UpgradeHtmlUnit_3_3_0
recipeList:
- org.openrewrite.java.ChangePackage:
oldPackageName: com.gargoylesoftware.htmlunit
Expand Down Expand Up @@ -219,7 +219,7 @@ recipeList:
- org.openrewrite.maven.security.UseHttpsForRepositories
---
type: specs.openrewrite.org/v1beta/recipe
name: net.sghill.jenkins.rewrite.JavaxAnnotationsToSpotbugs
name: org.openrewrite.jenkins.JavaxAnnotationsToSpotbugs
recipeList:
- org.openrewrite.java.ChangeType:
oldFullyQualifiedTypeName: javax.annotation.Nonnull
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/*
* 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.jenkins;

import org.junit.jupiter.api.Test;
import org.openrewrite.java.JavaParser;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.java.Assertions.java;

public class JavaxAnnotationsToSpotBugsTest implements RewriteTest {
@Override
public void defaults(RecipeSpec spec) {
spec.parser(JavaParser.fromJavaVersion().classpath("jsr305", "spotbugs-annotations"));
spec.recipeFromResource("/META-INF/rewrite/rewrite.yml", "org.openrewrite.jenkins.JavaxAnnotationsToSpotbugs");
}

@Test
void shouldChangeClassName() {
rewriteRun(java(
"""
import javax.annotation.Nonnull;
public class A {
static @Nonnull String CONSTANT = "A";
}
""".stripIndent(),
"""
import edu.umd.cs.findbugs.annotations.NonNull;
public class A {
static @NonNull String CONSTANT = "A";
}
""".stripIndent()
));
}

@Test
void shouldChangePackage() {
rewriteRun(java(
"""
import javax.annotation.CheckForNull;
public class A {
@CheckForNull
public String key() {
return null;
}
}
""".stripIndent(),
"""
import edu.umd.cs.findbugs.annotations.CheckForNull;
public class A {
@CheckForNull
public String key() {
return null;
}
}
""".stripIndent()
));
}

@Test
void shouldOrderImports() {
rewriteRun(java(
"""
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.util.Objects;
public class A {
@CheckForNull
public String key() {
return null;
}
public @Nonnull String myMethod(String in) {
return Objects.equals(in, "a") ? "yes" : "no";
}
}
""".stripIndent(),
"""
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import java.util.Objects;
public class A {
@CheckForNull
public String key() {
return null;
}
public @NonNull String myMethod(String in) {
return Objects.equals(in, "a") ? "yes" : "no";
}
}
""".stripIndent()
));
}
}
164 changes: 164 additions & 0 deletions src/test/java/org/openrewrite/jenkins/UpgradeHtmlUnit330Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
/*
* 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.jenkins;

import org.intellij.lang.annotations.Language;
import org.junit.jupiter.api.Test;
import org.openrewrite.java.JavaParser;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.java.Assertions.java;
import static org.openrewrite.java.Assertions.srcMainJava;

public class UpgradeHtmlUnit330Test implements RewriteTest {
@Language("java")
private final String webClient2 = """
package com.gargoylesoftware.htmlunit;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
public class WebClient {
public HtmlPage getPage(String in) { return null; }
}
""".stripIndent();

@Language("java")
private final String htmlPage2 = """
package com.gargoylesoftware.htmlunit.html;
public class HtmlPage {
public HtmlForm getFormByName(String in) { return null; }
}
""".stripIndent();

@Language("java")
private final String htmlForm2 = """
package com.gargoylesoftware.htmlunit.html;
public class HtmlForm {
public HtmlInput getInputByName(String in) { return null; }
}
""".stripIndent();

@Language("java")
private final String htmlInput2 = """
package com.gargoylesoftware.htmlunit.html;
public class HtmlInput {
public String getValueAttribute() { return ""; }
public String getValue() { return ""; }
public void setAttribute(String attributeName, String attributeValue) {}
public void setValueAttribute(String newValue) {}
public void setValue(String newValue) {}
}
""".stripIndent();

@Language("java")
private final String webClient3 = """
package org.htmlunit;
import org.htmlunit.html.HtmlPage;
public class WebClient {
public HtmlPage getPage(String in) { return null; }
}
""".stripIndent();

@Language("java")
private final String htmlPage3 = """
package org.htmlunit.html;
public class HtmlPage {
public HtmlForm getFormByName(String in) { return null; }
}
""".stripIndent();

@Language("java")
private final String htmlForm3 = """
package org.htmlunit.html;
public class HtmlForm {
public HtmlInput getInputByName(String in) { return null; }
}
""".stripIndent();

@Language("java")
private final String htmlInput3 = """
package org.htmlunit.html;
public class HtmlInput {
public String getValueAttribute() { return ""; }
public String getValue() { return ""; }
public void setAttribute(String attributeName, String attributeValue) {}
public void setValueAttribute(String newValue) {}
public void setValue(String newValue) {}
}
""".stripIndent();

@Override
public void defaults(RecipeSpec spec) {
spec.parser(JavaParser.fromJavaVersion().dependsOn(webClient2, htmlPage2, htmlForm2, htmlInput2, webClient3, htmlPage3, htmlForm3, htmlInput3));
spec.recipeFromResource("/META-INF/rewrite/rewrite.yml", "org.openrewrite.jenkins.UpgradeHtmlUnit_3_3_0");
}

@Test
void shouldUpdateHtmlUnit() {
rewriteRun(
srcMainJava(spec -> spec.path("org/example/HtmlUnitUse.java")),
java("""
package org.example;
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");
}
}
}
""".stripIndent(),
"""
package org.example;
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");
}
}
}
""".stripIndent())
);
}
}
Loading

0 comments on commit 4ad665a

Please sign in to comment.