public static AssertionError assertionError(String message, Exception e) { return (AssertionError) new AssertionError(message).initCause(e); }
/** * Fails a test with the given message and wrapping the original exception. * * @param message the assertion error message * @param realCause the original exception */ static public void fail(String message, Throwable realCause) { AssertionError ae = new AssertionError(message); ae.initCause(realCause); throw ae; }
/** * Fails a test with the given message and wrapping the original exception. * * @param message the assertion error message * @param realCause the original exception */ public static void fail(String message, Throwable realCause) { AssertionError ae = new AssertionError(message); ae.initCause(realCause); throw ae; }
/** * Adds a Throwable to the table. Execution continues, but the test will fail at the end. */ public void addError(Throwable error) { if (error == null) { throw new NullPointerException("Error cannot be null"); } if (error instanceof AssumptionViolatedException) { AssertionError e = new AssertionError(error.getMessage()); e.initCause(error); errors.add(e); } else { errors.add(error); } }
/** Alternative to AssertionError(String, Throwable), which doesn't exist in Java 1.6 */ private static AssertionError newAssertionError(String message, Throwable cause) { AssertionError e = new AssertionError(message); e.initCause(cause); return e; } }
/** Alternative to AssertionError(String, Throwable), which doesn't exist in GWT 2.6.1. */ private static AssertionError newAssertionError(String message, Throwable cause) { AssertionError e = new AssertionError(message); e.initCause(cause); return e; }
AssertionError error = new AssertionFailedError("Serialization failed on return value of " + factory); error.initCause(e.getCause()); throw error; } catch (AssertionFailedError e) { new AssertionFailedError( "Return value of " + factory + " reserialized to an unequal value"); error.initCause(e); throw error;
public static void assertError(List<Throwable> list, int index, Class<? extends Throwable> clazz) { Throwable ex = list.get(index); if (!clazz.isInstance(ex)) { AssertionError err = new AssertionError(clazz + " expected but got " + list.get(index)); err.initCause(list.get(index)); throw err; } }
public static void assertUndeliverable(List<Throwable> list, int index, Class<? extends Throwable> clazz) { Throwable ex = list.get(index); if (!(ex instanceof UndeliverableException)) { AssertionError err = new AssertionError("Outer exception UndeliverableException expected but got " + list.get(index)); err.initCause(list.get(index)); throw err; } ex = ex.getCause(); if (!clazz.isInstance(ex)) { AssertionError err = new AssertionError("Inner exception " + clazz + " expected but got " + list.get(index)); err.initCause(list.get(index)); throw err; } }
/** * Runs serialization test on the return values of the static methods. * * <p>Test fails if default value cannot be determined for a constructor or factory method * parameter, or if the constructor or factory method throws exception. * * @return this tester */ public FactoryMethodReturnValueTester testSerializable() throws Exception { for (Invokable<?, ?> factory : getFactoriesToTest()) { Object instance = instantiate(factory); if (instance != null) { try { SerializableTester.reserialize(instance); } catch (RuntimeException e) { AssertionError error = new AssertionFailedError("Serialization failed on return value of " + factory); error.initCause(e.getCause()); throw error; } } } return this; }
public static void assertError(List<Throwable> list, int index, Class<? extends Throwable> clazz, String message) { Throwable ex = list.get(index); if (!clazz.isInstance(ex)) { AssertionError err = new AssertionError("Type " + clazz + " expected but got " + ex); err.initCause(ex); throw err; } if (!ObjectHelper.equals(message, ex.getMessage())) { AssertionError err = new AssertionError("Message " + message + " expected but got " + ex.getMessage()); err.initCause(ex); throw err; } }
/** * Tests null checks against the instance methods of the return values, if any. * * <p>Test fails if default value cannot be determined for a constructor or factory method * parameter, or if the constructor or factory method throws exception. * * @return this tester */ public FactoryMethodReturnValueTester testNulls() throws Exception { for (Invokable<?, ?> factory : getFactoriesToTest()) { Object instance = instantiate(factory); if (instance != null && packagesToTest.contains(Reflection.getPackageName(instance.getClass()))) { try { nullPointerTester.testAllPublicInstanceMethods(instance); } catch (AssertionError e) { AssertionError error = new AssertionFailedError("Null check failed on return value of " + factory); error.initCause(e); throw error; } } } return this; }
public static void assertUndeliverable(List<Throwable> list, int index, Class<? extends Throwable> clazz, String message) { Throwable ex = list.get(index); if (!(ex instanceof UndeliverableException)) { AssertionError err = new AssertionError("Outer exception UndeliverableException expected but got " + list.get(index)); err.initCause(list.get(index)); throw err; } ex = ex.getCause(); if (!clazz.isInstance(ex)) { AssertionError err = new AssertionError("Inner exception " + clazz + " expected but got " + list.get(index)); err.initCause(list.get(index)); throw err; } if (!ObjectHelper.equals(message, ex.getMessage())) { AssertionError err = new AssertionError("Message " + message + " expected but got " + ex.getMessage()); err.initCause(ex); throw err; } }
/** * Throws an {@link AssertionError} with the given message and with the {@link Throwable} that caused the failure. * * @param <T> dummy return value type * @param failureMessage the description of the failed assertion. It can be {@code null}. * @param realCause cause of the error. * @return nothing, it's just to be used in doSomething(optional.orElse(() -> fail("boom", cause)));. * @throws AssertionError with the given message and with the {@link Throwable} that caused the failure. */ public static <T> T fail(String failureMessage, Throwable realCause) { AssertionError error = Failures.instance().failure(failureMessage); error.initCause(realCause); throw error; }
/** * Validates that the given class, when forcefully instantiated throws * an IllegalArgumentException("No instances!") exception. * @param clazz the class to test, not null */ public static void checkUtilityClass(Class<?> clazz) { try { Constructor<?> c = clazz.getDeclaredConstructor(); c.setAccessible(true); try { c.newInstance(); fail("Should have thrown InvocationTargetException(IllegalStateException)"); } catch (InvocationTargetException ex) { assertEquals("No instances!", ex.getCause().getMessage()); } } catch (Exception ex) { AssertionError ae = new AssertionError(ex.toString()); ae.initCause(ex); throw ae; } }
/** * Returns the URLs in the class path specified by the {@code java.class.path} {@linkplain * System#getProperty system property}. */ // TODO(b/65488446): Make this a public API. private static ImmutableList<URL> parseJavaClassPath() { ImmutableList.Builder<URL> urls = ImmutableList.builder(); for (String entry : Splitter.on(PATH_SEPARATOR.value()).split(JAVA_CLASS_PATH.value())) { try { try { urls.add(new File(entry).toURI().toURL()); } catch (SecurityException e) { // File.toURI checks to see if the file is a directory urls.add(new URL("file", null, new File(entry).getAbsolutePath())); } } catch (MalformedURLException e) { AssertionError error = new AssertionError("malformed class path entry: " + entry); error.initCause(e); throw error; } } return urls.build(); }
/** * Returns the URLs in the class path specified by the {@code java.class.path} {@linkplain * System#getProperty system property}. */ // TODO(b/65488446): Make this a public API. static URL[] parseJavaClassPath() { ImmutableList.Builder<URL> urls = ImmutableList.builder(); for (String entry : Splitter.on(PATH_SEPARATOR.value()).split(JAVA_CLASS_PATH.value())) { try { try { urls.add(new File(entry).toURI().toURL()); } catch (SecurityException e) { // File.toURI checks to see if the file is a directory urls.add(new URL("file", null, new File(entry).getAbsolutePath())); } } catch (MalformedURLException e) { AssertionError error = new AssertionError("malformed class path entry: " + entry); error.initCause(e); throw error; } } return urls.build().toArray(new URL[0]); }
/** * 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); }
/** * Returns the URLs in the class path specified by the {@code java.class.path} {@linkplain * System#getProperty system property}. */ // TODO(b/65488446): Make this a public API. @GwtIncompatible private static ImmutableList<URL> parseJavaClassPath() { ImmutableList.Builder<URL> urls = ImmutableList.builder(); for (String entry : Splitter.on(PATH_SEPARATOR.value()).split(JAVA_CLASS_PATH.value())) { try { try { urls.add(new File(entry).toURI().toURL()); } catch (SecurityException e) { // File.toURI checks to see if the file is a directory urls.add(new URL("file", null, new File(entry).getAbsolutePath())); } } catch (MalformedURLException e) { AssertionError error = new AssertionError("malformed class path entry: " + entry); error.initCause(e); throw error; } } return urls.build(); } }