Skip to content

Commit b6f389e

Browse files
author
Tim te Beek
authored
Drop SummaryPrinter #264 (#266)
* Drop SummaryPrinter #264 * Stream.ofNullable is only in Java 9+
1 parent 0df9047 commit b6f389e

File tree

3 files changed

+158
-0
lines changed

3 files changed

+158
-0
lines changed

build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ dependencies {
127127
runtimeOnly("org.openrewrite:rewrite-java-17:$rewriteVersion")
128128
runtimeOnly("io.cucumber:cucumber-java8:7.+")
129129
runtimeOnly("io.cucumber:cucumber-java:7.+")
130+
runtimeOnly("io.cucumber:cucumber-plugin:7.+")
130131

131132
compileOnly("org.projectlombok:lombok:latest.release")
132133
annotationProcessor("org.projectlombok:lombok:latest.release")
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
/*
2+
* Copyright 2022 the original author or authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.openrewrite.java.testing.cucumber;
17+
18+
import java.time.Duration;
19+
import java.util.Collection;
20+
import java.util.Objects;
21+
import java.util.stream.Stream;
22+
23+
import org.openrewrite.ExecutionContext;
24+
import org.openrewrite.Recipe;
25+
import org.openrewrite.TreeVisitor;
26+
import org.openrewrite.internal.ListUtils;
27+
import org.openrewrite.internal.lang.Nullable;
28+
import org.openrewrite.java.ChangeType;
29+
import org.openrewrite.java.JavaIsoVisitor;
30+
import org.openrewrite.java.RemoveImport;
31+
import org.openrewrite.java.search.UsesType;
32+
import org.openrewrite.java.tree.J;
33+
import org.openrewrite.java.tree.TypeUtils;
34+
35+
public class DropSummaryPrinter extends Recipe {
36+
37+
private static final String IO_CUCUMBER_PLUGIN_SUMMARY_PRINTER = "io.cucumber.plugin.SummaryPrinter";
38+
private static final String IO_CUCUMBER_PLUGIN_PLUGIN = "io.cucumber.plugin.Plugin";
39+
40+
@Override
41+
protected TreeVisitor<?, ExecutionContext> getSingleSourceApplicableTest() {
42+
return new UsesType<>(IO_CUCUMBER_PLUGIN_SUMMARY_PRINTER);
43+
}
44+
45+
@Override
46+
public String getDisplayName() {
47+
return "Drop SummaryPrinter.";
48+
}
49+
50+
@Override
51+
public String getDescription() {
52+
return "Replace SummaryPrinter with Plugin, if not already present.";
53+
}
54+
55+
@Override
56+
public @Nullable Duration getEstimatedEffortPerOccurrence() {
57+
return Duration.ofMinutes(1);
58+
}
59+
60+
@Override
61+
protected TreeVisitor<?, ExecutionContext> getVisitor() {
62+
return new DropSummaryPrinterVisitor();
63+
}
64+
65+
static final class DropSummaryPrinterVisitor extends JavaIsoVisitor<ExecutionContext> {
66+
67+
@Override
68+
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration cd, ExecutionContext p) {
69+
J.ClassDeclaration classDeclaration = super.visitClassDeclaration(cd, p);
70+
boolean implementsSummaryPrinter = Stream.of(classDeclaration.getImplements())
71+
.filter(Objects::nonNull)
72+
.flatMap(Collection::stream)
73+
.anyMatch(t -> TypeUtils.isOfClassType(t.getType(), IO_CUCUMBER_PLUGIN_SUMMARY_PRINTER));
74+
boolean alreadyImplementsPlugin = Stream.of(classDeclaration.getImplements())
75+
.filter(Objects::nonNull)
76+
.flatMap(Collection::stream)
77+
.anyMatch(t -> TypeUtils.isOfClassType(t.getType(), IO_CUCUMBER_PLUGIN_PLUGIN));
78+
if (!implementsSummaryPrinter) {
79+
return classDeclaration;
80+
}
81+
doAfterVisit(new ChangeType(
82+
IO_CUCUMBER_PLUGIN_SUMMARY_PRINTER,
83+
IO_CUCUMBER_PLUGIN_PLUGIN,
84+
true));
85+
doAfterVisit(new RemoveImport<>(IO_CUCUMBER_PLUGIN_SUMMARY_PRINTER));
86+
return classDeclaration.withImplements(ListUtils.map(classDeclaration.getImplements(), i -> {
87+
// Remove duplicate implements
88+
if (TypeUtils.isOfClassType(i.getType(), IO_CUCUMBER_PLUGIN_SUMMARY_PRINTER)
89+
&& alreadyImplementsPlugin) {
90+
return null;
91+
}
92+
return i;
93+
}));
94+
}
95+
}
96+
97+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package org.openrewrite.java.testing.cucumber;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.openrewrite.Issue;
5+
import org.openrewrite.java.JavaParser;
6+
import org.openrewrite.test.RecipeSpec;
7+
import org.openrewrite.test.RewriteTest;
8+
9+
import static org.openrewrite.java.Assertions.java;
10+
import static org.openrewrite.java.Assertions.version;
11+
12+
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/264")
13+
class DropSummaryPrinterTest implements RewriteTest {
14+
15+
@Override
16+
public void defaults(RecipeSpec spec) {
17+
spec.recipe(new DropSummaryPrinter());
18+
spec.parser(JavaParser.fromJavaVersion()
19+
.logCompilationWarningsAndErrors(true)
20+
.classpath("cucumber-plugin"));
21+
}
22+
23+
@Test
24+
void should_replace_summary_printer_with_plugin() {
25+
rewriteRun(version(java("""
26+
package com.example.app;
27+
28+
import io.cucumber.plugin.SummaryPrinter;
29+
30+
public class CucumberJava8Definitions implements SummaryPrinter {
31+
}""", """
32+
package com.example.app;
33+
34+
import io.cucumber.plugin.Plugin;
35+
36+
public class CucumberJava8Definitions implements Plugin {
37+
}"""),
38+
17));
39+
}
40+
41+
@Test
42+
void should_not_duplicate_plugin() {
43+
rewriteRun(version(java("""
44+
package com.example.app;
45+
46+
import io.cucumber.plugin.Plugin;
47+
import io.cucumber.plugin.SummaryPrinter;
48+
49+
public class CucumberJava8Definitions implements Plugin, SummaryPrinter {
50+
}""", """
51+
package com.example.app;
52+
53+
import io.cucumber.plugin.Plugin;
54+
55+
public class CucumberJava8Definitions implements Plugin {
56+
}"""),
57+
17));
58+
}
59+
60+
}

0 commit comments

Comments
 (0)