StackTraceFinder { public SortedSet<SubText> findStackTraces(String text) { SortedSet<SubText> set = new TreeSet<>(); LinkedList<Integer> newLineIndexes = new LinkedList<>(); newLineIndexes.add(0); int newLineIndex = -1; while ((newLineIndex = StringUtils.indexOf(text, '\n', newLineIndex + 1)) > -1) { newLineIndexes.addLast(newLineIndex); } if (newLineIndexes.getLast() < text.length()) { newLineIndexes.addLast(text.length()); } int startLine; int endLine; boolean found = false; int startedLineException = -1; for (int i = 0; i < newLineIndexes.size() - 1; i++) { startLine = newLineIndexes.get(i); endLine = newLineIndexes.get(i + 1); String line = text.substring(startLine, endLine); Matcher matcher = exceptionLine.matcher(line); boolean f = matcher.find(); if (f && !found) { startedLineException = i - 1; } else if (!f && found) { int start = newLineIndexes.get(startedLineException); int end = newLineIndexes.get(i); SubText subText = new SubText(start, end); set.add(subText); } found = f; } if (found) { int start = newLineIndexes.get(startedLineException); int end = newLineIndexes.getLast(); SubText subText = new SubText(start, end); set.add(subText); } return set; }  SortedSet<SubText> findStackTraces(String text);  }
@Test public void testFindStackTraces() throws IOException { String stacktraceFile = IOUtils.toString(StackTraceColorizer.class.getClassLoader().getResourceAsStream("stacktrace/stacktrace.txt")); SortedSet<SubText> findStackTraces = finder.findStackTraces(stacktraceFile); assertEquals(1, findStackTraces.size()); SubText subtext = findStackTraces.iterator().next(); assertEquals(10, subtext.getStart()); assertEquals(241, subtext.getEnd()); }
@Test public void testFindInOnlyStackTrace() throws IOException { String stacktraceFile = IOUtils.toString(StackTraceColorizer.class.getClassLoader().getResourceAsStream("stacktrace/stacktTraceOnly.txt")); SortedSet<SubText> findStackTraces = finder.findStackTraces(stacktraceFile); assertEquals(1, findStackTraces.size()); SubText subtext = findStackTraces.iterator().next(); assertEquals(0, subtext.getStart()); assertEquals(234, subtext.getEnd()); }
@Test public void testFindForEmptyPackage() throws IOException { String stacktraceFile = IOUtils.toString(StackTraceColorizer.class.getClassLoader().getResourceAsStream("stacktrace/stacktTraceWtihEmptyPackage.txt")); SortedSet<SubText> findStackTraces = finder.findStackTraces(stacktraceFile); assertEquals(1, findStackTraces.size()); SubText subtext = findStackTraces.iterator().next(); assertEquals(0, subtext.getStart()); assertEquals(213, subtext.getEnd()); }
@Test public void testFind2StackTraces() throws IOException { String stacktraceFile = IOUtils.toString(StackTraceColorizer.class.getClassLoader().getResourceAsStream("stacktrace/stacktrace2.txt")); SortedSet<SubText> findStackTraces = finder.findStackTraces(stacktraceFile); assertEquals(2, findStackTraces.size()); Iterator<SubText> iterator = findStackTraces.iterator(); SubText subtext = iterator.next(); assertEquals(44, subtext.getStart()); assertEquals(3142, subtext.getEnd()); subtext = iterator.next(); assertEquals(3183, subtext.getStart()); assertEquals(3439, subtext.getEnd()); }