Skip to content

Commit

Permalink
polish
Browse files Browse the repository at this point in the history
  • Loading branch information
justine-gehring committed Jan 23, 2025
1 parent 34a0d0e commit 17819c9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright 2024 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.search;

import lombok.Data;
Expand All @@ -12,7 +28,7 @@ public class FindUnitTests extends ScanningRecipe<FindUnitTests.Accumulator> {

@Override
public String getDisplayName() {
return "Find Unit Tests";
return "Find unit tests";
}

@Override
Expand All @@ -37,36 +53,36 @@ public Accumulator getInitialValue(ExecutionContext ctx) {
@Override
public TreeVisitor<?, ExecutionContext> getScanner(Accumulator acc) {
return new JavaVisitor<ExecutionContext>() {
@Override
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
// get the method declaration the method invocation is in
J.MethodDeclaration methodDeclaration = getCursor().firstEnclosing(J.MethodDeclaration.class);
if (methodDeclaration != null
&& methodDeclaration.getLeadingAnnotations().stream()
.filter(o -> o.getAnnotationType() instanceof J.Identifier)
.anyMatch(o -> "Test".equals(o.getSimpleName()))) {
UnitTest unitTest = new UnitTest();
unitTest.clazz = getCursor().firstEnclosingOrThrow(J.ClassDeclaration.class).getType().getFullyQualifiedName();
unitTest.unitTestName = methodDeclaration.getSimpleName();
unitTest.unitTest = methodDeclaration.printTrimmed(getCursor());
if (acc.unitTestAndTheirMethods.containsKey(unitTest)) {
acc.unitTestAndTheirMethods.get(unitTest).add(method);
}else {
List<J.MethodInvocation> methodList = new ArrayList<>();
methodList.add(method);
acc.unitTestAndTheirMethods.put(unitTest, methodList);
}
}
return super.visitMethodInvocation(method, ctx);
@Override
public J visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
// get the method declaration the method invocation is in
J.MethodDeclaration methodDeclaration = getCursor().firstEnclosing(J.MethodDeclaration.class);
if (methodDeclaration != null
&& methodDeclaration.getLeadingAnnotations().stream()
.filter(o -> o.getAnnotationType() instanceof J.Identifier)
.anyMatch(o -> "Test".equals(o.getSimpleName()))) {
UnitTest unitTest = new UnitTest();
unitTest.clazz = getCursor().firstEnclosingOrThrow(J.ClassDeclaration.class).getType().getFullyQualifiedName();
unitTest.unitTestName = methodDeclaration.getSimpleName();
unitTest.unitTest = methodDeclaration.printTrimmed(getCursor());
if (acc.unitTestAndTheirMethods.containsKey(unitTest)) {
acc.unitTestAndTheirMethods.get(unitTest).add(method);
}else {
List<J.MethodInvocation> methodList = new ArrayList<>();
methodList.add(method);
acc.unitTestAndTheirMethods.put(unitTest, methodList);
}
};
}
return super.visitMethodInvocation(method, ctx);
}
};
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor(Accumulator acc) {
return new JavaVisitor<ExecutionContext>() {
@Override
public J visitMethodDeclaration(J.MethodDeclaration methodDeclaration, ExecutionContext ctx) {
public J visitMethodDeclaration(J. MethodDeclaration methodDeclaration, ExecutionContext ctx) {
for (UnitTest unitTest : acc.unitTestAndTheirMethods.keySet()) {
for (J.MethodInvocation method : acc.unitTestAndTheirMethods.get(unitTest)) {
if (method.getSimpleName().equals(methodDeclaration.getSimpleName())) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.openrewrite.java.testing.search;

import org.junit.jupiter.api.Test;
import org.openrewrite.java.JavaParser;
import org.openrewrite.java.testing.table.FindUnitTestTable;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;
Expand Down Expand Up @@ -77,9 +76,7 @@ void testng() {
//language=java
rewriteRun(
spec -> spec.recipe(new FindUnitTests())
.dataTable(FindUnitTestTable.Row.class, rows -> {
assertThat(rows).hasSize(1);
}),
.dataTable(FindUnitTestTable.Row.class, rows -> assertThat(rows).hasSize(1)),
java(
"""
import org.testng.annotations.Test;
Expand Down

0 comments on commit 17819c9

Please sign in to comment.