diff --git a/org.osgi.test.assertj.framework/src/main/java/org/osgi/test/assertj/bundle/AbstractBundleDTOAssert.java b/org.osgi.test.assertj.framework/src/main/java/org/osgi/test/assertj/bundle/AbstractBundleDTOAssert.java new file mode 100644 index 00000000..c5f28be4 --- /dev/null +++ b/org.osgi.test.assertj.framework/src/main/java/org/osgi/test/assertj/bundle/AbstractBundleDTOAssert.java @@ -0,0 +1,158 @@ +package org.osgi.test.assertj.bundle; + +import org.assertj.core.api.AbstractObjectAssert; +import org.osgi.framework.dto.BundleDTO; + +/** + * Abstract base class for {@link BundleDTO} specific assertions + */ + +public abstract class AbstractBundleDTOAssert, A extends BundleDTO> + extends AbstractObjectAssert { + + /** + * Creates a new {@link AbstractBundleDTOAssert} to make + * assertions on actual BundleDTO. + * + * @param actual the BundleDTO we want to make assertions on. + */ + protected AbstractBundleDTOAssert(A actual, Class selfType) { + super(actual, selfType); + } + + /** + * Verifies that the actual BundleDTO's id is equal to the given one. + * + * @param id the given id to compare the actual BundleDTO's id to. + * @return this assertion object. + * @throws AssertionError - if the actual BundleDTO's id is not equal to the + * given one. + */ + public S hasId(long id) { + // check that actual BundleDTO we want to make assertions on is not + // null. + isNotNull(); + + // overrides the default error message with a more explicit one + String assertjErrorMessage = "\nExpecting id of:\n <%s>\nto be:\n <%s>\nbut was:\n <%s>"; + + // check + long actualId = actual.id; + if (actualId != id) { + failWithMessage(assertjErrorMessage, actual, id, actualId); + } + + // return the current assertion for method chaining + return myself; + } + + /** + * Verifies that the actual BundleDTO's lastModified is equal to the given + * one. + * + * @param lastModified the given lastModified to compare the actual + * BundleDTO's lastModified to. + * @return this assertion object. + * @throws AssertionError - if the actual BundleDTO's lastModified is not + * equal to the given one. + */ + public S hasLastModified(long lastModified) { + // check that actual BundleDTO we want to make assertions on is not + // null. + isNotNull(); + + // overrides the default error message with a more explicit one + String assertjErrorMessage = "\nExpecting lastModified of:\n <%s>\nto be:\n <%s>\nbut was:\n <%s>"; + + // check + long actualLastModified = actual.lastModified; + if (actualLastModified != lastModified) { + failWithMessage(assertjErrorMessage, actual, lastModified, actualLastModified); + } + + // return the current assertion for method chaining + return myself; + } + + /** + * Verifies that the actual BundleDTO's state is equal to the given one. + * + * @param state the given state to compare the actual BundleDTO's state to. + * @return this assertion object. + * @throws AssertionError - if the actual BundleDTO's state is not equal to + * the given one. + */ + public S hasState(int state) { + // check that actual BundleDTO we want to make assertions on is not + // null. + isNotNull(); + + // overrides the default error message with a more explicit one + String assertjErrorMessage = "\nExpecting state of:\n <%s>\nto be:\n <%s>\nbut was:\n <%s>"; + + // check + int actualState = actual.state; + if (actualState != state) { + failWithMessage(assertjErrorMessage, actual, state, actualState); + } + + // return the current assertion for method chaining + return myself; + } + + /** + * Verifies that the actual BundleDTO's symbolicName is equal to the given + * one. + * + * @param symbolicName the given symbolicName to compare the actual + * BundleDTO's symbolicName to. + * @return this assertion object. + * @throws AssertionError - if the actual BundleDTO's symbolicName is not + * equal to the given one. + */ + public S hasSymbolicName(String symbolicName) { + // check that actual BundleDTO we want to make assertions on is not + // null. + isNotNull(); + + // overrides the default error message with a more explicit one + String assertjErrorMessage = "\nExpecting symbolicName of:\n <%s>\nto be:\n <%s>\nbut was:\n <%s>"; + + // null safe check + String actualSymbolicName = actual.symbolicName; + if (!java.util.Objects.deepEquals(actualSymbolicName, symbolicName)) { + failWithMessage(assertjErrorMessage, actual, symbolicName, actualSymbolicName); + } + + // return the current assertion for method chaining + return myself; + } + + /** + * Verifies that the actual BundleDTO's version is equal to the given one. + * + * @param version the given version to compare the actual BundleDTO's + * version to. + * @return this assertion object. + * @throws AssertionError - if the actual BundleDTO's version is not equal + * to the given one. + */ + public S hasVersion(String version) { + // check that actual BundleDTO we want to make assertions on is not + // null. + isNotNull(); + + // overrides the default error message with a more explicit one + String assertjErrorMessage = "\nExpecting version of:\n <%s>\nto be:\n <%s>\nbut was:\n <%s>"; + + // null safe check + String actualVersion = actual.version; + if (!java.util.Objects.deepEquals(actualVersion, version)) { + failWithMessage(assertjErrorMessage, actual, version, actualVersion); + } + + // return the current assertion for method chaining + return myself; + } + +} diff --git a/org.osgi.test.assertj.framework/src/main/java/org/osgi/test/assertj/bundle/BundleDTOAssert.java b/org.osgi.test.assertj.framework/src/main/java/org/osgi/test/assertj/bundle/BundleDTOAssert.java new file mode 100644 index 00000000..b1366505 --- /dev/null +++ b/org.osgi.test.assertj.framework/src/main/java/org/osgi/test/assertj/bundle/BundleDTOAssert.java @@ -0,0 +1,36 @@ +package org.osgi.test.assertj.bundle; + +import org.osgi.framework.dto.BundleDTO; + +/** + * {@link BundleDTO} specific assertions Although this class is not final to + * allow Soft assertions proxy, if you wish to extend it, extend + * {@link AbstractBundleDTOAssert} instead. + */ + +public class BundleDTOAssert extends AbstractBundleDTOAssert { + + /** + * Creates a new {@link BundleDTOAssert} to make assertions on + * actual BundleDTO. + * + * @param actual the BundleDTO we want to make assertions on. + */ + public BundleDTOAssert(BundleDTO actual) { + super(actual, BundleDTOAssert.class); + } + + /** + * An entry point for BundleDTOAssert to follow AssertJ standard + * assertThat() statements.
+ * With a static import, one can write directly: + * assertThat(myBundleDTO) and get specific assertion with code + * completion. + * + * @param actual the BundleDTO we want to make assertions on. + * @return a new {@link BundleDTOAssert} + */ + public static BundleDTOAssert assertThat(BundleDTO actual) { + return new BundleDTOAssert(actual); + } +} diff --git a/org.osgi.test.assertj.framework/src/main/java/org/osgi/test/assertj/framework/AbstractFrameworkDTOAssert.java b/org.osgi.test.assertj.framework/src/main/java/org/osgi/test/assertj/framework/AbstractFrameworkDTOAssert.java new file mode 100644 index 00000000..34724a53 --- /dev/null +++ b/org.osgi.test.assertj.framework/src/main/java/org/osgi/test/assertj/framework/AbstractFrameworkDTOAssert.java @@ -0,0 +1,459 @@ +package org.osgi.test.assertj.framework; + +import org.assertj.core.api.AbstractObjectAssert; +import org.assertj.core.internal.Iterables; +import org.osgi.framework.dto.BundleDTO; +import org.osgi.framework.dto.FrameworkDTO; +import org.osgi.framework.dto.ServiceReferenceDTO; + +/** + * Abstract base class for {@link FrameworkDTO} specific assertions + */ + +public abstract class AbstractFrameworkDTOAssert, A extends FrameworkDTO> + extends AbstractObjectAssert { + + /** + * Creates a new {@link AbstractFrameworkDTOAssert} to make + * assertions on actual FrameworkDTO. + * + * @param actual the FrameworkDTO we want to make assertions on. + */ + protected AbstractFrameworkDTOAssert(A actual, Class selfType) { + super(actual, selfType); + } + + /** + * Verifies that the actual FrameworkDTO's bundles contains the given + * BundleDTO elements. + * + * @param bundles the given elements that should be contained in actual + * FrameworkDTO's bundles. + * @return this assertion object. + * @throws AssertionError if the actual FrameworkDTO's bundles does not + * contain all given BundleDTO elements. + */ + public S hasBundles(BundleDTO... bundles) { + // check that actual FrameworkDTO we want to make assertions on is not + // null. + isNotNull(); + + // check that given BundleDTO varargs is not null. + if (bundles == null) + failWithMessage("Expecting bundles parameter not to be null."); + + // check with standard error message, to set another message call: + // info.overridingErrorMessage("my error message"); + Iterables.instance() + .assertContains(info, actual.bundles, bundles); + + // return the current assertion for method chaining + return myself; + } + + /** + * Verifies that the actual FrameworkDTO's bundles contains the given + * BundleDTO elements in Collection. + * + * @param bundles the given elements that should be contained in actual + * FrameworkDTO's bundles. + * @return this assertion object. + * @throws AssertionError if the actual FrameworkDTO's bundles does not + * contain all given BundleDTO elements. + */ + public S hasBundles(java.util.Collection bundles) { + // check that actual FrameworkDTO we want to make assertions on is not + // null. + isNotNull(); + + // check that given BundleDTO collection is not null. + if (bundles == null) { + failWithMessage("Expecting bundles parameter not to be null."); + return myself; // to fool Eclipse "Null pointer access" warning on + // toArray. + } + + // check with standard error message, to set another message call: + // info.overridingErrorMessage("my error message"); + Iterables.instance() + .assertContains(info, actual.bundles, bundles.toArray()); + + // return the current assertion for method chaining + return myself; + } + + /** + * Verifies that the actual FrameworkDTO's bundles contains only the + * given BundleDTO elements and nothing else in whatever order. + * + * @param bundles the given elements that should be contained in actual + * FrameworkDTO's bundles. + * @return this assertion object. + * @throws AssertionError if the actual FrameworkDTO's bundles does not + * contain all given BundleDTO elements. + */ + public S hasOnlyBundles(BundleDTO... bundles) { + // check that actual FrameworkDTO we want to make assertions on is not + // null. + isNotNull(); + + // check that given BundleDTO varargs is not null. + if (bundles == null) + failWithMessage("Expecting bundles parameter not to be null."); + + // check with standard error message, to set another message call: + // info.overridingErrorMessage("my error message"); + Iterables.instance() + .assertContainsOnly(info, actual.bundles, bundles); + + // return the current assertion for method chaining + return myself; + } + + /** + * Verifies that the actual FrameworkDTO's bundles contains only the + * given BundleDTO elements in Collection and nothing else in whatever + * order. + * + * @param bundles the given elements that should be contained in actual + * FrameworkDTO's bundles. + * @return this assertion object. + * @throws AssertionError if the actual FrameworkDTO's bundles does not + * contain all given BundleDTO elements. + */ + public S hasOnlyBundles(java.util.Collection bundles) { + // check that actual FrameworkDTO we want to make assertions on is not + // null. + isNotNull(); + + // check that given BundleDTO collection is not null. + if (bundles == null) { + failWithMessage("Expecting bundles parameter not to be null."); + return myself; // to fool Eclipse "Null pointer access" warning on + // toArray. + } + + // check with standard error message, to set another message call: + // info.overridingErrorMessage("my error message"); + Iterables.instance() + .assertContainsOnly(info, actual.bundles, bundles.toArray()); + + // return the current assertion for method chaining + return myself; + } + + /** + * Verifies that the actual FrameworkDTO's bundles does not contain the + * given BundleDTO elements. + * + * @param bundles the given elements that should not be in actual + * FrameworkDTO's bundles. + * @return this assertion object. + * @throws AssertionError if the actual FrameworkDTO's bundles contains any + * given BundleDTO elements. + */ + public S doesNotHaveBundles(BundleDTO... bundles) { + // check that actual FrameworkDTO we want to make assertions on is not + // null. + isNotNull(); + + // check that given BundleDTO varargs is not null. + if (bundles == null) + failWithMessage("Expecting bundles parameter not to be null."); + + // check with standard error message (use overridingErrorMessage before + // contains to set your own message). + Iterables.instance() + .assertDoesNotContain(info, actual.bundles, bundles); + + // return the current assertion for method chaining + return myself; + } + + /** + * Verifies that the actual FrameworkDTO's bundles does not contain the + * given BundleDTO elements in Collection. + * + * @param bundles the given elements that should not be in actual + * FrameworkDTO's bundles. + * @return this assertion object. + * @throws AssertionError if the actual FrameworkDTO's bundles contains any + * given BundleDTO elements. + */ + public S doesNotHaveBundles(java.util.Collection bundles) { + // check that actual FrameworkDTO we want to make assertions on is not + // null. + isNotNull(); + + // check that given BundleDTO collection is not null. + if (bundles == null) { + failWithMessage("Expecting bundles parameter not to be null."); + return myself; // to fool Eclipse "Null pointer access" warning on + // toArray. + } + + // check with standard error message (use overridingErrorMessage before + // contains to set your own message). + Iterables.instance() + .assertDoesNotContain(info, actual.bundles, bundles.toArray()); + + // return the current assertion for method chaining + return myself; + } + + /** + * Verifies that the actual FrameworkDTO has no bundles. + * + * @return this assertion object. + * @throws AssertionError if the actual FrameworkDTO's bundles is not empty. + */ + public S hasNoBundles() { + // check that actual FrameworkDTO we want to make assertions on is not + // null. + isNotNull(); + + // we override the default error message with a more explicit one + String assertjErrorMessage = "\nExpecting :\n <%s>\nnot to have bundles but had :\n <%s>"; + + // check + if (actual.bundles.iterator() + .hasNext()) { + failWithMessage(assertjErrorMessage, actual, actual.bundles); + } + + // return the current assertion for method chaining + return myself; + } + + /** + * Verifies that the actual FrameworkDTO's properties is equal to the given + * one. + * + * @param properties the given properties to compare the actual + * FrameworkDTO's properties to. + * @return this assertion object. + * @throws AssertionError - if the actual FrameworkDTO's properties is not + * equal to the given one. + */ + public S hasProperties(java.util.Map properties) { + // check that actual FrameworkDTO we want to make assertions on is not + // null. + isNotNull(); + + // overrides the default error message with a more explicit one + String assertjErrorMessage = "\nExpecting properties of:\n <%s>\nto be:\n <%s>\nbut was:\n <%s>"; + + // null safe check + java.util.Map actualProperties = actual.properties; + if (!java.util.Objects.deepEquals(actualProperties, properties)) { + failWithMessage(assertjErrorMessage, actual, properties, actualProperties); + } + + // return the current assertion for method chaining + return myself; + } + + /** + * Verifies that the actual FrameworkDTO's services contains the given + * ServiceReferenceDTO elements. + * + * @param services the given elements that should be contained in actual + * FrameworkDTO's services. + * @return this assertion object. + * @throws AssertionError if the actual FrameworkDTO's services does not + * contain all given ServiceReferenceDTO elements. + */ + public S hasServices(ServiceReferenceDTO... services) { + // check that actual FrameworkDTO we want to make assertions on is not + // null. + isNotNull(); + + // check that given ServiceReferenceDTO varargs is not null. + if (services == null) + failWithMessage("Expecting services parameter not to be null."); + + // check with standard error message, to set another message call: + // info.overridingErrorMessage("my error message"); + Iterables.instance() + .assertContains(info, actual.services, services); + + // return the current assertion for method chaining + return myself; + } + + /** + * Verifies that the actual FrameworkDTO's services contains the given + * ServiceReferenceDTO elements in Collection. + * + * @param services the given elements that should be contained in actual + * FrameworkDTO's services. + * @return this assertion object. + * @throws AssertionError if the actual FrameworkDTO's services does not + * contain all given ServiceReferenceDTO elements. + */ + public S hasServices(java.util.Collection services) { + // check that actual FrameworkDTO we want to make assertions on is not + // null. + isNotNull(); + + // check that given ServiceReferenceDTO collection is not null. + if (services == null) { + failWithMessage("Expecting services parameter not to be null."); + return myself; // to fool Eclipse "Null pointer access" warning on + // toArray. + } + + // check with standard error message, to set another message call: + // info.overridingErrorMessage("my error message"); + Iterables.instance() + .assertContains(info, actual.services, services.toArray()); + + // return the current assertion for method chaining + return myself; + } + + /** + * Verifies that the actual FrameworkDTO's services contains only the + * given ServiceReferenceDTO elements and nothing else in whatever order. + * + * @param services the given elements that should be contained in actual + * FrameworkDTO's services. + * @return this assertion object. + * @throws AssertionError if the actual FrameworkDTO's services does not + * contain all given ServiceReferenceDTO elements. + */ + public S hasOnlyServices(ServiceReferenceDTO... services) { + // check that actual FrameworkDTO we want to make assertions on is not + // null. + isNotNull(); + + // check that given ServiceReferenceDTO varargs is not null. + if (services == null) + failWithMessage("Expecting services parameter not to be null."); + + // check with standard error message, to set another message call: + // info.overridingErrorMessage("my error message"); + Iterables.instance() + .assertContainsOnly(info, actual.services, services); + + // return the current assertion for method chaining + return myself; + } + + /** + * Verifies that the actual FrameworkDTO's services contains only the + * given ServiceReferenceDTO elements in Collection and nothing else in + * whatever order. + * + * @param services the given elements that should be contained in actual + * FrameworkDTO's services. + * @return this assertion object. + * @throws AssertionError if the actual FrameworkDTO's services does not + * contain all given ServiceReferenceDTO elements. + */ + public S hasOnlyServices(java.util.Collection services) { + // check that actual FrameworkDTO we want to make assertions on is not + // null. + isNotNull(); + + // check that given ServiceReferenceDTO collection is not null. + if (services == null) { + failWithMessage("Expecting services parameter not to be null."); + return myself; // to fool Eclipse "Null pointer access" warning on + // toArray. + } + + // check with standard error message, to set another message call: + // info.overridingErrorMessage("my error message"); + Iterables.instance() + .assertContainsOnly(info, actual.services, services.toArray()); + + // return the current assertion for method chaining + return myself; + } + + /** + * Verifies that the actual FrameworkDTO's services does not contain the + * given ServiceReferenceDTO elements. + * + * @param services the given elements that should not be in actual + * FrameworkDTO's services. + * @return this assertion object. + * @throws AssertionError if the actual FrameworkDTO's services contains any + * given ServiceReferenceDTO elements. + */ + public S doesNotHaveServices(ServiceReferenceDTO... services) { + // check that actual FrameworkDTO we want to make assertions on is not + // null. + isNotNull(); + + // check that given ServiceReferenceDTO varargs is not null. + if (services == null) + failWithMessage("Expecting services parameter not to be null."); + + // check with standard error message (use overridingErrorMessage before + // contains to set your own message). + Iterables.instance() + .assertDoesNotContain(info, actual.services, services); + + // return the current assertion for method chaining + return myself; + } + + /** + * Verifies that the actual FrameworkDTO's services does not contain the + * given ServiceReferenceDTO elements in Collection. + * + * @param services the given elements that should not be in actual + * FrameworkDTO's services. + * @return this assertion object. + * @throws AssertionError if the actual FrameworkDTO's services contains any + * given ServiceReferenceDTO elements. + */ + public S doesNotHaveServices(java.util.Collection services) { + // check that actual FrameworkDTO we want to make assertions on is not + // null. + isNotNull(); + + // check that given ServiceReferenceDTO collection is not null. + if (services == null) { + failWithMessage("Expecting services parameter not to be null."); + return myself; // to fool Eclipse "Null pointer access" warning on + // toArray. + } + + // check with standard error message (use overridingErrorMessage before + // contains to set your own message). + Iterables.instance() + .assertDoesNotContain(info, actual.services, services.toArray()); + + // return the current assertion for method chaining + return myself; + } + + /** + * Verifies that the actual FrameworkDTO has no services. + * + * @return this assertion object. + * @throws AssertionError if the actual FrameworkDTO's services is not + * empty. + */ + public S hasNoServices() { + // check that actual FrameworkDTO we want to make assertions on is not + // null. + isNotNull(); + + // we override the default error message with a more explicit one + String assertjErrorMessage = "\nExpecting :\n <%s>\nnot to have services but had :\n <%s>"; + + // check + if (actual.services.iterator() + .hasNext()) { + failWithMessage(assertjErrorMessage, actual, actual.services); + } + + // return the current assertion for method chaining + return myself; + } + +} diff --git a/org.osgi.test.assertj.framework/src/main/java/org/osgi/test/assertj/framework/FrameworkDTOAssert.java b/org.osgi.test.assertj.framework/src/main/java/org/osgi/test/assertj/framework/FrameworkDTOAssert.java new file mode 100644 index 00000000..6afdba2e --- /dev/null +++ b/org.osgi.test.assertj.framework/src/main/java/org/osgi/test/assertj/framework/FrameworkDTOAssert.java @@ -0,0 +1,36 @@ +package org.osgi.test.assertj.framework; + +import org.osgi.framework.dto.FrameworkDTO; + +/** + * {@link FrameworkDTO} specific assertions Although this class is not final to + * allow Soft assertions proxy, if you wish to extend it, extend + * {@link AbstractFrameworkDTOAssert} instead. + */ + +public class FrameworkDTOAssert extends AbstractFrameworkDTOAssert { + + /** + * Creates a new {@link FrameworkDTOAssert} to make assertions + * on actual FrameworkDTO. + * + * @param actual the FrameworkDTO we want to make assertions on. + */ + public FrameworkDTOAssert(FrameworkDTO actual) { + super(actual, FrameworkDTOAssert.class); + } + + /** + * An entry point for FrameworkDTOAssert to follow AssertJ standard + * assertThat() statements.
+ * With a static import, one can write directly: + * assertThat(myFrameworkDTO) and get specific assertion with + * code completion. + * + * @param actual the FrameworkDTO we want to make assertions on. + * @return a new {@link FrameworkDTOAssert} + */ + public static FrameworkDTOAssert assertThat(FrameworkDTO actual) { + return new FrameworkDTOAssert(actual); + } +} diff --git a/org.osgi.test.assertj.framework/src/main/java/org/osgi/test/assertj/servicereference/AbstractServiceReferenceDTOAssert.java b/org.osgi.test.assertj.framework/src/main/java/org/osgi/test/assertj/servicereference/AbstractServiceReferenceDTOAssert.java new file mode 100644 index 00000000..9e3050ff --- /dev/null +++ b/org.osgi.test.assertj.framework/src/main/java/org/osgi/test/assertj/servicereference/AbstractServiceReferenceDTOAssert.java @@ -0,0 +1,215 @@ +package org.osgi.test.assertj.servicereference; + +import org.assertj.core.api.AbstractObjectAssert; +import org.assertj.core.api.Assertions; +import org.osgi.framework.dto.ServiceReferenceDTO; + +/** + * Abstract base class for {@link ServiceReferenceDTO} specific assertions + */ + +public abstract class AbstractServiceReferenceDTOAssert, A extends ServiceReferenceDTO> + extends AbstractObjectAssert { + + /** + * Creates a new {@link AbstractServiceReferenceDTOAssert} to + * make assertions on actual ServiceReferenceDTO. + * + * @param actual the ServiceReferenceDTO we want to make assertions on. + */ + protected AbstractServiceReferenceDTOAssert(A actual, Class selfType) { + super(actual, selfType); + } + + /** + * Verifies that the actual ServiceReferenceDTO's bundle is equal to the + * given one. + * + * @param bundle the given bundle to compare the actual + * ServiceReferenceDTO's bundle to. + * @return this assertion object. + * @throws AssertionError - if the actual ServiceReferenceDTO's bundle is + * not equal to the given one. + */ + public S hasBundle(long bundle) { + // check that actual ServiceReferenceDTO we want to make assertions on + // is not null. + isNotNull(); + + // overrides the default error message with a more explicit one + String assertjErrorMessage = "\nExpecting bundle of:\n <%s>\nto be:\n <%s>\nbut was:\n <%s>"; + + // check + long actualBundle = actual.bundle; + if (actualBundle != bundle) { + failWithMessage(assertjErrorMessage, actual, bundle, actualBundle); + } + + // return the current assertion for method chaining + return myself; + } + + /** + * Verifies that the actual ServiceReferenceDTO's id is equal to the given + * one. + * + * @param id the given id to compare the actual ServiceReferenceDTO's id to. + * @return this assertion object. + * @throws AssertionError - if the actual ServiceReferenceDTO's id is not + * equal to the given one. + */ + public S hasId(long id) { + // check that actual ServiceReferenceDTO we want to make assertions on + // is not null. + isNotNull(); + + // overrides the default error message with a more explicit one + String assertjErrorMessage = "\nExpecting id of:\n <%s>\nto be:\n <%s>\nbut was:\n <%s>"; + + // check + long actualId = actual.id; + if (actualId != id) { + failWithMessage(assertjErrorMessage, actual, id, actualId); + } + + // return the current assertion for method chaining + return myself; + } + + /** + * Verifies that the actual ServiceReferenceDTO's properties is equal to the + * given one. + * + * @param properties the given properties to compare the actual + * ServiceReferenceDTO's properties to. + * @return this assertion object. + * @throws AssertionError - if the actual ServiceReferenceDTO's properties + * is not equal to the given one. + */ + public S hasProperties(java.util.Map properties) { + // check that actual ServiceReferenceDTO we want to make assertions on + // is not null. + isNotNull(); + + // overrides the default error message with a more explicit one + String assertjErrorMessage = "\nExpecting properties of:\n <%s>\nto be:\n <%s>\nbut was:\n <%s>"; + + // null safe check + java.util.Map actualProperties = actual.properties; + if (!java.util.Objects.deepEquals(actualProperties, properties)) { + failWithMessage(assertjErrorMessage, actual, properties, actualProperties); + } + + // return the current assertion for method chaining + return myself; + } + + /** + * Verifies that the actual ServiceReferenceDTO's usingBundles contains the + * given long elements. + * + * @param usingBundles the given elements that should be contained in actual + * ServiceReferenceDTO's usingBundles. + * @return this assertion object. + * @throws AssertionError if the actual ServiceReferenceDTO's usingBundles + * does not contain all given long elements. + */ + public S hasUsingBundles(long... usingBundles) { + // check that actual ServiceReferenceDTO we want to make assertions on + // is not null. + isNotNull(); + + // check that given long varargs is not null. + if (usingBundles == null) + failWithMessage("Expecting usingBundles parameter not to be null."); + + // check with standard error message (use overridingErrorMessage before + // contains to set your own message). + Assertions.assertThat(actual.usingBundles) + .contains(usingBundles); + + // return the current assertion for method chaining + return myself; + } + + /** + * Verifies that the actual ServiceReferenceDTO's usingBundles contains + * only the given long elements and nothing else in whatever order. + * + * @param usingBundles the given elements that should be contained in actual + * ServiceReferenceDTO's usingBundles. + * @return this assertion object. + * @throws AssertionError if the actual ServiceReferenceDTO's usingBundles + * does not contain all given long elements and nothing else. + */ + public S hasOnlyUsingBundles(long... usingBundles) { + // check that actual ServiceReferenceDTO we want to make assertions on + // is not null. + isNotNull(); + + // check that given long varargs is not null. + if (usingBundles == null) + failWithMessage("Expecting usingBundles parameter not to be null."); + + // check with standard error message (use overridingErrorMessage before + // contains to set your own message). + Assertions.assertThat(actual.usingBundles) + .containsOnly(usingBundles); + + // return the current assertion for method chaining + return myself; + } + + /** + * Verifies that the actual ServiceReferenceDTO's usingBundles does not + * contain the given long elements. + * + * @param usingBundles the given elements that should not be in actual + * ServiceReferenceDTO's usingBundles. + * @return this assertion object. + * @throws AssertionError if the actual ServiceReferenceDTO's usingBundles + * contains any given long elements. + */ + public S doesNotHaveUsingBundles(long... usingBundles) { + // check that actual ServiceReferenceDTO we want to make assertions on + // is not null. + isNotNull(); + + // check that given long varargs is not null. + if (usingBundles == null) + failWithMessage("Expecting usingBundles parameter not to be null."); + + // check with standard error message (use overridingErrorMessage before + // contains to set your own message). + Assertions.assertThat(actual.usingBundles) + .doesNotContain(usingBundles); + + // return the current assertion for method chaining + return myself; + } + + /** + * Verifies that the actual ServiceReferenceDTO has no usingBundles. + * + * @return this assertion object. + * @throws AssertionError if the actual ServiceReferenceDTO's usingBundles + * is not empty. + */ + public S hasNoUsingBundles() { + // check that actual ServiceReferenceDTO we want to make assertions on + // is not null. + isNotNull(); + + // we override the default error message with a more explicit one + String assertjErrorMessage = "\nExpecting :\n <%s>\nnot to have usingBundles but had :\n <%s>"; + + // check that it is not empty + if (actual.usingBundles.length > 0) { + failWithMessage(assertjErrorMessage, actual, java.util.Arrays.toString(actual.usingBundles)); + } + + // return the current assertion for method chaining + return myself; + } + +} diff --git a/org.osgi.test.assertj.framework/src/main/java/org/osgi/test/assertj/servicereference/ServiceReferenceDTOAssert.java b/org.osgi.test.assertj.framework/src/main/java/org/osgi/test/assertj/servicereference/ServiceReferenceDTOAssert.java new file mode 100644 index 00000000..95f339e1 --- /dev/null +++ b/org.osgi.test.assertj.framework/src/main/java/org/osgi/test/assertj/servicereference/ServiceReferenceDTOAssert.java @@ -0,0 +1,37 @@ +package org.osgi.test.assertj.servicereference; + +import org.osgi.framework.dto.ServiceReferenceDTO; + +/** + * {@link ServiceReferenceDTO} specific assertions Although this class is not + * final to allow Soft assertions proxy, if you wish to extend it, extend + * {@link AbstractServiceReferenceDTOAssert} instead. + */ + +public class ServiceReferenceDTOAssert + extends AbstractServiceReferenceDTOAssert { + + /** + * Creates a new {@link ServiceReferenceDTOAssert} to make + * assertions on actual ServiceReferenceDTO. + * + * @param actual the ServiceReferenceDTO we want to make assertions on. + */ + public ServiceReferenceDTOAssert(ServiceReferenceDTO actual) { + super(actual, ServiceReferenceDTOAssert.class); + } + + /** + * An entry point for ServiceReferenceDTOAssert to follow AssertJ standard + * assertThat() statements.
+ * With a static import, one can write directly: + * assertThat(myServiceReferenceDTO) and get specific assertion + * with code completion. + * + * @param actual the ServiceReferenceDTO we want to make assertions on. + * @return a new {@link ServiceReferenceDTOAssert} + */ + public static ServiceReferenceDTOAssert assertThat(ServiceReferenceDTO actual) { + return new ServiceReferenceDTOAssert(actual); + } +}