-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
…532) * Ensure Jmockit expectations with no times or result transform to a mockito verify * Minor polish to text blocks * Make times, minTimes, maxTimes more flexible as it was originally so even if someone mistakenly puts times and minTimes together, it still migrates without issue * Add feature to enable migration of Jmockit Injectable annotation exactly as we are doing the Mocked annotation ie method parameter annotation as well as field annotation. Have moved all of the code from JMockitMockedVariableToMockito to JMockitAnnotationToMockito for code reuse. Also add the corresponding test cases and jmockit.yml file modification. * Use `@Option` instead of inheritance * Drop Option and just replace both variants * Update display name and description to match application --------- Co-authored-by: Tim te Beek <[email protected]>
- Loading branch information
1 parent
288c9ef
commit 8388b28
Showing
5 changed files
with
214 additions
and
116 deletions.
There are no files selected for viewing
108 changes: 108 additions & 0 deletions
108
src/main/java/org/openrewrite/java/testing/jmockit/JMockitAnnotatedArgumentToMockito.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,108 @@ | ||
/* | ||
* 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.jmockit; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.NoArgsConstructor; | ||
import org.openrewrite.ExecutionContext; | ||
import org.openrewrite.Preconditions; | ||
import org.openrewrite.Recipe; | ||
import org.openrewrite.TreeVisitor; | ||
import org.openrewrite.internal.ListUtils; | ||
import org.openrewrite.java.JavaIsoVisitor; | ||
import org.openrewrite.java.JavaParser; | ||
import org.openrewrite.java.JavaTemplate; | ||
import org.openrewrite.java.search.FindAnnotations; | ||
import org.openrewrite.java.search.UsesType; | ||
import org.openrewrite.java.tree.J; | ||
import org.openrewrite.java.tree.Statement; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
@EqualsAndHashCode(callSuper = false) | ||
public class JMockitAnnotatedArgumentToMockito extends Recipe { | ||
@Override | ||
public String getDisplayName() { | ||
return "Convert JMockit `@Mocked` and `@Injectable` annotated arguments"; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return "Convert JMockit `@Mocked` and `@Injectable` annotated arguments into Mockito statements."; | ||
} | ||
|
||
@Override | ||
public TreeVisitor<?, ExecutionContext> getVisitor() { | ||
return Preconditions.check( | ||
Preconditions.or( | ||
new UsesType<>("mockit.Mocked", false), | ||
new UsesType<>("mockit.Injectable", false) | ||
), | ||
new JavaIsoVisitor<ExecutionContext>() { | ||
@Override | ||
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration methodDeclaration, ExecutionContext ctx) { | ||
J.MethodDeclaration md = super.visitMethodDeclaration(methodDeclaration, ctx); | ||
|
||
List<Statement> parameters = md.getParameters(); | ||
if (!parameters.isEmpty() && !(parameters.get(0) instanceof J.Empty)) { | ||
maybeRemoveImport("mockit.Injectable"); | ||
maybeRemoveImport("mockit.Mocked"); | ||
maybeAddImport("org.mockito.Mockito"); | ||
|
||
// Create lists to store the mocked parameters and the new type parameters | ||
List<J.VariableDeclarations> mockedParameter = new ArrayList<>(); | ||
|
||
// Remove any mocked parameters from the method declaration | ||
md = md.withParameters(ListUtils.map(parameters, parameter -> { | ||
if (parameter instanceof J.VariableDeclarations) { | ||
J.VariableDeclarations variableDeclarations = (J.VariableDeclarations) parameter; | ||
// Check if the parameter has the annotation "mockit.Mocked or mockit.Injectable" | ||
if (!FindAnnotations.find(variableDeclarations, "mockit.Injectable").isEmpty() || | ||
!FindAnnotations.find(variableDeclarations, "mockit.Mocked").isEmpty() ) { | ||
mockedParameter.add(variableDeclarations); | ||
return null; | ||
} | ||
} | ||
return parameter; | ||
})); | ||
|
||
// Add mocked parameters as statements to the method declaration | ||
if (!mockedParameter.isEmpty()) { | ||
JavaTemplate addStatementsTemplate = JavaTemplate.builder("#{} #{} = Mockito.mock(#{}.class);\n") | ||
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "mockito-core-3.12")) | ||
.imports("org.mockito.Mockito") | ||
.contextSensitive() | ||
.build(); | ||
// Retain argument order by iterating in reverse | ||
for (int i = mockedParameter.size() - 1; i >= 0; i--) { | ||
J.VariableDeclarations variableDeclarations = mockedParameter.get(i); | ||
// Apply the template and update the method declaration | ||
md = addStatementsTemplate.apply(updateCursor(md), | ||
md.getBody().getCoordinates().firstStatement(), | ||
variableDeclarations.getTypeExpression().toString(), | ||
variableDeclarations.getVariables().get(0).getSimpleName(), | ||
variableDeclarations.getTypeExpression().toString()); | ||
} | ||
} | ||
} | ||
return md; | ||
} | ||
} | ||
); | ||
} | ||
} |
106 changes: 0 additions & 106 deletions
106
src/main/java/org/openrewrite/java/testing/jmockit/JMockitMockedVariableToMockito.java
This file was deleted.
Oops, something went wrong.
19 changes: 19 additions & 0 deletions
19
src/main/java/org/openrewrite/java/testing/jmockit/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 2021 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. | ||
*/ | ||
@NonNullApi | ||
package org.openrewrite.java.testing.jmockit; | ||
|
||
import org.openrewrite.internal.lang.NonNullApi; |
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
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