diff --git a/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjOptionalIsNotPresent.java b/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjOptionalIsNotPresent.java new file mode 100644 index 000000000..4b1a28518 --- /dev/null +++ b/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjOptionalIsNotPresent.java @@ -0,0 +1,49 @@ +/* + * (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.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.Optional; + +public final class AssertjOptionalIsNotPresent { + + @BeforeTemplate + void before1(Optional thing) { + assertThat(thing.isPresent()).isFalse(); + } + + @BeforeTemplate + void before2(Optional thing) { + assertThat(!thing.isPresent()).isTrue(); + } + + @BeforeTemplate + void before3(Optional thing) { + assertThat(thing).isEqualTo(Optional.empty()); + } + + @AfterTemplate + @UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS) + void after(Optional thing) { + assertThat(thing).isNotPresent(); + } +} diff --git a/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjOptionalIsNotPresentWithDescription.java b/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjOptionalIsNotPresentWithDescription.java new file mode 100644 index 000000000..14c5ee3ef --- /dev/null +++ b/baseline-refaster-rules/src/main/java/com/palantir/baseline/refaster/AssertjOptionalIsNotPresentWithDescription.java @@ -0,0 +1,49 @@ +/* + * (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.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.Optional; + +public final class AssertjOptionalIsNotPresentWithDescription { + + @BeforeTemplate + void before1(Optional thing, String description) { + assertThat(thing.isPresent()).describedAs(description).isFalse(); + } + + @BeforeTemplate + void before2(Optional thing, String description) { + assertThat(!thing.isPresent()).describedAs(description).isTrue(); + } + + @BeforeTemplate + void before3(Optional thing, String description) { + assertThat(thing).describedAs(description).isEqualTo(Optional.empty()); + } + + @AfterTemplate + @UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS) + void after(Optional thing, String description) { + assertThat(thing).describedAs(description).isNotPresent(); + } +} diff --git a/baseline-refaster-rules/src/test/java/com/palantir/baseline/refaster/AssertjOptionalPresenceTest.java b/baseline-refaster-rules/src/test/java/com/palantir/baseline/refaster/AssertjOptionalPresenceTest.java index cf9648a47..cf7f81aa2 100644 --- a/baseline-refaster-rules/src/test/java/com/palantir/baseline/refaster/AssertjOptionalPresenceTest.java +++ b/baseline-refaster-rules/src/test/java/com/palantir/baseline/refaster/AssertjOptionalPresenceTest.java @@ -16,6 +16,8 @@ package com.palantir.baseline.refaster; +import static org.assertj.core.api.Assumptions.assumeThat; + import org.junit.Test; public class AssertjOptionalPresenceTest { @@ -65,4 +67,64 @@ public void isPresent_description() { " }", "}"); } + + @Test + public void isNotPresent_simple() { + assumeThat(System.getProperty("java.specification.version")) + .describedAs("Refaster does not currently support fluent refactors on java 11") + .isEqualTo("1.8"); + RefasterTestHelper + .forRefactoring(AssertjOptionalIsNotPresent.class) + .withInputLines( + "Test", + "import static org.assertj.core.api.Assertions.assertThat;", + "import java.util.Optional;", + "public class Test {", + " void f(Optional in) {", + " assertThat(in.isPresent()).isFalse();", + " assertThat(!in.isPresent()).isTrue();", + " assertThat(in).isEqualTo(Optional.empty());", + " }", + "}") + .hasOutputLines( + "import static org.assertj.core.api.Assertions.assertThat;", + "import java.util.Optional;", + "public class Test {", + " void f(Optional in) {", + " assertThat(in).isNotPresent();", + " assertThat(in).isNotPresent();", + " assertThat(in).isNotPresent();", + " }", + "}"); + } + + @Test + public void isNotPresent_description() { + assumeThat(System.getProperty("java.specification.version")) + .describedAs("Refaster does not currently support fluent refactors on java 11") + .isEqualTo("1.8"); + RefasterTestHelper + .forRefactoring(AssertjOptionalIsNotPresentWithDescription.class) + .withInputLines( + "Test", + "import static org.assertj.core.api.Assertions.assertThat;", + "import java.util.Optional;", + "public class Test {", + " void f(Optional in) {", + " assertThat(in.isPresent()).describedAs(\"desc\").isFalse();", + " assertThat(!in.isPresent()).describedAs(\"desc\").isTrue();", + " assertThat(in).describedAs(\"desc\").isEqualTo(Optional.empty());", + " }", + "}") + .hasOutputLines( + "import static org.assertj.core.api.Assertions.assertThat;", + "import java.util.Optional;", + "public class Test {", + " void f(Optional in) {", + " assertThat(in).describedAs(\"desc\").isNotPresent();", + " assertThat(in).describedAs(\"desc\").isNotPresent();", + " assertThat(in).describedAs(\"desc\").isNotPresent();", + " }", + "}"); + } } diff --git a/changelog/@unreleased/pr-911.v2.yml b/changelog/@unreleased/pr-911.v2.yml new file mode 100644 index 000000000..53da3b97c --- /dev/null +++ b/changelog/@unreleased/pr-911.v2.yml @@ -0,0 +1,5 @@ +type: improvement +improvement: + description: Add refaster rule to simplify empty optional asserts + links: + - https://github.com/palantir/gradle-baseline/pull/911