@Test public void compareCompleteStacktraceWithExpectation() throws InterruptedException { Thread thread = new Thread(new Runnable() { @Override public void run() { DefaultStacktraceConverter converter = new DefaultStacktraceConverter(4000); StacktraceGenerator generator = new StacktraceGenerator(5, 10, 5); converter.convert(generator.generateException(), stringBuilder); } }); thread.start(); thread.join(); expectedString = "[\"java.lang.IllegalArgumentException: too long\"," + "\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.f3IsASimpleFunctionWithAnExceptionallyLongName(StacktraceGenerator.java:X)\"," + "\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.f3IsASimpleFunctionWithAnExceptionallyLongName(StacktraceGenerator.java:X)\"," + "\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.f3IsASimpleFunctionWithAnExceptionallyLongName(StacktraceGenerator.java:X)\"," + "\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.f3IsASimpleFunctionWithAnExceptionallyLongName(StacktraceGenerator.java:X)\"," + "\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.f3IsASimpleFunctionWithAnExceptionallyLongName(StacktraceGenerator.java:X)\"," + "\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.f3IsASimpleFunctionWithAnExceptionallyLongName(StacktraceGenerator.java:X)\"," + "\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.f2IsASimpleFunctionWithAnExceptionallyLongName(StacktraceGenerator.java:X)\"," + "\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.f2IsASimpleFunctionWithAnExceptionallyLongName(StacktraceGenerator.java:X)\"," + "\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.f2IsASimpleFunctionWithAnExceptionallyLongName(StacktraceGenerator.java:X)\"," + "\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.f2IsASimpleFunctionWithAnExceptionallyLongName(StacktraceGenerator.java:X)\"," + "\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.f2IsASimpleFunctionWithAnExceptionallyLongName(StacktraceGenerator.java:X)\"," + "\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.f2IsASimpleFunctionWithAnExceptionallyLongName(StacktraceGenerator.java:X)\"," + "\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.f2IsASimpleFunctionWithAnExceptionallyLongName(StacktraceGenerator.java:X)\"," + "\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.f2IsASimpleFunctionWithAnExceptionallyLongName(StacktraceGenerator.java:X)\"," + "\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.f2IsASimpleFunctionWithAnExceptionallyLongName(StacktraceGenerator.java:X)\"," + "\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.f2IsASimpleFunctionWithAnExceptionallyLongName(StacktraceGenerator.java:X)\"," + "\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.f2IsASimpleFunctionWithAnExceptionallyLongName(StacktraceGenerator.java:X)\"," + "\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.f1IsASimpleFunctionWithAnExceptionallyLongName(StacktraceGenerator.java:X)\"," + "\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.f1IsASimpleFunctionWithAnExceptionallyLongName(StacktraceGenerator.java:X)\"," + "\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.f1IsASimpleFunctionWithAnExceptionallyLongName(StacktraceGenerator.java:X)\"," + "\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.f1IsASimpleFunctionWithAnExceptionallyLongName(StacktraceGenerator.java:X)\"," + "\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.f1IsASimpleFunctionWithAnExceptionallyLongName(StacktraceGenerator.java:X)\"," + "\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.f1IsASimpleFunctionWithAnExceptionallyLongName(StacktraceGenerator.java:X)\"," + "\"\\tat com.sap.hcp.cf.logging.common.helper.StacktraceGenerator.generateException(StacktraceGenerator.java:X)\"," + "\"\\tat com.sap.hcp.cf.logging.common.converter.DefaultStacktraceConverterTest$X.run(DefaultStacktraceConverterTest.java:X)\"," + "\"\\tat " + getThreadClassName() + ".run(Thread.java:X)\"]"; Assert.assertEquals(expectedString, stringBuilder.toString().replaceAll(":\\d+\\)", ":X)"). replaceAll("\\$\\d", "\\$X")); }
@Override public void convert(Throwable t, StringBuilder appendTo) { if (t == null) { return; } try { LineWriter lw = new LineWriter(); t.printStackTrace(new PrintWriter(lw)); List<String> lines = lw.getLines(); StacktraceLines stacktraceLines = new StacktraceLines(lines); ArrayComposer<JSONComposer<String>> ac = JSON.std.composeString().startArray(); if (stacktraceLines.getTotalLineLength() <= maxSize) { for (String line: stacktraceLines.getLines()) { ac.add(line); } } else { ac.add("-------- STACK TRACE TRUNCATED --------"); for (String line: stacktraceLines.getFirstLines(maxSize / 3)) { ac.add(line); } ac.add("-------- OMITTED --------"); for (String line: stacktraceLines.getLastLines((maxSize / 3) * 2)) { ac.add(line); } } appendTo.append(ac.end().finish()); } catch (Exception ex) { LoggerFactory.getLogger(DefaultStacktraceConverter.class).error("Conversion failed ", ex); } }
DefaultStacktraceConverter extends StacktraceConverter { @Override public void convert(Throwable t, StringBuilder appendTo) { if (t == null) { return; } try { LineWriter lw = new LineWriter(); t.printStackTrace(new PrintWriter(lw)); List<String> lines = lw.getLines(); StacktraceLines stacktraceLines = new StacktraceLines(lines); ArrayComposer<JSONComposer<String>> ac = JSON.std.composeString().startArray(); if (stacktraceLines.getTotalLineLength() <= maxSize) { for (String line: stacktraceLines.getLines()) { ac.add(line); } } else { ac.add("-------- STACK TRACE TRUNCATED --------"); for (String line: stacktraceLines.getFirstLines(maxSize / 3)) { ac.add(line); } ac.add("-------- OMITTED --------"); for (String line: stacktraceLines.getLastLines((maxSize / 3) * 2)) { ac.add(line); } } appendTo.append(ac.end().finish()); } catch (Exception ex) { LoggerFactory.getLogger(DefaultStacktraceConverter.class).error("Conversion failed ", ex); } } }
DefaultStacktraceConverter extends StacktraceConverter { @Override public void convert(Throwable t, StringBuilder appendTo) { if (t == null) { return; } try { LineWriter lw = new LineWriter(); t.printStackTrace(new PrintWriter(lw)); List<String> lines = lw.getLines(); StacktraceLines stacktraceLines = new StacktraceLines(lines); ArrayComposer<JSONComposer<String>> ac = JSON.std.composeString().startArray(); if (stacktraceLines.getTotalLineLength() <= maxSize) { for (String line: stacktraceLines.getLines()) { ac.add(line); } } else { ac.add("-------- STACK TRACE TRUNCATED --------"); for (String line: stacktraceLines.getFirstLines(maxSize / 3)) { ac.add(line); } ac.add("-------- OMITTED --------"); for (String line: stacktraceLines.getLastLines((maxSize / 3) * 2)) { ac.add(line); } } appendTo.append(ac.end().finish()); } catch (Exception ex) { LoggerFactory.getLogger(DefaultStacktraceConverter.class).error("Conversion failed ", ex); } } DefaultStacktraceConverter();   DefaultStacktraceConverter(int maxSize); }
DefaultStacktraceConverter extends StacktraceConverter { @Override public void convert(Throwable t, StringBuilder appendTo) { if (t == null) { return; } try { LineWriter lw = new LineWriter(); t.printStackTrace(new PrintWriter(lw)); List<String> lines = lw.getLines(); StacktraceLines stacktraceLines = new StacktraceLines(lines); ArrayComposer<JSONComposer<String>> ac = JSON.std.composeString().startArray(); if (stacktraceLines.getTotalLineLength() <= maxSize) { for (String line: stacktraceLines.getLines()) { ac.add(line); } } else { ac.add("-------- STACK TRACE TRUNCATED --------"); for (String line: stacktraceLines.getFirstLines(maxSize / 3)) { ac.add(line); } ac.add("-------- OMITTED --------"); for (String line: stacktraceLines.getLastLines((maxSize / 3) * 2)) { ac.add(line); } } appendTo.append(ac.end().finish()); } catch (Exception ex) { LoggerFactory.getLogger(DefaultStacktraceConverter.class).error("Conversion failed ", ex); } } DefaultStacktraceConverter();   DefaultStacktraceConverter(int maxSize); @Override void convert(Throwable t, StringBuilder appendTo); }
DefaultStacktraceConverter extends StacktraceConverter { @Override public void convert(Throwable t, StringBuilder appendTo) { if (t == null) { return; } try { LineWriter lw = new LineWriter(); t.printStackTrace(new PrintWriter(lw)); List<String> lines = lw.getLines(); StacktraceLines stacktraceLines = new StacktraceLines(lines); ArrayComposer<JSONComposer<String>> ac = JSON.std.composeString().startArray(); if (stacktraceLines.getTotalLineLength() <= maxSize) { for (String line: stacktraceLines.getLines()) { ac.add(line); } } else { ac.add("-------- STACK TRACE TRUNCATED --------"); for (String line: stacktraceLines.getFirstLines(maxSize / 3)) { ac.add(line); } ac.add("-------- OMITTED --------"); for (String line: stacktraceLines.getLastLines((maxSize / 3) * 2)) { ac.add(line); } } appendTo.append(ac.end().finish()); } catch (Exception ex) { LoggerFactory.getLogger(DefaultStacktraceConverter.class).error("Conversion failed ", ex); } } DefaultStacktraceConverter();   DefaultStacktraceConverter(int maxSize); @Override void convert(Throwable t, StringBuilder appendTo); static final int MAX_SIZE; }