Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #238 : Can use extracting with SoftAssertions
Extracting was failing because the resulting Assert class was not a proxy collecting errors. This commit register a new method interceptor for cglib proxies: ProxifyExtractingResult. If the intercepted method name contains "extracting", a filter choose to use ProxifyExtractingResult. In all the other cases the interceptor used is the former ErrorCollector.
- Loading branch information
1 parent
fe0ddf0
commit d802115
Showing
14 changed files
with
442 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
66 changes: 66 additions & 0 deletions
66
src/main/java/org/assertj/core/api/ProxifyExtractingResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/** | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
* | ||
* Copyright 2012-2014 the original author or authors. | ||
*/ | ||
package org.assertj.core.api; | ||
|
||
import net.sf.cglib.proxy.MethodInterceptor; | ||
import net.sf.cglib.proxy.MethodProxy; | ||
|
||
import java.lang.reflect.Array; | ||
import java.lang.reflect.Method; | ||
import java.lang.reflect.ParameterizedType; | ||
import java.lang.reflect.Type; | ||
|
||
class ProxifyExtractingResult implements MethodInterceptor { | ||
|
||
private final SoftProxies proxies; | ||
|
||
ProxifyExtractingResult(SoftProxies proxies) { | ||
this.proxies = proxies; | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
@Override | ||
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { | ||
|
||
Object result = proxy.invokeSuper(obj, args); | ||
return proxies.create(result.getClass(), actualClass(result), actual(result)); | ||
} | ||
|
||
@SuppressWarnings("rawtypes") | ||
private static Class actualClass(Object result) { | ||
if (result instanceof ObjectArrayAssert) { | ||
return Array.newInstance(Object.class, 0).getClass(); | ||
} | ||
|
||
// Trying to create a proxy with cglib will only match exact constructor argument types. | ||
// To initialize one for ListAssert for example we can't use an ArrayList, we have to use a List. | ||
// So we can't just return actual.getClass() as we could read a concrete class whereas | ||
// *Assert classes define a constructor using interface (@see ListAssert for example). | ||
// | ||
// Instead we can read generic types from *Assert definition. | ||
// Inspecting: class ListAssert<T> extends AbstractListAssert<ListAssert<T>, List<? extends T>, T> | ||
// will return the generic defined by the super class AbstractListAssert at index 1, which is a List<? extends T> | ||
Type actualType = ((ParameterizedType) result.getClass().getGenericSuperclass()).getActualTypeArguments()[1]; | ||
if (actualType instanceof ParameterizedType) { | ||
return (Class<?>) ((ParameterizedType) actualType).getRawType(); | ||
} | ||
|
||
return (Class<?>) actualType; | ||
} | ||
|
||
private static Object actual(Object result) { | ||
if (result instanceof AbstractAssert) return ((AbstractAssert<?, ?>) result).actual; | ||
throw new IllegalStateException("We should be trying to make a proxy of an *Assert class."); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/** | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
* | ||
* Copyright 2012-2014 the original author or authors. | ||
*/ | ||
package org.assertj.core.api; | ||
|
||
import net.sf.cglib.proxy.Callback; | ||
import net.sf.cglib.proxy.CallbackFilter; | ||
import net.sf.cglib.proxy.Enhancer; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.List; | ||
|
||
import static org.assertj.core.util.Arrays.array; | ||
|
||
class SoftProxies { | ||
|
||
private final ErrorCollector collector = new ErrorCollector(); | ||
|
||
List<Throwable> errorsCollected() { | ||
return collector.errors(); | ||
} | ||
|
||
@SuppressWarnings("unchecked") | ||
<V, T> V create(Class<V> assertClass, Class<T> actualClass, T actual) { | ||
Enhancer enhancer = new Enhancer(); | ||
enhancer.setSuperclass(assertClass); | ||
enhancer.setCallbackFilter(CollectErrorsOrCreateExtractedProxy.FILTER); | ||
enhancer.setCallbacks(new Callback[] { collector, new ProxifyExtractingResult(this) }); | ||
return (V) enhancer.create(array(actualClass), array(actual)); | ||
} | ||
|
||
private enum CollectErrorsOrCreateExtractedProxy implements CallbackFilter { | ||
FILTER; | ||
|
||
private static final int ERROR_COLLECTOR_INDEX = 0; | ||
private static final int PROXIFY_EXTRACTING_INDEX = 1; | ||
|
||
public int accept(Method method) { | ||
return isExtractingMethod(method) ? PROXIFY_EXTRACTING_INDEX : ERROR_COLLECTOR_INDEX; | ||
} | ||
|
||
private boolean isExtractingMethod(Method method) { | ||
return method.getName().toLowerCase().contains("extracting"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.