Skip to content

Commit d775465

Browse files
fjalvingholamy
authored andcommitted
Fix JUnit test.
1 parent cc1ba27 commit d775465

File tree

2 files changed

+63
-43
lines changed

2 files changed

+63
-43
lines changed

plexus-compilers/plexus-compiler-eclipse/src/main/java/org/codehaus/plexus/compiler/eclipse/EclipseJavaCompiler.java

Lines changed: 58 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,7 @@
3838
import org.eclipse.jdt.core.compiler.batch.BatchCompiler;
3939
import org.eclipse.jdt.internal.compiler.ClassFile;
4040
import org.eclipse.jdt.internal.compiler.CompilationResult;
41-
import org.eclipse.jdt.internal.compiler.DefaultErrorHandlingPolicies;
4241
import org.eclipse.jdt.internal.compiler.ICompilerRequestor;
43-
import org.eclipse.jdt.internal.compiler.IErrorHandlingPolicy;
4442
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader;
4543
import org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException;
4644
import org.eclipse.jdt.internal.compiler.env.ICompilationUnit;
@@ -56,14 +54,11 @@
5654
import java.io.IOException;
5755
import java.io.InputStream;
5856
import java.io.PrintWriter;
57+
import java.io.StringWriter;
5958
import java.io.Writer;
60-
import java.net.MalformedURLException;
61-
import java.net.URL;
62-
import java.net.URLClassLoader;
6359
import java.util.ArrayList;
6460
import java.util.Iterator;
6561
import java.util.LinkedHashMap;
66-
import java.util.LinkedList;
6762
import java.util.List;
6863
import java.util.Map;
6964
import java.util.Properties;
@@ -89,35 +84,28 @@ public EclipseJavaCompiler()
8984
public CompilerResult performCompile( CompilerConfiguration config )
9085
throws CompilerException
9186
{
92-
List<CompilerMessage> errors = new LinkedList<CompilerMessage>();
9387

94-
List<String> classpathEntries = config.getClasspathEntries();
95-
96-
URL[] urls = new URL[1 + classpathEntries.size()];
97-
98-
int i = 0;
99-
100-
try
101-
{
102-
urls[i++] = new File( config.getOutputLocation() ).toURL();
103-
104-
for ( String entry : classpathEntries )
105-
{
106-
urls[i++] = new File( entry ).toURL();
107-
}
108-
}
109-
catch ( MalformedURLException e )
110-
{
111-
throw new CompilerException( "Error while converting the classpath entries to URLs.", e );
112-
}
113-
114-
ClassLoader classLoader = new URLClassLoader( urls );
115-
116-
SourceCodeLocator sourceCodeLocator = new SourceCodeLocator( config.getSourceLocations() );
117-
118-
INameEnvironment env = new EclipseCompilerINameEnvironment( sourceCodeLocator, classLoader, errors );
88+
//URL[] urls = new URL[1 + classpathEntries.size()];
89+
//
90+
//int i = 0;
91+
//
92+
//try
93+
//{
94+
// urls[i++] = new File( config.getOutputLocation() ).toURL();
95+
//
96+
// for ( String entry : classpathEntries )
97+
// {
98+
// urls[i++] = new File( entry ).toURL();
99+
// }
100+
//}
101+
//catch ( MalformedURLException e )
102+
//{
103+
// throw new CompilerException( "Error while converting the classpath entries to URLs.", e );
104+
//}
119105

120-
IErrorHandlingPolicy policy = DefaultErrorHandlingPolicies.proceedWithAllProblems();
106+
//ClassLoader classLoader = new URLClassLoader( urls );
107+
//
108+
//SourceCodeLocator sourceCodeLocator = new SourceCodeLocator( config.getSourceLocations() );
121109

122110
// ----------------------------------------------------------------------
123111
// Build settings from configuration
@@ -192,6 +180,15 @@ public CompilerResult performCompile( CompilerConfiguration config )
192180
this.errorsAsWarnings = true;
193181
}
194182

