JDTCompiler extends AbstractCompiler { protected static List<CompilerMessage> parseModernStream(BufferedReader input) throws IOException { List<CompilerMessage> messages = new ArrayList<>(); String type = null; String file = null; int lineNr = -1; StringBuilder messageBuffer = new StringBuilder(); for (String line = input.readLine(); line != null; line = input.readLine()) { Matcher matcher = LINE_PATTERN.matcher(line); if (matcher.matches()) { addMessageIfFound(messages, type, file, lineNr, messageBuffer.toString()); matcher.group(1); type = matcher.group(2); file = matcher.group(3); String lineNumberString = matcher.group(5); if (lineNumberString != null) { lineNr = Integer.parseInt(lineNumberString); } else { lineNr = -1; } messageBuffer = new StringBuilder(); } else { if (!SEPARATOR.equals(line) && line.trim().length() > 0) { messageBuffer.append(EOL).append(line); } } } addMessageIfFound(messages, type, file, lineNr, messageBuffer.toString()); return messages; } JDTCompiler(); @Override CompilerResult performCompile(CompilerConfiguration config); @Override String[] createCommandLine(CompilerConfiguration config); String[] buildCompilerArguments(CompilerConfiguration config, CustomCompilerConfiguration custom,
            String[] sourceFiles);  }
@Test public void testParseModernStreamErrorWithLineAndTrailingSpace() throws IOException { List<CompilerMessage> messages = JDTCompiler.parseModernStream( createOutputForLines("1. ERROR in foo bar (at line 3) ", "2. ERROR in test (at line 5)")); assertEquals(2, messages.size()); CompilerMessage message = messages.get(0); assertTrue(message.isError()); assertEquals("", message.getMessage()); assertEquals("foo bar", message.getFile()); assertEquals(3, message.getStartLine()); message = messages.get(1); assertTrue(message.isError()); assertEquals("", message.getMessage()); assertEquals("test", message.getFile()); assertEquals(5, message.getStartLine()); }
@Test public void testParseModernStreamContextLines() throws IOException { List<CompilerMessage> messages = JDTCompiler.parseModernStream(createOutputForLines( "prologue (must be discarded)", "1. ERROR in foo bar (at line 3) ", "message line1", "message line2")); assertEquals(1, messages.size()); CompilerMessage error = messages.get(0); assertEquals(EOL + "message line1" + EOL + "message line2", error.getMessage()); }
@Test public void testParseModernStreamIgnoreSeparatorLines() throws IOException { List<CompilerMessage> messages = JDTCompiler.parseModernStream(createOutputForLines( "----------", "1. ERROR in foo bar (at line 3) ", "a context line", "----------", "", "2. WARNING in test2 (at line 4)", "second context line", "----------")); assertEquals(2, messages.size()); CompilerMessage error = messages.get(0); assertTrue(error.isError()); assertEquals("foo bar", error.getFile()); assertEquals(3, error.getStartLine()); assertEquals(EOL + "a context line", error.getMessage()); error = messages.get(1); assertFalse(error.isError()); assertEquals("test2", error.getFile()); assertEquals(4, error.getStartLine()); assertEquals(EOL + "second context line", error.getMessage()); }
@Test public void testParseModernStreamErrorWithoutLine() throws IOException { List<CompilerMessage> messages = JDTCompiler.parseModernStream(createOutputForLines("1. ERROR in baz")); assertEquals(1, messages.size()); CompilerMessage message = messages.get(0); assertTrue(message.isError()); assertEquals("baz", message.getFile()); assertEquals(-1, message.getStartLine()); }
@Test public void testParseModernStreamWarning() throws IOException { List<CompilerMessage> messages = JDTCompiler .parseModernStream(createOutputForLines("2. WARNING in foo (at line 4)")); assertEquals(1, messages.size()); CompilerMessage message = messages.get(0); assertEquals(Kind.WARNING, message.getKind()); assertEquals("foo", message.getFile()); assertEquals(4, message.getStartLine()); }
@Test public void testParseModernStreamWarningNoNumber() throws IOException { List<CompilerMessage> messages = JDTCompiler .parseModernStream(createOutputForLines("WARNING in foo (at line 4)")); assertEquals(1, messages.size()); CompilerMessage message = messages.get(0); assertFalse(message.isError()); assertEquals("foo", message.getFile()); assertEquals(4, message.getStartLine()); }