diff --git a/.baseline/checkstyle/custom-suppressions.xml b/.baseline/checkstyle/custom-suppressions.xml index 706a5d062..c73af085d 100644 --- a/.baseline/checkstyle/custom-suppressions.xml +++ b/.baseline/checkstyle/custom-suppressions.xml @@ -8,4 +8,7 @@ + + + diff --git a/baseline-refaster-rules/build.gradle b/baseline-refaster-rules/build.gradle index 988115122..7a512aff2 100644 --- a/baseline-refaster-rules/build.gradle +++ b/baseline-refaster-rules/build.gradle @@ -4,7 +4,8 @@ apply plugin: 'java-library' apply from: "${rootDir}/gradle/publish-jar.gradle" dependencies { - compile 'com.google.errorprone:error_prone_refaster' + implementation 'com.google.errorprone:error_prone_refaster' + implementation 'org.assertj:assertj-core' testCompile 'junit:junit' testCompile project(':baseline-refaster-testing') diff --git a/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjCollectionHasSizeExactly.java b/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjCollectionHasSizeExactly.java new file mode 100644 index 000000000..888c11cbe --- /dev/null +++ b/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjCollectionHasSizeExactly.java @@ -0,0 +1,41 @@ +/* + * (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 static org.assertj.core.api.Assertions.assertThat; + +import com.google.errorprone.refaster.annotation.AfterTemplate; +import com.google.errorprone.refaster.annotation.BeforeTemplate; +import java.util.Collection; + +public final class AssertjCollectionHasSizeExactly { + + @BeforeTemplate + void bad1(Collection things, int size) { + assertThat(things.size() == size).isTrue(); + } + + @BeforeTemplate + void bad2(Collection things, int size) { + assertThat(things.size()).isEqualTo(size); + } + + @AfterTemplate + void after(Collection things, int size) { + assertThat(things).hasSize(size); + } +} diff --git a/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjCollectionHasSizeGreaterThan.java b/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjCollectionHasSizeGreaterThan.java new file mode 100644 index 000000000..ada10d6c0 --- /dev/null +++ b/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjCollectionHasSizeGreaterThan.java @@ -0,0 +1,36 @@ +/* + * (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 static org.assertj.core.api.Assertions.assertThat; + +import com.google.errorprone.refaster.annotation.AfterTemplate; +import com.google.errorprone.refaster.annotation.BeforeTemplate; +import java.util.Collection; + +public final class AssertjCollectionHasSizeGreaterThan { + + @BeforeTemplate + void before(Collection things, int size) { + assertThat(things.size() > size).isTrue(); + } + + @AfterTemplate + void after(Collection things, int size) { + assertThat(things).hasSizeGreaterThan(size); + } +} diff --git a/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjCollectionIsEmpty.java b/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjCollectionIsEmpty.java new file mode 100644 index 000000000..e3595b4ad --- /dev/null +++ b/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjCollectionIsEmpty.java @@ -0,0 +1,74 @@ +/* + * (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 static org.assertj.core.api.Assertions.assertThat; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; +import com.google.errorprone.refaster.annotation.AfterTemplate; +import com.google.errorprone.refaster.annotation.BeforeTemplate; +import java.util.Collection; +import java.util.Collections; + +public final class AssertjCollectionIsEmpty { + + @BeforeTemplate + void bad1(Collection things) { + assertThat(things.size() == 0).isTrue(); + } + + @BeforeTemplate + void bad2(Collection things) { + assertThat(things.isEmpty()).isTrue(); + } + + @BeforeTemplate + void bad3(Collection things) { + assertThat(things.size() == 0).isTrue(); + } + + @BeforeTemplate + void bad4(Collection things) { + assertThat(things).isEqualTo(Collections.emptyList()); + } + + @BeforeTemplate + void bad5(Collection things) { + assertThat(things).isEqualTo(Collections.emptySet()); + } + + @BeforeTemplate + void bad6(Collection things) { + assertThat(things).isEqualTo(ImmutableList.of()); + } + + @BeforeTemplate + void bad7(Collection things) { + assertThat(things).isEqualTo(ImmutableSet.of()); + } + + @BeforeTemplate + void bad8(Collection things) { + assertThat(things).hasSize(0); + } + + @AfterTemplate + void after(Collection things) { + assertThat(things).isEmpty(); + } +} diff --git a/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjCollectionIsNotEmpty.java b/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjCollectionIsNotEmpty.java new file mode 100644 index 000000000..1d359f180 --- /dev/null +++ b/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjCollectionIsNotEmpty.java @@ -0,0 +1,79 @@ +/* + * (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 static org.assertj.core.api.Assertions.assertThat; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; +import com.google.errorprone.refaster.annotation.AfterTemplate; +import com.google.errorprone.refaster.annotation.BeforeTemplate; +import java.util.Collection; +import java.util.Collections; + +public final class AssertjCollectionIsNotEmpty { + + @BeforeTemplate + void bad1(Collection things) { + assertThat(things.size() != 0).isTrue(); + } + + @BeforeTemplate + void bad2(Collection things) { + assertThat(things.size() == 0).isFalse(); + } + + @BeforeTemplate + void bad3(Collection things) { + assertThat(things.size()).isNotEqualTo(0); + } + + @BeforeTemplate + void bad4(Collection things) { + assertThat(things.isEmpty()).isFalse(); + } + + @BeforeTemplate + void bad5(Collection things) { + assertThat(!things.isEmpty()).isTrue(); + } + + @BeforeTemplate + void bad6(Collection things) { + assertThat(things).isNotEqualTo(Collections.emptyList()); + } + + @BeforeTemplate + void bad7(Collection things) { + assertThat(things).isNotEqualTo(Collections.emptySet()); + } + + @BeforeTemplate + void bad8(Collection things) { + assertThat(things).isNotEqualTo(ImmutableList.of()); + } + + @BeforeTemplate + void bad9(Collection things) { + assertThat(things).isNotEqualTo(ImmutableSet.of()); + } + + @AfterTemplate + void after(Collection things) { + assertThat(things).isNotEmpty(); + } +} diff --git a/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjFileContent.java b/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjFileContent.java new file mode 100644 index 000000000..9131768dd --- /dev/null +++ b/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjFileContent.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 static org.assertj.core.api.Assertions.assertThat; + +import com.google.common.io.Files; +import com.google.errorprone.refaster.annotation.AfterTemplate; +import com.google.errorprone.refaster.annotation.BeforeTemplate; +import java.io.File; +import java.io.IOException; +import java.nio.charset.StandardCharsets; + +public final class AssertjFileContent { + + @BeforeTemplate + void before(File file, String expected) throws IOException { + assertThat(new String(Files.toByteArray(file), StandardCharsets.UTF_8)).isEqualTo(expected); + } + + @BeforeTemplate + @SuppressWarnings("deprecation") // we're migrating people off a deprecated method + void before2(File file, String expected) throws IOException { + assertThat(Files.toString(file, StandardCharsets.UTF_8)).isEqualTo(expected); + } + + @AfterTemplate + void after(File file, String expected) { + assertThat(file).hasContent(expected); + } +} diff --git a/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjOptionalIsPresent.java b/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjOptionalIsPresent.java new file mode 100644 index 000000000..6086c6bd1 --- /dev/null +++ b/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjOptionalIsPresent.java @@ -0,0 +1,36 @@ +/* + * (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 static org.assertj.core.api.Assertions.assertThat; + +import com.google.errorprone.refaster.annotation.AfterTemplate; +import com.google.errorprone.refaster.annotation.BeforeTemplate; +import java.util.Optional; + +public final class AssertjOptionalIsPresent { + + @BeforeTemplate + void before(Optional thing) { + assertThat(thing.isPresent()).isTrue(); + } + + @AfterTemplate + void after(Optional thing) { + assertThat(thing).isPresent(); + } +} diff --git a/changelog/@unreleased/pr-851.v2.yml b/changelog/@unreleased/pr-851.v2.yml new file mode 100644 index 000000000..967e7db0a --- /dev/null +++ b/changelog/@unreleased/pr-851.v2.yml @@ -0,0 +1,6 @@ +type: improvement +improvement: + description: Some AssertJ assertions can now be automatically replaced with more + idiomatic ones using refaster. + links: + - https://github.com/palantir/gradle-baseline/pull/851