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

Changed AnyToNullable from declarative to imperative so that it can h… #378

Merged
merged 1 commit into from
Jul 11, 2023
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
@@ -0,0 +1,83 @@
/*
* Copyright 2023 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.mockito;

import org.openrewrite.*;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.java.ChangeMethodName;
import org.openrewrite.java.ChangeMethodTargetToStatic;
import org.openrewrite.java.tree.J;
import org.openrewrite.xml.tree.Xml;

import java.util.concurrent.atomic.AtomicBoolean;

public class AnyToNullable extends ScanningRecipe<AtomicBoolean> {
@Override
public String getDisplayName() {
return "Replace Mockito 1.x `anyString()`/`any()` with `nullable(Class)`";
}

@Override
public String getDescription() {
return "Since Mockito 2.10 `anyString()` and `any()` no longer matches null values. Use `nullable(Class)` instead.";
}

@Override
public AtomicBoolean getInitialValue(ExecutionContext ctx) {
return new AtomicBoolean();
}

@Override
public TreeVisitor<?, ExecutionContext> getScanner(AtomicBoolean acc) {
org.openrewrite.maven.search.FindDependency mavenFindDependency =
new org.openrewrite.maven.search.FindDependency("org.mockito", "mockito-all");
org.openrewrite.gradle.search.FindDependency gradleFindDependency =
new org.openrewrite.gradle.search.FindDependency("org.mockito", "mockito-all", null);
return new TreeVisitor<Tree, ExecutionContext>() {
@Override
public @Nullable Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
if (!acc.get()) {
if (tree instanceof Xml.Document && tree != mavenFindDependency.getVisitor().visit(tree, ctx)) {
acc.set(true);
} else if (tree instanceof J && tree != gradleFindDependency.getVisitor().visit(tree, ctx)) {
acc.set(true);
}
}
return tree;
}
};
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor(AtomicBoolean acc) {
ChangeMethodName changeMethodName = new ChangeMethodName(
"org.mockito.Mockito any(java.lang.Class)", "nullable", null, null);
ChangeMethodTargetToStatic changeMethodTargetToStatic = new ChangeMethodTargetToStatic(
"org.mockito.Mockito nullable(java.lang.Class)", "org.mockito.ArgumentMatchers", null, null);
AnyStringToNullable anyStringToNullable = new AnyStringToNullable();
return Preconditions.check(acc.get(), new TreeVisitor<Tree, ExecutionContext>() {
@Override
public @Nullable Tree visit(@Nullable Tree tree, ExecutionContext ctx) {
if (tree instanceof J) {
doAfterVisit(changeMethodName.getVisitor());
doAfterVisit(changeMethodTargetToStatic.getVisitor());
doAfterVisit(anyStringToNullable.getVisitor());
}
return super.visit(tree, ctx);
}
});
}
}
31 changes: 0 additions & 31 deletions src/main/resources/META-INF/rewrite/mockito.yml
Original file line number Diff line number Diff line change
Expand Up @@ -123,37 +123,6 @@ recipeList:
newVersion: 3.x
---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.java.testing.mockito.UsesMockitoAll
displayName: Uses Mockito all from v1.x
description: Finds projects that depend on `mockito-all` through Maven or Gradle.
tags:
- testing
- mockito
recipeList:
- org.openrewrite.maven.search.FindDependency:
groupId: org.mockito
artifactId: mockito-all
- org.openrewrite.gradle.search.FindDependency:
groupId: org.mockito
artifactId: mockito-all
---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.java.testing.mockito.AnyToNullable
displayName: Replace Mockito 1.x `any(Class)` and `anyString()` with `nullable(Class)`
description: Since Mockito 2.10 `any(Class)` and `anyString()` no longer match null values. Use `nullable(Class)` instead.
tags:
- testing
- mockito
recipeList:
- org.openrewrite.java.ChangeMethodName:
methodPattern: org.mockito.Mockito any(java.lang.Class)
newMethodName: nullable
- org.openrewrite.java.ChangeMethodTargetToStatic:
methodPattern: org.mockito.Mockito nullable(java.lang.Class)
fullyQualifiedTargetTypeName: org.mockito.ArgumentMatchers
- org.openrewrite.java.testing.mockito.AnyStringToNullable
---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.java.testing.mockito.ReplacePowerMockito
displayName: Replace PowerMock with raw Mockito
description: Replace PowerMock with raw Mockito.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ String greet(String name) {
import static org.mockito.Mockito.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class MyTest {
void test() {
Example example = mock(Example.class);
Expand All @@ -62,7 +62,7 @@ void test() {
import static org.mockito.ArgumentMatchers.nullable;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class MyTest {
void test() {
Example example = mock(Example.class);
Expand Down Expand Up @@ -90,7 +90,7 @@ String greet(int value) {
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.anyInt;

class MyTest {
void test() {
Example example = mock(Example.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,7 @@ public void defaults(RecipeSpec spec) {
.parser(JavaParser.fromJavaVersion()
.classpathFromResources(new InMemoryExecutionContext(), "mockito-core-3.12")
.logCompilationWarningsAndErrors(true))
.recipe(Environment.builder()
.scanRuntimeClasspath("org.openrewrite.java.testing.mockito")
.build()
.activateRecipes("org.openrewrite.java.testing.mockito.AnyToNullable"));
.recipe(new AnyToNullable());
}

@Test
Expand Down Expand Up @@ -73,7 +70,7 @@ String greet(Object obj) {
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.any;

class MyTest {
void test() {
Example example = mock(Example.class);
Expand All @@ -97,6 +94,52 @@ void test() {
);
}

@Test
void doesNotTouchIfMockitoTwoPlus() {
//language=java
rewriteRun(
//language=xml
pomXml("""
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>foo</artifactId>
<version>1.0.0</version>
<dependencies>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.12.0</version>
</dependency>
</dependencies>
</project>
"""),
//language=java
java("""
class Example {
String greet(Object obj) {
return "Hello " + obj;
}
}
"""),
//language=java
java(
"""
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.any;

class MyTest {
void test() {
Example example = mock(Example.class);
when(example.greet(any(Object.class))).thenReturn("Hello world");
}
}
"""
)
);
}

}