From 51cf86b5d2f8b022f07023e6cd31dad153a8c9e5 Mon Sep 17 00:00:00 2001 From: Philzen Date: Fri, 14 Jun 2024 04:07:12 +0200 Subject: [PATCH] Implement assertEqualsNoOrder migration --- .../testngtojupiter/MigrateAssertions.java | 124 ++++++++++++ .../MigrateAssertionsTests.java | 191 ++++++++++++++++++ .../oss/research/ApiComparisonTest.java | 59 ++++++ 3 files changed, 374 insertions(+) diff --git a/src/main/java/io/github/mboegers/openrewrite/testngtojupiter/MigrateAssertions.java b/src/main/java/io/github/mboegers/openrewrite/testngtojupiter/MigrateAssertions.java index 4a3215d0..6d9f483e 100644 --- a/src/main/java/io/github/mboegers/openrewrite/testngtojupiter/MigrateAssertions.java +++ b/src/main/java/io/github/mboegers/openrewrite/testngtojupiter/MigrateAssertions.java @@ -16,6 +16,7 @@ import org.testng.Assert; import java.util.Arrays; +import java.util.Collection; import java.util.Iterator; import java.util.Spliterators; import java.util.stream.StreamSupport; @@ -192,6 +193,129 @@ void after(double actual, double expected, double delta, String msg) { } } + @RecipeDescriptor( + name = "Migrate `Assert#assertEqualsNoOrder(Collection, Collection)`", + description = "Migrate `org.testng.Assert#assertEqualsNoOrder(Collection, Collection)` " + + "to `org.junit.jupiter.api.Assertions#assertArrayEquals(Object[], Object[])`, " + + "sorting the collection before applying the assertion." + ) + public static class MigrateAssertEqualsNoOrderCollection { + + @BeforeTemplate void before(Collection actual, Collection expected) { + Assert.assertEqualsNoOrder(actual, expected); + } + + @AfterTemplate void after(Collection actual, Collection expected) { + Assertions.assertArrayEquals( + expected.stream().sorted().toArray(), + actual.stream().sorted().toArray() + ); + } + } + + @RecipeDescriptor( + name = "Migrate `Assert#assertEqualsNoOrder(Collection, Collection, String)`", + description = "Migrate `org.testng.Assert#assertEqualsNoOrder(Collection, Collection, String)` " + + "to `org.junit.jupiter.api.Assertions#assertArrayEquals(Object[], Object[], String)`, " + + "sorting the collection before applying the assertion." + ) + public static class MigrateAssertEqualsNoOrderCollectionWithMessage { + + @BeforeTemplate void before(Collection actual, Collection expected, String message) { + Assert.assertEqualsNoOrder(actual, expected, message); + } + + @AfterTemplate void after(Collection actual, Collection expected, String message) { + Assertions.assertArrayEquals( + expected.stream().sorted().toArray(), + actual.stream().sorted().toArray(), + message + ); + } + } + + @RecipeDescriptor( + name = "Migrate `Assert#assertEqualsNoOrder(Object[], Object[])`", + description = "Migrate `org.testng.Assert#assertEqualsNoOrder(Object[], Object[])` " + + "to `org.junit.jupiter.api.Assertions#assertArrayEquals(Object[], Object[])`, " + + "sorting the arrays before applying the assertion." + ) + public static class MigrateAssertEqualsNoOrderArray { + + @BeforeTemplate void before(Object[] actual, Object[] expected) { + Assert.assertEqualsNoOrder(actual, expected); + } + + @AfterTemplate void after(Object[] actual, Object[] expected) { + Assertions.assertArrayEquals( + Arrays.stream(expected).sorted().toArray(), + Arrays.stream(actual).sorted().toArray() + ); + } + } + + @RecipeDescriptor( + name = "Migrate `Assert#assertEqualsNoOrder(Object[], Object[], String)`", + description = "Migrate `org.testng.Assert#assertEqualsNoOrder(Object[], Object[], String)` " + + "to `org.junit.jupiter.api.Assertions#assertArrayEquals(Object[], Object[], String)`, " + + "sorting the collection before applying the assertion." + ) + public static class MigrateAssertEqualsNoOrderArrayWithMessage { + + @BeforeTemplate void before(Object[] actual, Object[] expected, String message) { + Assert.assertEqualsNoOrder(actual, expected, message); + } + + @AfterTemplate void after(Object[] actual, Object[] expected, String message) { + Assertions.assertArrayEquals( + Arrays.stream(expected).sorted().toArray(), + Arrays.stream(actual).sorted().toArray(), + message + ); + } + } + + @RecipeDescriptor( + name = "Migrate `Assert#assertEqualsNoOrder(Iterator, Iterator)`", + description = "Migrate `org.testng.Assert#assertEqualsNoOrder(Iterator, Iterator)` " + + "to `org.junit.jupiter.api.Assertions#assertArrayEquals(Object[], Object[])`, " + + "sorting the arrays before applying the assertion." + ) + public static class MigrateAssertEqualsNoOrderIterator { + + @BeforeTemplate void before(Iterator actual, Iterator expected) { + Assert.assertEqualsNoOrder(actual, expected); + } + + @AfterTemplate void after(Iterator actual, Iterator expected) { + Assertions.assertArrayEquals( + StreamSupport.stream(Spliterators.spliteratorUnknownSize(expected, 0), false).sorted().toArray(), + StreamSupport.stream(Spliterators.spliteratorUnknownSize(actual, 0), false).sorted().toArray() + ); + } + } + + @RecipeDescriptor( + name = "Migrate `Assert#assertEqualsNoOrder(Iterator, Iterator, String)`", + description = "Migrate `org.testng.Assert#assertEqualsNoOrder(Iterator, Iterator, String)` " + + "to `org.junit.jupiter.api.Assertions#assertArrayEquals(Object[], Object[], String)`, " + + "sorting the arrays before applying the assertion." + ) + public static class MigrateAssertEqualsNoOrderIteratorWithMessage { + + @BeforeTemplate void before(Iterator actual, Iterator expected, String message) { + Assert.assertEqualsNoOrder(actual, expected, message); + } + + @AfterTemplate void after(Iterator actual, Iterator expected, String message) { + Assertions.assertArrayEquals( + StreamSupport.stream(Spliterators.spliteratorUnknownSize(expected, 0), false).sorted().toArray(), + StreamSupport.stream(Spliterators.spliteratorUnknownSize(actual, 0), false).sorted().toArray(), + message + ); + } + } + @RecipeDescriptor( name = "Replace `Assert#assertNotEquals(Object[], Object[])`", description = "Replace `org.testng.Assert#assertNotEquals(Object[], Object[])` with `org.junit.jupiter.api.Assertions#assertNotEquals(Arrays.toString(Object[], Arrays.toString(Object[]))`." diff --git a/src/test/java/io/github/mboegers/openrewrite/testngtojupiter/MigrateAssertionsTests.java b/src/test/java/io/github/mboegers/openrewrite/testngtojupiter/MigrateAssertionsTests.java index 3f34da00..209e9ec1 100644 --- a/src/test/java/io/github/mboegers/openrewrite/testngtojupiter/MigrateAssertionsTests.java +++ b/src/test/java/io/github/mboegers/openrewrite/testngtojupiter/MigrateAssertionsTests.java @@ -385,6 +385,197 @@ void testMethod() { } } + @Nested class MigrateAssertEqualsNoOrder { + + @SuppressWarnings("SimplifyStreamApiCallChains") + @Nested class WithErrorMessage { + + @Test void migratesCollectionToAssertArrayEquals() { + // language=java + rewriteRun(java( + """ + import java.util.Arrays; + import org.testng.Assert; + + class MyTest { + void testMethod() { + Assert.assertEqualsNoOrder(Arrays.asList("a", "b"), Arrays.asList("b", "a"), "Should contain the same elements"); + } + } + """, + """ + import org.junit.jupiter.api.Assertions; + + import java.util.Arrays; + + class MyTest { + void testMethod() { + Assertions.assertArrayEquals(Arrays.asList("b", "a").stream().sorted().toArray(), Arrays.asList("a", "b").stream().sorted().toArray(), "Should contain the same elements"); + } + } + """ + )); + } + + @Test void migratesArrayToAssertArrayEquals() { + // language=java + rewriteRun(java( + """ + import org.testng.Assert; + + class MyTest { + void testMethod() { + Assert.assertEqualsNoOrder(new String[] {"actual"}, new String[] {"expected"}, "Should contain the same elements"); + } + } + """, + """ + import org.junit.jupiter.api.Assertions; + + import java.util.Arrays; + + class MyTest { + void testMethod() { + Assertions.assertArrayEquals(Arrays.stream(new String[]{"expected"}).sorted().toArray(), Arrays.stream(new String[]{"actual"}).sorted().toArray(), "Should contain the same elements"); + } + } + """ + )); + } + + @Test void migratesIteratorToAssertArrayEquals() { + // language=java + rewriteRun(java( + """ + import java.util.Iterator; + import java.util.List; + import org.testng.Assert; + + class MyTest { + void testMethod() { + Iterator actual = List.of("a", "b").iterator(); + Iterator expected = List.of("b", "a").iterator(); + + Assert.assertEqualsNoOrder(actual, expected, "Should contain the same elements"); + } + } + """, + """ + import org.junit.jupiter.api.Assertions; + + import java.util.Iterator; + import java.util.List; + import java.util.Spliterators; + import java.util.stream.StreamSupport; + + class MyTest { + void testMethod() { + Iterator actual = List.of("a", "b").iterator(); + Iterator expected = List.of("b", "a").iterator(); + + Assertions.assertArrayEquals(StreamSupport.stream(Spliterators.spliteratorUnknownSize(expected, 0), false).sorted().toArray(), StreamSupport.stream(Spliterators.spliteratorUnknownSize(actual, 0), false).sorted().toArray(), "Should contain the same elements"); + } + } + """ + )); + } + } + + @SuppressWarnings("SimplifyStreamApiCallChains") + @Nested class WithoutErrorMessage { + + @Test void migratesCollectionToAssertArrayEquals() { + // language=java + rewriteRun(java( + """ + import java.util.Arrays; + import org.testng.Assert; + + class MyTest { + void testMethod() { + Assert.assertEqualsNoOrder(Arrays.asList("a", "b"), Arrays.asList("b", "a")); + } + } + """, + """ + import org.junit.jupiter.api.Assertions; + + import java.util.Arrays; + + class MyTest { + void testMethod() { + Assertions.assertArrayEquals(Arrays.asList("b", "a").stream().sorted().toArray(), Arrays.asList("a", "b").stream().sorted().toArray()); + } + } + """ + )); + } + + @Test void migratesArrayToAssertArrayEquals() { + // language=java + rewriteRun(java( + """ + import org.testng.Assert; + + class MyTest { + void testMethod() { + Assert.assertEqualsNoOrder(new String[] {"actual"}, new String[] {"expected"}); + } + } + """, + """ + import org.junit.jupiter.api.Assertions; + + import java.util.Arrays; + + class MyTest { + void testMethod() { + Assertions.assertArrayEquals(Arrays.stream(new String[]{"expected"}).sorted().toArray(), Arrays.stream(new String[]{"actual"}).sorted().toArray()); + } + } + """ + )); + } + + @Test void migratesIteratorToAssertArrayEquals() { + // language=java + rewriteRun(java( + """ + import java.util.Iterator; + import java.util.List; + import org.testng.Assert; + + class MyTest { + void testMethod() { + Iterator actual = List.of("a", "b").iterator(); + Iterator expected = List.of("b", "a").iterator(); + + Assert.assertEqualsNoOrder(actual, expected); + } + } + """, + """ + import org.junit.jupiter.api.Assertions; + + import java.util.Iterator; + import java.util.List; + import java.util.Spliterators; + import java.util.stream.StreamSupport; + + class MyTest { + void testMethod() { + Iterator actual = List.of("a", "b").iterator(); + Iterator expected = List.of("b", "a").iterator(); + + Assertions.assertArrayEquals(StreamSupport.stream(Spliterators.spliteratorUnknownSize(expected, 0), false).sorted().toArray(), StreamSupport.stream(Spliterators.spliteratorUnknownSize(actual, 0), false).sorted().toArray()); + } + } + """ + )); + } + } + } + @Nested class MigrateAssertNotEquals { @Nested class WithErrorMessage { diff --git a/src/test/java/org/philzen/oss/research/ApiComparisonTest.java b/src/test/java/org/philzen/oss/research/ApiComparisonTest.java index fa1461db..001c8c7a 100644 --- a/src/test/java/org/philzen/oss/research/ApiComparisonTest.java +++ b/src/test/java/org/philzen/oss/research/ApiComparisonTest.java @@ -11,6 +11,7 @@ import org.testng.Assert; import java.util.*; +import java.util.stream.Stream; import java.util.stream.StreamSupport; import static org.assertj.core.api.Assertions.assertThatExceptionOfType; @@ -184,6 +185,64 @@ class ApiComparisonTest { } } + @Tag("missing") + @Nested class assertEqualsNoOrder { // there is no equivalent in Jupiter + + @Test void collection() { + final Collection expected = ABC_list; + + thisWillPass(() -> Assert.assertEqualsNoOrder(CBA_list, expected)); + thisWillFail(() -> Assert.assertEqualsNoOrder(List.of("x", "y"), expected)); + + // possible migration (string only) + thisWillPass(() -> Assertions.assertLinesMatch(expected.stream().sorted(), CBA_list.stream().sorted())); + thisWillFail(() -> Assertions.assertLinesMatch(expected.stream().sorted(), Stream.of("x", "y").sorted())); + + // possible migration (any type) + thisWillPass(() -> Assertions.assertArrayEquals( + expected.stream().sorted().toArray(), CBA_list.stream().sorted().toArray() + )); + thisWillFail(() -> Assertions.assertArrayEquals( + expected.stream().sorted().toArray(), Stream.of("x", "y").sorted().toArray() + )); + } + + @Test void iterator() { + thisWillPass(() -> Assert.assertEqualsNoOrder(CBA_list.iterator(), ABC_list.iterator())); + thisWillFail(() -> Assert.assertEqualsNoOrder(List.of("x", "y").iterator(), ABC_list.iterator())); + + // possible migration + thisWillPass(() -> Assertions.assertArrayEquals( + StreamSupport.stream(Spliterators.spliteratorUnknownSize(CBA_list.iterator(), 0), false).sorted().toArray(), + StreamSupport.stream(Spliterators.spliteratorUnknownSize(ABC_list.iterator(), 0), false).sorted().toArray() + )); + thisWillFail(() -> Assertions.assertArrayEquals( + StreamSupport.stream(Spliterators.spliteratorUnknownSize(CBA_list.iterator(), 0), false).sorted().toArray(), + StreamSupport.stream(Spliterators.spliteratorUnknownSize(List.of("x", "y").iterator(), 0), false).sorted().toArray() + )); + } + + @Test void objectArray() { + final Object[] expected = new String[]{"a", "b", "c", "d"}; + + thisWillPass(() -> Assert.assertEqualsNoOrder(new String[]{"b", "a", "d", "c"}, expected)); + + thisWillFail(() -> Assert.assertEqualsNoOrder(new String[]{"b", "b", "a", "d", "c"}, expected)); + + // possible migration + thisWillPass(() -> Assertions.assertArrayEquals( + Arrays.stream(expected).sorted().toArray(), + Arrays.stream(new String[]{"b", "a", "d", "c"}).sorted().toArray() + )); + + // possible migration + thisWillFail(() -> Assertions.assertEquals( + Arrays.stream(expected).sorted(), + Arrays.stream(new String[]{"b", "b", "a", "d", "c"}).sorted() + )); + } + } + @Nested class assertNotEquals { @Tag("mismatch")