Skip to content

Commit 2e6d485

Browse files
OpenRewrite recipe best practices
Co-authored-by: Moderne <team@moderne.io>
1 parent 95ff73a commit 2e6d485

10 files changed

Lines changed: 667 additions & 67 deletions

src/main/resources/META-INF/rewrite/examples.yml

Lines changed: 637 additions & 6 deletions
Large diffs are not rendered by default.

src/test/java/org/openrewrite/java/testing/assertj/AssertJBestPracticesTest.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -493,11 +493,8 @@ void test(Set<Locale> localeSet) {
493493
);
494494
}
495495

496-
/**
497-
* Chained AssertJ assertions should be simplified to the corresponding dedicated assertion, as
498-
* per <a
499-
* href="https://next.sonarqube.com/sonarqube/coding_rules?open=java%3AS5838&rule_key=java%3AS5838">java:S5838</a>
500-
*/
496+
/// Chained AssertJ assertions should be simplified to the corresponding dedicated assertion, as
497+
/// per <a href="https://next.sonarqube.com/sonarqube/coding_rules?open=java%3AS5838&rule_key=java%3AS5838">java:S5838</a>
501498
@Nested
502499
class SonarDedicatedAssertions {
503500
private static Stream<Arguments> replacements() {

src/test/java/org/openrewrite/java/testing/assertj/JUnitAssertThrowsToAssertExceptionTypeTest.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -388,10 +388,8 @@ NullPointerException exception() {
388388
);
389389
}
390390

391-
/**
392-
* A degenerate case showing we don't perform the conversion when the <code>assertThrows</code> appears
393-
* immediately inside a J.Lambda.
394-
*/
391+
/// A degenerate case showing we don't perform the conversion when the `assertThrows` appears
392+
/// immediately inside a J.Lambda.
395393
@Test
396394
void assertThrowsConsumerUsage() {
397395
//language=java
@@ -414,10 +412,8 @@ public void throwsExceptionWithSpecificType() {
414412
);
415413
}
416414

417-
/**
418-
* A degenerate case showing we don't perform the conversion when the <code>assertThrows</code> appears
419-
* immediately inside a J.Lambda.
420-
*/
415+
/// A degenerate case showing we don't perform the conversion when the `assertThrows` appears
416+
/// immediately inside a J.Lambda.
421417
@Test
422418
void assertThrowsSupplierUsage() {
423419
//language=java

src/test/java/org/openrewrite/java/testing/jmockit/JMockitDelegateToMockitoTest.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,9 @@
2525
import static org.openrewrite.java.Assertions.java;
2626
import static org.openrewrite.java.testing.jmockit.JMockitTestUtils.setDefaultParserSettings;
2727

28-
/**
29-
* At the moment, JMockit Delegates are not migrated to mockito. What I'm seeing is that they are being trashed
30-
* with the template being printed out. These tests were written to try to replicate this issue, however I was unable to.
31-
* They may help anyone who wants to add Delegate migration.
32-
*/
28+
/// At the moment, JMockit Delegates are not migrated to mockito. What I'm seeing is that they are being trashed
29+
/// with the template being printed out. These tests were written to try to replicate this issue, however I was unable to.
30+
/// They may help anyone who wants to add Delegate migration.
3331
@Disabled
3432
@Issue("https://github.com/openrewrite/rewrite-testing-frameworks/issues/522")
3533
@SuppressWarnings("ResultOfMethodCallIgnored")

src/test/java/org/openrewrite/java/testing/jmockit/JMockitFullVerificationsToMockitoTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@
2323
import static org.openrewrite.java.Assertions.java;
2424
import static org.openrewrite.java.testing.jmockit.JMockitTestUtils.setDefaultParserSettings;
2525

26-
/**
27-
* Not doing comprehensive testing as it is covered in JMockitVerificationsToMockitoTest and shares same code path
28-
*/
26+
/// Not doing comprehensive testing as it is covered in JMockitVerificationsToMockitoTest and shares same code path
2927
@SuppressWarnings("SpellCheckingInspection")
3028
class JMockitFullVerificationsToMockitoTest implements RewriteTest {
3129

src/test/java/org/openrewrite/java/testing/jmockit/JMockitVerificationsInOrderToMockitoTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@
2424
import static org.openrewrite.java.Assertions.java;
2525
import static org.openrewrite.java.testing.jmockit.JMockitTestUtils.setDefaultParserSettings;
2626

27-
/**
28-
* Not doing full testing of VerificationsInOrder as it is covered in JMockitVerificationsToMockitoTest
29-
*/
27+
/// Not doing full testing of VerificationsInOrder as it is covered in JMockitVerificationsToMockitoTest
3028
class JMockitVerificationsInOrderToMockitoTest implements RewriteTest {
3129

3230
@Override

src/test/java/org/openrewrite/java/testing/junit5/JUnit5MigrationTest.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -940,12 +940,10 @@ void test() {
940940
);
941941
}
942942

943-
/**
944-
* The bug this test is reproducing is likely not specific to the JUnit5 upgrade, it's just where it was first
945-
* encountered and isolated. It would probably be reproducible using other recipes that reorder method parameters,
946-
* provided that a Javadoc link refers to an affected method, and the link has a newline in between the parameters.
947-
* If that proves to be true, this test should be generalized and moved to the tests of {@code rewrite-java}.
948-
*/
943+
/// The bug this test is reproducing is likely not specific to the JUnit5 upgrade, it's just where it was first
944+
/// encountered and isolated. It would probably be reproducible using other recipes that reorder method parameters,
945+
/// provided that a Javadoc link refers to an affected method, and the link has a newline in between the parameters.
946+
/// If that proves to be true, this test should be generalized and moved to the tests of `rewrite-java`.
949947
@Issue("https://github.com/openrewrite/rewrite/issues/6001")
950948
@Test
951949
void correctlyHandleALineBreakInAJavadocLinkThatReferencesAMethodWhoseParametersGetReordered() {

src/test/java/org/openrewrite/java/testing/mockito/JunitMockitoUpgradeIntegrationTest.java

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,7 @@
2525

2626
import static org.openrewrite.java.Assertions.java;
2727

28-
/**
29-
* Validates the recipes related to upgrading from Mockito 1 to Mockito 3
30-
*/
28+
/// Validates the recipes related to upgrading from Mockito 1 to Mockito 3
3129
@SuppressWarnings({"NotNullFieldNotInitialized", "NewClassNamingConvention"})
3230
class JunitMockitoUpgradeIntegrationTest implements RewriteTest {
3331

@@ -42,9 +40,7 @@ public void defaults(RecipeSpec spec) {
4240
.activateRecipes("org.openrewrite.java.testing.junit5.JUnit4to5Migration"));
4341
}
4442

45-
/**
46-
* Replace org.mockito.MockitoAnnotations.Mock with org.mockito.Mock
47-
*/
43+
/// Replace org.mockito.MockitoAnnotations.Mock with org.mockito.Mock
4844
@DocumentExample
4945
@Test
5046
void replaceMockAnnotation() {
@@ -129,10 +125,8 @@ void tearDown() throws Exception {
129125
);
130126
}
131127

132-
/**
133-
* Mockito 1 used Matchers.anyVararg() to match the arguments to a variadic function.
134-
* Mockito 2+ uses Matchers.any() to match anything including the arguments to a variadic function.
135-
*/
128+
/// Mockito 1 used Matchers.anyVararg() to match the arguments to a variadic function.
129+
/// Mockito 2+ uses Matchers.any() to match anything including the arguments to a variadic function.
136130
@Test
137131
void replacesAnyVararg() {
138132
//language=java
@@ -176,11 +170,9 @@ public void usesVarargMatcher() {
176170
);
177171
}
178172

179-
/**
180-
* Mockito 1 has InvocationOnMock.getArgumentAt(int, Class)
181-
* Mockito 3 has InvocationOnMock.getArgument(int, Class)
182-
* swap 'em
183-
*/
173+
/// Mockito 1 has InvocationOnMock.getArgumentAt(int, Class)
174+
/// Mockito 3 has InvocationOnMock.getArgument(int, Class)
175+
/// swap 'em
184176
@Test
185177
void replacesGetArgumentAt() {
186178
//language=java

src/test/java/org/openrewrite/java/testing/testcontainers/Testcontainers2LocalStackTest.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,9 @@ class A {
187187
);
188188
}
189189

190-
/**
191-
* End-to-end reproduction of the customer-reported Kotlin files (issue #2437) run through the full
192-
* {@code Testcontainers2Migration}, asserting the LocalStack member references are migrated alongside
193-
* the type rename so the result compiles against Testcontainers 2.x.
194-
*/
190+
/// End-to-end reproduction of the customer-reported Kotlin files (issue #2437) run through the full
191+
/// `Testcontainers2Migration`, asserting the LocalStack member references are migrated alongside
192+
/// the type rename so the result compiles against Testcontainers 2.x.
195193
@Nested
196194
class FullMigration implements RewriteTest {
197195

src/test/java/org/openrewrite/java/testing/testng/TestNgToAssertJTest.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ public void defaults(RecipeSpec spec) {
3131
.recipeFromResources("org.openrewrite.java.testing.assertj.Assertj");
3232
}
3333

34-
/**
35-
* Verify that we can throw new Exception from the JavaTemplate in the generated recipe.
36-
*/
34+
/// Verify that we can throw new Exception from the JavaTemplate in the generated recipe.
3735
@DocumentExample
3836
@Test
3937
void failWithMessage() {
@@ -66,9 +64,7 @@ void test() {
6664
);
6765
}
6866

69-
/**
70-
* Verify some assertions as implemented through Refaster rules converted to Recipes. No need to test all variants.
71-
*/
67+
/// Verify some assertions as implemented through Refaster rules converted to Recipes. No need to test all variants.
7268
@Test
7369
void assertTrueFalse() {
7470
rewriteRun(
@@ -141,11 +137,9 @@ void bbb(Object obj) {
141137
);
142138
}
143139

144-
/**
145-
* Qualified `Assert.assertX(...)` calls should become static-imported `assertThat(...)`, rather than
146-
* mirroring the source qualification as `Assertions.assertThat(...)`; see
147-
* <a href="https://github.com/openrewrite/rewrite-testing-frameworks/issues/1029">issue 1029</a>.
148-
*/
140+
/// Qualified `Assert.assertX(...)` calls should become static-imported `assertThat(...)`, rather than
141+
/// mirroring the source qualification as `Assertions.assertThat(...)`; see
142+
/// <a href="https://github.com/openrewrite/rewrite-testing-frameworks/issues/1029">issue 1029</a>.
149143
@Test
150144
void qualifiedAssertBecomesStaticImport() {
151145
rewriteRun(

0 commit comments

Comments
 (0)