SSJ API Documentation
Stochastic Simulation in Java
Loading...
Searching...
No Matches
RunClass.java
1package tutorial;
2
3import static org.junit.Assert.*;
4import java.io.ByteArrayOutputStream;
5import java.io.File;
6import java.io.IOException;
7import java.io.InputStream;
8import java.util.ArrayList;
9import java.util.Arrays;
10import java.util.Iterator;
11import java.util.List;
12import java.util.Scanner;
13import java.util.regex.Pattern;
14
15public class RunClass {
16
17 static class RunClassException extends Exception {
18 public RunClassException(String msg) {
19 super(msg);
20 }
21 }
22
23 // This class provides threads that read the streams from subprocesses to
24 // avoid blocking the main thread.
25 private static class StreamSink extends Thread {
26 private InputStream inputStream;
27 private ByteArrayOutputStream baos = new ByteArrayOutputStream();
28 private byte[] buffer = new byte[1024];
29
30 private StreamSink(InputStream inputStream) {
31 this.inputStream = inputStream;
32 }
33
34 @Override
35 public void run() {
36 int count = 0;
37 try {
38 while ((count = inputStream.read(buffer)) != -1) {
39 baos.write(buffer, 0, count);
40 }
41 } catch (Exception e) {
42 e.printStackTrace();
43 }
44 }
45
46 @Override
47 public String toString() {
48 try {
49 return baos.toString("UTF-8");
50 } catch (Exception e) {
51 return e.toString();
52 }
53 }
54 }
55
56 public static String run(Class prog) throws RunClassException {
57 return run(prog, null);
58 }
59
60 // We need to run programs in a distinct JVM because the initial states of
61 // the generators need to be reset for every program.
62 public static String run(Class prog, String[] args) throws RunClassException {
63
64 if (args == null)
65 args = new String[] {};
66
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";
70
71 ArrayList<String> cmd = new ArrayList<String>(5 + args.length);
72 cmd.add(path);
73 cmd.add("-cp");
74 cmd.add(classpath);
75 cmd.add("-Duser.language=C");
76 cmd.add(prog.getName());
77 for (String arg : args)
78 cmd.add(arg);
79
80 ProcessBuilder processBuilder = new ProcessBuilder().command(cmd);
81
82 try {
83
84 Process process = processBuilder.start();
85 StreamSink outSink = new StreamSink(process.getInputStream());
86 StreamSink errSink = new StreamSink(process.getErrorStream());
87 outSink.start();
88 errSink.start();
89 process.waitFor();
90 outSink.join();
91 errSink.join();
92
93 String err = errSink.toString();
94 if (err.length() > 0) {
95 System.err.println("==================== " + prog.getName() + " ====================");
96 System.err.print(err);
97 System.err.println();
98 }
99
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();
105 }
106 return out;
107 } catch (Exception e) {
108 throw new RunClassException(e.toString());
109 }
110 }
111
112 public static String readFile(String fileName) throws IOException {
113 return readFile(new File(fileName));
114 }
115
116 public static String readFile(File file) throws IOException {
117 return new Scanner(file).useDelimiter("\\A").next();
118 }
119
120 public static List<String> splitLines(String s) {
121 return new ArrayList<String>(Arrays.asList(s.split("\r?\n\r?")));
122 }
123
124 public static void compareLineByLine(String label, String expected, String actual, Pattern ignore) {
125
126 Iterator<String> it1 = splitLines(expected).iterator();
127 Iterator<String> it2 = splitLines(actual).iterator();
128 int lineNum = 0;
129 // check that every output line matches expected output
130 while (it1.hasNext()) {
131 String s1 = it1.next().trim();
132 // if it2 has finished just skip empty lines in expected output
133 if (s1.length() == 0 && !it2.hasNext())
134 continue;
135 String s2 = it2.next().trim();
136 lineNum += 1;
137 // ignore selected pattern
138 if (ignore.matcher(s1).matches())
139 continue;
140 assertEquals(label + ":" + lineNum, s1, s2);
141 }
142 // check that there are no non-empty trailing lines
143 int trailingLines = 0;
144 while (it2.hasNext()) {
145 if (it2.next().trim().length() > 0)
146 trailingLines += 1;
147 }
148 assertEquals(label + ":" + lineNum + " trailing lines", 0, trailingLines);
149 }
150}