Example usage for java.lang StackTraceElement StackTraceElement

List of usage examples for java.lang StackTraceElement StackTraceElement

Introduction

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

Prototype

public StackTraceElement(String declaringClass, String methodName, String fileName, int lineNumber) 

Source Link

Document

Creates a stack trace element representing the specified execution point.

Usage

From source file:net.logstash.logback.encoder.LogstashEncoderTest.java

@Test
public void callerDataIsNotIncludedIfSwitchedOff() throws Exception {
    ILoggingEvent event = mock(ILoggingEvent.class);
    when(event.getLoggerName()).thenReturn("LoggerName");
    when(event.getThreadName()).thenReturn("ThreadName");
    when(event.getFormattedMessage()).thenReturn("My message");
    when(event.getLevel()).thenReturn(Level.ERROR);
    when(event.getMDCPropertyMap()).thenReturn(Collections.<String, String>emptyMap());
    final StackTraceElement[] stackTraceElements = {
            new StackTraceElement("caller_class", "method_name", "file_name", 12345) };
    when(event.getCallerData()).thenReturn(stackTraceElements);

    encoder.setIncludeCallerInfo(false);

    encoder.doEncode(event);// www  .  ja  va  2s. c  om
    closeQuietly(outputStream);

    JsonNode node = MAPPER.readTree(outputStream.toByteArray());
    assertThat(node.get("caller_class_name"), is(nullValue()));
    assertThat(node.get("caller_method_name"), is(nullValue()));
    assertThat(node.get("caller_file_name"), is(nullValue()));
    assertThat(node.get("caller_line_number"), is(nullValue()));
}

From source file:org.bug4j.client.Bug4jTest.java

/**
 * Verifies that we don't report the same error twice
 *//*from w w w .  j a  v a 2s .c o  m*/
@Test
public void testNoReReport() throws Exception {
    {
        final IllegalStateException e = new IllegalStateException("SameTitle");
        e.setStackTrace(new StackTraceElement[] {
                new StackTraceElement("org.bug4j.SomeClass", "testSameTitle", "SomeClass.java", 100),
                new StackTraceElement("org.bug4j.SomeClass", "testSameTitle", "SomeClass.java", 200),
                new StackTraceElement("org.bug4j.SomeClass", "testSameTitle", "SomeClass.java", 300),
                new StackTraceElement("org.bug4j.SomeClass", "testSameTitle", "SomeClass.java", 400), });
        for (int i = 0; i < 10; i++) {
            Bug4jAgent.report("testSameTitle", e);
        }
    }

    Bug4jAgent.shutdown();
    Assert.assertEquals(1, Bug4jAgent.getReported());
}

From source file:org.openmrs.module.webservices.rest.web.RestUtilTest.java

/**
 * @see RestUtil#wrapErrorResponse(Exception,String)
 * @verifies set stack trace code if available
 */// w  ww. java  2s. co  m
@Test
public void wrapErrorResponse_shouldSetStackTraceCodeAndDetailIfAvailable() throws Exception {
    Exception mockException = Mockito.mock(Exception.class);
    Mockito.when(mockException.getMessage()).thenReturn("exceptionmessage");
    StackTraceElement ste = new StackTraceElement("org.mypackage.myclassname", "methodName", "fileName", 149);
    Mockito.when(mockException.getStackTrace()).thenReturn(new StackTraceElement[] { ste });

    SimpleObject returnObject = RestUtil.wrapErrorResponse(mockException, "wraperrorresponsemessage");

    LinkedHashMap errorResponseMap = (LinkedHashMap) returnObject.get("error");
    Assert.assertEquals("org.mypackage.myclassname:149", errorResponseMap.get("code"));
}

From source file:org.ebayopensource.twin.TwinConnection.java

@SuppressWarnings("unchecked")
private static TwinException deserializeException(TwinError responseCode, Map<String, Object> exception) {
    TwinException ex = responseCode.create((String) exception.get("message"));
    ex.className = (String) exception.get("class");
    if (exception.containsKey("stackTrace")) {
        List<?> array = (List<?>) exception.get("stackTrace");
        List<StackTraceElement> trace = new ArrayList<StackTraceElement>();
        for (Object elt : array) {
            Map<String, Object> element = (Map<String, Object>) elt;
            trace.add(new StackTraceElement((String) element.get("className"),
                    (String) element.get("methodName"), (String) element.get("fileName"),
                    (element.containsKey("lineNumber") ? ((Number) element.get("lineNumber")).intValue()
                            : -1)));/*from  w ww  .ja  v  a  2s  . c  o  m*/
        }
        for (StackTraceElement elt : new Exception().getStackTrace()) {
            if (TwinConnection.class.getName().equals(elt.getClassName()))
                continue;
            if (Application.class.getName().equals(elt.getClassName())
                    && ("ensureSuccess".equals(elt.getMethodName())
                            || elt.getMethodName().startsWith("request")))
                continue;
            trace.add(elt);
        }
        ex.setStackTrace(trace.toArray(new StackTraceElement[trace.size()]));
    }
    if (exception.containsKey("cause") && exception.get("cause") != null)
        ex.initCause(
                deserializeException(TwinError.UnknownError, (Map<String, Object>) exception.get("cause")));

    return ex;
}

From source file:minium.script.rhinojs.RhinoEngine.java

protected StackTraceElement[] process(StackTraceElement[] stackTrace) {
    List<StackTraceElement> processed = Lists.newArrayList();
    for (StackTraceElement element : stackTrace) {
        if (element.getClassName().startsWith("org.mozilla.javascript.gen") && element.getLineNumber() != -1) {
            String fileName = null;
            File file = new File(element.getFileName());
            if (file.exists() && file.isFile()) {
                fileName = file.getAbsolutePath();
            }/*from  w w  w.  j  a v a  2s  .c o m*/
            if (fileName == null)
                fileName = element.getFileName();
            processed.add(new StackTraceElement(element.getClassName(), element.getMethodName(), fileName,
                    element.getLineNumber()));
        }
    }
    return processed.toArray(new StackTraceElement[processed.size()]);
}

From source file:com.wolvereness.bluebutton.crash.ExceptionMessage.java

static StackTraceElement toStackTraceElement(final String element) {
    final Matcher matcher = STACK_ELEMENT.matcher(element);
    if (!matcher.matches())
        throw new IllegalArgumentException("Invalid stack trace element: " + element);
    if (matcher.group(6) == null)
        return new StackTraceElement(matcher.group(2), matcher.group(4), null, -2);
    else//from   w w  w  . j  av a2  s .c o m
        return new StackTraceElement(matcher.group(2), matcher.group(4), matcher.group(6),
                Integer.parseInt(matcher.group(7)));
}

From source file:de.vandermeer.skb.base.message.Message5WH_Tests.java

@Test
public void testWhereSG_STE() {
    //test setWhere(StackTraceElement)
    Message5WH m = new Message5WH();
    StackTraceElement ste = new StackTraceElement("", "", null, 0);

    //check with null first
    m = new Message5WH().setWhere((StackTraceElement) null);
    assertTrue(m.getWhere() == null);/*from   w  w w .  j  a v a  2 s  .c om*/

    //now check with an STE that doesn't have any values
    m = new Message5WH().setWhere(ste);
    assertTrue(m.getWhere() == null);

    //now put in valid line only -- null
    ste = new StackTraceElement("", "", null, 1);
    m = new Message5WH().setWhere(ste);
    assertTrue(m.getWhere() == null);

    //now put in valid class name only
    ste = new StackTraceElement("myClass", "", null, 0);
    m = new Message5WH().setWhere(ste);
    this.testWhereST(m.getWhere(), "myClass", null, null);

    //now put in valid method name only
    ste = new StackTraceElement("", "myMethod", null, 0);
    m = new Message5WH().setWhere(ste);
    this.testWhereST(m.getWhere(), "myMethod", null, null);

    //now put in valid class and method but not line
    ste = new StackTraceElement("myClass", "myMethod", null, 0);
    m = new Message5WH().setWhere(ste);
    this.testWhereST(m.getWhere(), Arrays.asList(new Object[] { "myClass", "myMethod" }), null, null);

    //now check for a complete STE
    ste = new StackTraceElement("myClass", "myMethod", null, 1);
    m = new Message5WH().setWhere(ste);
    this.testWhereST(m.getWhere(), Arrays.asList(new Object[] { "myClass", "myMethod" }), "1", null);
}

From source file:org.eclipse.epp.internal.logging.aeri.ui.LogListenerTest.java

@Test
public void testNoAcceptingOtherPackages() {
    Throwable t = new RuntimeException();
    t.fillInStackTrace();/*from   ww w.j a v  a 2  s .com*/
    StackTraceElement[] stackTrace = t.getStackTrace();
    stackTrace[0] = new StackTraceElement("any.third.party.Clazz", "thirdPartyMethod", "Clazz.java", 42);
    t.setStackTrace(stackTrace);
    Status status = new Status(IStatus.ERROR, TEST_PLUGIN_ID, "Error Message", t);

    configuration.setAcceptOtherPackages(false);

    sut.logging(status, "");
    verifyNoErrorReportLogged();
}