-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Drop SummaryPrinter #264 * Stream.ofNullable is only in Java 9+
- Loading branch information
Tim te Beek
authored
Oct 10, 2022
1 parent
0df9047
commit b6f389e
Showing
3 changed files
with
158 additions
and
0 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
97 changes: 97 additions & 0 deletions
97
src/main/java/org/openrewrite/java/testing/cucumber/DropSummaryPrinter.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,97 @@ | ||
/* | ||
* Copyright 2022 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.cucumber; | ||
|
||
import java.time.Duration; | ||
import java.util.Collection; | ||
import java.util.Objects; | ||
import java.util.stream.Stream; | ||
|
||
import org.openrewrite.ExecutionContext; | ||
import org.openrewrite.Recipe; | ||
import org.openrewrite.TreeVisitor; | ||
import org.openrewrite.internal.ListUtils; | ||
import org.openrewrite.internal.lang.Nullable; | ||
import org.openrewrite.java.ChangeType; | ||
import org.openrewrite.java.JavaIsoVisitor; | ||
import org.openrewrite.java.RemoveImport; | ||
import org.openrewrite.java.search.UsesType; | ||
import org.openrewrite.java.tree.J; | ||
import org.openrewrite.java.tree.TypeUtils; | ||
|
||
public class DropSummaryPrinter extends Recipe { | ||
|
||
private static final String IO_CUCUMBER_PLUGIN_SUMMARY_PRINTER = "io.cucumber.plugin.SummaryPrinter"; | ||
private static final String IO_CUCUMBER_PLUGIN_PLUGIN = "io.cucumber.plugin.Plugin"; | ||
|
||
@Override | ||
protected TreeVisitor<?, ExecutionContext> getSingleSourceApplicableTest() { | ||
return new UsesType<>(IO_CUCUMBER_PLUGIN_SUMMARY_PRINTER); | ||
} | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return "Drop SummaryPrinter."; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "Replace SummaryPrinter with Plugin, if not already present."; | ||
} | ||
|
||
@Override | ||
public @Nullable Duration getEstimatedEffortPerOccurrence() { | ||
return Duration.ofMinutes(1); | ||
} | ||
|
||
@Override | ||
protected TreeVisitor<?, ExecutionContext> getVisitor() { | ||
return new DropSummaryPrinterVisitor(); | ||
} | ||
|
||
static final class DropSummaryPrinterVisitor extends JavaIsoVisitor<ExecutionContext> { | ||
|
||
@Override | ||
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration cd, ExecutionContext p) { | ||
J.ClassDeclaration classDeclaration = super.visitClassDeclaration(cd, p); | ||
boolean implementsSummaryPrinter = Stream.of(classDeclaration.getImplements()) | ||
.filter(Objects::nonNull) | ||
.flatMap(Collection::stream) | ||
.anyMatch(t -> TypeUtils.isOfClassType(t.getType(), IO_CUCUMBER_PLUGIN_SUMMARY_PRINTER)); | ||
boolean alreadyImplementsPlugin = Stream.of(classDeclaration.getImplements()) | ||
.filter(Objects::nonNull) | ||
.flatMap(Collection::stream) | ||
.anyMatch(t -> TypeUtils.isOfClassType(t.getType(), IO_CUCUMBER_PLUGIN_PLUGIN)); | ||
if (!implementsSummaryPrinter) { | ||
return classDeclaration; | ||
} | ||
doAfterVisit(new ChangeType( | ||
IO_CUCUMBER_PLUGIN_SUMMARY_PRINTER, | ||
IO_CUCUMBER_PLUGIN_PLUGIN, | ||
true)); | ||
doAfterVisit(new RemoveImport<>(IO_CUCUMBER_PLUGIN_SUMMARY_PRINTER)); | ||
return classDeclaration.withImplements(ListUtils.map(classDeclaration.getImplements(), i -> { | ||
// Remove duplicate implements | ||
if (TypeUtils.isOfClassType(i.getType(), IO_CUCUMBER_PLUGIN_SUMMARY_PRINTER) | ||
&& alreadyImplementsPlugin) { | ||
return null; | ||
} | ||
return i; | ||
})); | ||
} | ||
} | ||
|
||
} |
60 changes: 60 additions & 0 deletions
60
src/test/java/org/openrewrite/java/testing/cucumber/DropSummaryPrinterTest.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,60 @@ | ||
package org.openrewrite.java.testing.cucumber; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.openrewrite.Issue; | ||
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.version; | ||
|
||
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/264") | ||
class DropSummaryPrinterTest implements RewriteTest { | ||
|
||
@Override | ||
public void defaults(RecipeSpec spec) { | ||
spec.recipe(new DropSummaryPrinter()); | ||
spec.parser(JavaParser.fromJavaVersion() | ||
.logCompilationWarningsAndErrors(true) | ||
.classpath("cucumber-plugin")); | ||
} | ||
|
||
@Test | ||
void should_replace_summary_printer_with_plugin() { | ||
rewriteRun(version(java(""" | ||
package com.example.app; | ||
import io.cucumber.plugin.SummaryPrinter; | ||
public class CucumberJava8Definitions implements SummaryPrinter { | ||
}""", """ | ||
package com.example.app; | ||
import io.cucumber.plugin.Plugin; | ||
public class CucumberJava8Definitions implements Plugin { | ||
}"""), | ||
17)); | ||
} | ||
|
||
@Test | ||
void should_not_duplicate_plugin() { | ||
rewriteRun(version(java(""" | ||
package com.example.app; | ||
import io.cucumber.plugin.Plugin; | ||
import io.cucumber.plugin.SummaryPrinter; | ||
public class CucumberJava8Definitions implements Plugin, SummaryPrinter { | ||
}""", """ | ||
package com.example.app; | ||
import io.cucumber.plugin.Plugin; | ||
public class CucumberJava8Definitions implements Plugin { | ||
}"""), | ||
17)); | ||
} | ||
|
||
} |