Skip to content

Commit

Permalink
Fix Error Prone warnings and pom violation
Browse files Browse the repository at this point in the history
  • Loading branch information
rickie committed Jun 19, 2024
1 parent 83428da commit 5cb254d
Show file tree
Hide file tree
Showing 15 changed files with 60 additions and 59 deletions.
1 change: 1 addition & 0 deletions testng-junit-migrator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

<name>Picnic :: Error Prone Support :: TestNG JUnit Migrator</name>
<description>A tool to migrate TestNG tests to JUnit</description>
<url>https://error-prone.picnic.tech</url>

<dependencies>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.google.errorprone.fixes.SuggestedFix;
import com.sun.source.tree.MethodTree;
import java.util.Optional;
import tech.picnic.errorprone.testngjunit.TestNGMetadata.AnnotationMetadata;
import tech.picnic.errorprone.testngjunit.TestNgMetadata.AnnotationMetadata;

/**
* Interface implemented by classes that define how to migrate a specific attribute from a TestNG
Expand All @@ -22,7 +22,7 @@ interface AttributeMigrator {
* Optional#empty()} if the {@link AttributeMigrator} is not able to migrate the attribute.
*/
Optional<SuggestedFix> migrate(
TestNGMetadata metadata,
TestNgMetadata metadata,
AnnotationMetadata annotation,
MethodTree methodTree,
VisitorState state);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.MethodTree;
import java.util.Optional;
import tech.picnic.errorprone.testngjunit.TestNGMetadata.AnnotationMetadata;
import tech.picnic.errorprone.testngjunit.TestNgMetadata.AnnotationMetadata;

