Skip to content

Commit 36fb5bb

Browse files
committed
EvaluationContextWrapperTest cleanup
- don't hide exceptions - use nio to properly report failures - record and print compiler output on fail See eclipse-jdt#2998
1 parent 1748c42 commit 36fb5bb

File tree

1 file changed

+25
-48
lines changed

1 file changed

+25
-48
lines changed

org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/eval/EvaluationContextWrapperTest.java

Lines changed: 25 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@
1313
*******************************************************************************/
1414
package org.eclipse.jdt.core.tests.eval;
1515

16-
import java.io.BufferedWriter;
1716
import java.io.File;
18-
import java.io.FileWriter;
19-
import java.io.IOException;
2017
import java.io.PrintWriter;
18+
import java.io.StringWriter;
19+
import java.nio.file.Files;
20+
import java.nio.file.Paths;
2121
import java.util.ArrayList;
2222
import java.util.HashMap;
2323
import java.util.List;
@@ -91,7 +91,7 @@ protected void tearDown() throws Exception {
9191
super.tearDown();
9292
}
9393

94-
public void testBug573589_StaticImport() throws JavaModelException, InstallException {
94+
public void testBug573589_StaticImport() throws Exception {
9595
try {
9696
StringBuilder source = new StringBuilder();
9797
source.append("import static java.lang.Math.max;\n");
@@ -116,7 +116,7 @@ public void testBug573589_StaticImport() throws JavaModelException, InstallExcep
116116
}
117117
}
118118

119-
public void testBug573589_StaticImport_AttachedSource() throws JavaModelException, InstallException {
119+
public void testBug573589_StaticImport_AttachedSource() throws Exception {
120120
try {
121121
StringBuilder source = new StringBuilder();
122122
source.append("import static java.lang.Math.max;\n");
@@ -142,11 +142,11 @@ public void testBug573589_StaticImport_AttachedSource() throws JavaModelExceptio
142142
}
143143
}
144144

145-
private void compileAndDeploy15(String source, String className) {
145+
private void compileAndDeploy15(String source, String className) throws Exception {
146146
compileAndDeploy15(source, className, "");
147147
}
148148

149-
private Map<String, String> compileAndDeploy15(String source, String className, String locationPrefix) {
149+
private Map<String, String> compileAndDeploy15(String source, String className, String locationPrefix) throws Exception {
150150
resetEnv(); // needed to reinitialize the caches
151151
String srcDir = this.project.getProject().getLocation().toFile().getAbsolutePath() + File.separator + SOURCE_DIRECTORY.concat(locationPrefix);
152152
String binDir = this.project.getProject().getLocation().toFile().getAbsolutePath() + File.separator + BIN_DIR.concat(locationPrefix);
@@ -156,22 +156,11 @@ private Map<String, String> compileAndDeploy15(String source, String className,
156156

157157
File directory = new File(srcDir);
158158
if (!directory.exists()) {
159-
if (!directory.mkdir()) {
160-
System.out.println("Could not create " + srcDir);
161-
return result;
162-
}
159+
Files.createDirectories(directory.toPath());
163160
}
164161

165162
String fileName = srcDir + File.separator + className + ".java";
166-
try {
167-
BufferedWriter writer = new BufferedWriter(new FileWriter(fileName));
168-
writer.write(source);
169-
writer.flush();
170-
writer.close();
171-
} catch(IOException e) {
172-
e.printStackTrace();
173-
return result;
174-
}
163+
Files.write(Paths.get(fileName), source.getBytes());
175164
StringBuilder buffer = new StringBuilder();
176165
buffer
177166
.append("\"")
@@ -182,16 +171,19 @@ private Map<String, String> compileAndDeploy15(String source, String className,
182171
.append(Util.getJavaClassLibsAsString())
183172
.append(srcDir)
184173
.append("\"");
185-
BatchCompiler.compile(buffer.toString(), new PrintWriter(System.out), new PrintWriter(System.err), null/*progress*/);
174+
StringWriter out = new StringWriter();
175+
StringWriter err = new StringWriter();
176+
PrintWriter errWriter = new PrintWriter(out);
177+
PrintWriter outWriter = new PrintWriter(err);
178+
boolean compiled = BatchCompiler.compile(buffer.toString(), outWriter, errWriter, null/*progress*/);
179+
if (!compiled) {
180+
fail("Failed to compile '" + className + "', system error: '" + err.toString() + "', system out: '" + out.toString()+ "'");
181+
}
186182
return result;
187183
}
188184

189-
private void refreshProject() {
190-
try {
191-
this.project.getProject().refreshLocal(IResource.DEPTH_INFINITE, null);
192-
} catch (CoreException e1) {
193-
e1.printStackTrace();
194-
}
185+
private void refreshProject() throws Exception {
186+
this.project.getProject().refreshLocal(IResource.DEPTH_INFINITE, null);
195187
}
196188

197189
private void removeTempClass(String className) {
@@ -261,10 +253,7 @@ private IJavaProject createProject(String name) throws Exception {
261253
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
262254
IProject proj = root.getProject(name);
263255
if (proj.exists()) {
264-
try {
265-
proj.delete(true, true, null);
266-
}
267-
catch(Exception e) {}
256+
proj.delete(true, true, null);
268257
}
269258
proj = root.getProject(name);
270259
proj.create(null);
@@ -300,31 +289,19 @@ private IJavaProject createProject(String name) throws Exception {
300289
return jproject;
301290
}
302291

303-
private void addSourceContainer(IJavaProject prj, String name) {
292+
private void addSourceContainer(IJavaProject prj, String name) throws Exception {
304293
IFolder folder = prj.getProject().getFolder(name);
305294
if(!folder.exists()) {
306-
try {
307-
folder.create(false, true, null);
308-
} catch (CoreException e) {
309-
throw new AssertionError(e);
310-
}
295+
folder.create(false, true, null);
311296
}
312297
IClasspathEntry entry = JavaCore.newSourceEntry(prj.getPackageFragmentRoot(folder).getPath());
313-
try {
314-
prj.setRawClasspath(addToClasspath(prj.getRawClasspath(), entry), null);
315-
} catch (JavaModelException e) {
316-
throw new AssertionError(e);
317-
}
298+
prj.setRawClasspath(addToClasspath(prj.getRawClasspath(), entry), null);
318299
}
319300

320-
private void addLibrary(IJavaProject prj, String path, String sourcePath) {
301+
private void addLibrary(IJavaProject prj, String path, String sourcePath) throws Exception {
321302
IClasspathEntry entry = JavaCore.newLibraryEntry(new Path(path), Optional.ofNullable(sourcePath).map(Path::new).orElse(null),
322303
Optional.ofNullable(sourcePath).map(p -> new Path(path)).orElse(null));
323-
try {
324-
prj.setRawClasspath(addToClasspath(prj.getRawClasspath(), entry), null);
325-
} catch (JavaModelException e) {
326-
throw new AssertionError(e);
327-
}
304+
prj.setRawClasspath(addToClasspath(prj.getRawClasspath(), entry), null);
328305
}
329306

330307
private static IClasspathEntry[] addToClasspath(IClasspathEntry[] original, IClasspathEntry add) {

0 commit comments

Comments
 (0)