Skip to content

Commit

Permalink
additional conditions and asserts
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Bischof <stbischof@bipolis.org>
  • Loading branch information
stbischof committed May 7, 2021
1 parent beafdac commit d4541b3
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -91,8 +90,7 @@ public MapAssert<String, String> 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());
}

/**
Expand All @@ -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());
}

/**
Expand All @@ -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());
}

/**
Expand All @@ -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());
}

/**
Expand All @@ -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());
}

/**
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -37,7 +39,11 @@ static Condition<Feature> nameNull() {
}

static Condition<Feature> complete() {
return new Condition<Feature>(Feature::isComplete, "must be complete");
return new Condition<Feature>(Feature::isComplete, "complete");
}

static Condition<Feature> notComplete() {
return not(complete());
}

static Condition<Feature> name(String name) {
Expand Down
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));
}

}
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;
}
}
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"));

}
}
}

This file was deleted.

0 comments on commit d4541b3

Please sign in to comment.