Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cucumber annotation to suite #268

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ dependencies {
implementation("org.openrewrite:rewrite-maven:$rewriteVersion")
runtimeOnly("com.fasterxml.jackson.core:jackson-core:2.12.+")
runtimeOnly("org.openrewrite:rewrite-java-17:$rewriteVersion")
runtimeOnly("io.cucumber:cucumber-junit-platform-engine:7.+")
runtimeOnly("org.junit.platform:junit-platform-suite-api:latest.release")
Comment on lines +128 to +129
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In general dependencies necessary for recipe testing should belong to the testRuntimeOnly group.


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,74 @@
package org.openrewrite.java.testing.cucumber;

import java.time.Duration;
import java.util.List;

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.*;
import org.openrewrite.java.search.UsesType;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.J.ClassDeclaration;

public class CucumberAnnotationToSuite extends Recipe {

private static final String IO_CUCUMBER_JUNIT_PLATFORM_ENGINE_CUCUMBER = "io.cucumber.junit.platform.engine.Cucumber";
private static final AnnotationMatcher ANNOTATION_MATCHER = new AnnotationMatcher(
IO_CUCUMBER_JUNIT_PLATFORM_ENGINE_CUCUMBER);

private static final String SUITE = "org.junit.platform.suite.api.Suite";
private static final String SELECT_CLASSPATH_RESOURCE = "org.junit.platform.suite.api.SelectClasspathResource";

@Override
public String getDisplayName() {
return "Replace @Cucumber with @Suite";
}

@Override
public String getDescription() {
return "Replace @Cucumber with @Suite and @SelectClasspathResource(\"cucumber/annotated/class/package\").";
}

@Override
public @Nullable Duration getEstimatedEffortPerOccurrence() {
return Duration.ofMinutes(2);
}

@Override
protected TreeVisitor<?, ExecutionContext> getSingleSourceApplicableTest() {
return new UsesType<>(IO_CUCUMBER_JUNIT_PLATFORM_ENGINE_CUCUMBER);
}

@Override
protected TreeVisitor<?, ExecutionContext> getVisitor() {
return new JavaIsoVisitor<ExecutionContext>() {
@Override
public J.ClassDeclaration visitClassDeclaration(ClassDeclaration cd, ExecutionContext p) {
J.ClassDeclaration classDecl = super.visitClassDeclaration(cd, p);
if (classDecl.getAllAnnotations().stream().noneMatch(ANNOTATION_MATCHER::matches)) {
return classDecl;
}

maybeRemoveImport(IO_CUCUMBER_JUNIT_PLATFORM_ENGINE_CUCUMBER);
maybeAddImport(SUITE);
maybeAddImport(SELECT_CLASSPATH_RESOURCE);

return classDecl.withLeadingAnnotations(ListUtils.flatMap(classDecl.getLeadingAnnotations(), ann -> {
String code = "@Suite\n@SelectClasspathResource(\"#{}\")";
String path = classDecl.getType().getPackageName().replace('.', '/');
JavaTemplate template = JavaTemplate.builder(this::getCursor, code)
.javaParser(
() -> JavaParser.fromJavaVersion().classpath("junit-platform-suite-api").build())
timtebeek marked this conversation as resolved.
Show resolved Hide resolved
.imports(SUITE, SELECT_CLASSPATH_RESOURCE)
.build();
List<J.Annotation> list = ann.withTemplate(template, ann.getCoordinates().replace(), path);
return list;
}));
}
};
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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 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 CucumberAnnotationToSuiteTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
spec.recipe(new CucumberAnnotationToSuite());
spec.parser(JavaParser.fromJavaVersion()
.logCompilationWarningsAndErrors(true)
.classpath(
"cucumber-junit-platform-engine",
"junit-platform-suite-api"));
}

@Test
void should_replace_cucumber_annotation_with_suite_with_selected_classpath_resource() {
rewriteRun(version(java("""
package com.example.app;

import io.cucumber.junit.platform.engine.Cucumber;

@Cucumber
public class CucumberJava8Definitions {
}""", """
package com.example.app;

import org.junit.platform.suite.api.SelectClasspathResource;
import org.junit.platform.suite.api.Suite;

@Suite
@SelectClasspathResource("com/example/app")
public class CucumberJava8Definitions {
}"""),
17));
}

}