source: tmcsimulator/trunk/test/tmcsim/cadsimulator/CADSimulatorFixture.java @ 558

Revision 558, 4.9 KB checked in by jdalbey, 6 years ago (diff)

Change hardcoded "scripts" as name of folder to a constant in SimulationManager?. See #228.

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