|
| 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.List; |
| 20 | + |
| 21 | +import lombok.Value; |
| 22 | +import lombok.With; |
| 23 | +import org.openrewrite.Applicability; |
| 24 | +import org.openrewrite.ExecutionContext; |
| 25 | +import org.openrewrite.Recipe; |
| 26 | +import org.openrewrite.TreeVisitor; |
| 27 | +import org.openrewrite.internal.lang.Nullable; |
| 28 | +import org.openrewrite.java.JavaVisitor; |
| 29 | +import org.openrewrite.java.MethodMatcher; |
| 30 | +import org.openrewrite.java.search.UsesMethod; |
| 31 | +import org.openrewrite.java.tree.Expression; |
| 32 | +import org.openrewrite.java.tree.J; |
| 33 | +import org.openrewrite.java.tree.JavaType.Primitive; |
| 34 | + |
| 35 | +public class CucumberJava8HookDefinitionToCucumberJava extends Recipe { |
| 36 | + |
| 37 | + private static final String IO_CUCUMBER_JAVA8 = "io.cucumber.java8"; |
| 38 | + private static final String IO_CUCUMBER_JAVA8_HOOK_BODY = "io.cucumber.java8.HookBody"; |
| 39 | + private static final String IO_CUCUMBER_JAVA8_HOOK_NO_ARGS_BODY = "io.cucumber.java8.HookNoArgsBody"; |
| 40 | + |
| 41 | + private static final String HOOK_BODY_DEFINITION = IO_CUCUMBER_JAVA8 |
| 42 | + + ".LambdaGlue *(.., " + IO_CUCUMBER_JAVA8_HOOK_BODY + ")"; |
| 43 | + private static final String HOOK_NO_ARGS_BODY_DEFINITION = IO_CUCUMBER_JAVA8 |
| 44 | + + ".LambdaGlue *(.., " + IO_CUCUMBER_JAVA8_HOOK_NO_ARGS_BODY + ")"; |
| 45 | + |
| 46 | + private static final MethodMatcher HOOK_BODY_DEFINITION_METHOD_MATCHER = new MethodMatcher( |
| 47 | + HOOK_BODY_DEFINITION); |
| 48 | + private static final MethodMatcher HOOK_NO_ARGS_BODY_DEFINITION_METHOD_MATCHER = new MethodMatcher( |
| 49 | + HOOK_NO_ARGS_BODY_DEFINITION); |
| 50 | + |
| 51 | + @Override |
| 52 | + protected TreeVisitor<?, ExecutionContext> getSingleSourceApplicableTest() { |
| 53 | + return Applicability.or( |
| 54 | + new UsesMethod<>(HOOK_BODY_DEFINITION, true), |
| 55 | + new UsesMethod<>(HOOK_NO_ARGS_BODY_DEFINITION, true)); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public String getDisplayName() { |
| 60 | + return "Replace Cucumber-Java8 hook definition with Cucumber-Java."; |
| 61 | + } |
| 62 | + |
| 63 | + @Override |
| 64 | + public String getDescription() { |
| 65 | + return "Replace LamdbaGlue hook definitions with new annotated methods with the same body"; |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public @Nullable Duration getEstimatedEffortPerOccurrence() { |
| 70 | + return Duration.ofMinutes(10); |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + protected TreeVisitor<?, ExecutionContext> getVisitor() { |
| 75 | + return new CucumberJava8HooksVisitor(); |
| 76 | + } |
| 77 | + |
| 78 | + static final class CucumberJava8HooksVisitor extends JavaVisitor<ExecutionContext> { |
| 79 | + @Override |
| 80 | + public J visitMethodInvocation(J.MethodInvocation mi, ExecutionContext p) { |
| 81 | + J.MethodInvocation methodInvocation = (J.MethodInvocation) super.visitMethodInvocation(mi, p); |
| 82 | + if (!HOOK_BODY_DEFINITION_METHOD_MATCHER.matches(methodInvocation) |
| 83 | + && !HOOK_NO_ARGS_BODY_DEFINITION_METHOD_MATCHER.matches(methodInvocation)) { |
| 84 | + return methodInvocation; |
| 85 | + } |
| 86 | + |
| 87 | + // Replacement annotations can only handle literals or constants |
| 88 | + if (methodInvocation.getArguments().stream() |
| 89 | + .anyMatch(arg -> !(arg instanceof J.Literal) && !(arg instanceof J.Lambda))) { |
| 90 | + return methodInvocation |
| 91 | + .withMarkers(methodInvocation.getMarkers().searchResult("TODO Migrate manually")); |
| 92 | + } |
| 93 | + |
| 94 | + // Extract arguments passed to method |
| 95 | + HookArguments hookArguments = parseHookArguments(methodInvocation.getSimpleName(), |
| 96 | + methodInvocation.getArguments()); |
| 97 | + |
| 98 | + // Add new template method at end of class declaration |
| 99 | + J.ClassDeclaration parentClass = getCursor() |
| 100 | + .dropParentUntil(J.ClassDeclaration.class::isInstance) |
| 101 | + .getValue(); |
| 102 | + doAfterVisit(new CucumberJava8ClassVisitor( |
| 103 | + parentClass.getType(), |
| 104 | + hookArguments.replacementImport(), |
| 105 | + hookArguments.template(), |
| 106 | + hookArguments.parameters())); |
| 107 | + |
| 108 | + // Remove original method invocation; it's replaced in the above visitor |
| 109 | + return null; |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Parse up to three arguments: |
| 114 | + * - last one is always a Lambda; |
| 115 | + * - first can also be a String or int. |
| 116 | + * - second can be an int; |
| 117 | + * |
| 118 | + * @param arguments |
| 119 | + * @return |
| 120 | + */ |
| 121 | + HookArguments parseHookArguments(String methodName, List<Expression> arguments) { |
| 122 | + // Lambda is always last, and can either contain a body with Scenario argument, or without |
| 123 | + int argumentsSize = arguments.size(); |
| 124 | + Expression lambdaArgument = arguments.get(argumentsSize - 1); |
| 125 | + HookArguments hookArguments = new HookArguments( |
| 126 | + methodName, |
| 127 | + null, |
| 128 | + null, |
| 129 | + (J.Lambda) lambdaArgument); |
| 130 | + if (argumentsSize == 1) { |
| 131 | + return hookArguments; |
| 132 | + } |
| 133 | + |
| 134 | + J.Literal firstArgument = (J.Literal) arguments.get(0); |
| 135 | + if (argumentsSize == 2) { |
| 136 | + // First argument is either a String or an int |
| 137 | + if (firstArgument.getType() == Primitive.String) { |
| 138 | + return hookArguments.withTagExpression((String) firstArgument.getValue()); |
| 139 | + } |
| 140 | + return hookArguments.withOrder((Integer) firstArgument.getValue()); |
| 141 | + } |
| 142 | + // First argument is always a String, second argument always an int |
| 143 | + return hookArguments |
| 144 | + .withTagExpression((String) firstArgument.getValue()) |
| 145 | + .withOrder((Integer) ((J.Literal) arguments.get(1)).getValue()); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | +} |
| 150 | + |
| 151 | +@Value |
| 152 | +class HookArguments { |
| 153 | + |
| 154 | + String annotationName; |
| 155 | + @Nullable |
| 156 | + @With |
| 157 | + String tagExpression; |
| 158 | + @Nullable |
| 159 | + @With |
| 160 | + Integer order; |
| 161 | + J.Lambda lambda; |
| 162 | + |
| 163 | + String replacementImport() { |
| 164 | + return String.format("io.cucumber.java.%s", annotationName); |
| 165 | + } |
| 166 | + |
| 167 | + String template() { |
| 168 | + return "@#{}#{}\npublic void #{}(#{}) throws Exception {\n\t#{any()}\n}"; |
| 169 | + } |
| 170 | + |
| 171 | + private String formatAnnotationArguments() { |
| 172 | + if (tagExpression == null && order == null) { |
| 173 | + return ""; |
| 174 | + } |
| 175 | + StringBuilder template = new StringBuilder(); |
| 176 | + template.append('('); |
| 177 | + if (order != null) { |
| 178 | + template.append("order = ").append(order); |
| 179 | + if (tagExpression != null) { |
| 180 | + template.append(", value = \"").append(tagExpression).append('"'); |
| 181 | + } |
| 182 | + } else { |
| 183 | + template.append('"').append(tagExpression).append('"'); |
| 184 | + } |
| 185 | + template.append(')'); |
| 186 | + return template.toString(); |
| 187 | + } |
| 188 | + |
| 189 | + private String formatMethodName() { |
| 190 | + return String.format("%s%s%s", |
| 191 | + annotationName |
| 192 | + .replaceFirst("^Before", "before") |
| 193 | + .replaceFirst("^After", "after"), |
| 194 | + tagExpression == null ? "" |
| 195 | + : "_tag_" + tagExpression |
| 196 | + .replaceAll("[^A-Za-z0-9]", "_"), |
| 197 | + order == null ? "" : "_order_" + order); |
| 198 | + } |
| 199 | + |
| 200 | + private String formatMethodArguments() { |
| 201 | + J firstLambdaParameter = lambda.getParameters().getParameters().get(0); |
| 202 | + if (firstLambdaParameter instanceof J.VariableDeclarations) { |
| 203 | + return String.format("io.cucumber.java.Scenario %s", |
| 204 | + ((J.VariableDeclarations) firstLambdaParameter).getVariables().get(0).getName()); |
| 205 | + } |
| 206 | + return ""; |
| 207 | + } |
| 208 | + |
| 209 | + public Object[] parameters() { |
| 210 | + return new Object[] { |
| 211 | + annotationName, |
| 212 | + formatAnnotationArguments(), |
| 213 | + formatMethodName(), |
| 214 | + formatMethodArguments(), |
| 215 | + lambda.getBody() }; |
| 216 | + } |
| 217 | + |
| 218 | +} |
0 commit comments