Skip to content

Commit

Permalink
Add LifecycleNonPrivate for JUnit 5.9.0 (#241) (#252)
Browse files Browse the repository at this point in the history
* Add LifecycleNonPrivate for JUnit 5.9.0 (#241)
* Add TempDirNonFinal for JUnit 5.9.0 (#241)
  • Loading branch information
Tim te Beek authored Aug 20, 2022
1 parent 2174027 commit 4e5955d
Show file tree
Hide file tree
Showing 3 changed files with 198 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright 2021 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.junit5;

import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.java.AnnotationMatcher;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.JavaVisitor;
import org.openrewrite.java.search.UsesType;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.J.MethodDeclaration;
import org.openrewrite.java.tree.J.Modifier.Type;
import org.openrewrite.java.tree.JavaSourceFile;

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

public class LifecycleNonPrivate extends Recipe {

private static final List<String> ANNOTATIONS = Arrays.asList(
"org.junit.jupiter.api.AfterAll",
"org.junit.jupiter.api.AfterEach",
"org.junit.jupiter.api.BeforeAll",
"org.junit.jupiter.api.BeforeEach");

@Override
public String getDisplayName() {
return "Make lifecycle methods non private";
}

@Override
public String getDescription() {
return "Make JUnit 5's `@AfterAll`, `@AfterEach`, `@BeforeAll` and `@BeforeEach` non private.";
}

@Override
protected TreeVisitor<?, ExecutionContext> getSingleSourceApplicableTest() {
return new JavaVisitor<ExecutionContext>() {
@Override
public J visitJavaSourceFile(JavaSourceFile cu, ExecutionContext executionContext) {
ANNOTATIONS.forEach(ann -> doAfterVisit(new UsesType<>(ann)));
return cu;
}
};
}

@Override
protected LifecycleNonPrivateVisitor getVisitor() {
return new LifecycleNonPrivateVisitor();
}

private static class LifecycleNonPrivateVisitor extends JavaIsoVisitor<ExecutionContext> {
@Override
public J.MethodDeclaration visitMethodDeclaration(MethodDeclaration method, ExecutionContext p) {
J.MethodDeclaration md = super.visitMethodDeclaration(method, p);
if (md.getModifiers().stream().anyMatch(mod -> mod.getType() == Type.Private)
&& md.getLeadingAnnotations().stream().anyMatch(ann -> ANNOTATIONS.stream()
.map(AnnotationMatcher::new)
.anyMatch(matcher -> matcher.matches(ann)))) {
return maybeAutoFormat(md,
md.withModifiers(ListUtils.map(md.getModifiers(),
modifier -> modifier.getType() == Type.Private ? null : modifier)),
p, getCursor().getParent());
}
return md;
}
}

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

}
1 change: 1 addition & 0 deletions src/main/resources/META-INF/rewrite/junit5.yml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ recipeList:
- org.openrewrite.java.testing.junit5.TempDirNonFinal
- org.openrewrite.java.testing.junit5.TestRuleToTestInfo
- org.openrewrite.java.testing.junit5.UpdateBeforeAfterAnnotations
- org.openrewrite.java.testing.junit5.LifecycleNonPrivate
- org.openrewrite.java.testing.junit5.UpdateTestAnnotation
- org.openrewrite.java.testing.junit5.ParameterizedRunnerToParameterized
- org.openrewrite.java.testing.junit5.JUnitParamsRunnerToParameterized
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Copyright 2021 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.junit5

import org.junit.jupiter.api.Test
import org.openrewrite.Issue
import org.openrewrite.Recipe
import org.openrewrite.java.JavaParser
import org.openrewrite.java.JavaRecipeTest

class LifecycleNonPrivateTest : JavaRecipeTest {
override val parser: JavaParser = JavaParser.fromJavaVersion()
.classpath("junit")
.build()

override val recipe: Recipe
get() = LifecycleNonPrivate()

@Test
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/241")
fun beforeEachPrivate() = assertChanged(
before = """
import org.junit.jupiter.api.BeforeEach;
class A {
@BeforeEach
private void beforeEach() {
}
private void unaffected() {
}
}
""",
after = """
import org.junit.jupiter.api.BeforeEach;
class A {
@BeforeEach
void beforeEach() {
}
private void unaffected() {
}
}
"""
)

@Test
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/241")
fun afterAllPrivate() = assertChanged(
before = """
import org.junit.jupiter.api.AfterAll;
class A {
@AfterAll
private static void afterAll() {
}
private void unaffected() {
}
}
""",
after = """
import org.junit.jupiter.api.AfterAll;
class A {
@AfterAll
static void afterAll() {
}
private void unaffected() {
}
}
"""
)

@Test
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/241")
fun beforeEachAfterAllUnchanged() = assertUnchanged(
before = """
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeEach;
class A {
@BeforeEach
void beforeEach() {
}
@AfterAll
static void afterAll() {
}
private void unaffected() {
}
}
"""
)
}

0 comments on commit 4e5955d

Please sign in to comment.