-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Stefan Bischof <stbischof@bipolis.org>
- Loading branch information
Showing
6 changed files
with
193 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
...sgi.test.assertj.feature/src/test/java/org/osgi/test/assertj/feature/ConditionAssert.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <T> AbstractThrowableAssert<?, ?> failingHas(Condition<T> condition, T actual, String msg, Object... args) { | ||
return failingHas(condition, actual, String.format(msg, args)); | ||
} | ||
|
||
default <T> AbstractThrowableAssert<?, ?> failingHas(Condition<T> condition, T actual, String msg) { | ||
String regex = regex_expecting_X_M_Y(actual, ConditionMethod.Has, msg); | ||
return failingHas(condition, actual).hasMessageMatching(regex); | ||
} | ||
|
||
default <T> AbstractThrowableAssert<?, ?> failingHas(Condition<T> condition, T actual) { | ||
return assertThatThrownBy(() -> passingHas(condition, actual)).isInstanceOf(AssertionError.class); | ||
} | ||
|
||
default <T> AbstractThrowableAssert<?, ?> failingIs(Condition<T> condition, T actual, String msg, Object... args) { | ||
return failingIs(condition, actual, String.format(msg, args)); | ||
} | ||
|
||
default <T> AbstractThrowableAssert<?, ?> failingIs(Condition<T> 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 <T> void passingHas(Condition<T> condition, T actual) { | ||
ObjectAssertFactory<T> factory = new ObjectAssertFactory<>(); | ||
factory.createAssert(actual) | ||
.has(condition); | ||
} | ||
|
||
default <T> void passingIs(Condition<T> condition, T actual) { | ||
ObjectAssertFactory<T> 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)); | ||
} | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
...sgi.test.assertj.feature/src/test/java/org/osgi/test/assertj/feature/ConditionMethod.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
...rtj.feature/src/test/java/org/osgi/test/assertj/feature/FeaturesConditionsAssertTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 :<complete>"); | ||
|
||
// assertion fail | ||
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> Assertions.assertThat(feature) | ||
.isNotComplete()) | ||
.withMessage(format("%nExpecting:%n " + featureName + "%nnot to be complete")); | ||
|
||
} | ||
} | ||
} |
28 changes: 0 additions & 28 deletions
28
...i.test.assertj.feature/src/test/java/org/osgi/test/assertj/promise/FeatureAssertTest.java
This file was deleted.
Oops, something went wrong.