Skip to content

Commit 1190b02

Browse files
committed
Fix compatibility issue with AssertJ > 3.9.1
1 parent 1cc41b3 commit 1190b02

File tree

8 files changed

+373
-226
lines changed

8 files changed

+373
-226
lines changed

pom.xml

Lines changed: 243 additions & 174 deletions
Large diffs are not rendered by default.

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

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

15-
import org.assertj.core.internal.Failures;
1615
import org.assertj.core.util.Lists;
1716

1817
import java.util.List;
@@ -30,31 +29,6 @@ public <T, V> V proxy(Class<V> assertClass, Class<T> actualClass, T actual) {
3029
return this.proxies.create(assertClass, actualClass, actual);
3130
}
3231

33-
public void fail(String failureMessage) {
34-
AssertionError error = Failures.instance().failure(failureMessage);
35-
this.proxies.collectError(error);
36-
}
37-
38-
public void fail(String failureMessage, Object... args) {
39-
AssertionError error = Failures.instance().failure(String.format(failureMessage, args));
40-
this.proxies.collectError(error);
41-
}
42-
43-
public void fail(String failureMessage, Throwable realCause) {
44-
AssertionError error = Failures.instance().failure(failureMessage);
45-
error.initCause(realCause);
46-
this.proxies.collectError(error);
47-
}
48-
49-
public void failBecauseExceptionWasNotThrown(Class<? extends Throwable> throwableClass) {
50-
this.shouldHaveThrown(throwableClass);
51-
}
52-
53-
public void shouldHaveThrown(Class<? extends Throwable> throwableClass) {
54-
AssertionError error = Failures.instance().expectedThrowableNotThrown(throwableClass);
55-
this.proxies.collectError(error);
56-
}
57-
5832
public List<Throwable> errorsCollected() {
5933
return Lists.newArrayList(this.proxies.errorsCollected());
6034
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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;
14+
15+
import net.sf.cglib.proxy.MethodInterceptor;
16+
import net.sf.cglib.proxy.MethodProxy;
17+
18+
import java.lang.reflect.Method;
19+
import java.util.ArrayList;
20+
import java.util.Collections;
21+
import java.util.List;
22+
23+
/** Collects error messages of all AssertionErrors thrown by the proxied method. */
24+
class ErrorCollector implements MethodInterceptor {
25+
26+
private static final String INTERCEPT_METHOD_NAME = "intercept";
27+
28+
private static final String CLASS_NAME = ErrorCollector.class.getName();
29+
30+
// scope : the current soft-assertion object
31+
private final List<Throwable> errors = new ArrayList<>();
32+
// scope : the last assertion call (might be nested)
33+
private final LastResult lastResult = new LastResult();
34+
35+
@Override
36+
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
37+
Object result = obj;
38+
try {
39+
result = proxy.invokeSuper(obj, args);
40+
lastResult.setSuccess(true);
41+
} catch (AssertionError e) {
42+
if (isNestedErrorCollectorProxyCall()) {
43+
// let the most outer call handle the assertion error
44+
throw e;
45+
}
46+
addError(e);
47+
}
48+
return result;
49+
}
50+
51+
public void addError(Throwable error) {
52+
errors.add(error);
53+
lastResult.recordError();
54+
}
55+
56+
public List<Throwable> errors() {
57+
return Collections.unmodifiableList(errors);
58+
}
59+
60+
public boolean wasSuccess() {
61+
return lastResult.wasSuccess();
62+
}
63+
64+
private boolean isNestedErrorCollectorProxyCall() {
65+
return countErrorCollectorProxyCalls() > 1;
66+
}
67+
68+
private static int countErrorCollectorProxyCalls() {
69+
int nbCalls = 0;
70+
for (StackTraceElement stackTraceElement : Thread.currentThread().getStackTrace()) {
71+
if (CLASS_NAME.equals(stackTraceElement.getClassName())
72+
&& INTERCEPT_METHOD_NAME.equals(stackTraceElement.getMethodName()))
73+
nbCalls++;
74+
}
75+
return nbCalls;
76+
}
77+
78+
private static class LastResult {
79+
private boolean wasSuccess = true;
80+
private boolean errorFound = false;
81+
82+
private boolean wasSuccess() {
83+
return wasSuccess;
84+
}
85+
86+
private void recordError() {
87+
errorFound = true;
88+
wasSuccess = false;
89+
}
90+
91+
private void setSuccess(boolean success) {
92+
93+
// errorFound must be true if any nested call ends up in error
94+
// Nested call Example : softly.assertThat(true).isFalse()
95+
// call chain :
96+
// -- softly.assertThat(true).isFalse()
97+
// ----- proxied isFalse() -> calls isEqualTo(false) which is proxied
98+
// ------- proxied isEqualTo(false) : catch AssertionError => last result success = false, back to outer call
99+
// ---- proxied isFalse() : no AssertionError caught => last result success = true
100+
// The overall last result success should not be true as one of the nested calls was not a success.
101+
errorFound |= !success;
102+
103+
if (resolvingOutermostErrorCollectorProxyNestedCall()) {
104+
// we are resolving the last nested call (if any), we can set a relevant value for wasSuccess
105+
wasSuccess = !errorFound;
106+
// need to reset errorFound for the next soft assertion
107+
errorFound = false;
108+
}
109+
}
110+
111+
private boolean resolvingOutermostErrorCollectorProxyNestedCall() {
112+
return countErrorCollectorProxyCalls() == 1;
113+
}
114+
}
115+
}

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

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

