generated from moderneinc/rewrite-recipe-starter
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Relocate UpgradeHtmlUnit_3_3_0, JavaxAnnotationsToSpotbugs
Rewrite test in java. Issue #4
- Loading branch information
Showing
5 changed files
with
282 additions
and
281 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
src/test/java/org/openrewrite/jenkins/JavaxAnnotationsToSpotBugsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
164
src/test/java/org/openrewrite/jenkins/UpgradeHtmlUnit330Test.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()) | ||
); | ||
} | ||
} |
Oops, something went wrong.