Skip to content

Commit

Permalink
Merge branch 'main' into issue-373
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek authored Jul 5, 2024
2 parents 569695e + 3211368 commit 08b64b1
Show file tree
Hide file tree
Showing 43 changed files with 2,379 additions and 536 deletions.
2 changes: 2 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ dependencies {

testImplementation("org.openrewrite:rewrite-java-17")
testImplementation("org.openrewrite:rewrite-groovy")
testImplementation("org.openrewrite:rewrite-kotlin:$rewriteVersion")
testImplementation("org.openrewrite.gradle.tooling:model:$rewriteVersion")

testRuntimeOnly("org.gradle:gradle-tooling-api:latest.release")
Expand All @@ -54,6 +55,7 @@ dependencies {
testRuntimeOnly("net.datafaker:datafaker:latest.release") {
exclude(group = "org.yaml", module = "snakeyaml")
}
testRuntimeOnly("org.mockito.kotlin:mockito-kotlin:latest.release")
testRuntimeOnly("org.testcontainers:testcontainers:latest.release")
testRuntimeOnly("org.testcontainers:nginx:latest.release")

Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.8-bin.zip
networkTimeout=10000
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionSha256Sum=544c35d6bd849ae8a5ed0bcea39ba677dc40f49df7d1835561582da2009b961d
distributionSha256Sum=a4b4158601f8636cdeeab09bd76afb640030bb5b144aafe261a5e8af027dc612
2 changes: 1 addition & 1 deletion gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
# Darwin, MinGW, and NonStop.
#
# (3) This script is generated from the Groovy template
# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# https://github.com/gradle/gradle/blob/HEAD/platforms/jvm/plugins-application/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
# within the Gradle project.
#
# You can find Gradle at https://github.com/gradle/gradle/.
Expand Down
27 changes: 15 additions & 12 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,34 @@ pluginManagement {
rootProject.name = "rewrite-testing-frameworks"

plugins {
id("com.gradle.enterprise") version "latest.release"
id("com.gradle.develocity") version "latest.release"
id("com.gradle.common-custom-user-data-gradle-plugin") version "latest.release"
}

gradleEnterprise {
val isCiServer = System.getenv("CI")?.equals("true") ?: false
develocity {
server = "https://ge.openrewrite.org/"

val isCiServer = System.getenv("CI")?.equals("true") ?: false
val accessKey = System.getenv("GRADLE_ENTERPRISE_ACCESS_KEY")
val authenticated = !accessKey.isNullOrBlank()
buildCache {
remote(gradleEnterprise.buildCache) {
remote(develocity.buildCache) {
isEnabled = true
val accessKey = System.getenv("GRADLE_ENTERPRISE_ACCESS_KEY")
isPush = isCiServer && !accessKey.isNullOrBlank()
isPush = isCiServer && authenticated
}
}

buildScan {
capture {
isTaskInputFiles = true
fileFingerprints = true
}

isUploadInBackground = !isCiServer

publishAlways()
this as com.gradle.enterprise.gradleplugin.internal.extension.BuildScanExtensionWithHiddenFeatures
publishIfAuthenticated()
publishing {
onlyIf {
authenticated
}
}

uploadInBackground = !isCiServer
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* 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.assertj;

import org.openrewrite.ExecutionContext;
import org.openrewrite.Preconditions;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.search.UsesMethod;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;

import java.util.Collections;

/**
* AssertJ has a more idiomatic way of asserting that a String is empty.
* This recipe will find instances of `assertThat(String).isEqualTo("")` and replace them with `isEmpty()`.
*/
public class IsEqualToEmptyString extends Recipe {

private static final MethodMatcher IS_EQUAL_TO = new MethodMatcher("org.assertj.core.api.AbstractStringAssert isEqualTo(java.lang.String)");

@Override
public String getDisplayName() {
return "Convert `assertThat(String).isEqualTo(\"\")` to `isEmpty()`";
}

@Override
public String getDescription() {
return "Adopt idiomatic AssertJ assertion for empty Strings.";
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(
new UsesMethod<>(IS_EQUAL_TO),
new JavaIsoVisitor<ExecutionContext>() {
@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation mi = super.visitMethodInvocation(method, ctx);
if (IS_EQUAL_TO.matches(mi) && J.Literal.isLiteralValue(mi.getArguments().get(0), "")) {
JavaType.Method isEmptyMethodType = mi.getMethodType().withName("isEmpty");
return mi
.withName(mi.getName().withSimpleName("isEmpty").withType(isEmptyMethodType))
.withMethodType(isEmptyMethodType)
.withArguments(Collections.emptyList());
}
return mi;
}
}
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
);
//Make sure there is a static import for "org.assertj.core.api.Assertions.assertThat" (even if not referenced)
maybeAddImport("org.assertj.core.api.Assertions", "fail", false);
maybeRemoveImport("org.junit.jupiter.api.Assertions.fail");
return super.visitMethodInvocation(method, ctx);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import java.time.Duration;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;

public class RemoveTestPrefix extends Recipe {

Expand Down Expand Up @@ -121,6 +122,21 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method,
}
}

// Skip when calling a similarly named method
AtomicBoolean skip = new AtomicBoolean(false);
new JavaIsoVisitor<AtomicBoolean>() {
@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, AtomicBoolean atomicBoolean) {
if (method.getName().getSimpleName().equals(newMethodName)) {
skip.set(true);
}
return super.visitMethodInvocation(method, atomicBoolean);
}
}.visitMethodDeclaration(m, skip);
if (skip.get()) {
return m;
}

// Rename method and return
type = type.withName(newMethodName);
return m.withName(m.getName().withSimpleName(newMethodName).withType(type))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,27 @@

import lombok.AllArgsConstructor;
import lombok.EqualsAndHashCode;
import lombok.RequiredArgsConstructor;
import lombok.Value;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Option;
import org.openrewrite.Recipe;
import org.openrewrite.ScanningRecipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.internal.lang.Nullable;
import org.openrewrite.java.ChangeMethodAccessLevelVisitor;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.tree.*;
import org.openrewrite.java.tree.Comment;
import org.openrewrite.java.tree.Flag;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.TypeUtils;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.*;

@AllArgsConstructor
@EqualsAndHashCode(callSuper = false)
public class TestsShouldNotBePublic extends Recipe {
public class TestsShouldNotBePublic extends ScanningRecipe<TestsShouldNotBePublic.Accumulator> {

@Option(displayName = "Remove protected modifiers",
description = "Also remove protected modifiers from test methods",
Expand All @@ -60,25 +62,46 @@ public Set<String> getTags() {
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new TestsNotPublicVisitor(Boolean.TRUE.equals(removeProtectedModifiers));
public Accumulator getInitialValue(ExecutionContext ctx) {
return new Accumulator();
}

@Override
public TreeVisitor<?, ExecutionContext> getScanner(Accumulator acc) {
return new JavaIsoVisitor<ExecutionContext>() {
@Override
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDeclaration, ExecutionContext ctx) {
J.ClassDeclaration cd = super.visitClassDeclaration(classDeclaration, ctx);
if (cd.getExtends() != null) {
acc.extendedClasses.add(String.valueOf(cd.getExtends().getType()));
}
return cd;
}
};
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor(Accumulator acc) {
return new TestsNotPublicVisitor(Boolean.TRUE.equals(removeProtectedModifiers), acc);
}

public static class Accumulator {
Set<String> extendedClasses = new HashSet<>();
}

@RequiredArgsConstructor
private static final class TestsNotPublicVisitor extends JavaIsoVisitor<ExecutionContext> {
private final Boolean orProtected;

private TestsNotPublicVisitor(Boolean orProtected) {
this.orProtected = orProtected;
}
private final Accumulator acc;

@Override
public J.ClassDeclaration visitClassDeclaration(J.ClassDeclaration classDecl, ExecutionContext ctx) {
J.ClassDeclaration c = super.visitClassDeclaration(classDecl, ctx);

if (c.getKind() != J.ClassDeclaration.Kind.Type.Interface
&& c.getModifiers().stream().anyMatch(mod -> mod.getType() == J.Modifier.Type.Public)
&& c.getModifiers().stream().noneMatch(mod -> mod.getType() == J.Modifier.Type.Abstract)) {

&& c.getModifiers().stream().noneMatch(mod -> mod.getType() == J.Modifier.Type.Abstract)
&& !acc.extendedClasses.contains(String.valueOf(c.getType()))) {
boolean hasTestMethods = c.getBody().getStatements().stream()
.filter(org.openrewrite.java.tree.J.MethodDeclaration.class::isInstance)
.map(J.MethodDeclaration.class::cast)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class ArgumentMatchersRewriter {
}

private static final Map<JavaType.Primitive, String> PRIMITIVE_TO_MOCKITO_ARGUMENT_MATCHER = new HashMap<>();

static {
PRIMITIVE_TO_MOCKITO_ARGUMENT_MATCHER.put(JavaType.Primitive.Int, "anyInt");
PRIMITIVE_TO_MOCKITO_ARGUMENT_MATCHER.put(JavaType.Primitive.Long, "anyLong");
Expand All @@ -81,7 +82,7 @@ class ArgumentMatchersRewriter {
this.expectationsBlock = expectationsBlock;
}

J.Block rewriteExpectationsBlock() {
J.Block rewriteJMockitBlock() {
List<Statement> newStatements = new ArrayList<>(expectationsBlock.getStatements().size());
for (Statement expectationStatement : expectationsBlock.getStatements()) {
// for each statement, check if it's a method invocation and replace any argument matchers
Expand Down
Loading

0 comments on commit 08b64b1

Please sign in to comment.