diff --git a/plexus-compiler-test/src/main/java/org/codehaus/plexus/compiler/AbstractCompilerTest.java b/plexus-compiler-test/src/main/java/org/codehaus/plexus/compiler/AbstractCompilerTest.java index 9c1c9d42..a6682e9b 100644 --- a/plexus-compiler-test/src/main/java/org/codehaus/plexus/compiler/AbstractCompilerTest.java +++ b/plexus-compiler-test/src/main/java/org/codehaus/plexus/compiler/AbstractCompilerTest.java @@ -1,6 +1,4 @@ -package org.codehaus.plexus.compiler; - -/** +/* * The MIT License * * Copyright (c) 2004, The Codehaus @@ -23,17 +21,17 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ +package org.codehaus.plexus.compiler; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.DefaultArtifact; import org.apache.maven.artifact.handler.DefaultArtifactHandler; import org.apache.maven.artifact.test.ArtifactTestCase; import org.apache.maven.artifact.versioning.VersionRange; - +import org.codehaus.plexus.compiler.CompilerMessage.Kind; import org.codehaus.plexus.util.FileUtils; import org.codehaus.plexus.util.StringUtils; -import javax.print.DocFlavor; import java.io.File; import java.util.ArrayList; import java.util.Collection; @@ -42,9 +40,6 @@ import java.util.List; import java.util.TreeSet; -/** - * - */ public abstract class AbstractCompilerTest extends ArtifactTestCase { @@ -91,6 +86,22 @@ protected void configureCompilerConfig( CompilerConfiguration compilerConfig ) } + + /** + * Called once per compile iteration to allow configuration customization for + * tests. + * + * @param compilerConfig + * configuration used for this compile iteration. + * @param filename + * file about to be compiled this iteration. + * @since 2.8.6 + */ + protected void configureCompilerConfig(CompilerConfiguration compilerConfig, String filename) + { + configureCompilerConfig( compilerConfig ); + } + public void testCompilingSources() throws Exception { @@ -113,14 +124,16 @@ public void testCompilingSources() int numCompilerErrors = compilerErrorCount( messages ); - int numCompilerWarnings = messages.size() - numCompilerErrors; + int numCompilerWarnings = compilerWarningCount( messages ); + + int numCompilerNotes = compilerNoteCount( messages ); if ( expectedErrors() != numCompilerErrors ) { System.out.println( numCompilerErrors + " error(s) found:" ); for ( CompilerMessage error : messages ) { - if ( !error.isError() ) + if ( error.getKind() != Kind.ERROR ) { continue; } @@ -139,7 +152,7 @@ public void testCompilingSources() System.out.println( numCompilerWarnings + " warning(s) found:" ); for ( CompilerMessage error : messages ) { - if ( error.isError() ) + if ( error.getKind() == Kind.ERROR || error.getKind() == Kind.NOTE ) { continue; } @@ -153,6 +166,25 @@ public void testCompilingSources() assertEquals( "Wrong number of compilation warnings.", expectedWarnings(), numCompilerWarnings ); } + if ( expectedNotes() != numCompilerNotes ) + { + System.out.println( numCompilerWarnings + " notes(s) found:" ); + for (CompilerMessage error : messages) + { + if ( error.getKind() != Kind.NOTE ) + { + continue; + } + + System.out.println( "----" ); + System.out.println( error.getFile() ); + System.out.println( error.getMessage() ); + System.out.println( "----" ); + } + + assertEquals( "Wrong number of compilation notes.", expectedNotes(), numCompilerNotes ); + } + assertEquals( new TreeSet<>( normalizePaths( expectedOutputFiles() ) ), files ); } @@ -190,7 +222,7 @@ private List getCompilerConfigurations() compilerConfig.setForceJavacCompilerUse( this.forceJavacCompilerUse ); - configureCompilerConfig( compilerConfig ); + configureCompilerConfig( compilerConfig, filename ); String target = getTargetVersion(); if( StringUtils.isNotEmpty( target) ) @@ -221,7 +253,6 @@ public String getSourceVersion() return null; } - private List normalizePaths( Collection relativePaths ) { List normalizedPaths = new ArrayList(); @@ -232,16 +263,32 @@ private List normalizePaths( Collection relativePaths ) return normalizedPaths; } - protected int compilerErrorCount( List messages ) + private int compilerErrorCount(List messages) + { + return countKind( messages, Kind.ERROR ); + } + + private int compilerWarningCount(List messages) + { + return messages.size() - (compilerErrorCount( messages ) + compilerNoteCount( messages )); + } + + private int compilerNoteCount(List messages) { - int count = 0; + return countKind( messages, Kind.NOTE ); + } - for ( CompilerMessage message : messages ) + private int countKind(List messages, Kind kind) + { + int c = 0; + for (CompilerMessage message : messages) { - count += message.isError() ? 1 : 0; + if ( message.getKind() == kind ) + { + c++; + } } - - return count; + return c; } protected int expectedErrors() @@ -254,6 +301,17 @@ protected int expectedWarnings() return 0; } + /** + * Count of output generated at the {@link Kind#NOTE} level. + * + * @return count + * @since 2.8.6 + */ + protected int expectedNotes() + { + return 0; + } + protected Collection expectedOutputFiles() { return Collections.emptyList(); diff --git a/plexus-compilers/plexus-compiler-eclipse/pom.xml b/plexus-compilers/plexus-compiler-eclipse/pom.xml index 2e1f1235..8cff7b88 100644 --- a/plexus-compilers/plexus-compiler-eclipse/pom.xml +++ b/plexus-compilers/plexus-compiler-eclipse/pom.xml @@ -21,7 +21,7 @@ org.eclipse.jdt ecj - 3.13.100 + 3.15.0 diff --git a/plexus-compilers/plexus-compiler-eclipse/src/main/java/org/codehaus/plexus/compiler/eclipse/EcjLogParser.java b/plexus-compilers/plexus-compiler-eclipse/src/main/java/org/codehaus/plexus/compiler/eclipse/EcjLogParser.java new file mode 100644 index 00000000..52dff5c2 --- /dev/null +++ b/plexus-compilers/plexus-compiler-eclipse/src/main/java/org/codehaus/plexus/compiler/eclipse/EcjLogParser.java @@ -0,0 +1,26 @@ +package org.codehaus.plexus.compiler.eclipse; + +import java.io.File; +import java.util.List; + +import org.codehaus.plexus.compiler.CompilerMessage; + +/** + * Log parser interface. + * + * @author Jason Faust + * @since 2.8.6 + */ +public interface EcjLogParser { + + /** + * Pares an Eclipse Compiler log file. + * + * @param logFile file to parse. + * @param errorsAsWarnings if errors should be down-graded to warnings. + * @return the messages. + * @throws Exception on parse errors. + */ + List parse(File logFile, boolean errorsAsWarnings) throws Exception; + +} diff --git a/plexus-compilers/plexus-compiler-eclipse/src/main/java/org/codehaus/plexus/compiler/eclipse/EcjResponseParser.java b/plexus-compilers/plexus-compiler-eclipse/src/main/java/org/codehaus/plexus/compiler/eclipse/EcjResponseParser.java index 10011149..ba7dcebc 100644 --- a/plexus-compilers/plexus-compiler-eclipse/src/main/java/org/codehaus/plexus/compiler/eclipse/EcjResponseParser.java +++ b/plexus-compilers/plexus-compiler-eclipse/src/main/java/org/codehaus/plexus/compiler/eclipse/EcjResponseParser.java @@ -16,16 +16,19 @@ import java.util.List; /** + * Created on 2018-03-31. + * * @author Frits Jalvingh - * Created on 31-3-18. + * @author Jason Faust */ -public class EcjResponseParser { +public class EcjResponseParser implements EcjLogParser { /*--------------------------------------------------------------*/ /* CODING: Decode ECJ -log format results. */ /*--------------------------------------------------------------*/ /** * Scan the specified response file for compilation messages. */ + @Override public List parse(File xmltf, boolean errorsAsWarnings) throws Exception { //if(xmltf.length() < 80) // return; @@ -87,18 +90,32 @@ private void decodeProblems(List list, String sourcePath, XMLSt private void decodeProblem(List list, String sourcePath, XMLStreamReader xsr, boolean errorsAsWarnings) throws Exception { String id = xsr.getAttributeValue(null, "optionKey"); // Key for the problem int startline = getInt(xsr, "line"); - int column = getInt(xsr, "charStart"); - int endCol = getInt(xsr, "charEnd"); + int column = 0; + int endCol = 0; String sev = xsr.getAttributeValue(null, "severity"); String message = "Unknown message?"; - //-- Go watch for "message" + //-- Go watch for "message" and "source_context" while(xsr.hasNext()) { int type = xsr.nextTag(); if(type == XMLStreamConstants.START_ELEMENT) { if("message".equals(xsr.getLocalName())) { message = xsr.getAttributeValue(null, "value"); } + if("source_context".equals(xsr.getLocalName())) { + column = getInt(xsr, "sourceStart"); + if (column != -1) { + column += 1; // Offset to 1-based index + } else { + column = 0; + } + endCol = getInt(xsr, "sourceEnd"); + if (endCol != -1) { + endCol += 2; // Offset to 1-based index with one-after counting + } else { + endCol = 0; + } + } ignoreTillEnd(xsr); } else if(type == XMLStreamConstants.END_ELEMENT) { diff --git a/plexus-compilers/plexus-compiler-eclipse/src/main/java/org/codehaus/plexus/compiler/eclipse/EcjTextLogParser.java b/plexus-compilers/plexus-compiler-eclipse/src/main/java/org/codehaus/plexus/compiler/eclipse/EcjTextLogParser.java new file mode 100644 index 00000000..0e475537 --- /dev/null +++ b/plexus-compilers/plexus-compiler-eclipse/src/main/java/org/codehaus/plexus/compiler/eclipse/EcjTextLogParser.java @@ -0,0 +1,105 @@ +package org.codehaus.plexus.compiler.eclipse; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; +import java.util.ArrayList; +import java.util.LinkedList; +import java.util.List; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +import org.codehaus.plexus.compiler.CompilerMessage; +import org.codehaus.plexus.compiler.CompilerMessage.Kind; + +/** + * Parser for non-XML Eclipse Compiler output. + * + * @author Jason Faust + * @since 2.8.6 + */ +public class EcjTextLogParser implements EcjLogParser { + + private static final String TEN_DASH = "----------"; + private static final String PATTERN_LEVEL_FILE = "^\\d+\\.\\s+(\\w+)\\s+in\\s+(.*)\\s+\\(at line (\\d+)\\)$"; + + private Pattern levelFile = Pattern.compile(PATTERN_LEVEL_FILE); + + @Override + public List parse(File logFile, boolean errorsAsWarnings) throws Exception { + List ret = new ArrayList<>(); + try (BufferedReader in = new BufferedReader( + new InputStreamReader(new FileInputStream(logFile), StandardCharsets.UTF_8))) { + String line; + List buffer = new LinkedList<>(); + boolean header = true; + + while ((line = in.readLine()) != null) { + if (header) { + if (!line.startsWith(TEN_DASH)) { + continue; + } + header = false; + } + if (line.startsWith(TEN_DASH)) { + if (!buffer.isEmpty()) { + ret.add(parse(buffer, errorsAsWarnings)); + } + buffer.clear(); + } else { + buffer.add(line); + } + } + } + return ret; + } + + private CompilerMessage parse(List buffer, boolean errorsAsWarnings) { + + Kind kind = null; + String file = null; + int lineNum = 0; + int startCol = 0; + int endCol = 0; + String message = null; + + // First line, kind, file, lineNum + if (buffer.size() > 1) { + String str = buffer.get(0); + Matcher matcher = levelFile.matcher(str); + if (matcher.find()) { + file = matcher.group(2); + kind = decodeKind(matcher.group(1), errorsAsWarnings); + lineNum = Integer.parseInt(matcher.group(3)); + } + } + + // Last line, message + if (buffer.size() >= 2) { + String str = buffer.get(buffer.size() - 1); + message = str.trim(); + } + + // 2nd to last line, columns + if (buffer.size() >= 3) { + String str = buffer.get(buffer.size() - 2); + startCol = str.indexOf('^'); + endCol = str.lastIndexOf('^') + 1; + } + + return new CompilerMessage(file, kind, lineNum, startCol, lineNum, endCol, message); + } + + private Kind decodeKind(String str, boolean errorsAsWarnings) { + if (str.equals("ERROR")) { + return errorsAsWarnings ? Kind.WARNING : Kind.ERROR; + } + if (str.equals("INFO")) { + return Kind.NOTE; + } + return Kind.WARNING; + } + +} diff --git a/plexus-compilers/plexus-compiler-eclipse/src/main/java/org/codehaus/plexus/compiler/eclipse/EclipseJavaCompiler.java b/plexus-compilers/plexus-compiler-eclipse/src/main/java/org/codehaus/plexus/compiler/eclipse/EclipseJavaCompiler.java index 3f84de5f..b5631e52 100644 --- a/plexus-compilers/plexus-compiler-eclipse/src/main/java/org/codehaus/plexus/compiler/eclipse/EclipseJavaCompiler.java +++ b/plexus-compilers/plexus-compiler-eclipse/src/main/java/org/codehaus/plexus/compiler/eclipse/EclipseJavaCompiler.java @@ -1,6 +1,4 @@ -package org.codehaus.plexus.compiler.eclipse; - -/** +/* * The MIT License * * Copyright (c) 2005, The Codehaus @@ -23,6 +21,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ +package org.codehaus.plexus.compiler.eclipse; import org.codehaus.plexus.compiler.AbstractCompiler; import org.codehaus.plexus.compiler.CompilerConfiguration; @@ -143,7 +142,7 @@ else if(extras.containsKey("-errorsAsWarnings")) if(null != props) { File propFile = new File(props); if(! propFile.exists() || ! propFile.isFile()) - throw new IllegalArgumentException("Properties file specified by -properties " + propFile + " does not exist"); + throw new EcjFailureException("Properties file specified by -properties " + propFile + " does not exist"); } for(Entry entry : extras.entrySet()) @@ -230,6 +229,27 @@ else if(extras.containsKey("-errorsAsWarnings")) } } + // Check for custom -log + boolean tempLog; + File logFile; + EcjLogParser logParser; + if (extras.containsKey("-log")) { + String key = extras.get("-log"); + logFile = new File(key); + tempLog = false; + logParser = key.endsWith(".xml") ? new EcjResponseParser() : new EcjTextLogParser(); + } else { + try { + logFile = File.createTempFile("ecjerr-", ".xml"); + } catch (IOException e) { + throw new CompilerException("Unable to create temporary file for compiler output", e); + } + args.add("-log"); + args.add(logFile.toString()); + tempLog = true; + logParser = new EcjResponseParser(); + } + //-- Write .class files even when error occur, but make sure methods with compile errors do abort when called if(extras.containsKey("-proceedOnError")) args.add("-proceedOnError:Fatal"); // Generate a class file even with errors, but make methods with errors fail when called @@ -240,15 +260,8 @@ else if(extras.containsKey("-errorsAsWarnings")) args.add("-classpath"); args.add(getPathString(classpathEntries)); - // Compile! Send all errors to xml temp file. - File errorF = null; try { - errorF = File.createTempFile("ecjerr-", ".xml"); - - args.add("-log"); - args.add(errorF.toString()); - // Add all sources. int argCount = args.size(); for(String source : config.getSourceLocations()) @@ -304,11 +317,11 @@ public void worked(int i, int i1) List messageList; boolean hasError = false; - if(errorF.length() < 80) + if(logFile.length() < 80) { throw new EcjFailureException(sw.toString()); } - messageList = new EcjResponseParser().parse(errorF, errorsAsWarnings); + messageList = logParser.parse(logFile, errorsAsWarnings); for(CompilerMessage compilerMessage : messageList) { @@ -341,9 +354,9 @@ public void worked(int i, int i1) } catch(Exception x) { throw new RuntimeException(x); // sigh } finally { - if(null != errorF) { + if(tempLog) { try { - errorF.delete(); + logFile.delete(); } catch(Exception x) {} } } diff --git a/plexus-compilers/plexus-compiler-eclipse/src/test-input/src/main/org/codehaus/foo/Info.java b/plexus-compilers/plexus-compiler-eclipse/src/test-input/src/main/org/codehaus/foo/Info.java new file mode 100644 index 00000000..a5cf7082 --- /dev/null +++ b/plexus-compilers/plexus-compiler-eclipse/src/test-input/src/main/org/codehaus/foo/Info.java @@ -0,0 +1,7 @@ +package org.codehaus.foo; + +public class Info { + { + "".equals(1); + } +} diff --git a/plexus-compilers/plexus-compiler-eclipse/src/test/java/org/codehaus/plexus/compiler/eclipse/AbstractEclipseCompilerTest.java b/plexus-compilers/plexus-compiler-eclipse/src/test/java/org/codehaus/plexus/compiler/eclipse/AbstractEclipseCompilerTest.java new file mode 100644 index 00000000..a1630c01 --- /dev/null +++ b/plexus-compilers/plexus-compiler-eclipse/src/test/java/org/codehaus/plexus/compiler/eclipse/AbstractEclipseCompilerTest.java @@ -0,0 +1,100 @@ +/* + * The MIT License + * + * Copyright (c) 2005, The Codehaus + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is furnished to do + * so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.codehaus.plexus.compiler.eclipse; + +import java.util.Arrays; +import java.util.Collection; + +import org.codehaus.plexus.compiler.AbstractCompilerTest; +import org.codehaus.plexus.compiler.CompilerConfiguration; + +/** + * @author Jason van Zyl + * @author Jason Faust + */ +public abstract class AbstractEclipseCompilerTest extends AbstractCompilerTest { + + private int expectedErrors; + private int expectedWarnings; + private int expectedNotes; + private Collection expectedOutputFiles; + + protected AbstractEclipseCompilerTest() { + this(5, 1, 1); + } + + protected AbstractEclipseCompilerTest(int expectedErrors, int expectedWarnings, int expectedNotes) { + this(expectedErrors, expectedWarnings, expectedNotes, Arrays + .asList(new String[] { + "org/codehaus/foo/Deprecation.class", + "org/codehaus/foo/ExternalDeps.class", + "org/codehaus/foo/Info.class", + "org/codehaus/foo/Person.class" })); + } + + protected AbstractEclipseCompilerTest(int expectedErrors, int expectedWarnings, int expectedNotes, + Collection expectedOutputFiles) { + this.expectedErrors = expectedErrors; + this.expectedWarnings = expectedWarnings; + this.expectedNotes = expectedNotes; + this.expectedOutputFiles = expectedOutputFiles; + } + + public void setUp() throws Exception { + super.setUp(); + + setCompilerDebug(true); + setCompilerDeprecationWarnings(true); + } + + @Override + protected String getRoleHint() { + return "eclipse"; + } + + @Override + protected int expectedErrors() { + return expectedErrors; + } + + @Override + protected int expectedWarnings() { + return expectedWarnings; + } + + @Override + protected int expectedNotes() { + return expectedNotes; + } + + protected Collection expectedOutputFiles() { + return expectedOutputFiles; + } + + @Override + protected void configureCompilerConfig(CompilerConfiguration compilerConfig) { + compilerConfig.setSourceVersion("1.8"); + compilerConfig.setTargetVersion("1.8"); + } +} diff --git a/plexus-compilers/plexus-compiler-eclipse/src/test/java/org/codehaus/plexus/compiler/eclipse/EcjResponseParserTest.java b/plexus-compilers/plexus-compiler-eclipse/src/test/java/org/codehaus/plexus/compiler/eclipse/EcjResponseParserTest.java new file mode 100644 index 00000000..2a1d3adf --- /dev/null +++ b/plexus-compilers/plexus-compiler-eclipse/src/test/java/org/codehaus/plexus/compiler/eclipse/EcjResponseParserTest.java @@ -0,0 +1,106 @@ +package org.codehaus.plexus.compiler.eclipse; + +import java.io.File; +import java.util.List; + +import org.codehaus.plexus.PlexusTestCase; +import org.codehaus.plexus.compiler.CompilerMessage; +import org.codehaus.plexus.compiler.CompilerMessage.Kind; +import org.junit.Test; + +/** + * Tests for {@link EcjResponseParser}. Tests are written against ecj 3.15.0 + * output. + * + * @author Jason Faust + * @since 2.8.6 + */ +public class EcjResponseParserTest extends PlexusTestCase { + + private static final String LOG_FILE_1 = "src/test/resources/org/codehaus/plexus/compiler/eclipse/3.15.0.log.xml"; + private static final String LOG_FILE_2 = "src/test/resources/org/codehaus/plexus/compiler/eclipse/eclipse-withinfo.xml"; + + /** + * Test for example compiler output in XML format. + * + * @throws Exception on test failure. + */ + @Test + public void testParse() throws Exception { + File logFile = getTestFile(LOG_FILE_1); + assertTrue("Input log file missing", logFile.exists()); + + EcjResponseParser parser = new EcjResponseParser(); + List cm = parser.parse(logFile, false); + + assertEquals("Wrong message count", 9, cm.size()); + + checkCompilerMessage(cm.get(0), "Bad.java", Kind.ERROR, 6, 1, 6); + checkCompilerMessage(cm.get(1), "Bad.java", Kind.ERROR, 6, 1, 6); + checkCompilerMessage(cm.get(2), "Deprecation.java", Kind.WARNING, 7, 5, 31); + checkCompilerMessage(cm.get(3), "ExternalDeps.java", Kind.ERROR, 4, 8, 18); + checkCompilerMessage(cm.get(4), "ExternalDeps.java", Kind.ERROR, 12, 21, 32); + checkCompilerMessage(cm.get(5), "Info.java", Kind.NOTE, 5, 11, 12); + checkCompilerMessage(cm.get(6), "ReservedWord.java", Kind.ERROR, 5, 8, 14); + checkCompilerMessage(cm.get(7), "UnknownSymbol.java", Kind.ERROR, 7, 1, 4); + checkCompilerMessage(cm.get(8), "WrongClassname.java", Kind.ERROR, 3, 14, 28); + } + + /** + * Test for example compiler output in XML format with errors reported as + * warnings. + * + * @throws Exception on test failure. + */ + @Test + public void testParseErrorsAsWarnings() throws Exception { + File logFile = getTestFile(LOG_FILE_1); + assertTrue("Input log file missing", logFile.exists()); + + EcjResponseParser parser = new EcjResponseParser(); + List cm = parser.parse(logFile, true); + + assertEquals("Wrong message count", 9, cm.size()); + + checkCompilerMessage(cm.get(0), "Bad.java", Kind.WARNING, 6, 1, 6); + checkCompilerMessage(cm.get(1), "Bad.java", Kind.WARNING, 6, 1, 6); + checkCompilerMessage(cm.get(2), "Deprecation.java", Kind.WARNING, 7, 5, 31); + checkCompilerMessage(cm.get(3), "ExternalDeps.java", Kind.WARNING, 4, 8, 18); + checkCompilerMessage(cm.get(4), "ExternalDeps.java", Kind.WARNING, 12, 21, 32); + checkCompilerMessage(cm.get(5), "Info.java", Kind.NOTE, 5, 11, 12); + checkCompilerMessage(cm.get(6), "ReservedWord.java", Kind.WARNING, 5, 8, 14); + checkCompilerMessage(cm.get(7), "UnknownSymbol.java", Kind.WARNING, 7, 1, 4); + checkCompilerMessage(cm.get(8), "WrongClassname.java", Kind.WARNING, 3, 14, 28); + } + + /** + * Tests for parsing the contents of {@value #LOG_FILE_2}. + * + * @throws Exception on test failure. + */ + public void testParse2() throws Exception { + File logFile = getTestFile(LOG_FILE_2); + assertTrue("Input log file missing", logFile.exists()); + + EcjResponseParser parser = new EcjResponseParser(); + List cm = parser.parse(logFile, false); + + assertEquals("Wrong message count", 6, cm.size()); + + checkCompilerMessage(cm.get(0), "ECE.java", Kind.ERROR, 8, 13, 17); + checkCompilerMessage(cm.get(1), "ECE.java", Kind.ERROR, 16, 8, 41); + checkCompilerMessage(cm.get(2), "ECE.java", Kind.WARNING, 22, 9, 10); + checkCompilerMessage(cm.get(3), "ECE.java", Kind.WARNING, 27, 8, 41); + checkCompilerMessage(cm.get(4), "ECE.java", Kind.NOTE, 33, 13, 19); + checkCompilerMessage(cm.get(5), "ECE.java", Kind.NOTE, 35, 1, 96); + } + + private void checkCompilerMessage(CompilerMessage cm, String file, Kind kind, int line, int startCol, int endCol) { + assertTrue("Failure checking output for " + file, cm.getFile().endsWith(file)); + assertEquals("Failure checking output for " + file, kind, cm.getKind()); + assertEquals("Failure checking output for " + file, line, cm.getStartLine()); + assertEquals("Failure checking output for " + file, startCol, cm.getStartColumn()); + assertEquals("Failure checking output for " + file, endCol, cm.getEndColumn()); + } + +} diff --git a/plexus-compilers/plexus-compiler-eclipse/src/test/java/org/codehaus/plexus/compiler/eclipse/EcjTextLogParserTest.java b/plexus-compilers/plexus-compiler-eclipse/src/test/java/org/codehaus/plexus/compiler/eclipse/EcjTextLogParserTest.java new file mode 100644 index 00000000..a4eb3928 --- /dev/null +++ b/plexus-compilers/plexus-compiler-eclipse/src/test/java/org/codehaus/plexus/compiler/eclipse/EcjTextLogParserTest.java @@ -0,0 +1,126 @@ +package org.codehaus.plexus.compiler.eclipse; + +import java.io.File; +import java.util.List; + +import org.codehaus.plexus.PlexusTestCase; +import org.codehaus.plexus.compiler.CompilerMessage; +import org.codehaus.plexus.compiler.CompilerMessage.Kind; +import org.junit.Test; + +/** + * Tests for {@link EcjTextLogParser}. Tests are written against ecj 3.15.0 + * output. + * + * @author Jason Faust + * @since 2.8.6 + */ +public class EcjTextLogParserTest extends PlexusTestCase { + + private static final String LOG_FILE_1 = "src/test/resources/org/codehaus/plexus/compiler/eclipse/3.15.0.log.txt"; + private static final String LOG_FILE_2 = "src/test/resources/org/codehaus/plexus/compiler/eclipse/log-text-0.log"; + private static final String LOG_FILE_3 = "src/test/resources/org/codehaus/plexus/compiler/eclipse/eclipse-withinfo.txt"; + + /** + * Test for example compiler output in non-XML format. + * + * @throws Exception on test failure. + */ + @Test + public void testParse() throws Exception { + File logFile = getTestFile(LOG_FILE_1); + assertTrue("Input log file missing", logFile.exists()); + + EcjTextLogParser parser = new EcjTextLogParser(); + List cm = parser.parse(logFile, false); + + assertEquals("Wrong message count", 9, cm.size()); + + checkCompilerMessage(cm.get(0), "Bad.java", Kind.ERROR, 6, 1, 6); + checkCompilerMessage(cm.get(1), "Bad.java", Kind.ERROR, 6, 1, 6); + checkCompilerMessage(cm.get(2), "Deprecation.java", Kind.WARNING, 7, 5, 31); + checkCompilerMessage(cm.get(3), "ExternalDeps.java", Kind.ERROR, 4, 8, 18); + checkCompilerMessage(cm.get(4), "ExternalDeps.java", Kind.ERROR, 12, 21, 32); + checkCompilerMessage(cm.get(5), "Info.java", Kind.NOTE, 5, 11, 12); + checkCompilerMessage(cm.get(6), "ReservedWord.java", Kind.ERROR, 5, 8, 14); + checkCompilerMessage(cm.get(7), "UnknownSymbol.java", Kind.ERROR, 7, 1, 4); + checkCompilerMessage(cm.get(8), "WrongClassname.java", Kind.ERROR, 3, 14, 28); + } + + /** + * Test for example compiler output in non-XML format with errors reported as + * warnings. + * + * @throws Exception on test failure. + */ + @Test + public void testParseErrorsAsWarnings() throws Exception { + File logFile = getTestFile(LOG_FILE_1); + assertTrue("Input log file missing", logFile.exists()); + + EcjTextLogParser parser = new EcjTextLogParser(); + List cm = parser.parse(logFile, true); + + assertEquals("Wrong message count", 9, cm.size()); + + checkCompilerMessage(cm.get(0), "Bad.java", Kind.WARNING, 6, 1, 6); + checkCompilerMessage(cm.get(1), "Bad.java", Kind.WARNING, 6, 1, 6); + checkCompilerMessage(cm.get(2), "Deprecation.java", Kind.WARNING, 7, 5, 31); + checkCompilerMessage(cm.get(3), "ExternalDeps.java", Kind.WARNING, 4, 8, 18); + checkCompilerMessage(cm.get(4), "ExternalDeps.java", Kind.WARNING, 12, 21, 32); + checkCompilerMessage(cm.get(5), "Info.java", Kind.NOTE, 5, 11, 12); + checkCompilerMessage(cm.get(6), "ReservedWord.java", Kind.WARNING, 5, 8, 14); + checkCompilerMessage(cm.get(7), "UnknownSymbol.java", Kind.WARNING, 7, 1, 4); + checkCompilerMessage(cm.get(8), "WrongClassname.java", Kind.WARNING, 3, 14, 28); + } + + /** + * Test for ignoring output before first output delimiter. + * + * @throws Exception on test failure. + */ + @Test + public void testHeaderErrors() throws Exception { + File logFile = getTestFile(LOG_FILE_2); + assertTrue("Input log file missing", logFile.exists()); + + EcjTextLogParser parser = new EcjTextLogParser(); + List cm = parser.parse(logFile, false); + + assertEquals("Wrong message count", 2, cm.size()); + + checkCompilerMessage(cm.get(0), "Bad.java", Kind.ERROR, 6, 1, 6); + checkCompilerMessage(cm.get(1), "Bad.java", Kind.ERROR, 6, 1, 6); + } + + /** + * Tests for parsing the contents of {@value #LOG_FILE_3}. + * + * @throws Exception on test failure. + */ + public void testParse3() throws Exception { + File logFile = getTestFile(LOG_FILE_3); + assertTrue("Input log file missing", logFile.exists()); + + EcjTextLogParser parser = new EcjTextLogParser(); + List cm = parser.parse(logFile, false); + + assertEquals("Wrong message count", 6, cm.size()); + + checkCompilerMessage(cm.get(0), "ECE.java", Kind.ERROR, 8, 13, 17); + checkCompilerMessage(cm.get(1), "ECE.java", Kind.ERROR, 16, 8, 41); + checkCompilerMessage(cm.get(2), "ECE.java", Kind.WARNING, 22, 9, 10); + checkCompilerMessage(cm.get(3), "ECE.java", Kind.WARNING, 27, 8, 41); + checkCompilerMessage(cm.get(4), "ECE.java", Kind.NOTE, 33, 13, 19); + checkCompilerMessage(cm.get(5), "ECE.java", Kind.NOTE, 35, 1, 96); + } + + private void checkCompilerMessage(CompilerMessage cm, String file, Kind kind, int line, int startCol, int endCol) { + assertTrue("Failure checking output for " + file, cm.getFile().endsWith(file)); + assertEquals("Failure checking output for " + file, kind, cm.getKind()); + assertEquals("Failure checking output for " + file, line, cm.getStartLine()); + assertEquals("Failure checking output for " + file, startCol, cm.getStartColumn()); + assertEquals("Failure checking output for " + file, endCol, cm.getEndColumn()); + } + +} diff --git a/plexus-compilers/plexus-compiler-eclipse/src/test/java/org/codehaus/plexus/compiler/eclipse/EclipseCompilerBasicTest.java b/plexus-compilers/plexus-compiler-eclipse/src/test/java/org/codehaus/plexus/compiler/eclipse/EclipseCompilerBasicTest.java new file mode 100644 index 00000000..1334c2b4 --- /dev/null +++ b/plexus-compilers/plexus-compiler-eclipse/src/test/java/org/codehaus/plexus/compiler/eclipse/EclipseCompilerBasicTest.java @@ -0,0 +1,33 @@ +/* + * The MIT License + * + * Copyright (c) 2005, The Codehaus + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is furnished to do + * so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.codehaus.plexus.compiler.eclipse; + +/** + * @author Jason van Zyl + * @author Jason Faust + * @since 2.8.6 + */ +public class EclipseCompilerBasicTest extends AbstractEclipseCompilerTest { + // Empty +} diff --git a/plexus-compilers/plexus-compiler-eclipse/src/test/java/org/codehaus/plexus/compiler/eclipse/EclipseCompilerCustomArgumentTest.java b/plexus-compilers/plexus-compiler-eclipse/src/test/java/org/codehaus/plexus/compiler/eclipse/EclipseCompilerCustomArgumentTest.java new file mode 100644 index 00000000..8c86b09b --- /dev/null +++ b/plexus-compilers/plexus-compiler-eclipse/src/test/java/org/codehaus/plexus/compiler/eclipse/EclipseCompilerCustomArgumentTest.java @@ -0,0 +1,58 @@ +/* + * The MIT License + * + * Copyright (c) 2005, The Codehaus + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is furnished to do + * so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.codehaus.plexus.compiler.eclipse; + +import java.util.Collections; + +import org.codehaus.plexus.compiler.CompilerConfiguration; + +/** + * @author Jason van Zyl + * @author Jason Faust + */ +public class EclipseCompilerCustomArgumentTest extends AbstractEclipseCompilerTest { + + @SuppressWarnings("unchecked") + public EclipseCompilerCustomArgumentTest() { + super(0, 0, 0, Collections.EMPTY_LIST); + } + + @Override + protected void configureCompilerConfig(CompilerConfiguration compilerConfig) { + super.configureCompilerConfig(compilerConfig); + compilerConfig.addCompilerCustomArgument("-key", "value"); + } + + @Override + public void testCompilingSources() throws Exception { + try { + super.testCompilingSources(); + fail("Compile should of thrown an exception"); + } catch (EcjFailureException e) { + assertTrue("Unexpected compiler error", + e.getMessage().startsWith("Failed to run the ecj compiler: Unrecognized option : -key")); + } + } + +} diff --git a/plexus-compilers/plexus-compiler-eclipse/src/test/java/org/codehaus/plexus/compiler/eclipse/EclipseCompilerDashedArgumentsTest.java b/plexus-compilers/plexus-compiler-eclipse/src/test/java/org/codehaus/plexus/compiler/eclipse/EclipseCompilerDashedArgumentsTest.java index 8e9486e0..3fd64fd5 100644 --- a/plexus-compilers/plexus-compiler-eclipse/src/test/java/org/codehaus/plexus/compiler/eclipse/EclipseCompilerDashedArgumentsTest.java +++ b/plexus-compilers/plexus-compiler-eclipse/src/test/java/org/codehaus/plexus/compiler/eclipse/EclipseCompilerDashedArgumentsTest.java @@ -1,6 +1,4 @@ -package org.codehaus.plexus.compiler.eclipse; - -/** +/* * The MIT License * * Copyright (c) 2005, The Codehaus @@ -23,78 +21,43 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ +package org.codehaus.plexus.compiler.eclipse; -import org.codehaus.plexus.PlexusTestCase; -import org.codehaus.plexus.compiler.Compiler; -import org.codehaus.plexus.compiler.CompilerConfiguration; -import org.codehaus.plexus.util.FileUtils; -import org.junit.Assert; - -import java.io.File; import java.util.Collections; -import java.util.HashSet; -import java.util.List; -import java.util.Set; + +import org.codehaus.plexus.compiler.CompilerConfiguration; /** + * Created on 2018-04-22. + * * @author Frits Jalvingh - * Created on 22-4-18. + * @author Jason Faust */ -public class EclipseCompilerDashedArgumentsTest extends PlexusTestCase { +public class EclipseCompilerDashedArgumentsTest extends AbstractEclipseCompilerTest { public static final String BAD_DOUBLEDASH_OPTION = "--grubbelparkplace"; - private CompilerConfiguration getConfig() throws Exception { - String sourceDir = getBasedir() + "/src/test-input/src/main"; - - List filenames = FileUtils.getFileNames( new File( sourceDir ), "**/*.java", null, false, true ); - Collections.sort( filenames ); - Set files = new HashSet<>(); - for(String filename : filenames) - { - files.add(new File(filename)); - } - - CompilerConfiguration compilerConfig = new CompilerConfiguration(); - compilerConfig.setDebug(false); - compilerConfig.setShowDeprecation(false); - -// compilerConfig.setClasspathEntries( getClasspath() ); - compilerConfig.addSourceLocation( sourceDir ); - compilerConfig.setOutputLocation( getBasedir() + "/target/eclipse/classes"); - FileUtils.deleteDirectory( compilerConfig.getOutputLocation() ); -// compilerConfig.addInclude( filename ); - compilerConfig.setForceJavacCompilerUse(false); - compilerConfig.setSourceFiles(files); - - compilerConfig.setTargetVersion("1.8"); - compilerConfig.setSourceVersion("1.8"); - return compilerConfig; + @SuppressWarnings("unchecked") + public EclipseCompilerDashedArgumentsTest() { + super(0, 0, 0, Collections.EMPTY_LIST); } - /** - * Start the eclipse compiler with a bad option that has two dashes. It should abort, and the error - * message should show the actual bad option with two dashes. This ensures that both dashes are passed - * to the compiler proper. - * - * This also tests that con-compile errors are shown properly, as the error caused by - * the invalid option is not part of the error output but part of the data sent to stdout/stderr. - */ - public void testDoubleDashOptionsArePassedWithTwoDashes() throws Exception - { - Compiler compiler = (Compiler) lookup( Compiler.ROLE, "eclipse" ); - CompilerConfiguration config = getConfig(); - config.addCompilerCustomArgument(BAD_DOUBLEDASH_OPTION, "b0rk3d"); + @Override + protected void configureCompilerConfig(CompilerConfiguration compilerConfig) { + super.configureCompilerConfig(compilerConfig); + compilerConfig.addCompilerCustomArgument(BAD_DOUBLEDASH_OPTION, "b0rk3d"); + } - try - { - compiler.performCompile(config); - Assert.fail("Expected an exception to be thrown"); - } catch(EcjFailureException x) { - String ecjOutput = x.getEcjOutput(); - Assert.assertTrue("The output should report the failure with two dashes: " + ecjOutput - , ecjOutput.contains(BAD_DOUBLEDASH_OPTION) && ! ecjOutput.contains("-" + BAD_DOUBLEDASH_OPTION) - ); + @Override + public void testCompilingSources() throws Exception { + try { + super.testCompilingSources(); + fail("Expected an exception to be thrown"); + } catch (EcjFailureException e) { + String ecjOutput = e.getEcjOutput(); + assertTrue("The output should report the failure with two dashes: " + ecjOutput, + ecjOutput.contains(BAD_DOUBLEDASH_OPTION) && !ecjOutput.contains("-" + BAD_DOUBLEDASH_OPTION)); } } + } diff --git a/plexus-compilers/plexus-compiler-eclipse/src/test/java/org/codehaus/plexus/compiler/eclipse/EclipseCompilerErrorsAsWarningsTest.java b/plexus-compilers/plexus-compiler-eclipse/src/test/java/org/codehaus/plexus/compiler/eclipse/EclipseCompilerErrorsAsWarningsTest.java index 363e0cd7..fb923718 100644 --- a/plexus-compilers/plexus-compiler-eclipse/src/test/java/org/codehaus/plexus/compiler/eclipse/EclipseCompilerErrorsAsWarningsTest.java +++ b/plexus-compilers/plexus-compiler-eclipse/src/test/java/org/codehaus/plexus/compiler/eclipse/EclipseCompilerErrorsAsWarningsTest.java @@ -1,52 +1,23 @@ package org.codehaus.plexus.compiler.eclipse; -import org.codehaus.plexus.compiler.AbstractCompilerTest; import org.codehaus.plexus.compiler.CompilerConfiguration; -import java.util.Arrays; -import java.util.Collection; +/** + * Test for errors being reported as warnings. + * + * @author Jason Faust + * @since 2.8.6 + */ +public class EclipseCompilerErrorsAsWarningsTest extends AbstractEclipseCompilerTest { -public class EclipseCompilerErrorsAsWarningsTest extends AbstractCompilerTest -{ - - protected void configureCompilerConfig( CompilerConfiguration compilerConfig ) - { - compilerConfig.addCompilerCustomArgument( "-errorsAsWarnings", "true" ); + public EclipseCompilerErrorsAsWarningsTest() { + super(0, 6, 1); } - public void setUp() throws Exception - { - super.setUp(); - - setCompilerDebug( true ); - setCompilerDeprecationWarnings( true ); + @Override + protected void configureCompilerConfig(CompilerConfiguration compilerConfig) { + super.configureCompilerConfig(compilerConfig); + compilerConfig.addCompilerCustomArgument("-errorsAsWarnings", "true"); } - protected String getRoleHint() - { - return "eclipse"; - } - - protected int expectedErrors() - { - return 0; - } - - protected int expectedWarnings() - { - return 6; - } - - protected Collection expectedOutputFiles() - { - return Arrays.asList( new String[] { - "org/codehaus/foo/Deprecation.class", - "org/codehaus/foo/ExternalDeps.class", - "org/codehaus/foo/Person.class", - "org/codehaus/foo/ReservedWord.class", - //"org/codehaus/foo/Bad.class", // This one has no class file generated as it's one big issue - //"org/codehaus/foo/UnknownSymbol.class", - //"org/codehaus/foo/RightClassname.class" - }); - } } diff --git a/plexus-compilers/plexus-compiler-eclipse/src/test/java/org/codehaus/plexus/compiler/eclipse/EclipseCompilerInitializeWarningsForPropertiesArgumentTest.java b/plexus-compilers/plexus-compiler-eclipse/src/test/java/org/codehaus/plexus/compiler/eclipse/EclipseCompilerInitializeWarningsForPropertiesArgumentTest.java new file mode 100644 index 00000000..55b40388 --- /dev/null +++ b/plexus-compilers/plexus-compiler-eclipse/src/test/java/org/codehaus/plexus/compiler/eclipse/EclipseCompilerInitializeWarningsForPropertiesArgumentTest.java @@ -0,0 +1,58 @@ +/* + * The MIT License + * + * Copyright (c) 2005, The Codehaus + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of + * this software and associated documentation files (the "Software"), to deal in + * the Software without restriction, including without limitation the rights to + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies + * of the Software, and to permit persons to whom the Software is furnished to do + * so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package org.codehaus.plexus.compiler.eclipse; + +import java.util.Collections; + +import org.codehaus.plexus.compiler.CompilerConfiguration; + +/** + * @author Jason van Zyl + * @author Jason Faust + */ +public class EclipseCompilerInitializeWarningsForPropertiesArgumentTest extends AbstractEclipseCompilerTest { + + @SuppressWarnings("unchecked") + public EclipseCompilerInitializeWarningsForPropertiesArgumentTest() { + super(0, 0, 0, Collections.EMPTY_LIST); + } + + @Override + protected void configureCompilerConfig(CompilerConfiguration compilerConfig) { + super.configureCompilerConfig(compilerConfig); + compilerConfig.addCompilerCustomArgument("-properties", "file_does_not_exist"); + } + + @Override + public void testCompilingSources() throws Exception { + try { + super.testCompilingSources(); + fail("looking up the properties file should have thrown an exception"); + } catch (EcjFailureException e) { + assertTrue("Unexpected compiler error", + e.getMessage().startsWith("Failed to run the ecj compiler: Properties file")); + } + } + +} diff --git a/plexus-compilers/plexus-compiler-eclipse/src/test/java/org/codehaus/plexus/compiler/eclipse/EclipseCompilerTckTest.java b/plexus-compilers/plexus-compiler-eclipse/src/test/java/org/codehaus/plexus/compiler/eclipse/EclipseCompilerTckTest.java index c1999e0b..e5a28d71 100644 --- a/plexus-compilers/plexus-compiler-eclipse/src/test/java/org/codehaus/plexus/compiler/eclipse/EclipseCompilerTckTest.java +++ b/plexus-compilers/plexus-compiler-eclipse/src/test/java/org/codehaus/plexus/compiler/eclipse/EclipseCompilerTckTest.java @@ -1,6 +1,4 @@ -package org.codehaus.plexus.compiler.eclipse; - -/** +/* * The MIT License * * Copyright (c) 2005, The Codehaus @@ -23,17 +21,15 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ +package org.codehaus.plexus.compiler.eclipse; import org.codehaus.plexus.compiler.AbstractCompilerTckTest; /** * @author Trygve Laugstøl */ -public class EclipseCompilerTckTest - extends AbstractCompilerTckTest -{ - public EclipseCompilerTckTest() - { - super( "eclipse" ); +public class EclipseCompilerTckTest extends AbstractCompilerTckTest { + public EclipseCompilerTckTest() { + super("eclipse"); } } diff --git a/plexus-compilers/plexus-compiler-eclipse/src/test/java/org/codehaus/plexus/compiler/eclipse/EclipseCompilerTest.java b/plexus-compilers/plexus-compiler-eclipse/src/test/java/org/codehaus/plexus/compiler/eclipse/EclipseCompilerTest.java deleted file mode 100644 index aa5d6259..00000000 --- a/plexus-compilers/plexus-compiler-eclipse/src/test/java/org/codehaus/plexus/compiler/eclipse/EclipseCompilerTest.java +++ /dev/null @@ -1,111 +0,0 @@ -package org.codehaus.plexus.compiler.eclipse; - -/** - * The MIT License - * - * Copyright (c) 2005, The Codehaus - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of - * this software and associated documentation files (the "Software"), to deal in - * the Software without restriction, including without limitation the rights to - * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies - * of the Software, and to permit persons to whom the Software is furnished to do - * so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -import org.codehaus.plexus.compiler.AbstractCompilerTest; -import org.codehaus.plexus.compiler.Compiler; -import org.codehaus.plexus.compiler.CompilerConfiguration; - -import java.util.Arrays; -import java.util.Collection; - -/** - * @author Jason van Zyl - */ -public class EclipseCompilerTest - extends AbstractCompilerTest -{ - - public void setUp() - throws Exception - { - super.setUp(); - - setCompilerDebug( true ); - setCompilerDeprecationWarnings( true ); - } - - protected String getRoleHint() - { - return "eclipse"; - } - - protected int expectedErrors() - { - return 4; - } - - protected int expectedWarnings() - { - return 2; - } - - protected Collection expectedOutputFiles() - { - return Arrays.asList( new String[] { "org/codehaus/foo/Deprecation.class", "org/codehaus/foo/ExternalDeps.class", - "org/codehaus/foo/Person.class", "org/codehaus/foo/ReservedWord.class" } ); - } - - // The test is fairly meaningless as we can not validate anything - public void testCustomArgument() - throws Exception - { - org.codehaus.plexus.compiler.Compiler compiler = (Compiler) lookup( Compiler.ROLE, getRoleHint() ); - - CompilerConfiguration compilerConfig = createMinimalCompilerConfig(); - - compilerConfig.addCompilerCustomArgument( "-key", "value" ); - - compiler.performCompile( compilerConfig ); - } - - public void testInitializeWarningsForPropertiesArgument() - throws Exception - { - org.codehaus.plexus.compiler.Compiler compiler = (Compiler) lookup( Compiler.ROLE, getRoleHint() ); - - CompilerConfiguration compilerConfig = createMinimalCompilerConfig(); - - compilerConfig.addCompilerCustomArgument( "-properties", "file_does_not_exist" ); - - try - { - compiler.performCompile( compilerConfig ); - fail( "looking up the properties file should have thrown an exception" ); - } - catch ( IllegalArgumentException e ) - { - assertTrue("Message must start with 'Properties file'", e.getMessage().startsWith("Properties file")); - } - } - - private CompilerConfiguration createMinimalCompilerConfig() - { - CompilerConfiguration compilerConfig = new CompilerConfiguration(); - compilerConfig.setOutputLocation( getBasedir() + "/target/" + getRoleHint() + "/classes-CustomArgument" ); - return compilerConfig; - } - -} diff --git a/plexus-compilers/plexus-compiler-eclipse/src/test/java/org/codehaus/plexus/compiler/eclipse/EclipseCompilerTextLogTest.java b/plexus-compilers/plexus-compiler-eclipse/src/test/java/org/codehaus/plexus/compiler/eclipse/EclipseCompilerTextLogTest.java new file mode 100644 index 00000000..deb50fb4 --- /dev/null +++ b/plexus-compilers/plexus-compiler-eclipse/src/test/java/org/codehaus/plexus/compiler/eclipse/EclipseCompilerTextLogTest.java @@ -0,0 +1,36 @@ +package org.codehaus.plexus.compiler.eclipse; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +import org.codehaus.plexus.compiler.CompilerConfiguration; + +/** + * Test of a XML user log specified. + * + * @author Jason Faust + * @since 2.8.6 + */ +public class EclipseCompilerTextLogTest extends AbstractEclipseCompilerTest { + + private List logFiles = new ArrayList<>(); + private int idx = 0; + + @Override + protected void configureCompilerConfig(CompilerConfiguration compilerConfig, String filename) { + super.configureCompilerConfig(compilerConfig, filename); + String logFile = getTestPath("/target/" + getRoleHint() + "/log-text-" + idx + ".log"); + compilerConfig.addCompilerCustomArgument("-log", logFile); + idx++; + } + + @Override + public void testCompilingSources() throws Exception { + super.testCompilingSources(); + for (String file : logFiles) { + assertTrue(new File(file).exists()); + } + } + +} diff --git a/plexus-compilers/plexus-compiler-eclipse/src/test/java/org/codehaus/plexus/compiler/eclipse/EclipseCompilerXMLLogTest.java b/plexus-compilers/plexus-compiler-eclipse/src/test/java/org/codehaus/plexus/compiler/eclipse/EclipseCompilerXMLLogTest.java new file mode 100644 index 00000000..4d2d23c4 --- /dev/null +++ b/plexus-compilers/plexus-compiler-eclipse/src/test/java/org/codehaus/plexus/compiler/eclipse/EclipseCompilerXMLLogTest.java @@ -0,0 +1,36 @@ +package org.codehaus.plexus.compiler.eclipse; + +import java.io.File; +import java.util.ArrayList; +import java.util.List; + +import org.codehaus.plexus.compiler.CompilerConfiguration; + +/** + * Test of a XML user log specified. + * + * @author Jason Faust + * @since 2.8.6 + */ +public class EclipseCompilerXMLLogTest extends AbstractEclipseCompilerTest { + + private List logFiles = new ArrayList<>(); + private int idx = 0; + + @Override + protected void configureCompilerConfig(CompilerConfiguration compilerConfig, String filename) { + super.configureCompilerConfig(compilerConfig, filename); + String logFile = getTestPath("/target/" + getRoleHint() + "/log-xml-" + idx + ".xml"); + compilerConfig.addCompilerCustomArgument("-log", logFile); + idx++; + } + + @Override + public void testCompilingSources() throws Exception { + super.testCompilingSources(); + for (String file : logFiles) { + assertTrue(new File(file).exists()); + } + } + +} diff --git a/plexus-compilers/plexus-compiler-eclipse/src/test/resources/org/codehaus/plexus/compiler/eclipse/3.15.0.log.txt b/plexus-compilers/plexus-compiler-eclipse/src/test/resources/org/codehaus/plexus/compiler/eclipse/3.15.0.log.txt new file mode 100644 index 00000000..cb9ce953 --- /dev/null +++ b/plexus-compilers/plexus-compiler-eclipse/src/test/resources/org/codehaus/plexus/compiler/eclipse/3.15.0.log.txt @@ -0,0 +1,55 @@ +# 9/26/18 1:10:25 PM EDT +# Eclipse Compiler for Java(TM) v20180905-0317, 3.15.0, Copyright IBM Corp 2000, 2015. All rights reserved. +---------- +1. ERROR in C:\devenv\gitrepo\plexus-compiler\plexus-compilers\plexus-compiler-eclipse\src\test-input\src\main\org\codehaus\foo\Bad.java (at line 6) + pubic String name; + ^^^^^ +Syntax error on token "pubic", public expected +---------- +2. ERROR in C:\devenv\gitrepo\plexus-compiler\plexus-compilers\plexus-compiler-eclipse\src\test-input\src\main\org\codehaus\foo\Bad.java (at line 6) + pubic String name; + ^^^^^ +pubic cannot be resolved to a type +---------- +---------- +3. WARNING in C:\devenv\gitrepo\plexus-compiler\plexus-compilers\plexus-compiler-eclipse\src\test-input\src\main\org\codehaus\foo\Deprecation.java (at line 7) + new java.util.Date("testDate"); + ^^^^^^^^^^^^^^^^^^^^^^^^^^ +The constructor Date(String) is deprecated +---------- +---------- +4. ERROR in C:\devenv\gitrepo\plexus-compiler\plexus-compilers\plexus-compiler-eclipse\src\test-input\src\main\org\codehaus\foo\ExternalDeps.java (at line 4) + import org.apache.commons.lang.StringUtils; + ^^^^^^^^^^ +The import org.apache cannot be resolved +---------- +5. ERROR in C:\devenv\gitrepo\plexus-compiler\plexus-compilers\plexus-compiler-eclipse\src\test-input\src\main\org\codehaus\foo\ExternalDeps.java (at line 12) + System.out.println( StringUtils.upperCase( str) ); + ^^^^^^^^^^^ +StringUtils cannot be resolved +---------- +---------- +6. INFO in C:\devenv\gitrepo\plexus-compiler\plexus-compilers\plexus-compiler-eclipse\src\test-input\src\main\org\codehaus\foo\Info.java (at line 5) + "".equals(1); + ^ +Unlikely argument type for equals(): int seems to be unrelated to String +---------- +---------- +7. ERROR in C:\devenv\gitrepo\plexus-compiler\plexus-compilers\plexus-compiler-eclipse\src\test-input\src\main\org\codehaus\foo\ReservedWord.java (at line 5) + String assert; + ^^^^^^ +Syntax error on token "assert", invalid VariableDeclarator +---------- +---------- +8. ERROR in C:\devenv\gitrepo\plexus-compiler\plexus-compilers\plexus-compiler-eclipse\src\test-input\src\main\org\codehaus\foo\UnknownSymbol.java (at line 7) + foo(); + ^^^ +The method foo() is undefined for the type UnknownSymbol +---------- +---------- +9. ERROR in C:\devenv\gitrepo\plexus-compiler\plexus-compilers\plexus-compiler-eclipse\src\test-input\src\main\org\codehaus\foo\WrongClassname.java (at line 3) + public class RightClassname + ^^^^^^^^^^^^^^ +The public type RightClassname must be defined in its own file +---------- +9 problems (7 errors, 1 warning, 1 info) diff --git a/plexus-compilers/plexus-compiler-eclipse/src/test/resources/org/codehaus/plexus/compiler/eclipse/3.15.0.log.xml b/plexus-compilers/plexus-compiler-eclipse/src/test/resources/org/codehaus/plexus/compiler/eclipse/3.15.0.log.xml new file mode 100644 index 00000000..c1b34d7c --- /dev/null +++ b/plexus-compilers/plexus-compiler-eclipse/src/test/resources/org/codehaus/plexus/compiler/eclipse/3.15.0.log.xml @@ -0,0 +1,282 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/plexus-compilers/plexus-compiler-eclipse/src/test/resources/org/codehaus/plexus/compiler/eclipse/eclipse-withinfo.txt b/plexus-compilers/plexus-compiler-eclipse/src/test/resources/org/codehaus/plexus/compiler/eclipse/eclipse-withinfo.txt new file mode 100644 index 00000000..bc7102ad --- /dev/null +++ b/plexus-compilers/plexus-compiler-eclipse/src/test/resources/org/codehaus/plexus/compiler/eclipse/eclipse-withinfo.txt @@ -0,0 +1,42 @@ +# 10/15/18, 2:50:59 PM EDT +# Eclipse Compiler for Java(TM) v20180905-0317, 3.15.0, Copyright IBM Corp 2000, 2015. All rights reserved. +---------- +1. ERROR in C:\devenv\workspace\x\y\src\main\java\y\ECE.java (at line 8) + Integer x = 0.0f; + ^^^^ +Type mismatch: cannot convert from float to Integer +---------- +2. ERROR in C:\devenv\workspace\x\y\src\main\java\y\ECE.java (at line 16) + } else { + x = 20; + } + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +Dead code +---------- +3. WARNING in C:\devenv\workspace\x\y\src\main\java\y\ECE.java (at line 22) + Integer x; + ^ +The value of the local variable x is not used +---------- +4. WARNING in C:\devenv\workspace\x\y\src\main\java\y\ECE.java (at line 27) + } else { + x = 30; + } + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +Statement unnecessarily nested within else clause. The corresponding then clause does not complete normally +---------- +5. INFO in C:\devenv\workspace\x\y\src\main\java\y\ECE.java (at line 33) + Boolean x = 7 == 7; + ^^^^^^ +Comparing identical expressions +---------- +6. INFO in C:\devenv\workspace\x\y\src\main\java\y\ECE.java (at line 35) + new Object() { + { + System.out.println(x); + } + }; + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +The allocated object is never used +---------- +6 problems (2 errors, 2 warnings, 2 info) diff --git a/plexus-compilers/plexus-compiler-eclipse/src/test/resources/org/codehaus/plexus/compiler/eclipse/eclipse-withinfo.xml b/plexus-compilers/plexus-compiler-eclipse/src/test/resources/org/codehaus/plexus/compiler/eclipse/eclipse-withinfo.xml new file mode 100644 index 00000000..0204c553 --- /dev/null +++ b/plexus-compilers/plexus-compiler-eclipse/src/test/resources/org/codehaus/plexus/compiler/eclipse/eclipse-withinfo.xml @@ -0,0 +1,230 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/plexus-compilers/plexus-compiler-eclipse/src/test/resources/org/codehaus/plexus/compiler/eclipse/log-text-0.log b/plexus-compilers/plexus-compiler-eclipse/src/test/resources/org/codehaus/plexus/compiler/eclipse/log-text-0.log new file mode 100644 index 00000000..e492cce0 --- /dev/null +++ b/plexus-compilers/plexus-compiler-eclipse/src/test/resources/org/codehaus/plexus/compiler/eclipse/log-text-0.log @@ -0,0 +1,15 @@ +# 9/26/18 4:37:25 PM EDT +# Eclipse Compiler for Java(TM) v20180905-0317, 3.15.0, Copyright IBM Corp 2000, 2015. All rights reserved. +incorrect classpath: C:\devenv\gitrepo\plexus-compiler\plexus-compilers\plexus-compiler-eclipse/target/eclipse/classes-0 +---------- +1. ERROR in C:\devenv\gitrepo\plexus-compiler\plexus-compilers\plexus-compiler-eclipse\src\test-input\src\main\org\codehaus\foo\Bad.java (at line 6) + pubic String name; + ^^^^^ +Syntax error on token "pubic", public expected +---------- +2. ERROR in C:\devenv\gitrepo\plexus-compiler\plexus-compilers\plexus-compiler-eclipse\src\test-input\src\main\org\codehaus\foo\Bad.java (at line 6) + pubic String name; + ^^^^^ +pubic cannot be resolved to a type +---------- +2 problems (2 errors) diff --git a/plexus-compilers/plexus-compiler-eclipse/src/test/resources/org/codehaus/plexus/compiler/eclipse/log-xml-0.xml b/plexus-compilers/plexus-compiler-eclipse/src/test/resources/org/codehaus/plexus/compiler/eclipse/log-xml-0.xml new file mode 100644 index 00000000..9d1ed77a --- /dev/null +++ b/plexus-compilers/plexus-compiler-eclipse/src/test/resources/org/codehaus/plexus/compiler/eclipse/log-xml-0.xml @@ -0,0 +1,209 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +