-
Notifications
You must be signed in to change notification settings - Fork 80
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
Feat find unit tests #669
Merged
Merged
Feat find unit tests #669
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
3aa3c0a
find unit tests
justine-gehring 6ba6370
Add to datatable method invocations used in unit tests
justine-gehring e8cb795
Change accumulator unit test and its method invocations to a hasmap
justine-gehring 083f687
polish
justine-gehring 34a0d0e
polish
justine-gehring 17819c9
polish
justine-gehring efb9f01
Apply formatter
timtebeek ea3ba55
Light polish
timtebeek c024a27
Use preconditions to tell tests and non tests apart
timtebeek b824b32
Apply correct license headers
timtebeek 607c079
Further polish
timtebeek 0de9e43
Loop over entrySet
timtebeek 2d7574d
Merge branch 'main' into feat-find-unit-tests
justine-gehring dbb3fdd
Update src/main/java/org/openrewrite/java/testing/search/FindUnitTest…
justine-gehring File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
53 changes: 53 additions & 0 deletions
53
src/main/java/org/openrewrite/java/testing/search/FindUnitTestTable.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,53 @@ | ||
/* | ||
* Copyright 2025 the original author or authors. | ||
* <p> | ||
* Licensed under the Moderne Source Available License (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://docs.moderne.io/licensing/moderne-source-available-license | ||
* <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.Value; | ||
import org.openrewrite.Column; | ||
import org.openrewrite.DataTable; | ||
import org.openrewrite.Recipe; | ||
|
||
public class FindUnitTestTable extends DataTable<FindUnitTestTable.Row> { | ||
public FindUnitTestTable(Recipe recipe) { | ||
super(recipe, | ||
recipe.getName(), | ||
recipe.getDescription()); | ||
} | ||
|
||
@Value | ||
public static class Row { | ||
@Column(displayName = "Full method name", | ||
description = "The fully qualified name of the method declaration") | ||
String fullyQualifiedMethodName; | ||
|
||
@Column(displayName = "Method name", | ||
description = "The name of the method declaration") | ||
String methodName; | ||
|
||
@Column(displayName = "Method invocation", | ||
description = "How the method declaration is used as method invocation in a unit test.") | ||
String methodInvocationExample; | ||
|
||
@Column(displayName = "Name of test", | ||
description = "The name of the unit test where the method declaration is used.") | ||
String nameOfTest; | ||
|
||
@Column(displayName = "Location of test", | ||
description = "The location of the unit test where the method declaration is used.") | ||
String locationOfTest; | ||
} | ||
} | ||
|
||
117 changes: 117 additions & 0 deletions
117
src/main/java/org/openrewrite/java/testing/search/FindUnitTests.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,117 @@ | ||
/* | ||
* Copyright 2025 the original author or authors. | ||
* <p> | ||
* Licensed under the Moderne Source Available License (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://docs.moderne.io/licensing/moderne-source-available-license | ||
* <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.Value; | ||
import org.openrewrite.ExecutionContext; | ||
import org.openrewrite.Preconditions; | ||
import org.openrewrite.ScanningRecipe; | ||
import org.openrewrite.TreeVisitor; | ||
import org.openrewrite.java.JavaVisitor; | ||
import org.openrewrite.java.search.IsLikelyNotTest; | ||
import org.openrewrite.java.search.IsLikelyTest; | ||
import org.openrewrite.java.tree.J; | ||
|
||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import static java.util.Collections.singletonList; | ||
|
||
public class FindUnitTests extends ScanningRecipe<FindUnitTests.Accumulator> { | ||
|
||
@Override | ||
public String getDisplayName() { | ||
return "Find unit tests"; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "Produces a data table showing examples of how methods declared get used in unit tests."; | ||
} | ||
|
||
transient FindUnitTestTable unitTestTable = new FindUnitTestTable(this); | ||
|
||
public static class Accumulator { | ||
Map<UnitTest, Set<J.MethodInvocation>> unitTestAndTheirMethods = new HashMap<>(); | ||
} | ||
|
||
@Override | ||
public Accumulator getInitialValue(ExecutionContext ctx) { | ||
return new Accumulator(); | ||
} | ||
|
||
@Override | ||
public TreeVisitor<?, ExecutionContext> getScanner(Accumulator acc) { | ||
JavaVisitor<ExecutionContext> scanningVisitor = 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( | ||
getCursor().firstEnclosingOrThrow(J.ClassDeclaration.class).getType().getFullyQualifiedName(), | ||
methodDeclaration.getSimpleName(), | ||
methodDeclaration.printTrimmed(getCursor())); | ||
acc.unitTestAndTheirMethods.merge(unitTest, | ||
new HashSet<>(singletonList(method)), | ||
(a, b) -> { | ||
a.addAll(b); | ||
return a; | ||
}); | ||
} | ||
return super.visitMethodInvocation(method, ctx); | ||
} | ||
}; | ||
return Preconditions.check(new IsLikelyTest().getVisitor(), scanningVisitor); | ||
} | ||
|
||
@Override | ||
public TreeVisitor<?, ExecutionContext> getVisitor(Accumulator acc) { | ||
JavaVisitor<ExecutionContext> tableRowVisitor = new JavaVisitor<ExecutionContext>() { | ||
@Override | ||
public J visitMethodDeclaration(J.MethodDeclaration methodDeclaration, ExecutionContext ctx) { | ||
for (Map.Entry<UnitTest, Set<J.MethodInvocation>> entry : acc.unitTestAndTheirMethods.entrySet()) { | ||
for (J.MethodInvocation method : entry.getValue()) { | ||
if (method.getSimpleName().equals(methodDeclaration.getSimpleName())) { | ||
unitTestTable.insertRow(ctx, new FindUnitTestTable.Row( | ||
methodDeclaration.getName().toString(), | ||
methodDeclaration.getSimpleName(), | ||
method.printTrimmed(getCursor()), | ||
entry.getKey().getClazz(), | ||
entry.getKey().getUnitTestName() | ||
)); | ||
} | ||
} | ||
} | ||
return super.visitMethodDeclaration(methodDeclaration, ctx); | ||
} | ||
}; | ||
return Preconditions.check(new IsLikelyNotTest().getVisitor(), tableRowVisitor); | ||
} | ||
|
||
} | ||
|
||
@Value | ||
class UnitTest { | ||
String clazz; | ||
String unitTestName; | ||
String unitTest; | ||
Comment on lines
+114
to
+116
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of String we might want to use the types of the LST elements matched here instead. |
||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/org/openrewrite/java/testing/search/package-info.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,19 @@ | ||
/* | ||
* Copyright 2024 the original author or authors. | ||
* <p> | ||
* Licensed under the Moderne Source Available License (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://docs.moderne.io/licensing/moderne-source-available-license | ||
* <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. | ||
*/ | ||
@NullMarked | ||
package org.openrewrite.java.testing.search; | ||
|
||
import org.jspecify.annotations.NullMarked; |
125 changes: 125 additions & 0 deletions
125
src/test/java/org/openrewrite/java/testing/search/FindUnitTestsTest.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,125 @@ | ||
/* | ||
* Copyright 2025 the original author or authors. | ||
* <p> | ||
* Licensed under the Moderne Source Available License (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://docs.moderne.io/licensing/moderne-source-available-license | ||
* <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 org.intellij.lang.annotations.Language; | ||
import org.junit.jupiter.api.Nested; | ||
import org.junit.jupiter.api.Test; | ||
import org.openrewrite.DocumentExample; | ||
import org.openrewrite.InMemoryExecutionContext; | ||
import org.openrewrite.java.JavaParser; | ||
import org.openrewrite.test.RecipeSpec; | ||
import org.openrewrite.test.RewriteTest; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.openrewrite.java.Assertions.java; | ||
|
||
class FindUnitTestsTest implements RewriteTest { | ||
|
||
@Language("java") | ||
private static final String CLASS_FOO = """ | ||
package foo; | ||
|
||
public class Foo { | ||
public void bar() { | ||
} | ||
public void baz() { | ||
} | ||
} | ||
"""; | ||
|
||
@Override | ||
public void defaults(RecipeSpec spec) { | ||
spec.recipe(new FindUnitTests()) | ||
.parser(JavaParser.fromJavaVersion().classpathFromResources(new InMemoryExecutionContext(), | ||
"junit-jupiter-api-5.9")); | ||
} | ||
|
||
@DocumentExample | ||
@Test | ||
void dataTable() { | ||
rewriteRun( | ||
spec -> spec.dataTable(FindUnitTestTable.Row.class, rows -> assertThat(rows) | ||
.extracting(FindUnitTestTable.Row::getFullyQualifiedMethodName) | ||
.containsExactly("bar", "baz")), | ||
java(CLASS_FOO), | ||
//language=java | ||
java( | ||
""" | ||
import foo.Foo; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class FooTest { | ||
@Test | ||
public void test() { | ||
Foo foo = new Foo(); | ||
foo.bar(); | ||
foo.baz(); | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Nested | ||
class NotFound { | ||
|
||
@Test | ||
void notATest() { | ||
//language=java | ||
rewriteRun( | ||
spec -> spec.afterRecipe(run -> assertThat(run.getDataTables()).hasSize(1)), // stats table | ||
java(CLASS_FOO), | ||
java( | ||
""" | ||
import foo.Foo; | ||
|
||
public class FooTest { | ||
public void test() { | ||
new Foo().bar(); | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
|
||
@Test | ||
void methodFromTest() { | ||
//language=java | ||
rewriteRun( | ||
spec -> spec.afterRecipe(run -> assertThat(run.getDataTables()).hasSize(1)), // stats table | ||
java(CLASS_FOO), | ||
java( | ||
""" | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class FooTest { | ||
@Test | ||
public void test() { | ||
beep(); | ||
} | ||
|
||
public void beep() { | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.