From d4541b313c7cd1ff8cca3968a4aab37595850e5d Mon Sep 17 00:00:00 2001 From: Stefan Bischof Date: Fri, 7 May 2021 22:50:48 +0200 Subject: [PATCH] additional conditions and asserts Signed-off-by: Stefan Bischof --- .../feature/AbstractFeatureAssert.java | 30 +++--- .../assertj/feature/FeaturesConditions.java | 8 +- .../test/assertj/feature/ConditionAssert.java | 57 +++++++++++ .../test/assertj/feature/ConditionMethod.java | 21 +++++ .../feature/FeaturesConditionsAssertTest.java | 94 +++++++++++++++++++ .../assertj/promise/FeatureAssertTest.java | 28 ------ 6 files changed, 193 insertions(+), 45 deletions(-) create mode 100644 org.osgi.test.assertj.feature/src/test/java/org/osgi/test/assertj/feature/ConditionAssert.java create mode 100644 org.osgi.test.assertj.feature/src/test/java/org/osgi/test/assertj/feature/ConditionMethod.java create mode 100644 org.osgi.test.assertj.feature/src/test/java/org/osgi/test/assertj/feature/FeaturesConditionsAssertTest.java delete mode 100644 org.osgi.test.assertj.feature/src/test/java/org/osgi/test/assertj/promise/FeatureAssertTest.java diff --git a/org.osgi.test.assertj.feature/src/main/java/org/osgi/test/assertj/feature/AbstractFeatureAssert.java b/org.osgi.test.assertj.feature/src/main/java/org/osgi/test/assertj/feature/AbstractFeatureAssert.java index 23849bc32..f6b40f068 100644 --- a/org.osgi.test.assertj.feature/src/main/java/org/osgi/test/assertj/feature/AbstractFeatureAssert.java +++ b/org.osgi.test.assertj.feature/src/main/java/org/osgi/test/assertj/feature/AbstractFeatureAssert.java @@ -25,7 +25,6 @@ import org.assertj.core.api.InstanceOfAssertFactory; import org.assertj.core.api.ListAssert; import org.assertj.core.api.MapAssert; -import org.assertj.core.error.ShouldBe; import org.osgi.util.feature.Feature; import org.osgi.util.feature.FeatureBundle; import org.osgi.util.feature.FeatureConfiguration; @@ -91,8 +90,7 @@ public MapAssert hasVariablesThat() { * @throws AssertionError - if the actual Feature is not complete. */ public S isComplete() { - ShouldBe.shouldBe(actual, FeaturesConditions.FeatureConditions.complete()); - return isNameNull().is(FeaturesConditions.FeatureConditions.complete()); + return isNotNull().is(FeaturesConditions.FeatureConditions.complete()); } /** @@ -102,7 +100,7 @@ public S isComplete() { * @throws AssertionError - if the actual Feature is complete. */ public S isNotComplete() { - return isNameNull().isNot(FeaturesConditions.FeatureConditions.complete()); + return isNotNull().isNot(FeaturesConditions.FeatureConditions.complete()); } /** @@ -115,15 +113,15 @@ public S isNotComplete() { * to the given one. */ public S hasDescription(String description) { - return isNameNull().has(FeaturesConditions.FeatureConditions.description(description)); + return isNotNull().has(FeaturesConditions.FeatureConditions.description(description)); } public S hasDescriptionMatching(String pattern) { - return isNameNull().has(FeaturesConditions.FeatureConditions.descriptionMatches(pattern)); + return isNotNull().has(FeaturesConditions.FeatureConditions.descriptionMatches(pattern)); } public S isDescriptionNull() { - return isNameNull().is(FeaturesConditions.FeatureConditions.descriptionNull()); + return isNotNull().is(FeaturesConditions.FeatureConditions.descriptionNull()); } /** @@ -136,15 +134,15 @@ public S isDescriptionNull() { * the given one. */ public S hasLicense(String license) { - return isNameNull().has(FeaturesConditions.FeatureConditions.license(license)); + return isNotNull().has(FeaturesConditions.FeatureConditions.license(license)); } public S hasLicenseMatching(String pattern) { - return isNameNull().has(FeaturesConditions.FeatureConditions.licenseMatches(pattern)); + return isNotNull().has(FeaturesConditions.FeatureConditions.licenseMatches(pattern)); } public S isLicenseNull() { - return isNameNull().is(FeaturesConditions.FeatureConditions.licenseNull()); + return isNotNull().is(FeaturesConditions.FeatureConditions.licenseNull()); } /** @@ -156,15 +154,15 @@ public S isLicenseNull() { * given one. */ public S hasName(String name) { - return isNameNull().has(FeaturesConditions.FeatureConditions.name(name)); + return isNotNull().has(FeaturesConditions.FeatureConditions.name(name)); } public S hasNameMatching(String pattern) { - return isNameNull().has(FeaturesConditions.FeatureConditions.nameMatches(pattern)); + return isNotNull().has(FeaturesConditions.FeatureConditions.nameMatches(pattern)); } public S isNameNull() { - return isNameNull().is(FeaturesConditions.FeatureConditions.nameNull()); + return isNotNull().is(FeaturesConditions.FeatureConditions.nameNull()); } /** @@ -176,15 +174,15 @@ public S isNameNull() { * the given one. */ public S hasVendor(String vendor) { - return isNameNull().has(FeaturesConditions.FeatureConditions.vendor(vendor)); + return isNotNull().has(FeaturesConditions.FeatureConditions.vendor(vendor)); } public S hasVendorMatching(String pattern) { - return isNameNull().has(FeaturesConditions.FeatureConditions.vendorMatches(pattern)); + return isNotNull().has(FeaturesConditions.FeatureConditions.vendorMatches(pattern)); } public S isVendorNull() { - return isNameNull().is(FeaturesConditions.FeatureConditions.vendorNull()); + return isNotNull().is(FeaturesConditions.FeatureConditions.vendorNull()); } // TODO: categories, copyright, docURL and SCM. diff --git a/org.osgi.test.assertj.feature/src/main/java/org/osgi/test/assertj/feature/FeaturesConditions.java b/org.osgi.test.assertj.feature/src/main/java/org/osgi/test/assertj/feature/FeaturesConditions.java index 6e898d3ea..a65c9f9e6 100644 --- a/org.osgi.test.assertj.feature/src/main/java/org/osgi/test/assertj/feature/FeaturesConditions.java +++ b/org.osgi.test.assertj.feature/src/main/java/org/osgi/test/assertj/feature/FeaturesConditions.java @@ -18,6 +18,8 @@ package org.osgi.test.assertj.feature; +import static org.assertj.core.api.Assertions.not; + import java.util.Objects; import org.assertj.core.api.Condition; @@ -37,7 +39,11 @@ static Condition nameNull() { } static Condition complete() { - return new Condition(Feature::isComplete, "must be complete"); + return new Condition(Feature::isComplete, "complete"); + } + + static Condition notComplete() { + return not(complete()); } static Condition name(String name) { diff --git a/org.osgi.test.assertj.feature/src/test/java/org/osgi/test/assertj/feature/ConditionAssert.java b/org.osgi.test.assertj.feature/src/test/java/org/osgi/test/assertj/feature/ConditionAssert.java new file mode 100644 index 000000000..b2f3a1c18 --- /dev/null +++ b/org.osgi.test.assertj.feature/src/test/java/org/osgi/test/assertj/feature/ConditionAssert.java @@ -0,0 +1,57 @@ +package org.osgi.test.assertj.feature; + +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import java.util.regex.Pattern; + +import org.assertj.core.api.AbstractThrowableAssert; +import org.assertj.core.api.Condition; +import org.assertj.core.api.ObjectAssertFactory; + +public interface ConditionAssert { + String regex_startWith_Expecting = "(?si).*Expecting.*"; + + default AbstractThrowableAssert failingHas(Condition condition, T actual, String msg, Object... args) { + return failingHas(condition, actual, String.format(msg, args)); + } + + default AbstractThrowableAssert failingHas(Condition condition, T actual, String msg) { + String regex = regex_expecting_X_M_Y(actual, ConditionMethod.Has, msg); + return failingHas(condition, actual).hasMessageMatching(regex); + } + + default AbstractThrowableAssert failingHas(Condition condition, T actual) { + return assertThatThrownBy(() -> passingHas(condition, actual)).isInstanceOf(AssertionError.class); + } + + default AbstractThrowableAssert failingIs(Condition condition, T actual, String msg, Object... args) { + return failingIs(condition, actual, String.format(msg, args)); + } + + default AbstractThrowableAssert failingIs(Condition condition, T actual, String msg) { + String regex = regex_expecting_X_M_Y(actual, ConditionMethod.Is, msg); + return assertThatThrownBy(() -> passingIs(condition, actual)).isInstanceOf(AssertionError.class) + .hasMessageMatching(regex); + } + + default void passingHas(Condition condition, T actual) { + ObjectAssertFactory factory = new ObjectAssertFactory<>(); + factory.createAssert(actual) + .has(condition); + } + + default void passingIs(Condition condition, T actual) { + ObjectAssertFactory factory = new ObjectAssertFactory<>(); + factory.createAssert(actual) + .is(condition); + } + + default String regex_expecting_X_M_Y(Object x, ConditionMethod m, Object y) { + return String.format(regex_startWith_Expecting + "%s.*" + m + ".*%s.*", Pattern.quote(x.toString()), y); + } + + default String rexex_expecting_X_M_Y_Z(Object x, ConditionMethod m, Object y, Object z) { + return regex_expecting_X_M_Y(x, m, String.format("%s.*%s", y, z)); + } + +} diff --git a/org.osgi.test.assertj.feature/src/test/java/org/osgi/test/assertj/feature/ConditionMethod.java b/org.osgi.test.assertj.feature/src/test/java/org/osgi/test/assertj/feature/ConditionMethod.java new file mode 100644 index 000000000..63ba6503a --- /dev/null +++ b/org.osgi.test.assertj.feature/src/test/java/org/osgi/test/assertj/feature/ConditionMethod.java @@ -0,0 +1,21 @@ +package org.osgi.test.assertj.feature; + +public enum ConditionMethod { + + Has("to have"), + DoesNotHas("not to have"), + Is("to be"), + IsNot("not to be"); + + private String text; + + ConditionMethod(String text) { + this.text = text; + } + + @Override + public String toString() { + + return text; + } +} diff --git a/org.osgi.test.assertj.feature/src/test/java/org/osgi/test/assertj/feature/FeaturesConditionsAssertTest.java b/org.osgi.test.assertj.feature/src/test/java/org/osgi/test/assertj/feature/FeaturesConditionsAssertTest.java new file mode 100644 index 000000000..7c8344258 --- /dev/null +++ b/org.osgi.test.assertj.feature/src/test/java/org/osgi/test/assertj/feature/FeaturesConditionsAssertTest.java @@ -0,0 +1,94 @@ +/******************************************************************************* + * Copyright (c) Contributors to the Eclipse Foundation + * + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + *******************************************************************************/ + +package org.osgi.test.assertj.feature; + +import static java.lang.String.format; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import org.assertj.core.api.junit.jupiter.SoftAssertionsExtension; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Nested; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.osgi.util.feature.Feature; + +@ExtendWith(SoftAssertionsExtension.class) +public class FeaturesConditionsAssertTest implements ConditionAssert { + + @Nested + class FeatureContitionsTest { + + private String featureName = "theFeature"; + Feature feature = null; + + @BeforeEach + private void beforEach() { + feature = mock(Feature.class, featureName); + } + + @Test + void testComplete() throws Exception { + + when(feature.isComplete()).thenReturn(true); + + // condition pass + passingIs(FeaturesConditions.FeatureConditions.complete(), feature); + + // assertion pass + Assertions.assertThat(feature) + .isComplete(); + + when(feature.isComplete()).thenReturn(false); + + // condition fail + failingIs(FeaturesConditions.FeatureConditions.complete(), feature, "complete"); + + // assertion fail + assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> Assertions.assertThat(feature) + .isComplete()) + .withMessage(format("%nExpecting:%n " + featureName + "%nto be complete")); + } + + @Test + void testNotComplete() throws Exception { + + when(feature.isComplete()).thenReturn(false); + + // condition pass + passingIs(FeaturesConditions.FeatureConditions.notComplete(), feature); + + // assertion pass + Assertions.assertThat(feature) + .isNotComplete(); + + when(feature.isComplete()).thenReturn(true); + + // condition fail + failingIs(FeaturesConditions.FeatureConditions.notComplete(), feature, "not :"); + + // assertion fail + assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> Assertions.assertThat(feature) + .isNotComplete()) + .withMessage(format("%nExpecting:%n " + featureName + "%nnot to be complete")); + + } + } +} diff --git a/org.osgi.test.assertj.feature/src/test/java/org/osgi/test/assertj/promise/FeatureAssertTest.java b/org.osgi.test.assertj.feature/src/test/java/org/osgi/test/assertj/promise/FeatureAssertTest.java deleted file mode 100644 index 7f5e8becf..000000000 --- a/org.osgi.test.assertj.feature/src/test/java/org/osgi/test/assertj/promise/FeatureAssertTest.java +++ /dev/null @@ -1,28 +0,0 @@ -/******************************************************************************* - * Copyright (c) Contributors to the Eclipse Foundation - * - * 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. - * - * SPDX-License-Identifier: Apache-2.0 - *******************************************************************************/ - -package org.osgi.test.assertj.promise; - -import org.assertj.core.api.junit.jupiter.SoftAssertionsExtension; -import org.junit.jupiter.api.extension.ExtendWith; - -@ExtendWith(SoftAssertionsExtension.class) -public class FeatureAssertTest { - - -}