183+
//-- Handle the properties silliness
184+
String props = extras.get("properties");
185+
if(null == props)
186+
props = extras.get("properties");
187+
if(null != props) {
188+
File propFile = new File(props);
189+
if(! propFile.exists() || ! propFile.isFile())
190+
throw new IllegalArgumentException("Properties file " + propFile + " does not exist");
191+
}
195192
//settings.putAll( extras );
196193

197194
//if ( settings.containsKey( "properties" ) )
@@ -257,6 +254,20 @@ public CompilerResult performCompile( CompilerConfiguration config )
257254
}
258255
}
259256

257+
// Output path
258+
args.add("-d");
259+
args.add(config.getOutputLocation());
260+
261+
//-- Write .class files even when error occur, but make sure methods with compile errors do abort when called
262+
args.add("-proceedOnError:Fatal");
263+
264+
//-- classpath
265+
List<String> classpathEntries = config.getClasspathEntries();
266+
if(classpathEntries.size() != 0) {
267+
args.add("-classpath");
268+
args.add(getPathString(classpathEntries));
269+
}
270+
260271
// ----------------------------------------------------------------------
261272
// Compile!
262273
// ----------------------------------------------------------------------
@@ -280,10 +291,11 @@ public CompilerResult performCompile( CompilerConfiguration config )
280291

281292
System.out.println(">>>> ECJ: " + args);
282293

283-
PrintWriter devNull = new PrintWriter(new NullWriter());
294+
StringWriter sw = new StringWriter();
295+
PrintWriter devNull = new PrintWriter(sw);
284296

285297
//BatchCompiler.compile(args.toArray(new String[args.size()]), new PrintWriter(System.err), new PrintWriter(System.out), new CompilationProgress() {
286-
BatchCompiler.compile(args.toArray(new String[args.size()]), devNull, devNull, new CompilationProgress() {
298+
boolean worked = BatchCompiler.compile(args.toArray(new String[args.size()]), devNull, devNull, new CompilationProgress() {
287299
@Override public void begin(int i) {
288300

289301
}
@@ -305,16 +317,24 @@ public CompilerResult performCompile( CompilerConfiguration config )
305317
}
306318
});
307319

308-
List<CompilerMessage> messageList = new EcjResponseParser().parse(errorF, errorsAsWarnings);
309-
320+
List<CompilerMessage> messageList;
310321
boolean hasError = false;
311-
for(CompilerMessage compilerMessage : errors) {
322+
if(errorF.length() < 80) {
323+
messageList = new ArrayList<>();
324+
messageList.add(new CompilerMessage("Internal compiler error"));
325+
System.err.println(">> " + sw.toString());
326+
return new CompilerResult(false, messageList);
327+
}
328+
messageList = new EcjResponseParser().parse(errorF, errorsAsWarnings);
329+
330+
for(CompilerMessage compilerMessage : messageList) {
312331
if(compilerMessage.isError()) {
313332
hasError = true;
314333
break;
315334
}
316335
}
317336
return new CompilerResult(! hasError, messageList);
337+
318338
} catch(Exception x) {
319339
throw new RuntimeException(x); // sigh
320340
} finally {

plexus-compilers/plexus-compiler-eclipse/src/test/java/org/codehaus/plexus/compiler/eclipse/EclipseCompilerTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,14 @@
2424
* SOFTWARE.
2525
*/
2626

27-
import java.util.Arrays;
28-
import java.util.Collection;
29-
import java.util.Map;
30-
3127
import org.codehaus.plexus.compiler.AbstractCompilerTest;
3228
import org.codehaus.plexus.compiler.Compiler;
3329
import org.codehaus.plexus.compiler.CompilerConfiguration;
3430

31+
import java.util.Arrays;
32+
import java.util.Collection;
33+
import java.util.Map;
34+
3535
/**
3636
* @author <a href="mailto:[email protected]">Jason van Zyl</a>
3737
*/
@@ -117,7 +117,7 @@ public void testInitializeWarningsForPropertiesArgument()
117117
}
118118
catch ( IllegalArgumentException e )
119119
{
120-
assertEquals( "Properties file not exist", e.getMessage() );
120+
assertTrue("Message must start with 'Properties file'", e.getMessage().startsWith("Properties file"));
121121
}
122122
}
123123

0 commit comments

Comments
 (0)