Fix error messages not shown if invoke(...) throws InvocationTargetException

This commit is contained in:
Sam Harwell 2012-10-30 08:35:00 -05:00
parent 6db106855f
commit 13e940b1db
1 changed files with 31 additions and 27 deletions

View File

@ -105,6 +105,9 @@ import java.util.logging.Logger;
public abstract class BaseTest { public abstract class BaseTest {
// -J-Dorg.antlr.v4.test.BaseTest.level=FINE
private static final Logger LOGGER = Logger.getLogger(BaseTest.class.getName());
public static final String newline = System.getProperty("line.separator"); public static final String newline = System.getProperty("line.separator");
public static final String pathSep = System.getProperty("path.separator"); public static final String pathSep = System.getProperty("path.separator");
@ -571,8 +574,6 @@ public abstract class BaseTest {
public String execClass(String className) { public String execClass(String className) {
if (TEST_IN_SAME_PROCESS) { if (TEST_IN_SAME_PROCESS) {
PrintStream originalOut = System.out;
PrintStream originalErr = System.err;
try { try {
ClassLoader loader = new URLClassLoader(new URL[] { new File(tmpdir).toURI().toURL() }, ClassLoader.getSystemClassLoader()); ClassLoader loader = new URLClassLoader(new URL[] { new File(tmpdir).toURI().toURL() }, ClassLoader.getSystemClassLoader());
final Class<?> mainClass = (Class<?>)loader.loadClass(className); final Class<?> mainClass = (Class<?>)loader.loadClass(className);
@ -581,17 +582,27 @@ public abstract class BaseTest {
PipedInputStream stderrIn = new PipedInputStream(); PipedInputStream stderrIn = new PipedInputStream();
PipedOutputStream stdoutOut = new PipedOutputStream(stdoutIn); PipedOutputStream stdoutOut = new PipedOutputStream(stdoutIn);
PipedOutputStream stderrOut = new PipedOutputStream(stderrIn); PipedOutputStream stderrOut = new PipedOutputStream(stderrIn);
System.setOut(new PrintStream(stdoutOut));
System.setErr(new PrintStream(stderrOut));
StreamVacuum stdoutVacuum = new StreamVacuum(stdoutIn); StreamVacuum stdoutVacuum = new StreamVacuum(stdoutIn);
StreamVacuum stderrVacuum = new StreamVacuum(stderrIn); StreamVacuum stderrVacuum = new StreamVacuum(stderrIn);
stdoutVacuum.start();
stderrVacuum.start(); PrintStream originalOut = System.out;
mainMethod.invoke(null, (Object)new String[] { new File(tmpdir, "input").getAbsolutePath() }); System.setOut(new PrintStream(stdoutOut));
System.setOut(originalOut); try {
originalOut = null; PrintStream originalErr = System.err;
System.setErr(originalErr); try {
originalErr = null; System.setErr(new PrintStream(stderrOut));
stdoutVacuum.start();
stderrVacuum.start();
mainMethod.invoke(null, (Object)new String[] { new File(tmpdir, "input").getAbsolutePath() });
}
finally {
System.setErr(originalErr);
}
}
finally {
System.setOut(originalOut);
}
stdoutOut.close(); stdoutOut.close();
stderrOut.close(); stderrOut.close();
stdoutVacuum.join(); stdoutVacuum.join();
@ -603,30 +614,23 @@ public abstract class BaseTest {
} }
return output; return output;
} catch (MalformedURLException ex) { } catch (MalformedURLException ex) {
Logger.getLogger(BaseTest.class.getName()).log(Level.SEVERE, null, ex); LOGGER.log(Level.SEVERE, null, ex);
} catch (IOException ex) { } catch (IOException ex) {
Logger.getLogger(BaseTest.class.getName()).log(Level.SEVERE, null, ex); LOGGER.log(Level.SEVERE, null, ex);
} catch (InterruptedException ex) { } catch (InterruptedException ex) {
Logger.getLogger(BaseTest.class.getName()).log(Level.SEVERE, null, ex); LOGGER.log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) { } catch (IllegalAccessException ex) {
Logger.getLogger(BaseTest.class.getName()).log(Level.SEVERE, null, ex); LOGGER.log(Level.SEVERE, null, ex);
} catch (IllegalArgumentException ex) { } catch (IllegalArgumentException ex) {
Logger.getLogger(BaseTest.class.getName()).log(Level.SEVERE, null, ex); LOGGER.log(Level.SEVERE, null, ex);
} catch (InvocationTargetException ex) { } catch (InvocationTargetException ex) {
Logger.getLogger(BaseTest.class.getName()).log(Level.SEVERE, null, ex); LOGGER.log(Level.SEVERE, null, ex);
} catch (NoSuchMethodException ex) { } catch (NoSuchMethodException ex) {
Logger.getLogger(BaseTest.class.getName()).log(Level.SEVERE, null, ex); LOGGER.log(Level.SEVERE, null, ex);
} catch (SecurityException ex) { } catch (SecurityException ex) {
Logger.getLogger(BaseTest.class.getName()).log(Level.SEVERE, null, ex); LOGGER.log(Level.SEVERE, null, ex);
} catch (ClassNotFoundException ex) { } catch (ClassNotFoundException ex) {
Logger.getLogger(BaseTest.class.getName()).log(Level.SEVERE, null, ex); LOGGER.log(Level.SEVERE, null, ex);
} finally {
if (originalOut != null) {
System.setOut(originalOut);
}
if (originalErr != null) {
System.setErr(originalErr);
}
} }
} }