|
| 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 | +} |
0 commit comments