| 1 | package tmcsim.cadsimulator; |
|---|
| 2 | |
|---|
| 3 | import java.io.ByteArrayOutputStream; |
|---|
| 4 | import java.io.File; |
|---|
| 5 | import java.io.FileWriter; |
|---|
| 6 | import java.io.IOException; |
|---|
| 7 | import java.io.PrintStream; |
|---|
| 8 | import java.io.PrintWriter; |
|---|
| 9 | import java.net.MalformedURLException; |
|---|
| 10 | import java.net.URL; |
|---|
| 11 | import java.util.Scanner; |
|---|
| 12 | import java.util.logging.Level; |
|---|
| 13 | import java.util.logging.Logger; |
|---|
| 14 | import static tmcsim.cadsimulator.CADSimulatorFixture.bos; |
|---|
| 15 | |
|---|
| 16 | /** |
|---|
| 17 | * Note: This test requires an internet connection, for building data files from |
|---|
| 18 | * a master copy at pastebin. |
|---|
| 19 | * |
|---|
| 20 | * @author jdalbey |
|---|
| 21 | */ |
|---|
| 22 | public class CADSimulatorFixture |
|---|
| 23 | { |
|---|
| 24 | |
|---|
| 25 | static CADServer app; |
|---|
| 26 | static ByteArrayOutputStream bos; |
|---|
| 27 | static PrintStream ps; |
|---|
| 28 | static final String configData = |
|---|
| 29 | "CADClientPort = 4444 \n" |
|---|
| 30 | + "CoordinatorRMIPort = 4445 \n" |
|---|
| 31 | + "CADRmiPort = 4446 \n" |
|---|
| 32 | + "UserInterface = tmcsim.cadsimulator.viewer.CADConsoleViewer\n" |
|---|
| 33 | + "ParamicsProperties = pconfig.txt\n" |
|---|
| 34 | + "ATMSProperties = empty.txt\n" |
|---|
| 35 | + "MediaProperties = empty.txt\n"; |
|---|
| 36 | static final String paramicsData = "ParamicsCommHost = 192.168.251.45\n" |
|---|
| 37 | + "ParamicsCommPort = 4450\n" |
|---|
| 38 | + "IncidentUpdateInterval = 30\n" |
|---|
| 39 | + "IncidentUpdateFile = exchange.xml\n" |
|---|
| 40 | + "ParamicsStatusInterval = 15\n" |
|---|
| 41 | + "ParamicsStatusFile = paramics_status.xml\n" |
|---|
| 42 | + "CameraStatusInterval = 30\n" |
|---|
| 43 | + "CameraStatusFile = camera_status.xml\n"; |
|---|
| 44 | static final String paramConfig = "SocketPort=4450\n" |
|---|
| 45 | +"WorkingDirectory=.\nGUIvisible=false"; |
|---|
| 46 | static final String cardfileURL = "http://pastebin.com/raw/Yr26nfp7"; |
|---|
| 47 | static final String smallXMLURL = "http://pastebin.com/raw/Eqj2N5qD"; |
|---|
| 48 | /* |
|---|
| 49 | * Creating instance of app must be done only once or you get registry |
|---|
| 50 | * bind problems, and code Written in Constructor is Executed |
|---|
| 51 | * before each Test Method |
|---|
| 52 | */ |
|---|
| 53 | |
|---|
| 54 | public static void writeConfigData() |
|---|
| 55 | { |
|---|
| 56 | // Declare a stream to the output |
|---|
| 57 | bos = new ByteArrayOutputStream(); |
|---|
| 58 | ps = new PrintStream(bos); |
|---|
| 59 | // Redirect the standard output |
|---|
| 60 | System.setOut(ps); |
|---|
| 61 | writedata("config.txt", configData); |
|---|
| 62 | writedata("pconfig.txt", paramicsData); |
|---|
| 63 | writedata("empty.txt", ""); |
|---|
| 64 | writeScriptfiles(); |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | public static void writeScriptfiles() |
|---|
| 68 | { |
|---|
| 69 | writeFileFromURL("scripts/Cardfile.xml", cardfileURL); |
|---|
| 70 | writeFileFromURL("scripts/one-incident.xml", smallXMLURL); |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | private static void writeFileFromURL(String filename, String urlstring) |
|---|
| 74 | { |
|---|
| 75 | File cardFile = new File(filename); |
|---|
| 76 | // If a cardfile exists, leave it |
|---|
| 77 | if (!cardFile.exists()) |
|---|
| 78 | { |
|---|
| 79 | // if cardfile doesn't exist, copy from our special pastebin |
|---|
| 80 | java.io.File dir = new java.io.File("scripts"); |
|---|
| 81 | dir.mkdir(); |
|---|
| 82 | URL url = null; |
|---|
| 83 | try |
|---|
| 84 | { |
|---|
| 85 | url = new URL(urlstring); |
|---|
| 86 | } catch (MalformedURLException ex) |
|---|
| 87 | { |
|---|
| 88 | Logger.getLogger(CADSimulatorFixture.class.getName()).log(Level.SEVERE, null, ex); |
|---|
| 89 | } |
|---|
| 90 | try |
|---|
| 91 | { |
|---|
| 92 | Scanner in = null; |
|---|
| 93 | in = new Scanner(url.openStream()).useDelimiter("\\A"); |
|---|
| 94 | String out = in.next(); |
|---|
| 95 | writedata(filename, out); |
|---|
| 96 | } catch (IOException ex) |
|---|
| 97 | { |
|---|
| 98 | Logger.getLogger(CADSimulatorFixture.class.getName()).log(Level.SEVERE, null, ex); |
|---|
| 99 | } |
|---|
| 100 | } |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | public static void startCADSim() |
|---|
| 104 | { |
|---|
| 105 | // This will set the desired property as long as you don't call the main() method. |
|---|
| 106 | System.setProperty("CAD_SIM_PROPERTIES", "config.txt"); |
|---|
| 107 | if (System.getProperty("CAD_SIM_PROPERTIES") != null) |
|---|
| 108 | { |
|---|
| 109 | try |
|---|
| 110 | { |
|---|
| 111 | app = new CADServer(System.getProperty("CAD_SIM_PROPERTIES")); |
|---|
| 112 | } catch (Exception ex) |
|---|
| 113 | { |
|---|
| 114 | ex.printStackTrace(); |
|---|
| 115 | junit.framework.TestCase.fail("Couldn't launch CADSimulator"); |
|---|
| 116 | } |
|---|
| 117 | } else |
|---|
| 118 | { |
|---|
| 119 | junit.framework.TestCase.fail("CAD_SIM_PROPERTIES system property not defined."); |
|---|
| 120 | } |
|---|
| 121 | } |
|---|
| 122 | // Write the test data to a file |
|---|
| 123 | |
|---|
| 124 | public static void writedata(String filename, String data) |
|---|
| 125 | { |
|---|
| 126 | PrintWriter writer = null; |
|---|
| 127 | try |
|---|
| 128 | { |
|---|
| 129 | writer = new PrintWriter(new FileWriter(filename)); |
|---|
| 130 | writer.println(data); |
|---|
| 131 | writer.close(); |
|---|
| 132 | } catch (Exception ex) |
|---|
| 133 | { |
|---|
| 134 | ex.printStackTrace(); |
|---|
| 135 | } |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | public static void pause(int millis) |
|---|
| 139 | { |
|---|
| 140 | try |
|---|
| 141 | { |
|---|
| 142 | Thread.sleep(millis); |
|---|
| 143 | } catch (InterruptedException ex) |
|---|
| 144 | { |
|---|
| 145 | ex.printStackTrace(); |
|---|
| 146 | } |
|---|
| 147 | } |
|---|
| 148 | } |
|---|