-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Tim te Beek
authored
Aug 20, 2022
1 parent
2174027
commit 4e5955d
Showing
3 changed files
with
198 additions
and
0 deletions.
There are no files selected for viewing
91 changes: 91 additions & 0 deletions
91
src/main/java/org/openrewrite/java/testing/junit5/LifecycleNonPrivate.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
src/test/kotlin/org/openrewrite/java/testing/junit5/LifecycleNonPrivateTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() { | ||
} | ||
} | ||
""" | ||
) | ||
} | ||
|