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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not reduce visibility of extended classes in TestsShouldNotBePublic #524

Merged
merged 2 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,27 @@

import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.RequiredArgsConstructor;
import lombok.Value;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Option;
import org.openrewrite.Recipe;
import org.openrewrite.ScanningRecipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.java.ChangeMethodAccessLevelVisitor;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.tree.*;
import org.openrewrite.java.tree.Comment;
import org.openrewrite.java.tree.Flag;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.TypeUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.*;

@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class TestsShouldNotBePublic extends Recipe {
public class TestsShouldNotBePublic extends ScanningRecipe<TestsShouldNotBePublic.Accumulator> {

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

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new TestsNotPublicVisitor(Boolean.TRUE.equals(removeProtectedModifiers));
public Accumulator getInitialValue(ExecutionContext ctx) {
return new Accumulator();
}

@Override
public TreeVisitor<?, ExecutionContext> getScanner(Accumulator acc) {
return new JavaIsoVisitor<ExecutionContext>() {
@Override
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDeclaration, ExecutionContext ctx) {
J.ClassDeclaration cd = super.visitClassDeclaration(classDeclaration, ctx);
if (cd.getExtends() != null) {
acc.extendedClasses.add(String.valueOf(cd.getExtends().getType()));
}
return cd;
}
};
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor(Accumulator acc) {
return new TestsNotPublicVisitor(Boolean.TRUE.equals(removeProtectedModifiers), acc);
}

public static class Accumulator {
Set<String> extendedClasses = new HashSet<>();
}

@RequiredArgsConstructor
private static final class TestsNotPublicVisitor extends JavaIsoVisitor<ExecutionContext> {
private final Boolean orProtected;

private TestsNotPublicVisitor(Boolean orProtected) {
this.orProtected = orProtected;
}
private final Accumulator acc;

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

if (c.getKind() != J.ClassDeclaration.Kind.Type.Interface
&& c.getModifiers().stream().anyMatch(mod -> mod.getType() == J.Modifier.Type.Public)
&& c.getModifiers().stream().noneMatch(mod -> mod.getType() == J.Modifier.Type.Abstract)) {

&& c.getModifiers().stream().noneMatch(mod -> mod.getType() == J.Modifier.Type.Abstract)
&& !acc.extendedClasses.contains(String.valueOf(c.getType()))) {
boolean hasTestMethods = c.getBody().getStatements().stream()
.filter(org.openrewrite.java.tree.J.MethodDeclaration.class::isInstance)
.map(J.MethodDeclaration.class::cast)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.Issue;
import org.openrewrite.java.JavaParser;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;
Expand Down Expand Up @@ -493,4 +494,42 @@ Collection<DynamicTest> testFactoryMethod() {
)
);
}

@Test
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/309")
void baseclassForTestsNeedsToStayPublic() {
//language=java
rewriteRun(
spec -> spec.recipe(new TestsShouldNotBePublic(true)),
java(
// base class for tests should stay public
"""
package com.hello;

import org.junit.jupiter.api.BeforeEach;

public class MyTestBase {
@BeforeEach
void setUp() {
}
}
"""
),
java(
// test class extends base class from another package
"""
package com.world;

import com.hello.MyTestBase;
import org.junit.jupiter.api.Test;

class MyTest extends MyTestBase {
@Test
void isWorking() {
}
}
"""
)
);
}
}