Advertisement
riking

Untitled

May 16th, 2014
629
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.23 KB | None | 0 0
  1. package org.riking.mctesting;
  2.  
  3. import joptsimple.OptionSet;
  4. import org.apache.commons.lang.text.StrMatcher;
  5. import org.apache.commons.lang.text.StrTokenizer;
  6. import org.riking.mctesting.runner.*;
  7.  
  8. import java.io.*;
  9. import java.util.HashSet;
  10. import java.util.Set;
  11. import java.util.regex.Pattern;
  12.  
  13. public class Tester {
  14.     public final String name;
  15.     public TestResult result = null;
  16.     public Set<String> ignoredExceptions = new HashSet<String>();
  17.  
  18.     private final OptionSet optionSet;
  19.     private BufferedReader fileReader;
  20.  
  21.     private BufferedReader outputReader;
  22.     private OutputStreamWriter inputWriter;
  23.  
  24.     public Tester(OptionSet optionSet, String testName, File inputFile) {
  25.         this.name = testName;
  26.         this.optionSet = optionSet;
  27.         try {
  28.             fileReader = new BufferedReader(new FileReader(inputFile));
  29.         } catch (Throwable t) {
  30.             result = new TestResult(name, t);
  31.         }
  32.     }
  33.  
  34.     /**
  35.      * Run the test. This method catches all errors.
  36.      *
  37.      * @return Result of the test.
  38.      */
  39.     public TestResult runTest() {
  40.         if (result != null) return result;
  41.  
  42.         Process process = null;
  43.         try {
  44. @            verbose("Entering pre-server stage...");
  45. @            runPhase(new StageBeforeServer()); // does not call getLine()
  46.  
  47.             if (result != null) return result;
  48.  
  49. @            verbose("Starting server...");
  50. @            process = startServer();
  51.  
  52.             try {
  53. @                runPhase(new StageServerStartup()); // calls getLine() until "Done! (x.xxx ms)"
  54.             } catch (Throwable t) {
  55.                 // Don't exit right away - need to stop the server
  56.                 result = new TestResult(name, t);
  57.             }
  58.  
  59.             if (result != null) {
  60.                 stopServerEarly(process);
  61.                 return result;
  62.             }
  63.  
  64. @            verbose("Server is running. Running tests...");
  65.             try {
  66. @                runPhase(new StageServerRunning()); // might call getLine(), but doesn't yet
  67.             } catch (Throwable t) {
  68.                 // Don't exit right away - need to stop the server
  69.                 result = new TestResult(name, t);
  70.             }
  71.  
  72. @            if (result != null) {
  73. @                stopServerEarly(process); // Is entered
  74.                 return result;
  75.             }
  76.  
  77.             verbose("Stopping server...");
  78.             writeLine("stop");
  79.             verbose("wrote stop");
  80.  
  81.             try {
  82.                 runPhase(new StageServerShutdown());
  83.                 getMatchingLine(savingChunksPattern);
  84.             } catch (Throwable t) {
  85.                 // Don't exit right away - need to stop the server
  86.                 result = new TestResult(name, t);
  87.             }
  88.  
  89.             getMatchingLine(savingChunksPattern);
  90.             process.waitFor();
  91.             process = null;
  92.             inputWriter.close();
  93.             inputWriter = null;
  94.  
  95.             if (result != null) return result;
  96.  
  97.             runPhase(new StagePostShutdown());
  98.  
  99.             if (result != null) return result;
  100.  
  101.             return new TestResult(name);
  102.         } catch (Throwable t) {
  103.             System.out.println("Test error - " + t.getMessage());
  104.             return new TestResult(name, t);
  105.         } finally {
  106.             if (process != null) {
  107.                 if (inputWriter != null) {
  108.                     try {
  109.                         writeLine("stop");
  110.                         verbose("in finally block");
  111.                         getMatchingLine(savingChunksPattern);
  112.                     } catch (IOException ignored) {
  113.                     }
  114.                 }
  115.                 boolean interrupted = Thread.interrupted();
  116.                 try {
  117.                     process.waitFor();
  118.                 } catch (InterruptedException e) {
  119.                     interrupted = true;
  120.                     System.out.println("Killed server");
  121.                     process.destroy();
  122.                 }
  123.                 if (interrupted) Thread.currentThread().interrupt();
  124.             }
  125.         }
  126.     }
  127.  
  128.     public void writeLine(String line) throws IOException {
  129.         inputWriter.write(line);
  130.         inputWriter.flush();
  131.         verbose("Wrote " + line);
  132.     }
  133.  
  134.     public String getLine() throws IOException {
  135.         return outputReader.readLine();
  136.     }
  137.  
  138. @    public String getMatchingLine(Pattern pattern) throws IOException {
  139. @        String line;
  140. @
  141. @        while ((line = getLine()) != null) {
  142.             if (pattern.matcher(line).matches()) {
  143.                 return line;
  144.             } else {
  145.                 System.out.println("Dropping " + line);
  146.             }
  147.         }
  148.  
  149.         return null;
  150.     }
  151.  
  152.     private Process startServer() {
  153.         ProcessBuilder builder = new ProcessBuilder(
  154.                 "java",
  155.                 "-Xmx" + optionSet.valueOf("memory"),
  156.                 "-XX:MaxPermSize=128M",
  157.                 "-jar",
  158.                 (String) optionSet.valueOf("jar"),
  159.                 "-nojline"
  160.         );
  161.  
  162.         Process process;
  163.         try {
  164.             process = builder.start();
  165.             OutputStream stdIn = process.getOutputStream();
  166.             InputStream stdOut = process.getInputStream();
  167.             outputReader = new BufferedReader(new InputStreamReader(stdOut));
  168.             inputWriter = new OutputStreamWriter(stdIn);
  169.         } catch (IOException e) {
  170.             throw new RuntimeException("Failed to start server: " + e.getMessage(), e);
  171.         }
  172.         return process;
  173.     }
  174.  
  175. @    Pattern savingChunksPattern = Pattern.compile(".*?Saving chunks for level.*");
  176. @    private void stopServerEarly(Process process) throws Exception {
  177. @        verbose("Stopping server early...");
  178. @        writeLine("stop");
  179. @        writeLine("");
  180. @        verbose("Early wrote stop");
  181. @        getMatchingLine(savingChunksPattern); // Hang here
  182.         process.waitFor();
  183.     }
  184.  
  185.     /**
  186.      * Run a command in the pre-server phase.
  187.      *
  188.      * @param handler The ActionHandler for this stage
  189.      */
  190.     private void runPhase(ActionHandler handler) throws Exception {
  191.         - snip -
  192.     }
  193.  
  194.     public void verbose(String string) {
  195.         if (optionSet.has("v")) {
  196.             System.out.println(string);
  197.         }
  198.     }
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement