From 3711b0c8a7743b121c8628b4e0a7444932cff26d Mon Sep 17 00:00:00 2001
From: timo <129042237+timo-abele@users.noreply.github.com>
Date: Thu, 25 Jan 2024 19:39:00 +0100
Subject: [PATCH 1/9] add dependencies copied from rewrite-recipe-starter
---
build.gradle.kts | 16 ++++++++++++++++
1 file changed, 16 insertions(+)
diff --git a/build.gradle.kts b/build.gradle.kts
index aa19eca88..8526311e8 100644
--- a/build.gradle.kts
+++ b/build.gradle.kts
@@ -52,4 +52,20 @@ dependencies {
// testImplementation("org.hamcrest:hamcrest:latest.release")
// testImplementation("org.assertj:assertj-core:latest.release")
+
+ // The bom version can also be set to a specific version
+ // https://github.com/openrewrite/rewrite-recipe-bom/releases
+ implementation(platform("org.openrewrite.recipe:rewrite-recipe-bom:latest.release"))
+
+ // Refaster style recipes need the rewrite-templating annotation processor and dependency for generated recipes
+ // https://github.com/openrewrite/rewrite-templating/releases
+ annotationProcessor("org.openrewrite:rewrite-templating:latest.release")
+ implementation("org.openrewrite:rewrite-templating")
+ // The `@BeforeTemplate` and `@AfterTemplate` annotations are needed for refaster style recipes
+ compileOnly("com.google.errorprone:error_prone_core:2.19.1") {
+ exclude("com.google.auto.service", "auto-service-annotations")
+ }
+
+ implementation("org.assertj:assertj-core:3.+")
+
}
From 02465c239dc71710a9be48d695159bbd48e62093 Mon Sep 17 00:00:00 2001
From: timo <129042237+timo-abele@users.noreply.github.com>
Date: Thu, 25 Jan 2024 19:38:02 +0100
Subject: [PATCH 2/9] add refaster recipes and tests
---
.../assertj/SimplifyAssertExpectations.java | 80 ++++++++++
.../SimplifyAssertExpectationsTest.java | 142 ++++++++++++++++++
2 files changed, 222 insertions(+)
create mode 100644 src/main/java/org/openrewrite/java/testing/assertj/SimplifyAssertExpectations.java
create mode 100644 src/test/java/org/openrewrite/java/testing/assertj/SimplifyAssertExpectationsTest.java
diff --git a/src/main/java/org/openrewrite/java/testing/assertj/SimplifyAssertExpectations.java b/src/main/java/org/openrewrite/java/testing/assertj/SimplifyAssertExpectations.java
new file mode 100644
index 000000000..c50920b47
--- /dev/null
+++ b/src/main/java/org/openrewrite/java/testing/assertj/SimplifyAssertExpectations.java
@@ -0,0 +1,80 @@
+/*
+ * Copyright 2024 the original author or authors.
+ *
+ * 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
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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 com.google.errorprone.refaster.annotation.AfterTemplate;
+import com.google.errorprone.refaster.annotation.BeforeTemplate;
+import org.openrewrite.java.template.RecipeDescriptor;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+@RecipeDescriptor(
+ name = "Simplify the expectations of assertions",
+ description = "Simplifies expectations on various assertions."
+)
+public class SimplifyAssertExpectations {
+
+ @RecipeDescriptor(
+ name = "Simplify `assertThat(int).isEqualTo(0)`",
+ description = "Simplify `assertThat(int).isEqualTo(0)` to `assertThat(int).isZero()`."
+ )
+ public class SimplifyToIsZero {
+
+ @BeforeTemplate
+ void before(Integer i) {
+ assertThat(i).isEqualTo(0);
+ }
+
+ @AfterTemplate
+ void after(Integer i) {
+ assertThat(i).isZero();
+ }
+ }
+
+ @RecipeDescriptor(
+ name = "Simplify `assertThat(o).isEqualTo(null)`",
+ description = "Simplify `assertThat(o).isEqualTo(null)` to `assertThat(o).isNull()`."
+ )
+ public class SimplifyToIsNull {
+
+ @BeforeTemplate
+ void before(Object o) {
+ assertThat(o).isEqualTo(null);
+ }
+
+ @AfterTemplate
+ void after(Object o) {
+ assertThat(o).isNull();
+ }
+ }
+
+ @RecipeDescriptor(
+ name = "Simplify `assertThat(i >= j).isTrue()`",
+ description = "Simplify `assertThat(i >= j).isTrue()` to `assertThat(i).isGreaterThanOrEqualTo(j)`."
+ )
+ public class SimplifyGreaterEqualComparison {
+
+ @BeforeTemplate
+ void before(Integer i, Integer j) {
+ assertThat(i >= j).isTrue();
+ }
+
+ @AfterTemplate
+ void after(Integer i, Integer j) {
+ assertThat(i).isGreaterThanOrEqualTo(j);
+ }
+ }
+}
diff --git a/src/test/java/org/openrewrite/java/testing/assertj/SimplifyAssertExpectationsTest.java b/src/test/java/org/openrewrite/java/testing/assertj/SimplifyAssertExpectationsTest.java
new file mode 100644
index 000000000..5d5e4317e
--- /dev/null
+++ b/src/test/java/org/openrewrite/java/testing/assertj/SimplifyAssertExpectationsTest.java
@@ -0,0 +1,142 @@
+/*
+ * Copyright 2024 the original author or authors.
+ *
+ * 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
+ *
+ * https://www.apache.org/licenses/LICENSE-2.0
+ *
+ * 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.junit.jupiter.api.Test;
+import org.openrewrite.java.JavaParser;
+import org.openrewrite.test.RecipeSpec;
+import org.openrewrite.test.RewriteTest;
+
+import static org.openrewrite.java.Assertions.java;
+
+class SimplifyAssertExpectationsTest implements RewriteTest {
+
+ @Override
+ public void defaults(RecipeSpec spec) {
+ spec.parser(JavaParser.fromJavaVersion().classpath("assertj-core"))
+ .recipe(new SimplifyAssertExpectationsRecipes());
+ }
+
+ @Test
+ void simplifyIsZero() {
+ rewriteRun(
+ //language=java
+ java(
+ """
+ import static org.assertj.core.api.Assertions.assertThat;
+ import java.util.List;
+ import org.junit.jupiter.api.Test;
+
+ class Test {
+
+ @Test
+ void simpleTest() {
+ List list = List.of();
+ assertThat(list.size()).isEqualTo(0);
+ assertThat(list.size()).isNotEqualTo(0);
+ }
+ }
+ """,
+ """
+ import static org.assertj.core.api.Assertions.assertThat;
+ import java.util.List;
+ import org.junit.jupiter.api.Test;
+
+ class Test {
+
+ @Test
+ void simpleTest() {
+ List list = List.of();
+ assertThat(list.size()).isZero();
+ assertThat(list.size()).isNotZero();
+ }
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void simplifyIsNull() {
+ rewriteRun(
+ //language=java
+ java(
+ """
+ import static org.assertj.core.api.Assertions.assertThat;
+ import org.junit.jupiter.api.Test;
+
+ class Test {
+
+ @Test
+ void simpleTest() {
+ String s = null;
+ assertThat(s).isEqualTo(null);
+ assertThat(s).isNotEqualTo(null);
+ }
+ }
+ """,
+ """
+ import static org.assertj.core.api.Assertions.assertThat;
+ import org.junit.jupiter.api.Test;
+
+ class Test {
+
+ @Test
+ void simpleTest() {
+ String s = null;
+ assertThat(s).isNull();
+ assertThat(s).isNull();
+ }
+ }
+ """
+ )
+ );
+ }
+
+ @Test
+ void simplifyIsGreaterThanOrEqual() {
+ rewriteRun(
+ //language=java
+ java(
+ """
+ import static org.assertj.core.api.Assertions.assertThat;
+ import org.junit.jupiter.api.Test;
+
+ class Test {
+
+ @Test
+ void simpleTest() {
+ assertThat(2 >= 1).isTrue();
+ }
+ }
+ """,
+ """
+ import static org.assertj.core.api.Assertions.assertThat;
+ import org.junit.jupiter.api.Test;
+
+ class Test {
+
+ @Test
+ void simpleTest() {
+ assertThat(2).isGreaterThanOrEqualTo(1);
+ }
+ }
+ """
+ )
+ );
+ }
+
+}
\ No newline at end of file
From 2fdafa0d806f7e1197ce3ac50886f01c391f1306 Mon Sep 17 00:00:00 2001
From: timo <129042237+timo-abele@users.noreply.github.com>
Date: Sun, 4 Feb 2024 00:19:58 +0100
Subject: [PATCH 3/9] include in assertJ best practices recipe
---
src/main/resources/META-INF/rewrite/assertj.yml | 1 +
1 file changed, 1 insertion(+)
diff --git a/src/main/resources/META-INF/rewrite/assertj.yml b/src/main/resources/META-INF/rewrite/assertj.yml
index 5f768581c..569a979a7 100644
--- a/src/main/resources/META-INF/rewrite/assertj.yml
+++ b/src/main/resources/META-INF/rewrite/assertj.yml
@@ -26,6 +26,7 @@ recipeList:
- org.openrewrite.java.testing.assertj.JUnitToAssertj
- org.openrewrite.java.testing.assertj.StaticImports
- org.openrewrite.java.testing.assertj.SimplifyChainedAssertJAssertions
+ - org.openrewrite.java.testing.assertj.SimplifyAssertExpectationsRecipes
---
type: specs.openrewrite.org/v1beta/recipe
From 4bae294fd6c42082bb83123f350972e997535a1f Mon Sep 17 00:00:00 2001
From: timo <129042237+timo-abele@users.noreply.github.com>
Date: Wed, 7 Feb 2024 00:48:36 +0100
Subject: [PATCH 4/9] add dead simple case with literal to verify that the test
case works
---
.../java/testing/assertj/SimplifyAssertExpectationsTest.java | 2 ++
1 file changed, 2 insertions(+)
diff --git a/src/test/java/org/openrewrite/java/testing/assertj/SimplifyAssertExpectationsTest.java b/src/test/java/org/openrewrite/java/testing/assertj/SimplifyAssertExpectationsTest.java
index 5d5e4317e..37a54cd22 100644
--- a/src/test/java/org/openrewrite/java/testing/assertj/SimplifyAssertExpectationsTest.java
+++ b/src/test/java/org/openrewrite/java/testing/assertj/SimplifyAssertExpectationsTest.java
@@ -46,6 +46,7 @@ class Test {
void simpleTest() {
List list = List.of();
assertThat(list.size()).isEqualTo(0);
+ assertThat(0).isEqualTo(0);
assertThat(list.size()).isNotEqualTo(0);
}
}
@@ -61,6 +62,7 @@ class Test {
void simpleTest() {
List list = List.of();
assertThat(list.size()).isZero();
+ assertThat(0).isZero();
assertThat(list.size()).isNotZero();
}
}
From 9981a30374c57e2695de5e520d05addd39c28092 Mon Sep 17 00:00:00 2001
From: Tim te Beek
Date: Wed, 7 Feb 2024 01:23:01 +0100
Subject: [PATCH 5/9] Apply suggestions from code review
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
---
.../java/testing/assertj/SimplifyAssertExpectationsTest.java | 2 ++
1 file changed, 2 insertions(+)
diff --git a/src/test/java/org/openrewrite/java/testing/assertj/SimplifyAssertExpectationsTest.java b/src/test/java/org/openrewrite/java/testing/assertj/SimplifyAssertExpectationsTest.java
index 37a54cd22..87ea522da 100644
--- a/src/test/java/org/openrewrite/java/testing/assertj/SimplifyAssertExpectationsTest.java
+++ b/src/test/java/org/openrewrite/java/testing/assertj/SimplifyAssertExpectationsTest.java
@@ -16,6 +16,7 @@
package org.openrewrite.java.testing.assertj;
import org.junit.jupiter.api.Test;
+import org.openrewrite.DocumentExample;
import org.openrewrite.java.JavaParser;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;
@@ -30,6 +31,7 @@ public void defaults(RecipeSpec spec) {
.recipe(new SimplifyAssertExpectationsRecipes());
}
+ @DocumentExample
@Test
void simplifyIsZero() {
rewriteRun(
From 1c8ad7f0fb2af543fde6897c397a8fdaf2cdb1c6 Mon Sep 17 00:00:00 2001
From: timo <129042237+timo-abele@users.noreply.github.com>
Date: Wed, 7 Feb 2024 09:11:47 +0100
Subject: [PATCH 6/9] use primitives (Integer -> int) for two of three recipes
---
.../java/testing/assertj/SimplifyAssertExpectations.java | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/src/main/java/org/openrewrite/java/testing/assertj/SimplifyAssertExpectations.java b/src/main/java/org/openrewrite/java/testing/assertj/SimplifyAssertExpectations.java
index c50920b47..807a6aa13 100644
--- a/src/main/java/org/openrewrite/java/testing/assertj/SimplifyAssertExpectations.java
+++ b/src/main/java/org/openrewrite/java/testing/assertj/SimplifyAssertExpectations.java
@@ -34,12 +34,12 @@ public class SimplifyAssertExpectations {
public class SimplifyToIsZero {
@BeforeTemplate
- void before(Integer i) {
+ void before(int i) {
assertThat(i).isEqualTo(0);
}
@AfterTemplate
- void after(Integer i) {
+ void after(int i) {
assertThat(i).isZero();
}
}
@@ -68,12 +68,12 @@ void after(Object o) {
public class SimplifyGreaterEqualComparison {
@BeforeTemplate
- void before(Integer i, Integer j) {
+ void before(int i, int j) {
assertThat(i >= j).isTrue();
}
@AfterTemplate
- void after(Integer i, Integer j) {
+ void after(int i, int j) {
assertThat(i).isGreaterThanOrEqualTo(j);
}
}
From bda13fecaa421e4a7235ce1bc104f88218811f90 Mon Sep 17 00:00:00 2001
From: Tim te Beek
Date: Wed, 7 Feb 2024 10:44:00 +0100
Subject: [PATCH 7/9] Conform to project standards for versions
---
build.gradle.kts | 13 +++----------
1 file changed, 3 insertions(+), 10 deletions(-)
diff --git a/build.gradle.kts b/build.gradle.kts
index 8526311e8..6a68bef22 100644
--- a/build.gradle.kts
+++ b/build.gradle.kts
@@ -39,6 +39,7 @@ dependencies {
compileOnly("org.projectlombok:lombok:latest.release")
annotationProcessor("org.projectlombok:lombok:latest.release")
+ implementation("org.assertj:assertj-core:latest.release")
implementation("org.testcontainers:testcontainers:latest.release")
testImplementation("org.openrewrite:rewrite-java-17")
@@ -51,21 +52,13 @@ dependencies {
testRuntimeOnly("org.testcontainers:nginx:latest.release")
// testImplementation("org.hamcrest:hamcrest:latest.release")
-// testImplementation("org.assertj:assertj-core:latest.release")
-
- // The bom version can also be set to a specific version
- // https://github.com/openrewrite/rewrite-recipe-bom/releases
- implementation(platform("org.openrewrite.recipe:rewrite-recipe-bom:latest.release"))
// Refaster style recipes need the rewrite-templating annotation processor and dependency for generated recipes
// https://github.com/openrewrite/rewrite-templating/releases
- annotationProcessor("org.openrewrite:rewrite-templating:latest.release")
- implementation("org.openrewrite:rewrite-templating")
+ annotationProcessor("org.openrewrite:rewrite-templating:$rewriteVersion")
+ implementation("org.openrewrite:rewrite-templating:$rewriteVersion")
// The `@BeforeTemplate` and `@AfterTemplate` annotations are needed for refaster style recipes
compileOnly("com.google.errorprone:error_prone_core:2.19.1") {
exclude("com.google.auto.service", "auto-service-annotations")
}
-
- implementation("org.assertj:assertj-core:3.+")
-
}
From d2a28e27b3ad3dc2023d729b864862c481b67028 Mon Sep 17 00:00:00 2001
From: Tim te Beek
Date: Fri, 14 Jun 2024 14:14:18 +0200
Subject: [PATCH 8/9] Update
src/test/java/org/openrewrite/java/testing/assertj/SimplifyAssertExpectationsTest.java
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
---
.../java/testing/assertj/SimplifyAssertExpectationsTest.java | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/src/test/java/org/openrewrite/java/testing/assertj/SimplifyAssertExpectationsTest.java b/src/test/java/org/openrewrite/java/testing/assertj/SimplifyAssertExpectationsTest.java
index 87ea522da..2c67879fe 100644
--- a/src/test/java/org/openrewrite/java/testing/assertj/SimplifyAssertExpectationsTest.java
+++ b/src/test/java/org/openrewrite/java/testing/assertj/SimplifyAssertExpectationsTest.java
@@ -143,4 +143,4 @@ void simpleTest() {
);
}
-}
\ No newline at end of file
+}
From f36a3a2105c28cabb83c9b0fb1dda99a4a6c2226 Mon Sep 17 00:00:00 2001
From: Tim te Beek
Date: Fri, 14 Jun 2024 14:30:19 +0200
Subject: [PATCH 9/9] Adopt `@Primitive Integer i` in static nested classes
---
.../testing/assertj/SimplifyAssertExpectations.java | 13 ++++++-------
1 file changed, 6 insertions(+), 7 deletions(-)
diff --git a/src/main/java/org/openrewrite/java/testing/assertj/SimplifyAssertExpectations.java b/src/main/java/org/openrewrite/java/testing/assertj/SimplifyAssertExpectations.java
index 807a6aa13..12ce7e9c1 100644
--- a/src/main/java/org/openrewrite/java/testing/assertj/SimplifyAssertExpectations.java
+++ b/src/main/java/org/openrewrite/java/testing/assertj/SimplifyAssertExpectations.java
@@ -17,6 +17,7 @@
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
+import org.openrewrite.java.template.Primitive;
import org.openrewrite.java.template.RecipeDescriptor;
import static org.assertj.core.api.Assertions.assertThat;
@@ -31,15 +32,14 @@ public class SimplifyAssertExpectations {
name = "Simplify `assertThat(int).isEqualTo(0)`",
description = "Simplify `assertThat(int).isEqualTo(0)` to `assertThat(int).isZero()`."
)
- public class SimplifyToIsZero {
-
+ public static class SimplifyToIsZero {
@BeforeTemplate
- void before(int i) {
+ void before(@Primitive Integer i) {
assertThat(i).isEqualTo(0);
}
@AfterTemplate
- void after(int i) {
+ void after(@Primitive int i) {
assertThat(i).isZero();
}
}
@@ -48,8 +48,7 @@ void after(int i) {
name = "Simplify `assertThat(o).isEqualTo(null)`",
description = "Simplify `assertThat(o).isEqualTo(null)` to `assertThat(o).isNull()`."
)
- public class SimplifyToIsNull {
-
+ public static class SimplifyToIsNull {
@BeforeTemplate
void before(Object o) {
assertThat(o).isEqualTo(null);
@@ -65,7 +64,7 @@ void after(Object o) {
name = "Simplify `assertThat(i >= j).isTrue()`",
description = "Simplify `assertThat(i >= j).isTrue()` to `assertThat(i).isGreaterThanOrEqualTo(j)`."
)
- public class SimplifyGreaterEqualComparison {
+ public static class SimplifyGreaterEqualComparison {
@BeforeTemplate
void before(int i, int j) {