Warning: Can't use blame annotator:
svn blame failed on trunk/test/tmcsim/cadsimulator/CADSimulatorFixture.java: ("Can't find a temporary directory: Internal error", 20014)

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

Revision 19, 4.7 KB checked in by jdalbey, 10 years ago (diff)

Add unit test for CADSimulator GUI

RevLine 
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;
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 */
22public class CADSimulatorFixture
23{
24
25    static CADSimulator 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 cardfileURL = "http://pastebin.com/raw/Yr26nfp7";
45    static final String smallXMLURL = "http://pastebin.com/raw/Eqj2N5qD";
46    /*
47     * Creating instance of app must be done only once or you get registry
48     * bind problems, and code Written in Constructor is Executed
49     * before each Test Method
50     */
51
52    public static void writeConfigData()
53    {
54        // Declare a stream to the output
55        bos = new ByteArrayOutputStream();
56        ps = new PrintStream(bos);
57        // Redirect the standard output
58        System.setOut(ps);
59        writedata("config.txt", configData);
60        writedata("pconfig.txt", paramicsData);
61        writedata("empty.txt", "");
62        writeScriptfiles();
63    }
64
65    public static void writeScriptfiles()
66    {
67        writeFileFromURL("scripts/Cardfile.xml", cardfileURL);
68        writeFileFromURL("scripts/one-incident.xml", smallXMLURL);
69    }
70
71    private static void writeFileFromURL(String filename, String urlstring)
72    {
73        File cardFile = new File(filename);
74        // If a cardfile exists, leave it
75        if (!cardFile.exists())
76        {
77            // if cardfile doesn't exist, copy from our special pastebin
78            java.io.File dir = new java.io.File("scripts");
79            dir.mkdir();
80            URL url = null;
81            try
82            {
83                url = new URL(urlstring);
84            } catch (MalformedURLException ex)
85            {
86                Logger.getLogger(CADSimulatorFixture.class.getName()).log(Level.SEVERE, null, ex);
87            }
88            try
89            {
90                Scanner in = null;
91                in = new Scanner(url.openStream()).useDelimiter("\\A");
92                String out = in.next();
93                writedata(filename, out);
94            } catch (IOException ex)
95            {
96                Logger.getLogger(CADSimulatorFixture.class.getName()).log(Level.SEVERE, null, ex);
97            }
98        }
99    }
100
101    public static void startCADSim()
102    {
103        // This will set the desired property as long as you don't call the main() method.
104        System.setProperty("CAD_SIM_PROPERTIES", "config.txt");
105        if (System.getProperty("CAD_SIM_PROPERTIES") != null)
106        {
107            try
108            {
109                app = new CADSimulator(System.getProperty("CAD_SIM_PROPERTIES"));
110            } catch (Exception ex)
111            {
112                ex.printStackTrace();
113                junit.framework.TestCase.fail("Couldn't launch CADSimulator");
114            }
115        } else
116        {
117            junit.framework.TestCase.fail("CAD_SIM_PROPERTIES system property not defined.");
118        }
119    }
120    // Write the test data to a file
121
122    public static void writedata(String filename, String data)
123    {
124        PrintWriter writer = null;
125        try
126        {
127            writer = new PrintWriter(new FileWriter(filename));
128            writer.println(data);
129            writer.close();
130        } catch (Exception ex)
131        {
132            ex.printStackTrace();
133        }
134    }
135
136    public static void pause(int millis)
137    {
138        try
139        {
140            Thread.sleep(millis);
141        } catch (InterruptedException ex)
142        {
143            ex.printStackTrace();
144        }
145    }
146}
Note: See TracBrowser for help on using the repository browser.