Skip to content

Commit 65711d5

Browse files
Do not reduce visibility of extended classes in TestsShouldNotBePublic (#524)
* Do not reduce visibility on extended classes Fixes #309 * Update src/main/java/org/openrewrite/java/testing/cleanup/TestsShouldNotBePublic.java Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> --------- Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
1 parent caad474 commit 65711d5

File tree

2 files changed

+77
-15
lines changed

2 files changed

+77
-15
lines changed

src/main/java/org/openrewrite/java/testing/cleanup/TestsShouldNotBePublic.java

Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,27 @@
1717

1818
import lombok.AllArgsConstructor;
1919
import lombok.EqualsAndHashCode;
20+
import lombok.RequiredArgsConstructor;
21+
import lombok.Value;
2022
import org.openrewrite.ExecutionContext;
2123
import org.openrewrite.Option;
22-
import org.openrewrite.Recipe;
24+
import org.openrewrite.ScanningRecipe;
2325
import org.openrewrite.TreeVisitor;
2426
import org.openrewrite.internal.ListUtils;
2527
import org.openrewrite.internal.lang.Nullable;
2628
import org.openrewrite.java.ChangeMethodAccessLevelVisitor;
2729
import org.openrewrite.java.JavaIsoVisitor;
2830
import org.openrewrite.java.MethodMatcher;
29-
import org.openrewrite.java.tree.*;
31+
import org.openrewrite.java.tree.Comment;
32+
import org.openrewrite.java.tree.Flag;
33+
import org.openrewrite.java.tree.J;
34+
import org.openrewrite.java.tree.TypeUtils;
3035

31-
import java.util.ArrayList;
32-
import java.util.Collections;
33-
import java.util.List;
34-
import java.util.Set;
36+
import java.util.*;
3537

3638
@AllArgsConstructor
3739
@EqualsAndHashCode(callSuper = false)
38-
public class TestsShouldNotBePublic extends Recipe {
40+
public class TestsShouldNotBePublic extends ScanningRecipe<TestsShouldNotBePublic.Accumulator> {
3941

4042
@Option(displayName = "Remove protected modifiers",
4143
description = "Also remove protected modifiers from test methods",
@@ -60,25 +62,46 @@ public Set<String> getTags() {
6062
}
6163

6264
@Override
63-
public TreeVisitor<?, ExecutionContext> getVisitor() {
64-
return new TestsNotPublicVisitor(Boolean.TRUE.equals(removeProtectedModifiers));
65+
public Accumulator getInitialValue(ExecutionContext ctx) {
66+
return new Accumulator();
6567
}
6668

69+
@Override
70+
public TreeVisitor<?, ExecutionContext> getScanner(Accumulator acc) {
71+
return new JavaIsoVisitor<ExecutionContext>() {
72+
@Override
73+
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDeclaration, ExecutionContext ctx) {
74+
J.ClassDeclaration cd = super.visitClassDeclaration(classDeclaration, ctx);
75+
if (cd.getExtends() != null) {
76+
acc.extendedClasses.add(String.valueOf(cd.getExtends().getType()));
77+
}
78+
return cd;
79+
}
80+
};
81+
}
82+
83+
@Override
84+
public TreeVisitor<?, ExecutionContext> getVisitor(Accumulator acc) {
85+
return new TestsNotPublicVisitor(Boolean.TRUE.equals(removeProtectedModifiers), acc);
86+
}
87+
88+
public static class Accumulator {
89+
Set<String> extendedClasses = new HashSet<>();
90+
}
91+
92+
@RequiredArgsConstructor
6793
private static final class TestsNotPublicVisitor extends JavaIsoVisitor<ExecutionContext> {
6894
private final Boolean orProtected;
69-
70-
private TestsNotPublicVisitor(Boolean orProtected) {
71-
this.orProtected = orProtected;
72-
}
95+
private final Accumulator acc;
7396

7497
@Override
7598
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) {
7699
J.ClassDeclaration c = super.visitClassDeclaration(classDecl, ctx);
77100

78101
if (c.getKind() != J.ClassDeclaration.Kind.Type.Interface
79102
&& c.getModifiers().stream().anyMatch(mod -> mod.getType() == J.Modifier.Type.Public)
80-
&& c.getModifiers().stream().noneMatch(mod -> mod.getType() == J.Modifier.Type.Abstract)) {
81-
103+
&& c.getModifiers().stream().noneMatch(mod -> mod.getType() == J.Modifier.Type.Abstract)
104+
&& !acc.extendedClasses.contains(String.valueOf(c.getType()))) {
82105
boolean hasTestMethods = c.getBody().getStatements().stream()
83106
.filter(org.openrewrite.java.tree.J.MethodDeclaration.class::isInstance)
84107
.map(J.MethodDeclaration.class::cast)

src/test/java/org/openrewrite/java/testing/cleanup/TestsShouldNotBePublicTest.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.junit.jupiter.api.Test;
1919
import org.openrewrite.DocumentExample;
2020
import org.openrewrite.InMemoryExecutionContext;
21+
import org.openrewrite.Issue;
2122
import org.openrewrite.java.JavaParser;
2223
import org.openrewrite.test.RecipeSpec;
2324
import org.openrewrite.test.RewriteTest;
@@ -493,4 +494,42 @@ Collection<DynamicTest> testFactoryMethod() {
493494
)
494495
);
495496
}
497+
498+
@Test
499+
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/309")
500+
void baseclassForTestsNeedsToStayPublic() {
501+
//language=java
502+
rewriteRun(
503+
spec -> spec.recipe(new TestsShouldNotBePublic(true)),
504+
java(
505+
// base class for tests should stay public
506+
"""
507+
package com.hello;
508+
509+
import org.junit.jupiter.api.BeforeEach;
510+
511+
public class MyTestBase {
512+
@BeforeEach
513+
void setUp() {
514+
}
515+
}
516+
"""
517+
),
518+
java(
519+
// test class extends base class from another package
520+
"""
521+
package com.world;
522+
523+
import com.hello.MyTestBase;
524+
import org.junit.jupiter.api.Test;
525+
526+
class MyTest extends MyTestBase {
527+
@Test
528+
void isWorking() {
529+
}
530+
}
531+
"""
532+
)
533+
);
534+
}
496535
}

0 commit comments

Comments
 (0)