From fbff3951b470411fbbc479f95a09bd8a6546422b Mon Sep 17 00:00:00 2001 From: Dan Fox Date: Tue, 24 Sep 2019 23:26:58 +0100 Subject: [PATCH 1/5] One more assertj refaster --- .../AssertjCollectionHasSameSizeAs.java | 38 ++++++++++++++++ .../AssertjCollectionHasSameSizeAsTest.java | 45 +++++++++++++++++++ .../baseline/refaster/RefasterTestHelper.java | 4 +- 3 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjCollectionHasSameSizeAs.java create mode 100644 baseline-refaster-rules/src/test/java/com/palantir/baseline/refaster/AssertjCollectionHasSameSizeAsTest.java diff --git a/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjCollectionHasSameSizeAs.java b/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjCollectionHasSameSizeAs.java new file mode 100644 index 000000000..68172dc0a --- /dev/null +++ b/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjCollectionHasSameSizeAs.java @@ -0,0 +1,38 @@ +/* + * (c) Copyright 2019 Palantir Technologies Inc. All rights reserved. + * + * 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 + * + * http://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 com.palantir.baseline.refaster; + +import com.google.errorprone.refaster.ImportPolicy; +import com.google.errorprone.refaster.annotation.AfterTemplate; +import com.google.errorprone.refaster.annotation.BeforeTemplate; +import com.google.errorprone.refaster.annotation.UseImportPolicy; +import java.util.List; +import org.assertj.core.api.IterableAssert; + +public final class AssertjCollectionHasSameSizeAs { + + @BeforeTemplate + IterableAssert before(IterableAssert assertInProgress, List expected) { + return assertInProgress.hasSize(expected.size()); + } + + @AfterTemplate + @UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS) + IterableAssert after(IterableAssert assertInProgress, List expected) { + return assertInProgress.hasSameSizeAs(expected); + } +} diff --git a/baseline-refaster-rules/src/test/java/com/palantir/baseline/refaster/AssertjCollectionHasSameSizeAsTest.java b/baseline-refaster-rules/src/test/java/com/palantir/baseline/refaster/AssertjCollectionHasSameSizeAsTest.java new file mode 100644 index 000000000..6a857cf91 --- /dev/null +++ b/baseline-refaster-rules/src/test/java/com/palantir/baseline/refaster/AssertjCollectionHasSameSizeAsTest.java @@ -0,0 +1,45 @@ +/* + * (c) Copyright 2019 Palantir Technologies Inc. All rights reserved. + * + * 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 + * + * http://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 com.palantir.baseline.refaster; + +import org.junit.Test; + +public class AssertjCollectionHasSameSizeAsTest { + + @Test + public void test() { + RefasterTestHelper + .forRefactoring(AssertjCollectionHasSameSizeAs.class) + .withInputLines( + "Test", + "import static org.assertj.core.api.Assertions.assertThat;", + "import java.util.List;", + "public class Test {", + " void f(List a, List b) {", + " assertThat(a).hasSize(b.size());", + " }", + "}") + .hasOutputLines( + "import static org.assertj.core.api.Assertions.assertThat;", + "import java.util.List;", + "public class Test {", + " void f(List a, List b) {", + " assertThat(a).hasSameSizeAs(b);", + " }", + "}"); + } +} diff --git a/baseline-refaster-testing/src/main/java/com/palantir/baseline/refaster/RefasterTestHelper.java b/baseline-refaster-testing/src/main/java/com/palantir/baseline/refaster/RefasterTestHelper.java index f5083ebb0..72ad74222 100644 --- a/baseline-refaster-testing/src/main/java/com/palantir/baseline/refaster/RefasterTestHelper.java +++ b/baseline-refaster-testing/src/main/java/com/palantir/baseline/refaster/RefasterTestHelper.java @@ -87,7 +87,9 @@ public void hasOutputLines(String... lines) { .orElseThrow(() -> new IllegalArgumentException("Failed to compile input lines")); DescriptionBasedDiff diff = DescriptionBasedDiff.create(tree, ImportOrganizer.STATIC_FIRST_ORGANIZER); - transformers.forEach(transformer -> transformer.apply(new TreePath(tree), result.context(), diff)); + for (CodeTransformer transformer : transformers) { + transformer.apply(new TreePath(tree), result.context(), diff); + } SourceFile inputSourceFile = sourceFile(input); diff.applyDifferences(inputSourceFile); From 5a8951532457bad6194bf452d9465c56b833c496 Mon Sep 17 00:00:00 2001 From: Carter Kozak Date: Fri, 27 Sep 2019 10:05:04 -0400 Subject: [PATCH 2/5] revert RefasterTestHelper --- .../com/palantir/baseline/refaster/RefasterTestHelper.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/baseline-refaster-testing/src/main/java/com/palantir/baseline/refaster/RefasterTestHelper.java b/baseline-refaster-testing/src/main/java/com/palantir/baseline/refaster/RefasterTestHelper.java index 72ad74222..f5083ebb0 100644 --- a/baseline-refaster-testing/src/main/java/com/palantir/baseline/refaster/RefasterTestHelper.java +++ b/baseline-refaster-testing/src/main/java/com/palantir/baseline/refaster/RefasterTestHelper.java @@ -87,9 +87,7 @@ public void hasOutputLines(String... lines) { .orElseThrow(() -> new IllegalArgumentException("Failed to compile input lines")); DescriptionBasedDiff diff = DescriptionBasedDiff.create(tree, ImportOrganizer.STATIC_FIRST_ORGANIZER); - for (CodeTransformer transformer : transformers) { - transformer.apply(new TreePath(tree), result.context(), diff); - } + transformers.forEach(transformer -> transformer.apply(new TreePath(tree), result.context(), diff)); SourceFile inputSourceFile = sourceFile(input); diff.applyDifferences(inputSourceFile); From a9ce5744cd9b1b37395b05345247c2d10d59a7c7 Mon Sep 17 00:00:00 2001 From: Carter Kozak Date: Fri, 27 Sep 2019 10:26:28 -0400 Subject: [PATCH 3/5] Fix refaster rule --- .../AssertjCollectionHasSameSizeAs.java | 12 ++++++++--- .../AssertjCollectionHasSameSizeAsTest.java | 20 +++++++++++++++---- 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjCollectionHasSameSizeAs.java b/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjCollectionHasSameSizeAs.java index 68172dc0a..55b8c2947 100644 --- a/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjCollectionHasSameSizeAs.java +++ b/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjCollectionHasSameSizeAs.java @@ -20,19 +20,25 @@ import com.google.errorprone.refaster.annotation.AfterTemplate; import com.google.errorprone.refaster.annotation.BeforeTemplate; import com.google.errorprone.refaster.annotation.UseImportPolicy; -import java.util.List; +import java.util.Collection; import org.assertj.core.api.IterableAssert; +import org.assertj.core.api.ListAssert; public final class AssertjCollectionHasSameSizeAs { @BeforeTemplate - IterableAssert before(IterableAssert assertInProgress, List expected) { + IterableAssert before(IterableAssert assertInProgress, Collection expected) { + return assertInProgress.hasSize(expected.size()); + } + + @BeforeTemplate + ListAssert before(ListAssert assertInProgress, Collection expected) { return assertInProgress.hasSize(expected.size()); } @AfterTemplate @UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS) - IterableAssert after(IterableAssert assertInProgress, List expected) { + IterableAssert after(IterableAssert assertInProgress, Collection expected) { return assertInProgress.hasSameSizeAs(expected); } } diff --git a/baseline-refaster-rules/src/test/java/com/palantir/baseline/refaster/AssertjCollectionHasSameSizeAsTest.java b/baseline-refaster-rules/src/test/java/com/palantir/baseline/refaster/AssertjCollectionHasSameSizeAsTest.java index 6a857cf91..70d68af92 100644 --- a/baseline-refaster-rules/src/test/java/com/palantir/baseline/refaster/AssertjCollectionHasSameSizeAsTest.java +++ b/baseline-refaster-rules/src/test/java/com/palantir/baseline/refaster/AssertjCollectionHasSameSizeAsTest.java @@ -28,17 +28,29 @@ public void test() { "Test", "import static org.assertj.core.api.Assertions.assertThat;", "import java.util.List;", + "import java.util.Collection;", "public class Test {", - " void f(List a, List b) {", - " assertThat(a).hasSize(b.size());", + " void f(List a, Collection b, Iterable c, List target) {", + " assertThat(a).hasSize(target.size());", + " assertThat(b).hasSize(target.size());", + " assertThat(c).hasSize(target.size());", + " assertThat(a).describedAs(\"desc\").hasSize(target.size());", + " assertThat(b).describedAs(\"desc\").hasSize(target.size());", + " assertThat(c).describedAs(\"desc\").hasSize(target.size());", " }", "}") .hasOutputLines( "import static org.assertj.core.api.Assertions.assertThat;", "import java.util.List;", + "import java.util.Collection;", "public class Test {", - " void f(List a, List b) {", - " assertThat(a).hasSameSizeAs(b);", + " void f(List a, Collection b, Iterable c, List target) {", + " assertThat(a).hasSameSizeAs(target);", + " assertThat(b).hasSameSizeAs(target);", + " assertThat(c).hasSameSizeAs(target);", + " assertThat(a).describedAs(\"desc\").hasSameSizeAs(target);", + " assertThat(b).describedAs(\"desc\").hasSameSizeAs(target);", + " assertThat(c).describedAs(\"desc\").hasSameSizeAs(target);", " }", "}"); } From ee7c4ecb4b989e8b2e49ab750595141c9da28327 Mon Sep 17 00:00:00 2001 From: Carter Kozak Date: Fri, 27 Sep 2019 14:26:28 +0000 Subject: [PATCH 4/5] Add generated changelog entries --- changelog/@unreleased/pr-900.v2.yml | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 changelog/@unreleased/pr-900.v2.yml diff --git a/changelog/@unreleased/pr-900.v2.yml b/changelog/@unreleased/pr-900.v2.yml new file mode 100644 index 000000000..72bc1d2c1 --- /dev/null +++ b/changelog/@unreleased/pr-900.v2.yml @@ -0,0 +1,5 @@ +type: improvement +improvement: + description: refaster replacement for assertj hasSize(foo.size) -> hasSameSizeAs + links: + - https://github.com/palantir/gradle-baseline/pull/900 From b3294d40fc96b51c3245b3ecfb0ee2489948ae57 Mon Sep 17 00:00:00 2001 From: Carter Kozak Date: Fri, 27 Sep 2019 10:35:25 -0400 Subject: [PATCH 5/5] style + java11 --- .../baseline/refaster/AssertjCollectionHasSameSizeAs.java | 2 +- .../refaster/AssertjCollectionHasSameSizeAsTest.java | 5 +++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjCollectionHasSameSizeAs.java b/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjCollectionHasSameSizeAs.java index 55b8c2947..ede04f835 100644 --- a/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjCollectionHasSameSizeAs.java +++ b/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjCollectionHasSameSizeAs.java @@ -24,7 +24,7 @@ import org.assertj.core.api.IterableAssert; import org.assertj.core.api.ListAssert; -public final class AssertjCollectionHasSameSizeAs { +public final class AssertjCollectionHasSameSizeAs { @BeforeTemplate IterableAssert before(IterableAssert assertInProgress, Collection expected) { diff --git a/baseline-refaster-rules/src/test/java/com/palantir/baseline/refaster/AssertjCollectionHasSameSizeAsTest.java b/baseline-refaster-rules/src/test/java/com/palantir/baseline/refaster/AssertjCollectionHasSameSizeAsTest.java index 70d68af92..c91b8164c 100644 --- a/baseline-refaster-rules/src/test/java/com/palantir/baseline/refaster/AssertjCollectionHasSameSizeAsTest.java +++ b/baseline-refaster-rules/src/test/java/com/palantir/baseline/refaster/AssertjCollectionHasSameSizeAsTest.java @@ -16,12 +16,17 @@ package com.palantir.baseline.refaster; +import static org.assertj.core.api.Assumptions.assumeThat; + import org.junit.Test; public class AssertjCollectionHasSameSizeAsTest { @Test public void test() { + assumeThat(System.getProperty("java.specification.version")) + .describedAs("Refaster does not currently support fluent refactors on java 11") + .isEqualTo("1.8"); RefasterTestHelper .forRefactoring(AssertjCollectionHasSameSizeAs.class) .withInputLines(