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 Feb 5, 2024
2 parents 0f3736d + 339c068 commit 68e5d3e
Show file tree
Hide file tree
Showing 49 changed files with 2,124 additions and 538 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/comment-pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: comment-pr

# https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#using-data-from-the-triggering-workflow
on:
workflow_run:
workflows: ["receive-pr"]
types:
- completed

# https://securitylab.github.com/research/github-actions-preventing-pwn-requests/
# Since this pull request has write permissions on the target repo, we should **NOT** execute any untrusted code.
jobs:
post-suggestions:
uses: openrewrite/gh-automation/.github/workflows/comment-pr.yml@main
secrets:
GH_PAT_ACTIONS_READ: ${{ secrets.GH_PAT_ACTIONS_READ }}
12 changes: 12 additions & 0 deletions .github/workflows/receive-pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: receive-pr
on:
pull_request:
types: [opened, synchronize]
branches:
- main

# https://securitylab.github.com/research/github-actions-preventing-pwn-requests/
# Since this pull request receives untrusted code, we should **NOT** have any secrets in the environment.
jobs:
upload-patch:
uses: openrewrite/gh-automation/.github/workflows/receive-pr.yml@main
2 changes: 1 addition & 1 deletion gradle/licenseHeader.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright 2023 the original author or authors.
Copyright ${year} 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.
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.5-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.6-bin.zip
networkTimeout=10000
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionSha256Sum=9d926787066a081739e8200858338b4a69e837c3a821a33aca9db09dd4a41026
distributionSha256Sum=9631d53cf3e74bfa726893aee1f8994fee4e060c401335946dba2156f440f24c
20 changes: 10 additions & 10 deletions gradlew.bat
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if %ERRORLEVEL% equ 0 goto execute

echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
echo. 1>&2
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 1>&2
echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2

goto fail

Expand All @@ -57,11 +57,11 @@ set JAVA_EXE=%JAVA_HOME%/bin/java.exe

if exist "%JAVA_EXE%" goto execute

echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.
echo. 1>&2
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 1>&2
echo. 1>&2
echo Please set the JAVA_HOME variable in your environment to match the 1>&2
echo location of your Java installation. 1>&2

goto fail

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import org.openrewrite.Preconditions;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.internal.NameCaseConvention;
import org.openrewrite.java.AnnotationMatcher;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.search.UsesType;
import org.openrewrite.java.tree.J;
Expand Down Expand Up @@ -70,6 +72,9 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
}

private static class RemoveTestPrefixVisitor extends JavaIsoVisitor<ExecutionContext> {

private static final AnnotationMatcher ANNOTATION_MATCHER = new AnnotationMatcher("@org.junit.jupiter.params.provider.MethodSource");

@Override
public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method,
ExecutionContext ctx) {
Expand All @@ -80,6 +85,7 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method,
int nameLength = simpleName.length();
if (nameLength < 5
|| !simpleName.startsWith("test")
|| !(simpleName.charAt(4) == '_' || Character.isUpperCase(simpleName.charAt(4)))
|| TypeUtils.isOverride(method.getMethodType())
|| !hasJUnit5MethodAnnotation(method)) {
return m;
Expand All @@ -93,19 +99,29 @@ public J.MethodDeclaration visitMethodDeclaration(J.MethodDeclaration method,
return m;
}

// Rename method
// Avoid reserved keywords
String newMethodName = snakecase
? Character.toLowerCase(simpleName.charAt(5)) + simpleName.substring(6)
: Character.toLowerCase(simpleName.charAt(4)) + simpleName.substring(5);
? NameCaseConvention.format(NameCaseConvention.LOWER_UNDERSCORE, simpleName.substring(5))
: NameCaseConvention.format(NameCaseConvention.LOWER_CAMEL, simpleName.substring(4));
if (RESERVED_KEYWORDS.contains(newMethodName)) {
return m;
}

// Prevent conflicts with existing methods
JavaType.Method type = m.getMethodType();
if (type == null || methodExists(type, newMethodName)) {
return m;
}

// Skip implied methodSource
for (J.Annotation annotation : method.getLeadingAnnotations()) {
if (ANNOTATION_MATCHER.matches(annotation) &&
(annotation.getArguments() == null || annotation.getArguments().isEmpty())) {
return m;
}
}

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

@SuppressWarnings("SimplifyStreamApiCallChains")
@Value
@EqualsAndHashCode(callSuper = true)
@EqualsAndHashCode(callSuper = false)
public class TestsShouldIncludeAssertions extends Recipe {
private static final List<String> TEST_ANNOTATIONS = Collections.singletonList("org.junit.jupiter.api.Test");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
import java.util.Set;

@AllArgsConstructor
@EqualsAndHashCode(callSuper = true)
@EqualsAndHashCode(callSuper = false)
public class TestsShouldNotBePublic extends Recipe {

@Option(displayName = "Remove protected modifiers",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation methodInvocat
String replacement = 2 <= arguments.size() &&
TypeUtils.asArray(arguments.get(arguments.size() - 2).getType()) != null ?
"containsExactly" : "isEqualTo";
doAfterVisit(new HamcrestMatcherToAssertJ("is", replacement).getVisitor());
doAfterVisit(new HamcrestMatcherToAssertJ("is", replacement, null).getVisitor());

return super.visitMethodInvocation(methodInvocation, ctx);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;
import org.openrewrite.java.tree.TypeUtils;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -51,6 +52,13 @@ public class HamcrestMatcherToAssertJ extends Recipe {
@Nullable
String assertion;

@Option(displayName = "Argument Type",
description = "The type of the argument to the Hamcrest `Matcher`.",
example = "java.math.BigDecimal",
required = false)
@Nullable
String argumentType;

@Override
public String getDisplayName() {
return "Migrate from Hamcrest `Matcher` to AssertJ";
Expand All @@ -71,7 +79,6 @@ private class MigrateToAssertJVisitor extends JavaIsoVisitor<ExecutionContext> {
private final MethodMatcher matchersMatcher = new MethodMatcher("org.hamcrest.*Matchers " + matcher + "(..)");
private final MethodMatcher subMatcher = new MethodMatcher("org.hamcrest.*Matchers *(org.hamcrest.Matcher)");


@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation mi = super.visitMethodInvocation(method, ctx);
Expand All @@ -89,6 +96,10 @@ private J.MethodInvocation replace(J.MethodInvocation mi, ExecutionContext ctx)
if (!matchersMatcher.matches(matcherArgument) || subMatcher.matches(matcherArgument)) {
return mi;
}
if (argumentType != null && !TypeUtils.isOfClassType(actualArgument.getType(), argumentType)) {
return mi;
}

String actual = typeToIndicator(actualArgument.getType());
J.MethodInvocation matcherArgumentMethod = (J.MethodInvocation) matcherArgument;
JavaTemplate template = JavaTemplate.builder(String.format(
Expand Down
Loading

0 comments on commit 68e5d3e

Please sign in to comment.