15-
import org.assertj.core.api.ObjectArrayAssert;
16-
import org.assertj.core.internal.cglib.proxy.MethodInterceptor;
17-
import org.assertj.core.internal.cglib.proxy.MethodProxy;
15+
import net.sf.cglib.proxy.MethodInterceptor;
16+
import net.sf.cglib.proxy.MethodProxy;
1817
import org.assertj.core.util.Arrays;
1918
import org.assertj.db.type.Change;
2019
import org.assertj.db.type.Column;
2120
import org.assertj.db.type.Row;
2221
import org.assertj.db.type.Value;
2322

24-
import java.lang.reflect.Array;
2523
import java.lang.reflect.Method;
26-
import java.lang.reflect.ParameterizedType;
27-
import java.lang.reflect.Type;
2824

2925
import static org.assertj.db.util.Proxies.isProxified;
3026
import static org.assertj.db.util.Proxies.unProxy;
@@ -44,7 +40,7 @@ class ProxifyPositionResult implements MethodInterceptor {
4440

4541
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
4642
Object result = proxy.invokeSuper(obj, args);
47-
if (isProxified(result.getClass()) || actual(result) == null) {
43+
if (isProxified(result.getClass()) || Arrays.isNullOrEmpty(actual(result))) {
4844
return result;
4945
}
5046
return this.proxies.create(result.getClass(), actualClass(result), actual(result));
@@ -79,14 +75,8 @@ private static Class[] actualClass(Object result) {
7975
Value.class,
8076
Value.class
8177
);
82-
} else if (result instanceof ObjectArrayAssert) {
83-
return Arrays.array(Array.newInstance(Object.class, 0).getClass());
8478
} else {
85-
Type actualType = ((ParameterizedType) result.getClass().getGenericSuperclass()).getActualTypeArguments()[1];
86-
Class type = actualType instanceof ParameterizedType ?
87-
(Class) ((ParameterizedType) actualType).getRawType() :
88-
(Class) actualType;
89-
return Arrays.array(type);
79+
return Arrays.array();
9080
}
9181
}
9282

@@ -119,7 +109,7 @@ private static Object[] actual(Object result) {
119109
((ChangeColumnAssert) result).valueAtEndPoint
120110
);
121111
} else {
122-
return null;
112+
return Arrays.array();
123113
}
124114
}
125115
}

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,9 @@
1212
*/
1313
package org.assertj.db.api;
1414

15-
import org.assertj.core.api.ErrorCollector;
16-
import org.assertj.core.internal.cglib.proxy.Callback;
17-
import org.assertj.core.internal.cglib.proxy.CallbackFilter;
18-
import org.assertj.core.internal.cglib.proxy.Enhancer;
15+
import net.sf.cglib.proxy.Callback;
16+
import net.sf.cglib.proxy.CallbackFilter;
17+
import net.sf.cglib.proxy.Enhancer;
1918
import org.assertj.core.util.Arrays;
2019

2120
import java.lang.reflect.Method;
@@ -32,10 +31,6 @@ class SoftProxies {
3231
SoftProxies() {
3332
}
3433

35-
void collectError(Throwable error) {
36-
this.collector.addError(error);
37-
}
38-
3934
List<Throwable> errorsCollected() {
4035
return this.collector.errors();
4136
}

src/main/java/org/assertj/db/util/Proxies.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
*/
1313
package org.assertj.db.util;
1414

15-
import org.assertj.core.internal.cglib.proxy.Enhancer;
15+
import net.sf.cglib.proxy.Enhancer;
1616

1717
/**
1818
* Utilities for manage proxies.

src/test/java/org/assertj/db/api/SoftAssertions_Test.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public void test_soft_assert_table() {
5151
.returnToColumn()
5252
.column("var1").value().isEqualTo(0);
5353

54+
assertThat(softly.wasSuccess()).isFalse();
5455
assertThat(softly.errorsCollected()).hasSize(8);
5556

5657
assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {
@@ -79,6 +80,7 @@ public void test_soft_assert_request() {
7980
.returnToColumn()
8081
.column("var1").value().isEqualTo(0);
8182

83+
assertThat(softly.wasSuccess()).isFalse();
8284
assertThat(softly.errorsCollected()).hasSize(8);
8385

8486
assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {
@@ -102,6 +104,8 @@ public void test_soft_assert_changes() {
102104
final SoftAssertions softly = new SoftAssertions();
103105
softly.assertThat(changes).change().column("var1").hasValues(0);
104106
softly.assertThat(changes).change().rowAtStartPoint().changeOfModification().column("var1").hasValues(0);
107+
108+
assertThat(softly.wasSuccess()).isFalse();
105109
assertThat(softly.errorsCollected()).hasSize(2);
106110

107111
assertThatThrownBy(new ThrowableAssert.ThrowingCallable() {

src/test/java/org/assertj/db/util/Proxies_Test.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
*/
1313
package org.assertj.db.util;
1414

15-
import org.assertj.core.internal.cglib.proxy.Enhancer;
16-
import org.assertj.core.internal.cglib.proxy.NoOp;
15+
import net.sf.cglib.proxy.Enhancer;
16+
import net.sf.cglib.proxy.NoOp;
1717
import org.junit.Test;
1818

1919
import static org.assertj.core.api.Assertions.assertThat;

0 commit comments

Comments
 (0)