/**
* A {@link AttributeMigrator} that migrates the {@code org.testng.annotations.Test#dataProvider}
Expand All @@ -17,7 +17,7 @@
final class DataProviderAttributeMigrator implements AttributeMigrator {
@Override
public Optional<SuggestedFix> migrate(
TestNGMetadata metadata,
TestNgMetadata metadata,
AnnotationMetadata annotation,
MethodTree methodTree,
VisitorState state) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
import com.google.errorprone.fixes.SuggestedFix;
import com.sun.source.tree.MethodTree;
import java.util.Optional;
import tech.picnic.errorprone.testngjunit.TestNGMetadata.AnnotationMetadata;
import tech.picnic.errorprone.testngjunit.TestNgMetadata.AnnotationMetadata;
import tech.picnic.errorprone.util.SourceCode;

/** A {@link AttributeMigrator} that migrates the {@code description} attribute. */
@Immutable
final class DescriptionAttributeMigrator implements AttributeMigrator {
@Override
public Optional<SuggestedFix> migrate(
TestNGMetadata metadata,
TestNgMetadata metadata,
AnnotationMetadata annotation,
MethodTree methodTree,
VisitorState state) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
import com.sun.source.tree.LiteralTree;
import com.sun.source.tree.MethodTree;
import java.util.Optional;
import tech.picnic.errorprone.testngjunit.TestNGMetadata.AnnotationMetadata;
import tech.picnic.errorprone.testngjunit.TestNgMetadata.AnnotationMetadata;

/** A {@link AttributeMigrator} that migrates the {@code enabled} attribute. */
@Immutable
final class EnabledAttributeMigrator implements AttributeMigrator {
@Override
public Optional<SuggestedFix> migrate(
TestNGMetadata metadata,
TestNgMetadata metadata,
AnnotationMetadata annotation,
MethodTree methodTree,
VisitorState state) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
import com.sun.source.tree.MethodTree;
import com.sun.source.tree.NewArrayTree;
import java.util.Optional;
import tech.picnic.errorprone.testngjunit.TestNGMetadata.AnnotationMetadata;
import tech.picnic.errorprone.testngjunit.TestNgMetadata.AnnotationMetadata;
import tech.picnic.errorprone.util.SourceCode;

/** A {@link AttributeMigrator} that migrates the {@code expectedExceptions} attribute. */
@Immutable
final class ExpectedExceptionsAttributeMigrator implements AttributeMigrator {
@Override
public Optional<SuggestedFix> migrate(
TestNGMetadata metadata,
TestNgMetadata metadata,
AnnotationMetadata annotation,
MethodTree methodTree,
VisitorState state) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
import com.sun.source.tree.NewArrayTree;
import com.sun.source.tree.Tree;
import java.util.Optional;
import tech.picnic.errorprone.testngjunit.TestNGMetadata.AnnotationMetadata;
import tech.picnic.errorprone.testngjunit.TestNgMetadata.AnnotationMetadata;
import tech.picnic.errorprone.util.SourceCode;

/** A {@link AttributeMigrator} that migrates the {@code group} attribute. */
@Immutable
final class GroupsAttributeMigrator implements AttributeMigrator {
@Override
public Optional<SuggestedFix> migrate(
TestNGMetadata metadata,
TestNgMetadata metadata,
AnnotationMetadata annotation,
MethodTree methodTree,
VisitorState state) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.google.errorprone.fixes.SuggestedFix;
import com.sun.source.tree.MethodTree;
import java.util.Optional;
import tech.picnic.errorprone.testngjunit.TestNGMetadata.AnnotationMetadata;
import tech.picnic.errorprone.testngjunit.TestNgMetadata.AnnotationMetadata;
import tech.picnic.errorprone.util.SourceCode;

/**
Expand All @@ -16,7 +16,7 @@
final class PriorityAttributeMigrator implements AttributeMigrator {
@Override
public Optional<SuggestedFix> migrate(
TestNGMetadata metadata,
TestNgMetadata metadata,
AnnotationMetadata annotation,
MethodTree methodTree,
VisitorState state) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import com.sun.source.tree.MethodTree;
import java.util.Optional;
import javax.lang.model.element.Modifier;
import tech.picnic.errorprone.testngjunit.TestNGMetadata.SetupTeardownType;
import tech.picnic.errorprone.testngjunit.TestNgMetadata.SetupTeardownType;

/**
* A helper class that migrates TestNG setup and teardown methods to their JUnit Jupiter equivalent.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
import java.util.Optional;
import javax.inject.Inject;
import org.jspecify.annotations.Nullable;
import tech.picnic.errorprone.testngjunit.TestNGMetadata.AnnotationMetadata;
import tech.picnic.errorprone.testngjunit.TestNGMetadata.DataProviderMetadata;
import tech.picnic.errorprone.testngjunit.TestNGMetadata.SetupTeardownType;
import tech.picnic.errorprone.testngjunit.TestNgMetadata.AnnotationMetadata;
import tech.picnic.errorprone.testngjunit.TestNgMetadata.DataProviderMetadata;
import tech.picnic.errorprone.testngjunit.TestNgMetadata.SetupTeardownType;

/**
* A {@link BugChecker} that migrates TestNG unit tests to JUnit 5.
Expand Down Expand Up @@ -80,14 +80,14 @@ public TestNGJUnitMigration() {
@Override
public Description matchCompilationUnit(CompilationUnitTree tree, VisitorState state) {
TestNGScanner scanner = new TestNGScanner(state);
ImmutableMap<ClassTree, TestNGMetadata> classMetaData = scanner.collectMetadataForClasses(tree);
ImmutableMap<ClassTree, TestNgMetadata> classMetaData = scanner.collectMetadataForClasses(tree);

new TreeScanner<@Nullable Void, TestNGMetadata>() {
new TreeScanner<@Nullable Void, TestNgMetadata>() {
@Override
public @Nullable Void visitClass(ClassTree node, TestNGMetadata testNGMetadata) {
TestNGMetadata metadata = classMetaData.get(node);
public @Nullable Void visitClass(ClassTree node, TestNgMetadata testNgMetadata) {
TestNgMetadata metadata = classMetaData.get(node);
if (metadata == null) {

Check warning on line 89 in testng-junit-migrator/src/main/java/tech/picnic/errorprone/testngjunit/TestNGJUnitMigration.java

View workflow job for this annotation

GitHub Actions / pitest

A change can be made to line 89 without causing a test to fail

removed conditional - replaced equality check with false (covered by 3 tests RemoveConditionalMutator_EQUAL_ELSE)
return super.visitClass(node, testNGMetadata);
return super.visitClass(node, testNgMetadata);

Check warning on line 90 in testng-junit-migrator/src/main/java/tech/picnic/errorprone/testngjunit/TestNGJUnitMigration.java

View workflow job for this annotation

GitHub Actions / pitest

A change can be made to line 90 without causing a test to fail

replaced return value with null for visitClass (no tests cover this line NullReturnValsMutator)
}

for (DataProviderMetadata dataProviderMetadata : metadata.getDataProvidersInUse()) {
Expand All @@ -108,7 +108,7 @@ public Description matchCompilationUnit(CompilationUnitTree tree, VisitorState s
}

@Override
public @Nullable Void visitMethod(MethodTree tree, TestNGMetadata metadata) {
public @Nullable Void visitMethod(MethodTree tree, TestNgMetadata metadata) {
/* Make sure ALL Tests in the class can be migrated. */
if (conservativeMode && !canMigrateAllTestsInClass(metadata, state)) {
return super.visitMethod(tree, metadata);

Check warning on line 114 in testng-junit-migrator/src/main/java/tech/picnic/errorprone/testngjunit/TestNGJUnitMigration.java

View workflow job for this annotation

GitHub Actions / pitest

A change can be made to line 114 without causing a test to fail

replaced return value with null for visitMethod (covered by 1 tests NullReturnValsMutator)
Expand All @@ -133,7 +133,7 @@ public Description matchCompilationUnit(CompilationUnitTree tree, VisitorState s
}

private static ImmutableList<SuggestedFix> buildAttributeFixes(
TestNGMetadata metadata,
TestNgMetadata metadata,
AnnotationMetadata annotationMetadata,
MethodTree methodTree,
VisitorState state) {
Expand All @@ -147,7 +147,7 @@ private static ImmutableList<SuggestedFix> buildAttributeFixes(

private static boolean canMigrateTest(
MethodTree methodTree,
TestNGMetadata metadata,
TestNgMetadata metadata,
AnnotationMetadata annotationMetadata,
VisitorState state) {
ImmutableList<TestAnnotationAttribute> attributes =
Expand All @@ -164,13 +164,13 @@ private static boolean canMigrateTest(
.isPresent());
}

private static boolean canMigrateAllTestsInClass(TestNGMetadata metadata, VisitorState state) {
private static boolean canMigrateAllTestsInClass(TestNgMetadata metadata, VisitorState state) {
return metadata.getMethodAnnotations().entrySet().stream()
.allMatch(entry -> canMigrateTest(entry.getKey(), metadata, entry.getValue(), state));
}

private static Optional<SuggestedFix> trySuggestFix(
TestNGMetadata metadata,
TestNgMetadata metadata,
AnnotationMetadata annotation,
String attributeName,
MethodTree methodTree,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,23 @@
import java.util.Optional;
import javax.lang.model.element.Modifier;
import org.jspecify.annotations.Nullable;
import tech.picnic.errorprone.testngjunit.TestNGMetadata.AnnotationMetadata;
import tech.picnic.errorprone.testngjunit.TestNGMetadata.DataProviderMetadata;
import tech.picnic.errorprone.testngjunit.TestNGMetadata.SetupTeardownType;
import tech.picnic.errorprone.testngjunit.TestNgMetadata.AnnotationMetadata;
import tech.picnic.errorprone.testngjunit.TestNgMetadata.DataProviderMetadata;
import tech.picnic.errorprone.testngjunit.TestNgMetadata.SetupTeardownType;

/**
* A {@link TreeScanner} which will scan a {@link com.sun.source.tree.CompilationUnitTree} and
* collect data required for the migration from each class in the compilation unit.
*
* <p>This data can be retrieved using {@link #collectMetadataForClasses(CompilationUnitTree)}.
*/
final class TestNGScanner extends TreeScanner<@Nullable Void, TestNGMetadata.Builder> {
final class TestNGScanner extends TreeScanner<@Nullable Void, TestNgMetadata.Builder> {
private static final Matcher<MethodTree> TESTNG_TEST_METHOD =
anyOf(
hasAnnotation("org.testng.annotations.Test"),
allOf(hasModifier(Modifier.PUBLIC), not(hasModifier(Modifier.STATIC))));

private final ImmutableMap.Builder<ClassTree, TestNGMetadata> metadataBuilder =
private final ImmutableMap.Builder<ClassTree, TestNgMetadata> metadataBuilder =
ImmutableMap.builder();
private final VisitorState state;

Expand All @@ -49,18 +49,18 @@ final class TestNGScanner extends TreeScanner<@Nullable Void, TestNGMetadata.Bui
}

@Override
public @Nullable Void visitClass(ClassTree tree, TestNGMetadata.Builder unused) {
TestNGMetadata.Builder builder = TestNGMetadata.builder();
public @Nullable Void visitClass(ClassTree tree, TestNgMetadata.Builder unused) {
TestNgMetadata.Builder builder = TestNgMetadata.builder();
builder.setClassTree(tree);
getTestNGAnnotation(tree, state).ifPresent(builder::setClassLevelAnnotationMetadata);
getTestNgAnnotation(tree, state).ifPresent(builder::setClassLevelAnnotationMetadata);
super.visitClass(tree, builder);
metadataBuilder.put(tree, builder.build());

return null;
}

@Override
public @Nullable Void visitMethod(MethodTree tree, TestNGMetadata.Builder builder) {
public @Nullable Void visitMethod(MethodTree tree, TestNgMetadata.Builder builder) {
if (ASTHelpers.isGeneratedConstructor(tree)) {
return super.visitMethod(tree, builder);

Check warning on line 65 in testng-junit-migrator/src/main/java/tech/picnic/errorprone/testngjunit/TestNGScanner.java

View workflow job for this annotation

GitHub Actions / pitest

A change can be made to line 65 without causing a test to fail

replaced return value with null for visitMethod (covered by 8 tests NullReturnValsMutator)
}
Expand All @@ -79,22 +79,22 @@ final class TestNGScanner extends TreeScanner<@Nullable Void, TestNGMetadata.Bui
}

if (TESTNG_TEST_METHOD.matches(tree, state)) {
getTestNGAnnotation(tree, state)
getTestNgAnnotation(tree, state)
.or(builder::getClassLevelAnnotationMetadata)
.ifPresent(annotation -> builder.methodAnnotationsBuilder().put(tree, annotation));
}

return super.visitMethod(tree, builder);

Check warning on line 87 in testng-junit-migrator/src/main/java/tech/picnic/errorprone/testngjunit/TestNGScanner.java

View workflow job for this annotation

GitHub Actions / pitest

A change can be made to line 87 without causing a test to fail

replaced return value with null for visitMethod (covered by 7 tests NullReturnValsMutator)
}

public ImmutableMap<ClassTree, TestNGMetadata> collectMetadataForClasses(
public ImmutableMap<ClassTree, TestNgMetadata> collectMetadataForClasses(
CompilationUnitTree tree) {
scan(tree, null);
return metadataBuilder.build();
}

@CanIgnoreReturnValue
private static Optional<AnnotationMetadata> getTestNGAnnotation(Tree tree, VisitorState state) {
private static Optional<AnnotationMetadata> getTestNgAnnotation(Tree tree, VisitorState state) {
return ASTHelpers.getAnnotations(tree).stream()
.filter(annotation -> TESTNG_TEST_ANNOTATION.matches(annotation, state))
.findFirst()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
* TestNGJUnitMigration}.
*/
@AutoValue
abstract class TestNGMetadata {
abstract class TestNgMetadata {
abstract ClassTree getClassTree();

abstract Optional<AnnotationMetadata> getClassLevelAnnotationMetadata();
Expand Down Expand Up @@ -70,7 +70,7 @@ final Optional<AnnotationMetadata> getAnnotation(MethodTree methodTree) {
}

static Builder builder() {
return new AutoValue_TestNGMetadata.Builder();
return new AutoValue_TestNgMetadata.Builder();
}

@AutoValue.Builder
Expand All @@ -93,7 +93,7 @@ abstract static class Builder {

abstract Builder setDataProviderMetadata(ImmutableMap<String, DataProviderMetadata> value);

abstract TestNGMetadata build();
abstract TestNgMetadata build();
}

/**
Expand Down Expand Up @@ -125,7 +125,7 @@ public abstract static class AnnotationMetadata {
*/
public static AnnotationMetadata create(
AnnotationTree annotationTree, ImmutableMap<String, ExpressionTree> attributes) {
return new AutoValue_TestNGMetadata_AnnotationMetadata(annotationTree, attributes);
return new AutoValue_TestNgMetadata_AnnotationMetadata(annotationTree, attributes);
}
}

Expand Down Expand Up @@ -166,9 +166,9 @@ public enum SetupTeardownType {
private final boolean requiresStaticMethod;

SetupTeardownType(
String testNGAnnotationClass, String junitAnnotationClass, boolean requiresStaticMethod) {
annotationMatcher = isType(testNGAnnotationClass);
methodTreeMatcher = hasAnnotation(testNGAnnotationClass);
String testNgAnnotationClass, String junitAnnotationClass, boolean requiresStaticMethod) {
annotationMatcher = isType(testNgAnnotationClass);
methodTreeMatcher = hasAnnotation(testNgAnnotationClass);
this.junitAnnotationClass = junitAnnotationClass;
this.requiresStaticMethod = requiresStaticMethod;
}
Expand Down Expand Up @@ -209,7 +209,7 @@ public abstract static class DataProviderMetadata {
* @return A new {@link DataProviderMetadata} instance.
*/
public static DataProviderMetadata create(MethodTree methodTree) {
return new AutoValue_TestNGMetadata_DataProviderMetadata(
return new AutoValue_TestNgMetadata_DataProviderMetadata(
methodTree, methodTree.getName().toString());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
final class TimeOutAttributeMigrator implements AttributeMigrator {
@Override
public Optional<SuggestedFix> migrate(
TestNGMetadata metadata,
TestNGMetadata.AnnotationMetadata annotation,
TestNgMetadata metadata,
TestNgMetadata.AnnotationMetadata annotation,
MethodTree methodTree,
VisitorState state) {
return Optional.ofNullable(annotation.getAttributes().get("timeOut"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ final class UnsupportedAttributeMigrator {
private UnsupportedAttributeMigrator() {}

static Optional<SuggestedFix> migrate(
TestNGMetadata.AnnotationMetadata annotation,
TestNgMetadata.AnnotationMetadata annotation,
MethodTree methodTree,
String attributeName,
VisitorState state) {
Expand Down
Loading

0 comments on commit 5cb254d

Please sign in to comment.