Example usage for java.lang AssertionError setStackTrace

List of usage examples for java.lang AssertionError setStackTrace

Introduction

In this page you can find the example usage for java.lang AssertionError setStackTrace.

Prototype

public void setStackTrace(StackTraceElement[] stackTrace) 

Source Link

Document

Sets the stack trace elements that will be returned by #getStackTrace() and printed by #printStackTrace() and related methods.

Usage

From source file:com.mta.sharedutils.Util.java

/**
 * Some low level stuff to make the exception point to the correct method throwing the exception.
 * Or else all the assertions will point to the "assertTrue" method as the crash point.
 *
 * @param comment/*from  www  . j ava2s  .  c  om*/
 * @return
 */
private static AssertionError generateAssertException(String comment) {
    AssertionError exception = new AssertionError(comment);
    //  you can add some more info to the comment reported to you, for example .. = new AssertionError(comment + "/" + DEBUG_PROCESS_ID) :
    StackTraceElement[] stackTrace = exception.getStackTrace();
    StackTraceElement[] newArray = Arrays.copyOfRange(stackTrace, 2, stackTrace.length);
    exception.setStackTrace(newArray);
    return exception;
}

From source file:com.googlecode.fightinglayoutbugs.helpers.TestHelper.java

private static void fail() {
    String assertionErrorMessage = "";
    // Try to extract assertion error message from the source file, from which assertThat has been called ...
    final StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
    int i = 0;/*w w w .java  2s .  c  o m*/
    while (!"assertThat".equals(stackTrace[i].getMethodName())) {
        ++i;
    }
    ++i;
    final File javaFile = findSourceFileFor(stackTrace[i]);
    if (javaFile != null) {
        final int lineNumber = stackTrace[i].getLineNumber();
        try {
            final List<String> lines = FileUtils.readLines(javaFile, "UTF-8");
            final String line = lines.get(lineNumber - 1).trim();
            if (line.startsWith("assertThat(")) {
                assertionErrorMessage = line.substring("assertThat(".length());
                if (assertionErrorMessage.endsWith(");")) {
                    assertionErrorMessage = assertionErrorMessage.substring(0,
                            assertionErrorMessage.length() - ");".length());
                } else {
                    assertionErrorMessage += " ...";
                }
            }
        } catch (IOException ignored) {
        }
    }
    AssertionError e = new AssertionError(assertionErrorMessage);
    e.setStackTrace(Arrays.copyOfRange(stackTrace, i, stackTrace.length));
    throw e;
}

From source file:com.cedarsoft.serialization.test.utils.AbstractXmlSerializerMultiTest.java

@Override
protected void verifySerialized(@Nonnull List<? extends byte[]> serialized) throws Exception {
    List<? extends String> expected = getExpectedSerialized();

    int index = 0;
    for (byte[] current : serialized) {
        String expectedWithNamespace = AbstractXmlSerializerTest2.addNameSpace(
                (AbstractXmlSerializer<?, ?, ?, ?>) getSerializer(), expected.get(index).getBytes());
        try {//from   w w w  . j  a v a  2s .  com
            AssertUtils.assertXMLEquals(new String(current, getCharset()), expectedWithNamespace);
        } catch (AssertionError e) {
            AssertionError newError = new AssertionError("Failed for index <" + index + ">: " + e.getMessage());
            newError.setStackTrace(e.getStackTrace());
            throw newError;
        }
        index++;
    }
}

From source file:com.gs.collections.impl.test.Verify.java

/**
 * Mangles the stack trace of {@link AssertionError} so that it looks like
 * its been thrown from the line that called to a custom assertion.
 * <p>/* www .  j av a2 s . c  o m*/
 * This is useful for when you are in a debugging session and you want to go to the source
 * of the problem in the test case quickly. The regular use case for this would be something
 * along the lines of:
 * <pre>
 * public class TestFoo extends junit.framework.TestCase
 * {
 *   public void testFoo() throws Exception
 *   {
 *     Foo foo = new Foo();
 *     ...
 *     assertFoo(foo);
 *   }
 *
 *   // Custom assert
 *   private static void assertFoo(Foo foo)
 *   {
 *     try
 *     {
 *       assertEquals(...);
 *       ...
 *       assertSame(...);
 *     }
 *     catch (AssertionFailedException e)
 *     {
 *       AssertUtils.throwMangledException(e, 2);
 *     }
 *   }
 * }
 * </pre>
 * <p>
 * Without the {@code try ... catch} block around lines 11-13 the stack trace following a test failure
 * would look a little like:
 * <p>
 * <pre>
 * java.lang.AssertionError: ...
 *  at TestFoo.assertFoo(TestFoo.java:11)
 *  at TestFoo.testFoo(TestFoo.java:5)
 *  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 *  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 *  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 *  at java.lang.reflect.Method.invoke(Method.java:324)
 *  ...
 * </pre>
 * <p>
 * Note that the source of the error isn't readily apparent as the first line in the stack trace
 * is the code within the custom assert. If we were debugging the failure we would be more interested
 * in the second line of the stack trace which shows us where in our tests the assert failed.
 * <p>
 * With the {@code try ... catch} block around lines 11-13 the stack trace would look like the
 * following:
 * <p>
 * <pre>
 * java.lang.AssertionError: ...
 *  at TestFoo.testFoo(TestFoo.java:5)
 *  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 *  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 *  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 *  at java.lang.reflect.Method.invoke(Method.java:324)
 *  ...
 * </pre>
 * <p>
 * Here the source of the error is more visible as we can instantly see that the testFoo test is
 * failing at line 5.
 *
 * @param e           The exception to mangle.
 * @param framesToPop The number of frames to remove from the stack trace.
 * @throws AssertionError that was given as an argument with its stack trace mangled.
 */
public static void throwMangledException(AssertionError e, int framesToPop) {
    e.fillInStackTrace();
    StackTraceElement[] stackTrace = e.getStackTrace();
    StackTraceElement[] newStackTrace = new StackTraceElement[stackTrace.length - framesToPop];
    System.arraycopy(stackTrace, framesToPop, newStackTrace, 0, newStackTrace.length);
    e.setStackTrace(newStackTrace);
    throw e;
}