Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.riking.mctesting;
- import joptsimple.OptionSet;
- import org.apache.commons.lang.text.StrMatcher;
- import org.apache.commons.lang.text.StrTokenizer;
- import org.riking.mctesting.runner.*;
- import java.io.*;
- import java.util.HashSet;
- import java.util.Set;
- import java.util.regex.Pattern;
- public class Tester {
- public final String name;
- public TestResult result = null;
- public Set<String> ignoredExceptions = new HashSet<String>();
- private final OptionSet optionSet;
- private BufferedReader fileReader;
- private BufferedReader outputReader;
- private OutputStreamWriter inputWriter;
- public Tester(OptionSet optionSet, String testName, File inputFile) {
- this.name = testName;
- this.optionSet = optionSet;
- try {
- fileReader = new BufferedReader(new FileReader(inputFile));
- } catch (Throwable t) {
- result = new TestResult(name, t);
- }
- }
- /**
- * Run the test. This method catches all errors.
- *
- * @return Result of the test.
- */
- public TestResult runTest() {
- if (result != null) return result;
- Process process = null;
- try {
- @ verbose("Entering pre-server stage...");
- @ runPhase(new StageBeforeServer()); // does not call getLine()
- if (result != null) return result;
- @ verbose("Starting server...");
- @ process = startServer();
- try {
- @ runPhase(new StageServerStartup()); // calls getLine() until "Done! (x.xxx ms)"
- } catch (Throwable t) {
- // Don't exit right away - need to stop the server
- result = new TestResult(name, t);
- }
- if (result != null) {
- stopServerEarly(process);
- return result;
- }
- @ verbose("Server is running. Running tests...");
- try {
- @ runPhase(new StageServerRunning()); // might call getLine(), but doesn't yet
- } catch (Throwable t) {
- // Don't exit right away - need to stop the server
- result = new TestResult(name, t);
- }
- @ if (result != null) {
- @ stopServerEarly(process); // Is entered
- return result;
- }
- verbose("Stopping server...");
- writeLine("stop");
- verbose("wrote stop");
- try {
- runPhase(new StageServerShutdown());
- getMatchingLine(savingChunksPattern);
- } catch (Throwable t) {
- // Don't exit right away - need to stop the server
- result = new TestResult(name, t);
- }
- getMatchingLine(savingChunksPattern);
- process.waitFor();
- process = null;
- inputWriter.close();
- inputWriter = null;
- if (result != null) return result;
- runPhase(new StagePostShutdown());
- if (result != null) return result;
- return new TestResult(name);
- } catch (Throwable t) {
- System.out.println("Test error - " + t.getMessage());
- return new TestResult(name, t);
- } finally {
- if (process != null) {
- if (inputWriter != null) {
- try {
- writeLine("stop");
- verbose("in finally block");
- getMatchingLine(savingChunksPattern);
- } catch (IOException ignored) {
- }
- }
- boolean interrupted = Thread.interrupted();
- try {
- process.waitFor();
- } catch (InterruptedException e) {
- interrupted = true;
- System.out.println("Killed server");
- process.destroy();
- }
- if (interrupted) Thread.currentThread().interrupt();
- }
- }
- }
- public void writeLine(String line) throws IOException {
- inputWriter.write(line);
- inputWriter.flush();
- verbose("Wrote " + line);
- }
- public String getLine() throws IOException {
- return outputReader.readLine();
- }
- @ public String getMatchingLine(Pattern pattern) throws IOException {
- @ String line;
- @
- @ while ((line = getLine()) != null) {
- if (pattern.matcher(line).matches()) {
- return line;
- } else {
- System.out.println("Dropping " + line);
- }
- }
- return null;
- }
- private Process startServer() {
- ProcessBuilder builder = new ProcessBuilder(
- "java",
- "-Xmx" + optionSet.valueOf("memory"),
- "-XX:MaxPermSize=128M",
- "-jar",
- (String) optionSet.valueOf("jar"),
- "-nojline"
- );
- Process process;
- try {
- process = builder.start();
- OutputStream stdIn = process.getOutputStream();
- InputStream stdOut = process.getInputStream();
- outputReader = new BufferedReader(new InputStreamReader(stdOut));
- inputWriter = new OutputStreamWriter(stdIn);
- } catch (IOException e) {
- throw new RuntimeException("Failed to start server: " + e.getMessage(), e);
- }
- return process;
- }
- @ Pattern savingChunksPattern = Pattern.compile(".*?Saving chunks for level.*");
- @ private void stopServerEarly(Process process) throws Exception {
- @ verbose("Stopping server early...");
- @ writeLine("stop");
- @ writeLine("");
- @ verbose("Early wrote stop");
- @ getMatchingLine(savingChunksPattern); // Hang here
- process.waitFor();
- }
- /**
- * Run a command in the pre-server phase.
- *
- * @param handler The ActionHandler for this stage
- */
- private void runPhase(ActionHandler handler) throws Exception {
- - snip -
- }
- public void verbose(String string) {
- if (optionSet.has("v")) {
- System.out.println(string);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement