Skip to content

Commit ce5df86

Browse files
committed
Implement hasValuesSatisfying on changes and table rows (#110)
1 parent ef4ce65 commit ce5df86

8 files changed

+133
-18
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,6 @@ public E doesNotHave(Condition<?> condition) {
590590
/** {@inheritDoc} */
591591
@Override
592592
public E satisfies(Condition<?> condition) {
593-
return AssertionsOnValueCondition.is(myself, info, value, condition);
593+
return AssertionsOnValueCondition.satisfies(myself, info, value, condition);
594594
}
595595
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,6 @@ public V doesNotHave(Condition<?> condition) {
608608
/** {@inheritDoc} */
609609
@Override
610610
public V satisfies(Condition<?> condition) {
611-
return AssertionsOnValueCondition.is(myself, info, value, condition);
611+
return AssertionsOnValueCondition.satisfies(myself, info, value, condition);
612612
}
613613
}

src/main/java/org/assertj/db/api/assertions/impl/AssertionsOnRowCondition.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import static org.assertj.db.error.ShouldBeCompatible.shouldBeCompatible;
2626
import static org.assertj.db.error.ShouldBeEqual.shouldBeEqual;
2727
import static org.assertj.db.error.ShouldHaveColumnsSize.shouldHaveColumnsSize;
28+
import static org.assertj.db.error.ShouldSatisfy.shouldSatisfy;
2829
import static org.assertj.db.util.Values.areEqual;
2930

3031
/**
@@ -63,7 +64,7 @@ public static <A extends AbstractAssert<?>> A hasValuesSatisfying(A assertion, W
6364
Condition<Object> condition = (Condition<Object>) object;
6465
if (!condition.matches(value.getValue())) {
6566
Object actual = Values.getRepresentationFromValueInFrontOfExpected(value, object);
66-
throw failures.failure(info, shouldBeEqual(index, actual, object));
67+
throw failures.failure(info, shouldSatisfy(index, actual, condition));
6768
}
6869
index++;
6970
continue;

src/main/java/org/assertj/db/api/assertions/impl/AssertionsOnValueCondition.java

+17
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,21 @@ public static <A extends AbstractAssert<?>> A isNot(A assertion, WritableAsserti
7171
return assertion;
7272
}
7373

74+
/**
75+
* Verifies that the value satisfies with condition.
76+
*
77+
* @param <A> The type of the assertion which call this method.
78+
* @param assertion The assertion which call this method.
79+
* @param info Writable information about an assertion.
80+
* @param value The value.
81+
* @param condition The condition to use for validation.
82+
* @return {@code this} assertion object.
83+
* @throws AssertionError If the value is not equal to the number in parameter.
84+
*/
85+
@SuppressWarnings("unchecked")
86+
public static <A extends AbstractAssert<?>> A satisfies(A assertion, WritableAssertionInfo info, Value value, Condition<?> condition) {
87+
conditions.assertSatisfies(info, value.getValue(), (Condition<? super Object>) condition);
88+
return assertion;
89+
}
90+
7491
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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 2015-2021 the original author or authors.
12+
*/
13+
package org.assertj.db.error;
14+
15+
import org.assertj.core.api.Condition;
16+
import org.assertj.core.error.BasicErrorMessageFactory;
17+
import org.assertj.core.error.ErrorMessageFactory;
18+
19+
/**
20+
* Creates an error message indicating that an assertion that verifies that a value does not satisfying condition.
21+
*
22+
* @author Julien Roy
23+
*
24+
*/
25+
public class ShouldSatisfy extends BasicErrorMessageFactory {
26+
27+
private static final String EXPECTED_MESSAGE_WITH_INDEX = "%nExpecting that the value at index %s:%n %s%nto satisfy: %n %s";
28+
29+
public static ErrorMessageFactory shouldSatisfy(int index, Object actual, Condition<?> condition) {
30+
return new ShouldSatisfy(index, actual, condition);
31+
}
32+
33+
private ShouldSatisfy(int index, Object actual, Condition<?> condition) {
34+
super(EXPECTED_MESSAGE_WITH_INDEX, index, actual, condition);
35+
}
36+
37+
}

src/test/java/org/assertj/db/api/assertions/AssertOnRowCondition_HasValuesSatisfying_Test.java

+41-12
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,31 @@ public void test_has_values() {
5757
"1950-09-21",
5858
"30B443AE-C0C9-4790-9BEC-CE1380808435"
5959
)
60-
.hasValues(4, "Murray", "Bill", "1950-09-21", UUID.fromString("30B443AE-C0C9-4790-9BEC-CE1380808435"));
60+
.hasValuesSatisfying(
61+
4,
62+
new Condition<String>(v -> v.equals("Murray"), "isMurray"),
63+
new HamcrestCondition<>(CoreMatchers.is("Bill")),
64+
"1950-09-21",
65+
UUID.fromString("30B443AE-C0C9-4790-9BEC-CE1380808435")
66+
);
6167
Assertions.assertThat(changeRowAssert).isSameAs(changeRowAssert2);
6268

6369
TableRowAssert tableRowAssert = assertThat(table).row();
6470
TableRowAssert tableRowAssert2 = tableRowAssert
65-
.hasValues(1, "Weaver", "Susan Alexandra", "1949-10-08", "30B443AE-C0C9-4790-9BEC-CE1380808435")
66-
.hasValues(1, "Weaver", "Susan Alexandra", "1949-10-08", UUID.fromString("30B443AE-C0C9-4790-9BEC-CE1380808435"));
71+
.hasValuesSatisfying(
72+
1,
73+
new Condition<String>(v -> v.equals("Weaver"), "isWeaver"),
74+
new HamcrestCondition<>(CoreMatchers.is("Susan Alexandra")),
75+
"1949-10-08",
76+
"30B443AE-C0C9-4790-9BEC-CE1380808435"
77+
)
78+
.hasValuesSatisfying(
79+
1,
80+
new Condition<String>(v -> v.equals("Weaver"), "isWeaver"),
81+
new HamcrestCondition<>(CoreMatchers.is("Susan Alexandra")),
82+
"1949-10-08",
83+
UUID.fromString("30B443AE-C0C9-4790-9BEC-CE1380808435")
84+
);
6785
Assertions.assertThat(tableRowAssert).isSameAs(tableRowAssert2);
6886
}
6987

@@ -80,24 +98,35 @@ public void should_fail_because_values_are_different() {
8098

8199
try {
82100
assertThat(changes).change().rowAtEndPoint()
83-
.hasValues(4, "Murray", "Billy", "1950-09-21", UUID.fromString("30B443AE-C0C9-4790-9BEC-CE1380808435"));
101+
.hasValuesSatisfying(
102+
4,
103+
"Murray",
104+
new Condition<String>(v -> v.equals("Billy"), "isBilly"),
105+
"1950-09-21",
106+
UUID.fromString("30B443AE-C0C9-4790-9BEC-CE1380808435")
107+
);
84108
} catch (AssertionError e) {
85109
Assertions.assertThat(e.getMessage()).isEqualTo(String.format(
86110
"[Row at end point of Change at index 0 (with primary key : [4]) of Changes on ACTOR table of 'sa/jdbc:h2:mem:test' source] %n"
87111
+ "Expecting that the value at index 2:%n"
88-
+ " <\"Bill\">%n"
89-
+ "to be equal to: %n"
90-
+ " <\"Billy\">"));
112+
+ " \"Bill\"%n"
113+
+ "to satisfy: %n"
114+
+ " isBilly"));
91115
}
92116
try {
93-
assertThat(table).row().hasValues(1, "Weaver", "Sigourney", "1949-10-08",
94-
UUID.fromString("648DFAC8-14AC-47F7-95CF-3475525A3BE3"));
117+
assertThat(table).row().hasValuesSatisfying(
118+
1,
119+
"Weaver",
120+
new Condition<String>(v -> v.equals("Sigourney"), "isSigourney"),
121+
"1949-10-08",
122+
UUID.fromString("648DFAC8-14AC-47F7-95CF-3475525A3BE3")
123+
);
95124
} catch (AssertionError e) {
96125
Assertions.assertThat(e.getMessage()).isEqualTo(String.format("[Row at index 0 of ACTOR table] %n"
97126
+ "Expecting that the value at index 2:%n"
98-
+ " <\"Susan Alexandra\">%n"
99-
+ "to be equal to: %n"
100-
+ " <\"Sigourney\">"));
127+
+ " \"Susan Alexandra\"%n"
128+
+ "to satisfy: %n"
129+
+ " isSigourney"));
101130
}
102131
}
103132
}

src/test/java/org/assertj/db/api/assertions/AssertOnValueCondition_Satisfies_Test.java

+4-2
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ public void should_fail_because_value_not_match_with_condition() {
8181
+ "Changes on TEST table of 'sa/jdbc:h2:mem:test' source] %n"
8282
+ "Expecting:%n"
8383
+ " 2%n"
84-
+ "to be isZero"));
84+
+ "to satisfy:%n"
85+
+ " isZero"));
8586
}
8687
try {
8788
assertThat(table).column("var3").value().satisfies(zero);
@@ -91,7 +92,8 @@ public void should_fail_because_value_not_match_with_condition() {
9192
.isEqualTo(String.format("[Value at index 0 of Column at index 2 (column name : VAR3) of TEST table] %n"
9293
+ "Expecting:%n"
9394
+ " 2%n"
94-
+ "to be isZero"));
95+
+ "to satisfy:%n"
96+
+ " isZero"));
9597
}
9698
}
9799
}

src/test/java/org/assertj/db/api/assertions/impl/AssertionsOnRowCondition_HasValues_Satisfying_Test.java

+30-1
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,39 @@ public void test_has_values_satisfying() throws Exception {
6868
}
6969

7070
/**
71-
* This method should fail because the values are different.
71+
* This method should fail because the values not satisfying.
7272
*/
7373
@Test
7474
public void should_fail_because_values_are_different() throws Exception {
75+
WritableAssertionInfo info = new WritableAssertionInfo();
76+
info.description("description");
77+
Table table = new Table();
78+
TableAssert tableAssert = assertThat(table);
79+
List<Value> list = new ArrayList<>(Arrays.asList(getValue(null, 1),
80+
getValue(null, "Weaver"),
81+
getValue(null, "Sigourney"),
82+
getValue(null, Date.valueOf("1949-10-08"))));
83+
try {
84+
AssertionsOnRowCondition.hasValuesSatisfying(tableAssert, info, list, 1,
85+
new Condition<String>(v -> v.equals("Weaverr"), "isWeaverr"),
86+
"Sigourney",
87+
"1949-10-08"
88+
);
89+
fail("An exception must be raised");
90+
} catch (AssertionError e) {
91+
Assertions.assertThat(e.getMessage()).isEqualTo(String.format("[description] %n"
92+
+ "Expecting that the value at index 1:%n"
93+
+ " \"Weaver\"%n"
94+
+ "to satisfy: %n"
95+
+ " isWeaverr"));
96+
}
97+
}
98+
99+
/**
100+
* This method should fail because the values are different.
101+
*/
102+
@Test
103+
public void should_fail_because_values_not_satisfying() throws Exception {
75104
WritableAssertionInfo info = new WritableAssertionInfo();
76105
info.description("description");
77106
Table table = new Table();

0 commit comments

Comments
 (0)