17 static class RunClassException
extends Exception {
18 public RunClassException(String msg) {
25 private static class StreamSink
extends Thread {
26 private InputStream inputStream;
27 private ByteArrayOutputStream baos =
new ByteArrayOutputStream();
28 private byte[] buffer =
new byte[1024];
30 private StreamSink(InputStream inputStream) {
31 this.inputStream = inputStream;
38 while ((count = inputStream.read(buffer)) != -1) {
39 baos.write(buffer, 0, count);
41 }
catch (Exception e) {
47 public String toString() {
49 return baos.toString(
"UTF-8");
50 }
catch (Exception e) {
57 return run(prog,
null);
65 args =
new String[] {};
67 String separator = System.getProperty(
"file.separator");
68 String classpath = System.getProperty(
"java.class.path");
69 String path = System.getProperty(
"java.home") + separator +
"bin" + separator +
"java";
71 ArrayList<String> cmd =
new ArrayList<String>(5 + args.length);
75 cmd.add(
"-Duser.language=C");
76 cmd.add(prog.getName());
77 for (String arg : args)
80 ProcessBuilder processBuilder =
new ProcessBuilder().command(cmd);
84 Process process = processBuilder.start();
85 StreamSink outSink =
new StreamSink(process.getInputStream());
86 StreamSink errSink =
new StreamSink(process.getErrorStream());
93 String err = errSink.toString();
94 if (err.length() > 0) {
95 System.err.println(
"==================== " + prog.getName() +
" ====================");
96 System.err.print(err);
100 String out = outSink.toString();
101 if (out.length() > 0) {
102 System.out.println(
"==================== " + prog.getName() +
" ====================");
103 System.out.print(out);
104 System.out.println();
107 }
catch (Exception e) {
112 public static String readFile(String fileName)
throws IOException {
113 return readFile(
new File(fileName));
116 public static String readFile(File file)
throws IOException {
117 return new Scanner(file).useDelimiter(
"\\A").next();
120 public static List<String> splitLines(String s) {
121 return new ArrayList<String>(Arrays.asList(s.split(
"\r?\n\r?")));
124 public static void compareLineByLine(String label, String expected, String actual, Pattern ignore) {
126 Iterator<String> it1 = splitLines(expected).iterator();
127 Iterator<String> it2 = splitLines(actual).iterator();
130 while (it1.hasNext()) {
131 String s1 = it1.next().trim();
133 if (s1.length() == 0 && !it2.hasNext())
135 String s2 = it2.next().trim();
138 if (ignore.matcher(s1).matches())
140 assertEquals(label +
":" + lineNum, s1, s2);
143 int trailingLines = 0;
144 while (it2.hasNext()) {
145 if (it2.next().trim().length() > 0)
148 assertEquals(label +
":" + lineNum +
" trailing lines", 0, trailingLines);