StacktraceLines { public List<String> getLastLines(int lastSize) { int size = 0; int i = lines.size() - 1; while (size < lastSize && i >= 0) { size += lines.get(i).length(); i--; } return lines.subList(i + 2, lines.size()); } StacktraceLines(List<String> lines); List<String> getLines(); int getTotalLineLength(); List<String> getFirstLines(int firstSize); List<String> getLastLines(int lastSize);  }
@Test public void testStacktraceLinesGetLastLines() { int maxSizeOfLastPart = 25; StacktraceLines stackTraceLines = new StacktraceLines(lines); List<String> extractedLines = stackTraceLines.getLastLines(maxSizeOfLastPart); int size = 0; for (String line: extractedLines) { size += line.length(); } assertThat(extractedLines, hasItem(containsString("this is the third line"))); assertThat(extractedLines, not(hasItem(containsString("this is the second line")))); assertThat(size, lessThan(maxSizeOfLastPart)); }
@Test public void testStacktraceLinesGetLastLinesUnderestimatingSizeOfLastLine() { int maxSizeOfLastPart = 10; StacktraceLines stackTraceLines = new StacktraceLines(lines); List<String> extractedLines = stackTraceLines.getLastLines(maxSizeOfLastPart); assertThat(extractedLines.size(), equalTo(0)); }
@Test(timeout = 1000) public void testStacktraceLinesGetLastLinesOverEstimatingTotalSizeOfLines() { int maxSizeOfFirstPart = 2500; StacktraceLines stackTraceLines = new StacktraceLines(lines); stackTraceLines.getLastLines(maxSizeOfFirstPart); }