Skip to content

Commit

Permalink
Drop SummaryPrinter #264 (#266)
Browse files Browse the repository at this point in the history
* 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
Show file tree
Hide file tree
Showing 3 changed files with 158 additions and 0 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ dependencies {
runtimeOnly("org.openrewrite:rewrite-java-17:$rewriteVersion")
runtimeOnly("io.cucumber:cucumber-java8:7.+")
runtimeOnly("io.cucumber:cucumber-java:7.+")
runtimeOnly("io.cucumber:cucumber-plugin:7.+")

compileOnly("org.projectlombok:lombok:latest.release")
annotationProcessor("org.projectlombok:lombok:latest.release")
Expand Down
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;
}));
}
}

}
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));
}

}

0 comments on commit b6f389e

Please sign in to comment.