Skip to content

Commit 32b5252

Browse files
committed
additional conditions and asserts
Signed-off-by: Stefan Bischof <stbischof@bipolis.org>
1 parent 8871cd9 commit 32b5252

File tree

6 files changed

+193
-45
lines changed

6 files changed

+193
-45
lines changed

org.osgi.test.assertj.feature/src/main/java/org/osgi/test/assertj/feature/AbstractFeatureAssert.java

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import org.assertj.core.api.InstanceOfAssertFactory;
2626
import org.assertj.core.api.ListAssert;
2727
import org.assertj.core.api.MapAssert;
28-
import org.assertj.core.error.ShouldBe;
2928
import org.osgi.util.feature.Feature;
3029
import org.osgi.util.feature.FeatureBundle;
3130
import org.osgi.util.feature.FeatureConfiguration;
@@ -91,8 +90,7 @@ public MapAssert<String, String> hasVariablesThat() {
9190
* @throws AssertionError - if the actual Feature is not complete.
9291
*/
9392
public S isComplete() {
94-
ShouldBe.shouldBe(actual, FeaturesConditions.FeatureConditions.complete());
95-
return isNameNull().is(FeaturesConditions.FeatureConditions.complete());
93+
return isNotNull().is(FeaturesConditions.FeatureConditions.complete());
9694
}
9795

9896
/**
@@ -102,7 +100,7 @@ public S isComplete() {
102100
* @throws AssertionError - if the actual Feature is complete.
103101
*/
104102
public S isNotComplete() {
105-
return isNameNull().isNot(FeaturesConditions.FeatureConditions.complete());
103+
return isNotNull().isNot(FeaturesConditions.FeatureConditions.complete());
106104
}
107105

108106
/**
@@ -115,15 +113,15 @@ public S isNotComplete() {
115113
* to the given one.
116114
*/
117115
public S hasDescription(String description) {
118-
return isNameNull().has(FeaturesConditions.FeatureConditions.description(description));
116+
return isNotNull().has(FeaturesConditions.FeatureConditions.description(description));
119117
}
120118

121119
public S hasDescriptionMatching(String pattern) {
122-
return isNameNull().has(FeaturesConditions.FeatureConditions.descriptionMatches(pattern));
120+
return isNotNull().has(FeaturesConditions.FeatureConditions.descriptionMatches(pattern));
123121
}
124122

125123
public S isDescriptionNull() {
126-
return isNameNull().is(FeaturesConditions.FeatureConditions.descriptionNull());
124+
return isNotNull().is(FeaturesConditions.FeatureConditions.descriptionNull());
127125
}
128126

129127
/**
@@ -136,15 +134,15 @@ public S isDescriptionNull() {
136134
* the given one.
137135
*/
138136
public S hasLicense(String license) {
139-
return isNameNull().has(FeaturesConditions.FeatureConditions.license(license));
137+
return isNotNull().has(FeaturesConditions.FeatureConditions.license(license));
140138
}
141139

142140
public S hasLicenseMatching(String pattern) {
143-
return isNameNull().has(FeaturesConditions.FeatureConditions.licenseMatches(pattern));
141+
return isNotNull().has(FeaturesConditions.FeatureConditions.licenseMatches(pattern));
144142
}
145143

146144
public S isLicenseNull() {
147-
return isNameNull().is(FeaturesConditions.FeatureConditions.licenseNull());
145+
return isNotNull().is(FeaturesConditions.FeatureConditions.licenseNull());
148146
}
149147

150148
/**
@@ -156,15 +154,15 @@ public S isLicenseNull() {
156154
* given one.
157155
*/
158156
public S hasName(String name) {
159-
return isNameNull().has(FeaturesConditions.FeatureConditions.name(name));
157+
return isNotNull().has(FeaturesConditions.FeatureConditions.name(name));
160158
}
161159

162160
public S hasNameMatching(String pattern) {
163-
return isNameNull().has(FeaturesConditions.FeatureConditions.nameMatches(pattern));
161+
return isNotNull().has(FeaturesConditions.FeatureConditions.nameMatches(pattern));
164162
}
165163

166164
public S isNameNull() {
167-
return isNameNull().is(FeaturesConditions.FeatureConditions.nameNull());
165+
return isNotNull().is(FeaturesConditions.FeatureConditions.nameNull());
168166
}
169167

170168
/**
@@ -176,15 +174,15 @@ public S isNameNull() {
176174
* the given one.
177175
*/
178176
public S hasVendor(String vendor) {
179-
return isNameNull().has(FeaturesConditions.FeatureConditions.vendor(vendor));
177+
return isNotNull().has(FeaturesConditions.FeatureConditions.vendor(vendor));
180178
}
181179

182180
public S hasVendorMatching(String pattern) {
183-
return isNameNull().has(FeaturesConditions.FeatureConditions.vendorMatches(pattern));
181+
return isNotNull().has(FeaturesConditions.FeatureConditions.vendorMatches(pattern));
184182
}
185183

186184
public S isVendorNull() {
187-
return isNameNull().is(FeaturesConditions.FeatureConditions.vendorNull());
185+
return isNotNull().is(FeaturesConditions.FeatureConditions.vendorNull());
188186
}
189187

190188
// TODO: categories, copyright, docURL and SCM.

org.osgi.test.assertj.feature/src/main/java/org/osgi/test/assertj/feature/FeaturesConditions.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
package org.osgi.test.assertj.feature;
2020

21+
import static org.assertj.core.api.Assertions.not;
22+
2123
import java.util.Objects;
2224

2325
import org.assertj.core.api.Condition;
@@ -37,7 +39,11 @@ static Condition<Feature> nameNull() {
3739
}
3840

3941
static Condition<Feature> complete() {
40-
return new Condition<Feature>(Feature::isComplete, "must be complete");
42+
return new Condition<Feature>(Feature::isComplete, "complete");
43+
}
44+
45+
static Condition<Feature> notComplete() {
46+
return not(complete());
4147
}
4248

4349
static Condition<Feature> name(String name) {
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package org.osgi.test.assertj.feature;
2+
3+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
4+
5+
import java.util.regex.Pattern;
6+
7+
import org.assertj.core.api.AbstractThrowableAssert;
8+
import org.assertj.core.api.Condition;
9+
import org.assertj.core.api.ObjectAssertFactory;
10+
11+
public interface ConditionAssert {
12+
String regex_startWith_Expecting = "(?si).*Expecting.*";
13+
14+
default <T> AbstractThrowableAssert<?, ?> failingHas(Condition<T> condition, T actual, String msg, Object... args) {
15+
return failingHas(condition, actual, String.format(msg, args));
16+
}
17+
18+
default <T> AbstractThrowableAssert<?, ?> failingHas(Condition<T> condition, T actual, String msg) {
19+
String regex = regex_expecting_X_M_Y(actual, ConditionMethod.Has, msg);
20+
return failingHas(condition, actual).hasMessageMatching(regex);
21+
}
22+
23+
default <T> AbstractThrowableAssert<?, ?> failingHas(Condition<T> condition, T actual) {
24+
return assertThatThrownBy(() -> passingHas(condition, actual)).isInstanceOf(AssertionError.class);
25+
}
26+
27+
default <T> AbstractThrowableAssert<?, ?> failingIs(Condition<T> condition, T actual, String msg, Object... args) {
28+
return failingIs(condition, actual, String.format(msg, args));
29+
}
30+
31+
default <T> AbstractThrowableAssert<?, ?> failingIs(Condition<T> condition, T actual, String msg) {
32+
String regex = regex_expecting_X_M_Y(actual, ConditionMethod.Is, msg);
33+
return assertThatThrownBy(() -> passingIs(condition, actual)).isInstanceOf(AssertionError.class)
34+
.hasMessageMatching(regex);
35+
}
36+
37+
default <T> void passingHas(Condition<T> condition, T actual) {
38+
ObjectAssertFactory<T> factory = new ObjectAssertFactory<>();
39+
factory.createAssert(actual)
40+
.has(condition);
41+
}
42+
43+
default <T> void passingIs(Condition<T> condition, T actual) {
44+
ObjectAssertFactory<T> factory = new ObjectAssertFactory<>();
45+
factory.createAssert(actual)
46+
.is(condition);
47+
}
48+
49+
default String regex_expecting_X_M_Y(Object x, ConditionMethod m, Object y) {
50+
return String.format(regex_startWith_Expecting + "%s.*" + m + ".*%s.*", Pattern.quote(x.toString()), y);
51+
}
52+
53+
default String rexex_expecting_X_M_Y_Z(Object x, ConditionMethod m, Object y, Object z) {
54+
return regex_expecting_X_M_Y(x, m, String.format("%s.*%s", y, z));
55+
}
56+
57+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.osgi.test.assertj.feature;
2+
3+
public enum ConditionMethod {
4+
5+
Has("to have"),
6+
DoesNotHas("not to have"),
7+
Is("to be"),
8+
IsNot("not to be");
9+
10+
private String text;
11+
12+
ConditionMethod(String text) {
13+
this.text = text;
14+
}
15+
16+
@Override
17+
public String toString() {
18+
19+
return text;
20+
}
21+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*******************************************************************************
2+
* Copyright (c) Contributors to the Eclipse Foundation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
*******************************************************************************/
18+
19+
package org.osgi.test.assertj.feature;
20+
21+
import static java.lang.String.format;
22+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
23+
import static org.mockito.Mockito.mock;
24+
import static org.mockito.Mockito.when;
25+
26+
import org.assertj.core.api.junit.jupiter.SoftAssertionsExtension;
27+
import org.junit.jupiter.api.BeforeEach;
28+
import org.junit.jupiter.api.Nested;
29+
import org.junit.jupiter.api.Test;
30+
import org.junit.jupiter.api.extension.ExtendWith;
31+
import org.osgi.util.feature.Feature;
32+
33+
@ExtendWith(SoftAssertionsExtension.class)
34+
public class FeaturesConditionsAssertTest implements ConditionAssert {
35+
36+
@Nested
37+
class FeatureContitionsTest {
38+
39+
private String featureName = "theFeature";
40+
Feature feature = null;
41+
42+
@BeforeEach
43+
private void beforEach() {
44+
feature = mock(Feature.class, featureName);
45+
}
46+
47+
@Test
48+
void testComplete() throws Exception {
49+
50+
when(feature.isComplete()).thenReturn(true);
51+
52+
// condition pass
53+
passingIs(FeaturesConditions.FeatureConditions.complete(), feature);
54+
55+
// assertion pass
56+
Assertions.assertThat(feature)
57+
.isComplete();
58+
59+
when(feature.isComplete()).thenReturn(false);
60+
61+
// condition fail
62+
failingIs(FeaturesConditions.FeatureConditions.complete(), feature, "complete");
63+
64+
// assertion fail
65+
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> Assertions.assertThat(feature)
66+
.isComplete())
67+
.withMessage(format("%nExpecting:%n " + featureName + "%nto be complete"));
68+
}
69+
70+
@Test
71+
void testNotComplete() throws Exception {
72+
73+
when(feature.isComplete()).thenReturn(false);
74+
75+
// condition pass
76+
passingIs(FeaturesConditions.FeatureConditions.notComplete(), feature);
77+
78+
// assertion pass
79+
Assertions.assertThat(feature)
80+
.isNotComplete();
81+
82+
when(feature.isComplete()).thenReturn(true);
83+
84+
// condition fail
85+
failingIs(FeaturesConditions.FeatureConditions.notComplete(), feature, "not :<complete>");
86+
87+
// assertion fail
88+
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> Assertions.assertThat(feature)
89+
.isNotComplete())
90+
.withMessage(format("%nExpecting:%n " + featureName + "%nnot to be complete"));
91+
92+
}
93+
}
94+
}

org.osgi.test.assertj.feature/src/test/java/org/osgi/test/assertj/promise/FeatureAssertTest.java

Lines changed: 0 additions & 28 deletions
This file was deleted.

0 commit comments

Comments
 (0)