Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[assertj] initial framework.dto #375

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
package org.osgi.test.assertj.bundle;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't forget the copyright messages for your final submission.

I think that this package should be org.osgi.test.assertj.dto.bundle, so that we are following the original class' package structure.


import org.assertj.core.api.AbstractObjectAssert;
import org.osgi.framework.dto.BundleDTO;

/**
* Abstract base class for {@link BundleDTO} specific assertions
*/

public abstract class AbstractBundleDTOAssert<S extends AbstractBundleDTOAssert<S, A>, A extends BundleDTO>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A lot of the assertion implementation here could be cut-and-pasted from AbstractBundleAssert, because they have the same fields. I think this would give a more consistent user experience rather than rolling our own (slightly different) ones from scratch. Maybe even think of a way that they could share code.

extends AbstractObjectAssert<S, A> {

/**
* Creates a new <code>{@link AbstractBundleDTOAssert}</code> to make
* assertions on actual BundleDTO.
*
* @param actual the BundleDTO we want to make assertions on.
*/
protected AbstractBundleDTOAssert(A actual, Class<S> 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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bitmapped field. Having the extra bitmap functionality that AbstractBundleAssert has would make for more user-friendly usage. This is especially the case for the failure message: the code as written will report something like:

Expected:
 <4>
but was:
 <2>

Whereas the AbstractBundleAssert implementation gives you something more human-readable like:

Expected:
 <4:RESOLVED>
but was:
 <2:INSTALLED>

// 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)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need a deep equals here as it's a string and not a complicated object with deep nested hierarchy. Regular equals would be fine (that's what BundleAssert does).

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)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

equals() is sufficient.

failWithMessage(assertjErrorMessage, actual, version, actualVersion);
}

// return the current assertion for method chaining
return myself;
}

}
Original file line number Diff line number Diff line change
@@ -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<BundleDTOAssert, BundleDTO> {

/**
* Creates a new <code>{@link BundleDTOAssert}</code> 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
* <code>assertThat()</code> statements.<br>
* With a static import, one can write directly:
* <code>assertThat(myBundleDTO)</code> and get specific assertion with code
* completion.
*
* @param actual the BundleDTO we want to make assertions on.
* @return a new <code>{@link BundleDTOAssert}</code>
*/
public static BundleDTOAssert assertThat(BundleDTO actual) {
return new BundleDTOAssert(actual);
}
}
Loading
Loading