public AbstractSoftAssertions() { proxies = new SoftProxies(); }
private IterableSizeAssert<?> createIterableSizeAssertProxy(Object currentActual) { IterableSizeAssert<?> iterableSizeAssert = (IterableSizeAssert<?>) currentActual; // can't use the usual way of building soft proxy since IterableSizeAssert takes 2 parameters return proxies.createIterableSizeAssertProxy(iterableSizeAssert); }
private MapSizeAssert<?, ?> createMapSizeAssertProxy(Object currentActual) { MapSizeAssert<?, ?> mapSizeAssert = (MapSizeAssert<?, ?>) currentActual; // can't use the usual way of building soft proxy since MapSizeAssert takes 2 parameters return proxies.createMapSizeAssertProxy(mapSizeAssert); }
/** * Fails with the given message and with the {@link Throwable} that caused the failure. * * @param failureMessage error message. * @param realCause cause of the error. * @since 2.6.0 / 3.6.0 */ public void fail(String failureMessage, Throwable realCause) { AssertionError error = Failures.instance().failure(failureMessage); error.initCause(realCause); proxies.collectError(error); }
<V, T> V createSoftAssertionProxy(Class<V> assertClass, Class<T> actualClass, T actual) { try { Class<? extends V> proxyClass = createSoftAssertionProxyClass(assertClass); Constructor<? extends V> constructor = proxyClass.getConstructor(actualClass); V proxiedAssert = constructor.newInstance(actual); // instance is a AssertJProxySetup since it is a generated proxy implementing it (see createProxy) ((AssertJProxySetup) proxiedAssert).assertj$setup(new ProxifyMethodChangingTheObjectUnderTest(this), collector); return proxiedAssert; } catch (NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) { throw new RuntimeException(e); } }
public <T, V> V proxy(Class<V> assertClass, Class<T> actualClass, T actual) { return proxies.createSoftAssertionProxy(assertClass, actualClass, actual); }
/** * Returns a copy of list of soft assertions collected errors. * @return a copy of list of soft assertions collected errors. */ public List<Throwable> errorsCollected() { return decorateErrorsCollected(proxies.errorsCollected()); }
@SuppressWarnings("unchecked") private static <V> Class<? extends V> createSoftAssertionProxyClass(Class<V> assertClass) { SimpleKey cacheKey = new SimpleKey(assertClass); return (Class<V>) CACHE.findOrInsert(SoftProxies.class.getClassLoader(), cacheKey, () -> generateProxyClass(assertClass)); }
/** * Returns the result of last soft assertion which can be used to decide what the next one should be. * <p> * Example : * <pre><code class='java'> Person person = ... * SoftAssertions soft = new SoftAssertions(); * if (soft.assertThat(person.getAddress()).isNotNull().wasSuccess()) { * soft.assertThat(person.getAddress().getStreet()).isNotNull(); * }</code></pre> * * @return true if the last assertion was a success. */ public boolean wasSuccess() { return proxies.wasSuccess(); }
/** * Fails with the given message built like {@link String#format(String, Object...)}. * * @param failureMessage error message. * @param args Arguments referenced by the format specifiers in the format string. * @since 2.6.0 / 3.6.0 */ public void fail(String failureMessage, Object... args) { AssertionError error = Failures.instance().failure(String.format(failureMessage, args)); proxies.collectError(error); }
IterableSizeAssert<?> createIterableSizeAssertProxy(IterableSizeAssert<?> iterableSizeAssert) { Class<?> proxyClass = createSoftAssertionProxyClass(IterableSizeAssert.class); try { Constructor<?> constructor = proxyClass.getConstructor(AbstractIterableAssert.class, Integer.class); IterableSizeAssert<?> proxiedAssert = (IterableSizeAssert<?>) constructor.newInstance(iterableSizeAssert.returnToIterable(), iterableSizeAssert.actual); ((AssertJProxySetup) proxiedAssert).assertj$setup(new ProxifyMethodChangingTheObjectUnderTest(this), collector); return proxiedAssert; } catch (Exception e) { throw new RuntimeException(e); } }
public <T, V> V proxy(Class<V> assertClass, Class<T> actualClass, T actual) { return proxies.createSoftAssertionProxy(assertClass, actualClass, actual); }
@VisibleForTesting List<Throwable> getErrors() { return proxies.errorsCollected(); }
@SuppressWarnings("unchecked") private static <V> Class<? extends V> createSoftAssertionProxyClass(Class<V> assertClass) { SimpleKey cacheKey = new SimpleKey(assertClass); return (Class<V>) CACHE.findOrInsert(SoftProxies.class.getClassLoader(), cacheKey, () -> generateProxyClass(assertClass)); }
/** * Returns the result of last soft assertion which can be used to decide what the next one should be. * <p> * Example : * <pre><code class='java'> Person person = ... * SoftAssertions soft = new SoftAssertions(); * if (soft.assertThat(person.getAddress()).isNotNull().wasSuccess()) { * soft.assertThat(person.getAddress().getStreet()).isNotNull(); * }</code></pre> * * @return true if the last assertion was a success. */ public boolean wasSuccess() { return proxies.wasSuccess(); }
/** * Fails with the given message. * * @param failureMessage error message. * @since 2.6.0 / 3.6.0 */ public void fail(String failureMessage) { AssertionError error = Failures.instance().failure(failureMessage); proxies.collectError(error); }
MapSizeAssert<?, ?> createMapSizeAssertProxy(MapSizeAssert<?, ?> mapSizeAssert) { Class<?> proxyClass = createSoftAssertionProxyClass(MapSizeAssert.class); try { Constructor<?> constructor = proxyClass.getConstructor(AbstractMapAssert.class, Integer.class); MapSizeAssert<?, ?> proxiedAssert = (MapSizeAssert<?, ?>) constructor.newInstance(mapSizeAssert.returnToMap(), mapSizeAssert.actual); ((AssertJProxySetup) proxiedAssert).assertj$setup(new ProxifyMethodChangingTheObjectUnderTest(this), collector); return proxiedAssert; } catch (Exception e) { throw new RuntimeException(e); } }
@SuppressWarnings({ "unchecked", "rawtypes" }) private AbstractAssert createAssertProxy(Object currentActual) { if (currentActual instanceof IterableSizeAssert) return createIterableSizeAssertProxy(currentActual); if (currentActual instanceof MapSizeAssert) return createMapSizeAssertProxy(currentActual); return (AbstractAssert) proxies.createSoftAssertionProxy(currentActual.getClass(), actualClass(currentActual), actual(currentActual)); }
/** * Returns a copy of list of soft assertions collected errors. * @return a copy of list of soft assertions collected errors. */ public List<Throwable> errorsCollected() { return decorateErrorsCollected(proxies.errorsCollected()); }
private IterableSizeAssert<?> createIterableSizeAssertProxy(Object currentActual) { IterableSizeAssert<?> iterableSizeAssert = (IterableSizeAssert<?>) currentActual; // can't use the usual way of building soft proxy since IterableSizeAssert takes 2 parameters return proxies.createIterableSizeAssertProxy(iterableSizeAssert); }