Skip to content

Commit 88d64e9

Browse files
committed
Implement condition assert on values (#57, #24)
1 parent d2a72ce commit 88d64e9

11 files changed

+878
-22
lines changed

src/main/java/org/assertj/db/api/AbstractAssertWithValues.java

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313
package org.assertj.db.api;
1414

15+
import org.assertj.core.api.Condition;
1516
import org.assertj.db.api.assertions.*;
1617
import org.assertj.db.api.assertions.impl.*;
1718
import org.assertj.db.navigation.element.ValueElement;
@@ -29,18 +30,20 @@
2930
* @param <O> The type of the assertion class of {@link org.assertj.db.navigation.origin.Origin}.
3031
* @author Régis Pouiller
3132
* @author Otoniel Isidoro
33+
* @author Julien Roy
3234
*/
33-
public abstract class AbstractAssertWithValues <E extends AbstractAssertWithValues<E, O>, O extends OriginWithColumnsAndRowsFromChange<ChangesAssert, ChangeAssert, ChangeColumnAssert, ChangeRowAssert>>
34-
extends AbstractAssertWithOriginWithColumnsAndRowsFromChange<E, O>
35-
implements ValueElement,
36-
AssertOnValueClass<E>,
37-
AssertOnValueType<E>,
38-
AssertOnValueNullity<E>,
39-
AssertOnValueEquality<E>,
40-
AssertOnValueInequality<E>,
41-
AssertOnValueComparison<E>,
42-
AssertOnValueChronology<E>,
43-
AssertOnValueCloseness<E> {
35+
public abstract class AbstractAssertWithValues<E extends AbstractAssertWithValues<E, O>, O extends OriginWithColumnsAndRowsFromChange<ChangesAssert, ChangeAssert, ChangeColumnAssert, ChangeRowAssert>>
36+
extends AbstractAssertWithOriginWithColumnsAndRowsFromChange<E, O>
37+
implements ValueElement,
38+
AssertOnValueClass<E>,
39+
AssertOnValueType<E>,
40+
AssertOnValueNullity<E>,
41+
AssertOnValueEquality<E>,
42+
AssertOnValueInequality<E>,
43+
AssertOnValueComparison<E>,
44+
AssertOnValueChronology<E>,
45+
AssertOnValueCloseness<E>,
46+
AssertOnValueCondition<E> {
4447

4548
/**
4649
* The actual value on which the assertion is.
@@ -448,4 +451,34 @@ public E isCloseTo(DateTimeValue expected, TimeValue tolerance) {
448451
public E isCloseTo(DateTimeValue expected, DateTimeValue tolerance) {
449452
return AssertionsOnValueCloseness.isCloseTo(myself, info, value, expected, tolerance);
450453
}
454+
455+
/** {@inheritDoc} */
456+
@Override
457+
public E is(Condition<?> condition) {
458+
return AssertionsOnValueCondition.is(myself, info, value, condition);
459+
}
460+
461+
/** {@inheritDoc} */
462+
@Override
463+
public E isNot(Condition<?> condition) {
464+
return AssertionsOnValueCondition.isNot(myself, info, value, condition);
465+
}
466+
467+
/** {@inheritDoc} */
468+
@Override
469+
public E has(Condition<?> condition) {
470+
return AssertionsOnValueCondition.is(myself, info, value, condition);
471+
}
472+
473+
/** {@inheritDoc} */
474+
@Override
475+
public E doesNotHave(Condition<?> condition) {
476+
return AssertionsOnValueCondition.isNot(myself, info, value, condition);
477+
}
478+
479+
/** {@inheritDoc} */
480+
@Override
481+
public E satisfies(Condition<?> condition) {
482+
return AssertionsOnValueCondition.is(myself, info, value, condition);
483+
}
451484
}

src/main/java/org/assertj/db/api/AbstractValueAssert.java

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
*/
1313
package org.assertj.db.api;
1414

15+
import org.assertj.core.api.Condition;
1516
import org.assertj.db.api.assertions.*;
1617
import org.assertj.db.api.assertions.impl.*;
1718
import org.assertj.db.navigation.ToValue;
@@ -25,6 +26,7 @@
2526
*
2627
* @author Régis Pouiller
2728
* @author Otoniel Isidoro
29+
* @author Julien Roy
2830
*
2931
* @param <D> The class of the actual value (an sub-class of {@link AbstractDbData}).
3032
* @param <A> The class of the original assertion (an sub-class of {@link AbstractDbAssert}).
@@ -37,17 +39,18 @@
3739
* @param <RV> The class of the equivalent row assertion on the value (an sub-class of {@link AbstractRowValueAssert}).
3840
*/
3941
public abstract class AbstractValueAssert<D extends AbstractDbData<D>, A extends AbstractDbAssert<D, A, C, CV, R, RV>, S extends AbstractSubAssert<D, A, S, V, C, CV, R, RV>, V extends AbstractValueAssert<D, A, S, V, C, CV, R, RV>, C extends AbstractColumnAssert<D, A, C, CV, R, RV>, CV extends AbstractColumnValueAssert<D, A, C, CV, R, RV>, R extends AbstractRowAssert<D, A, C, CV, R, RV>, RV extends AbstractRowValueAssert<D, A, C, CV, R, RV>>
40-
extends AbstractAssertWithOriginWithColumnsAndRows<V, S, D, A, C, CV, R, RV>
41-
implements ValueElement,
42-
ToValue<V>,
43-
AssertOnValueClass<V>,
44-
AssertOnValueType<V>,
45-
AssertOnValueNullity<V>,
46-
AssertOnValueEquality<V>,
47-
AssertOnValueInequality<V>,
48-
AssertOnValueChronology<V>,
49-
AssertOnValueComparison<V>,
50-
AssertOnValueCloseness<V> {
42+
extends AbstractAssertWithOriginWithColumnsAndRows<V, S, D, A, C, CV, R, RV>
43+
implements ValueElement,
44+
ToValue<V>,
45+
AssertOnValueClass<V>,
46+
AssertOnValueType<V>,
47+
AssertOnValueNullity<V>,
48+
AssertOnValueEquality<V>,
49+
AssertOnValueInequality<V>,
50+
AssertOnValueChronology<V>,
51+
AssertOnValueComparison<V>,
52+
AssertOnValueCloseness<V>,
53+
AssertOnValueCondition<V> {
5154

5255
/**
5356
* The actual value on which this assertion is.
@@ -466,4 +469,34 @@ public V isCloseTo(DateTimeValue expected, TimeValue tolerance) {
466469
public V isCloseTo(DateTimeValue expected, DateTimeValue tolerance) {
467470
return AssertionsOnValueCloseness.isCloseTo(myself, info, value, expected, tolerance);
468471
}
472+
473+
/** {@inheritDoc} */
474+
@Override
475+
public V is(Condition<?> condition) {
476+
return AssertionsOnValueCondition.is(myself, info, value, condition);
477+
}
478+
479+
/** {@inheritDoc} */
480+
@Override
481+
public V isNot(Condition<?> condition) {
482+
return AssertionsOnValueCondition.isNot(myself, info, value, condition);
483+
}
484+
485+
/** {@inheritDoc} */
486+
@Override
487+
public V has(Condition<?> condition) {
488+
return AssertionsOnValueCondition.is(myself, info, value, condition);
489+
}
490+
491+
/** {@inheritDoc} */
492+
@Override
493+
public V doesNotHave(Condition<?> condition) {
494+
return AssertionsOnValueCondition.isNot(myself, info, value, condition);
495+
}
496+
497+
/** {@inheritDoc} */
498+
@Override
499+
public V satisfies(Condition<?> condition) {
500+
return AssertionsOnValueCondition.is(myself, info, value, condition);
501+
}
469502
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/**
2+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
3+
* the License. You may obtain a copy of the License at
4+
*
5+
* http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9+
* specific language governing permissions and limitations under the License.
10+
*
11+
* Copyright 2012-2016 the original author or authors.
12+
*/
13+
package org.assertj.db.api.assertions;
14+
15+
import org.assertj.core.api.Condition;
16+
17+
/**
18+
* Defines the assertion methods on the matching with condition.
19+
*
20+
* @param <T> The "self" type of this assertion class. Please read &quot;<a href="http://bit.ly/1IZIRcY"
21+
* target="_blank">Emulating 'self types' using Java Generics to simplify fluent API implementation</a>&quot;
22+
* for more details.
23+
* @author Julien Roy
24+
*/
25+
public interface AssertOnValueCondition<T extends AssertOnValueCondition<T>> {
26+
27+
/**
28+
* Verifies that the actual value satisfies the given condition.
29+
*
30+
* @param condition the given condition.
31+
* @return {@code this} assertion object.
32+
* @throws NullPointerException if the given condition is {@code null}.
33+
* @throws AssertionError if the actual value does not satisfy the given condition.
34+
* @see org.assertj.db.api.AbstractValueAssert#is(Condition)
35+
* @see org.assertj.db.api.AbstractAssertWithValues#is(Condition)
36+
*/
37+
T is(Condition<?> condition);
38+
39+
/**
40+
* Verifies that the actual value does not satisfy the given condition.
41+
*
42+
* @param condition the given condition.
43+
* @return {@code this} assertion object.
44+
* @throws NullPointerException if the given condition is {@code null}.
45+
* @throws AssertionError if the actual value satisfies the given condition.
46+
* @see org.assertj.db.api.AbstractValueAssert#isNot(Condition)
47+
* @see org.assertj.db.api.AbstractAssertWithValues#isNot(Condition)
48+
*/
49+
T isNot(Condition<?> condition);
50+
51+
/**
52+
* Verifies that the actual value satisfies the given condition. This method is an alias for <code>{@link #is(Condition)}</code>.
53+
*
54+
* @param condition the given condition.
55+
* @return {@code this} assertion object.
56+
* @throws NullPointerException if the given condition is {@code null}.
57+
* @throws AssertionError if the actual value does not satisfy the given condition.
58+
* @see org.assertj.db.api.AbstractValueAssert#has(Condition)
59+
* @see org.assertj.db.api.AbstractAssertWithValues#has(Condition)
60+
*/
61+
T has(Condition<?> condition);
62+
63+
/**
64+
* Verifies that the actual value does not satisfy the given condition. This method is an alias for <code>{@link #isNot(Condition)}</code>.
65+
*
66+
* @param condition the given condition.
67+
* @return {@code this} assertion object.
68+
* @throws NullPointerException if the given condition is {@code null}.
69+
* @throws AssertionError if the actual value satisfies the given condition.
70+
* @see org.assertj.db.api.AbstractValueAssert#doesNotHave(Condition)
71+
* @see org.assertj.db.api.AbstractAssertWithValues#doesNotHave(Condition)
72+
*/
73+
T doesNotHave(Condition<?> condition);
74+
75+
/**
76+
* Verifies that the actual value satisfies the given condition. This method is an alias for <code>{@link #is(Condition)}</code>.
77+
*
78+
* @param condition the given condition.
79+
* @return {@code this} assertion object.
80+
* @throws NullPointerException if the given condition is {@code null}.
81+
* @throws AssertionError if the actual value does not satisfy the given condition.
82+
* @see org.assertj.db.api.AbstractValueAssert#satisfies(Condition)
83+
* @see org.assertj.db.api.AbstractAssertWithValues#satisfies(Condition)
84+
*/
85+
T satisfies(Condition<?> condition);
86+
87+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/**
2+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
3+
* the License. You may obtain a copy of the License at
4+
*
5+
* http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
8+
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
9+
* specific language governing permissions and limitations under the License.
10+
*
11+
* Copyright 2012-2016 the original author or authors.
12+
*/
13+
package org.assertj.db.api.assertions.impl;
14+
15+
import org.assertj.core.api.Condition;
16+
import org.assertj.core.api.WritableAssertionInfo;
17+
import org.assertj.core.internal.Conditions;
18+
import org.assertj.db.api.AbstractAssert;
19+
import org.assertj.db.type.Value;
20+
21+
/**
22+
* Implements the assertion methods on the matching with condition.
23+
*
24+
* @author Julien Roy
25+
*/
26+
public class AssertionsOnValueCondition {
27+
28+
/**
29+
* Assertions for {@code Object}s provided by assertj-core.
30+
*/
31+
private static final Conditions conditions = Conditions.instance();
32+
33+
/**
34+
* Private constructor.
35+
*/
36+
private AssertionsOnValueCondition() {
37+
// Empty
38+
}
39+
40+
/**
41+
* Verifies that the value match with condition.
42+
*
43+
* @param <A> The type of the assertion which call this method.
44+
* @param assertion The assertion which call this method.
45+
* @param info Writable information about an assertion.
46+
* @param value The value.
47+
* @param condition The condition to use for validation.
48+
* @return {@code this} assertion object.
49+
* @throws AssertionError If the value is not equal to the number in parameter.
50+
*/
51+
public static <A extends AbstractAssert<?>> A is(A assertion, WritableAssertionInfo info, Value value, Condition<?> condition) {
52+
conditions.assertIs(info, value.getValue(), (Condition<? super Object>) condition);
53+
return assertion;
54+
}
55+
56+
/**
57+
* Verifies that the value not match with condition.
58+
*
59+
* @param <A> The type of the assertion which call this method.
60+
* @param assertion The assertion which call this method.
61+
* @param info Writable information about an assertion.
62+
* @param value The value.
63+
* @param condition The condition to use for validation.
64+
* @return {@code this} assertion object.
65+
* @throws AssertionError If the value is not equal to the number in parameter.
66+
*/
67+
public static <A extends AbstractAssert<?>> A isNot(A assertion, WritableAssertionInfo info, Value value, Condition<?> condition) {
68+
conditions.assertIsNot(info, value.getValue(), (Condition<? super Object>) condition);
69+
return assertion;
70+
}
71+
72+
}

0 commit comments

Comments
 (0)