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

source: tmcsimulator/trunk/test/tmcsim/cadsimulator/CADSimulatorConsoleTest.java @ 653

Revision 653, 21.8 KB checked in by jdalbey, 4 years ago (diff)

Update unit tests to correct changes in display messages, directory paths, and button names.

RevLine 
1package tmcsim.cadsimulator;
2
3import java.io.File;
4import java.io.FileWriter;
5import java.io.IOException;
6import java.io.PrintWriter;
7import java.io.StringWriter;
8import java.util.logging.Level;
9import java.util.logging.Logger;
10import junit.framework.TestCase;
11import org.apache.commons.lang.StringUtils;
12import static org.mockito.Mockito.mock;
13import tmcsim.cadsimulator.viewer.CADConsoleViewer;
14import tmcsim.common.CADEnums;
15import tmcsim.common.ScriptException;
16import tmcsim.common.SimulationException;
17import tmcsim.interfaces.CADClientInterface;
18import tmcsim.interfaces.SimulationManagerInterface;
19import static tmcsim.simulationmanager.SimulationManager.kScenarioFolder;
20
21/**
22 * Test of CADSimulator Console
23 *
24 * @author jdalbey
25 */
26public class CADSimulatorConsoleTest extends TestCase
27{
28
29    private static CADServer app = null;
30    private CADConsoleViewer console;
31    private StringWriter sw;
32
33    public CADSimulatorConsoleTest(String testName)
34    {
35        super(testName);
36    }
37
38    public void setUp() throws Exception
39    {
40        super.setUp();
41        writeConfigData();
42        // Since CADSimulator has registry, we musn't instantiate it more than once
43        if (app == null)
44        {
45            try
46            {
47                app = new CADServer("config.txt");
48            } catch (Exception ex)
49            {
50                fail("Couldn't launch CADSimulator");
51            }
52        }
53        console = (CADConsoleViewer) app.theViewer;
54        sw = new StringWriter();
55        console.setWriter(sw);
56    }
57
58    @Override
59    public void tearDown() throws java.io.IOException
60    {
61        File removeMe = new File("config.txt");
62        removeMe.delete();
63        removeMe = new File("pconfig.txt");
64        removeMe.delete();
65        removeMe = new File("tconfig.txt");
66        removeMe.delete();
67        removeMe = new File("empty.txt");
68        removeMe.delete();
69    }
70
71    /**
72     * compare StringWriter contents against expected
73     */
74    private void verify(String msg, String expect)
75    {
76        String result = sw.toString().trim();
77        System.out.println(""+result);
78        result = result.replaceAll("\n", ",");
79        String fullExpect = expect.trim();
80        fullExpect = fullExpect.replaceAll("\n", ",");
81//        System.out.println(fullExpect);
82        System.out.println(result.substring(result.length() - fullExpect.length(), result.length()));
83//        String diff = StringUtils.difference(fullExpect, result);
84        boolean match = result.endsWith(fullExpect);
85        assertTrue(msg + ": " + result, match);
86    }
87
88    public static void pause(int millis)
89    {
90        try
91        {
92            Thread.sleep(millis);
93        } catch (InterruptedException ex)
94        {
95        }
96    }
97
98    /**
99     * The tests must be all run in one method, because the order matters. As
100     * separate methods, we couldn't control which is executed first.
101     *
102     * @throws java.io.IOException
103     * @throws SimulationException
104     * @throws ScriptException
105     */
106    public void testAll() throws java.io.IOException, SimulationException, ScriptException
107    {
108        CADConsoleViewer console = (CADConsoleViewer) app.theViewer;
109        String expected1 =
110                "--- CAD Simulator ---\n"
111                + "Elapsed Simulation Time     : 0:00:00\n"
112                + "Status                      : No Script\n"
113                + "Connected CAD Terminals     : 0\n"
114                + "Simulation Manager Connected: No\n"
115                + "Connected to Paramics       : No\n"
116                + "Network Loaded              : None\n"
117                + "-- Info Messages --\n\n"
118                + "-- Error Messages --";
119        System.out.println("setVisible");
120        console.setVisible(true);
121        verify("Initial output incorrect: ", expected1);
122        String expected2 =
123                "--- CAD Simulator ---\n"
124                + "Elapsed Simulation Time     : 0:00:00\n"
125                + "Status                      : No Script\n"
126                + "Connected CAD Terminals     : 1\n"
127                + "Simulation Manager Connected: No\n"
128                + "Connected to Paramics       : No\n"
129                + "Network Loaded              : None\n"
130                + "-- Info Messages --\n\n"
131                + "-- Error Messages --\n\n";
132
133        System.out.println("connect one");
134        CADClientInterface ci = mock(CADClientInterface.class);
135        app.theCoordinator.registerForCallback(ci);
136        verify("connected 1 terminal output incorrect: ", expected2);
137        String expected3 =
138                "--- CAD Simulator ---\n"
139                + "Elapsed Simulation Time     : 0:00:00\n"
140                + "Status                      : No Script\n"
141                + "Connected CAD Terminals     : 2\n"
142                + "Simulation Manager Connected: No\n"
143                + "Connected to Paramics       : No\n"
144                + "Network Loaded              : None\n"
145                + "-- Info Messages --\n\n"
146                + "-- Error Messages --\n\n";
147        System.out.println("connect two");
148        app.theCoordinator.registerForCallback(ci);
149        verify("connected 2 terminals output incorrect: ", expected3);
150        System.out.println("disconnect");
151        app.theCoordinator.unregisterForCallback(ci);
152        verify("disconnect terminal output incorrect: ", expected2);
153        app.theCoordinator.unregisterForCallback(ci);
154        String expected4 =
155                "--- CAD Simulator ---\n"
156                + "Elapsed Simulation Time     : 0:00:00\n"
157                + "Status                      : No Script\n"
158                + "Connected CAD Terminals     : 0\n"
159                + "Simulation Manager Connected: Yes\n"
160                + "Connected to Paramics       : No\n"
161                + "Network Loaded              : None\n"
162                + "-- Info Messages --\n\n"
163                + "-- Error Messages --\n\n";
164        System.out.println("sim mgr connect");
165        SimulationManagerInterface si = mock(SimulationManagerInterface.class);
166        app.theCoordinator.registerForCallback(si);
167        verify("sim mgr connected output incorrect: ", expected4);
168        String expected5 =
169                "--- CAD Simulator ---\n"
170                + "Elapsed Simulation Time     : 0:00:00\n"
171                + "Status                      : No Script\n"
172                + "Connected CAD Terminals     : 0\n"
173                + "Simulation Manager Connected: Yes\n"
174                + "Connected to Paramics       : No\n"
175                + "Network Loaded              : None\n"
176                + "-- Info Messages --\n"
177                + ". = Console Info Message.\n"
178                + "-- Error Messages --\n\n";
179        System.out.println("Info msg");
180        Logger cadSimLogger = Logger.getLogger("tmcsim.cadsimulator");
181        cadSimLogger.logp(Level.INFO, "", "", "Console Info Message.");
182        verify("Info message output incorrect: ", expected5);
183        String expected7 =
184                "--- CAD Simulator ---\n"
185                + "Elapsed Simulation Time     : 0:00:00\n"
186                + "Status                      : No Script\n"
187                + "Connected CAD Terminals     : 0\n"
188                + "Simulation Manager Connected: Yes\n"
189                + "Connected to Paramics       : No\n"
190                + "Network Loaded              : None\n"
191                + "-- Info Messages --\n"
192                + ". = Console Info Message.\n"
193                + "-- Error Messages --\n"
194                + "Someclass.Somemethod = Sample error message.\n";
195        System.out.println("Error msg");
196        cadSimLogger = Logger.getLogger("tmcsim.cadsimulator");
197        cadSimLogger.logp(Level.SEVERE, "Someclass", "Somemethod", "Sample error message.");
198        verify("Error message output incorrect: ", expected7);
199        String expected6 =
200                "--- CAD Simulator ---\n"
201                + "Elapsed Simulation Time     : 0:00:00\n"
202                + "Status                      : No Script\n"
203                + "Connected CAD Terminals     : 0\n"
204                + "Simulation Manager Connected: Yes\n"
205                + "Connected to Paramics       : Yes\n"
206                + "Network Loaded              : None\n"
207                + "-- Info Messages --\n"
208                + ". = Console Info Message.\n"
209                + "-- Error Messages --\n"
210                + "Someclass.Somemethod = Sample error message.\n";
211        System.out.println("Paramics connect");
212        app.theCoordinator.setParamicsStatus(CADEnums.PARAMICS_STATUS.CONNECTED);
213        pause(500);
214        pause(500);
215        verify("paramics connected should be yes", expected6);
216        String expected8 =
217                "--- CAD Simulator ---\n"
218                + "Elapsed Simulation Time     : 0:00:00\n"
219                + "Status                      : Ready\n"
220                + "Connected CAD Terminals     : 0\n"
221                + "Simulation Manager Connected: Yes\n"
222                + "Connected to Paramics       : Yes\n"
223                + "Network Loaded              : None\n"
224                + "-- Info Messages --\n"
225                + ". = Console Info Message."
226                + "tmcsim.cadsimulator.Coordinator.copyXMLfile = Loaded script copied to webapps/dynamicdata/incident_script.xml\n"
227                + "-- Error Messages --\n"
228                + "Someclass.Somemethod = Sample error message.\n";
229        String expected9 =
230                "--- CAD Simulator ---\n"
231                + "Elapsed Simulation Time     : 0:00:01\n"
232                + "Status                      : Running\n"
233                + "Connected CAD Terminals     : 0\n"
234                + "Simulation Manager Connected: Yes\n"
235                + "Connected to Paramics       : Yes\n"
236                + "Network Loaded              : None\n"
237                + "-- Info Messages --\n"
238                + ". = Console Info Message."
239                + "tmcsim.cadsimulator.Coordinator.copyXMLfile = Loaded script copied to webapps/dynamicdata/incident_script.xml\n"
240                + "-- Error Messages --\n"
241                + "Someclass.Somemethod = Sample error message.\n";
242        System.out.println("Sim status");
243        // Load a script file - to put status at Ready
244        String autoloadScriptname = kScenarioFolder+"/one-incident.xml";
245        app.theCoordinator.loadScriptFile(new File(autoloadScriptname));
246        pause(2000);
247        verify("Status should be 'ready'", expected8);
248        // startSimulation to put status at Running
249        app.theCoordinator.startSimulation();
250        pause(500);
251        verify("Status should be 'running', time should be 0:01", expected9);
252    }
253//    public void testNetworkID()
254//    {
255//        String expected10 =
256//                "--- CAD Simulator ---\n"
257//                + "Elapsed Simulation Time     : 0:00:00\n"
258//                + "Status                      : No Script\n"
259//                + "Connected CAD Terminals     : 0\n"
260//                + "Simulation Manager Connected: No\n"
261//                + "Connected to Paramics       : No\n"
262//                + "Network Loaded              : 17\n"
263//                + "-- Info Messages --\n\n"
264//                + "-- Error Messages --\n\n";
265//        // this will tell the model it has a new network ID
266//        cadmodel.setParamicsNetworkLoaded("17");
267//        cadmodel.setParamicsStatus(CADEnums.PARAMICS_STATUS.LOADED);
268//        pause(500);
269//        verify("network id should be 17", expected10);
270//    }
271//    ByteArrayOutputStream bos;
272//    PrintStream ps;
273    static final String configData =
274            "CADClientPort          = 4444 \n"
275            + "CoordinatorRMIPort     = 4445 \n"
276            + "CADRmiPort             = 4446 \n"
277            + "UserInterface          = tmcsim.cadsimulator.viewer.CADConsoleViewer\n"
278            + "ParamicsProperties     = pconfig.txt\n"
279            + "ATMSProperties         = empty.txt\n"
280            + "TrafficMgrProperties   = tconfig.txt\n"
281            + "MediaProperties        = empty.txt\n"
282            + "ElapsedTimeFile        = webapps/dynamicdata/sim_elapsedtime.json\n"
283            + "CADcommentsLog         = CADcomments.log";
284    static final String trafficMgrData = "Highways_Map_File = config/vds_data/postmile_coordinates.txt\n"
285            +"Events_File = config/vds_data/atmsBatchEvents.txt\n"
286            +"FEPSim_IP_addr = localhost\n"
287            +"Output_Destination = Console\n"
288            +"Highway_Status_File = /tmp/highway_status.json\n";
289    static final String paramicsData = "ParamicsCommHost = 127.0.0.1\n"
290            + "ParamicsCommPort       = 4450\n"
291            + "IncidentUpdateInterval = 30\n"
292            + "IncidentUpdateFile     = exchange.xml\n"
293            + "ParamicsStatusInterval = 15\n"
294            + "ParamicsStatusFile     = paramics_status.xml\n"
295            + "CameraStatusInterval   = 30\n"
296            + "CameraStatusFile       = camera_status.xml\n";
297    static final String cardfileURL = "http://pastebin.com/raw/Yr26nfp7";
298    static final String smallXMLURL = "http://pastebin.com/raw/Eqj2N5qD";
299    /*
300     * Creating instance of app must be done only once or you get registry
301     * bind problems, and code Written in Constructor is Executed
302     * before each Test Method
303     */
304
305    private void writeConfigData()
306    {
307        // Declare a stream to the output
308//        bos = new ByteArrayOutputStream();
309//        ps = new PrintStream(bos);
310        // Redirect the standard output
311//        System.setOut(ps);
312        File cf = new File ("config.txt");
313        cf.delete();
314        writedata("config.txt", configData);
315        File tf = new File ("tconfig.txt");
316        tf.delete();
317        writedata("tconfig.txt", trafficMgrData);
318        writedata("pconfig.txt", paramicsData);
319        writedata("empty.txt", "");
320        writeScriptfiles();
321    }
322
323    private void writeScriptfiles()
324    {
325        new File("scripts").mkdir();
326        writedata(kScenarioFolder+"/Cardfile.xml", cardfileData);
327        writedata(kScenarioFolder+"/one-incident.xml", oneincidentXML);
328    }
329
330    // Write the test data to a file
331    private void writedata(String filename, String data)
332    {
333        File cardFile = new File(filename);
334        // If a cardfile exists, leave it
335        if (!cardFile.exists())
336        {
337            PrintWriter writer = null;
338            try
339            {
340                writer = new PrintWriter(new FileWriter(filename));
341                writer.println(data);
342                writer.close();
343            } catch (Exception ex)
344            {
345                ex.printStackTrace();
346            }
347        }
348    }
349    private static final String cardfileData =
350            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
351            + "<!--Please do not modify titles. Note that these titles are not"
352            + "sent into the actual screen. If titles are to be modified,"
353            + "they need to be changed in CardfileHandler.java  -->"
354            + ""
355            + ""
356            + ""
357            + "<CARDFILE> "
358            + " <TAB title = \"Coastal Division Units\">"
359            + "         <CARDFILE_OBJ name = \"Name\" >"
360            + "                 <ADDRESS>Address</ADDRESS>"
361            + "                 <CITY>City</CITY>"
362            + "                 <STATE>State</STATE>"
363            + "                 <ZIP>Zip</ZIP>"
364            + "                 <PHONE1>Phone1</PHONE1>"
365            + "                 <PHONE2>Phone2</PHONE2>"
366            + "                 <FAX>Fax</FAX>"
367            + "         </CARDFILE_OBJ>"
368            + "         "
369            + "         <CARDFILE_OBJ  name = \"Name2\" >"
370            + "                 <ADDRESS>Address2</ADDRESS>"
371            + "                 <CITY>City2</CITY>"
372            + "                 <STATE>State2</STATE>"
373            + "                 <ZIP>Zip2</ZIP>"
374            + "                 <PHONE1>Phone12</PHONE1>"
375            + "                 <PHONE2>Phone22</PHONE2>"
376            + "                 <FAX>Fax2</FAX>"
377            + "         </CARDFILE_OBJ>"
378            + "         "
379            + " </TAB>"
380            + " "
381            + " <TAB title = \"Police/Sheriff/Coroner\">"
382            + " "
383            + "         <CARDFILE_OBJ  name = \"Name\" >"
384            + "                 <ADDRESS>Address</ADDRESS>"
385            + "                 <CITY>City</CITY>"
386            + "                 <STATE>State</STATE>"
387            + "                 <ZIP>Zip</ZIP>"
388            + "                 <PHONE1>Phone1</PHONE1>"
389            + "                 <PHONE2>Phone2</PHONE2>"
390            + "                 <FAX>Fax</FAX>"
391            + "         </CARDFILE_OBJ>"
392            + "         "
393            + " </TAB>"
394            + " "
395            + " <TAB title = \"Courts\">"
396            + "         "
397            + "         "
398            + "         "
399            + " </TAB>"
400            + " "
401            + " <TAB title = \"Public Transportation\">"
402            + "         "
403            + "         "
404            + "         "
405            + " </TAB>"
406            + " "
407            + " <TAB title = \"GG Other\">"
408            + "         "
409            + "         "
410            + "         "
411            + " </TAB>"
412            + " "
413            + " <TAB title = \"MY Misc\">"
414            + "         "
415            + "         "
416            + "         "
417            + " </TAB>"
418            + " "
419            + " <TAB title = \"SL Misc\">"
420            + "         "
421            + "         "
422            + "         "
423            + " </TAB>"
424            + " "
425            + " <TAB title = \"VT Misc\">"
426            + "         "
427            + "         "
428            + "         "
429            + " </TAB>"
430            + " "
431            + " <TAB title = \"CHP Offices\">"
432            + "         "
433            + "         "
434            + "         "
435            + " </TAB>"
436            + " "
437            + " <TAB title = \"State Agencies/Facilities\">"
438            + "         "
439            + "         "
440            + "         "
441            + " </TAB>"
442            + " "
443            + " <TAB title = \"Government Officials\">"
444            + "         "
445            + "         "
446            + "         "
447            + " </TAB>"
448            + " "
449            + " <TAB title = \"Federal Agencies\">"
450            + "         "
451            + "         "
452            + "         "
453            + " </TAB>"
454            + " "
455            + " <TAB title = \"Fire/EMS\">"
456            + "         "
457            + "         "
458            + "         "
459            + " </TAB>"
460            + " "
461            + " <TAB title = \"Jails\">"
462            + "         "
463            + "         "
464            + "         "
465            + " </TAB>"
466            + " "
467            + " <TAB title = \"Hospitals/Med Centers\">"
468            + "         "
469            + "         "
470            + "         "
471            + " </TAB>"
472            + " "
473            + " <TAB title = \"Tow Companies\">"
474            + "         "
475            + "         "
476            + "         "
477            + " </TAB>"
478            + " "
479            + " <TAB title = \"CalTrans\">"
480            + "         "
481            + "         "
482            + "         "
483            + " </TAB>"
484            + " "
485            + " <TAB title = \"County Roads\">"
486            + "         "
487            + "         "
488            + "         "
489            + " </TAB>"
490            + " "
491            + " <TAB title = \"Utilities\">"
492            + "         "
493            + "         "
494            + "         "
495            + " </TAB>"
496            + " "
497            + " <TAB title = \"Animal Control\">"
498            + "         "
499            + "         "
500            + "         "
501            + " </TAB>"
502            + " "
503            + " <TAB title = \"Airports\">"
504            + "         "
505            + "         "
506            + "         "
507            + " </TAB>"
508            + " "
509            + " <TAB title = \"Credit Cards\">"
510            + "         "
511            + "         "
512            + "         "
513            + " </TAB>"
514            + " "
515            + " <TAB title = \"GG Crisis Shelters\">"
516            + "         "
517            + "         "
518            + "         "
519            + " </TAB>"
520            + " "
521            + " <TAB title = \"Ranges\">"
522            + "         "
523            + "         "
524            + "         "
525            + " </TAB>"
526            + " "
527            + " <TAB title = \"Hotlines\">"
528            + "         "
529            + "         "
530            + "         "
531            + " </TAB>"
532            + " "
533            + " <TAB title = \"Hwy Patrols OOS\">"
534            + "         "
535            + "         "
536            + "         "
537            + " </TAB>"
538            + " "
539            + " <TAB title = \"Parks/Recreation\">"
540            + "         "
541            + "         "
542            + "         "
543            + " </TAB>"
544            + " "
545            + " <TAB title = \"Shelters\">"
546            + "         "
547            + "         "
548            + "         "
549            + " </TAB>"
550            + " "
551            + " <TAB title = \"SL County Services\">"
552            + "         "
553            + "         "
554            + "         "
555            + " </TAB>"
556            + " "
557            + " <TAB title = \"SL Resources\">"
558            + "         "
559            + "         "
560            + "         "
561            + " </TAB>"
562            + " "
563            + " <TAB title = \"Truck/Tire Repair\">"
564            + "         "
565            + "         "
566            + "         "
567            + " </TAB>"
568            + " "
569            + " <TAB title = \"MCC Employees\">"
570            + "         "
571            + "         "
572            + "         "
573            + " </TAB>"
574            + " "
575            + " <TAB title = \"Gate Access Codes\">"
576            + "         "
577            + "         "
578            + "         "
579            + " </TAB>"
580            + " "
581            + " <TAB title = \"VT Call Signs\">"
582            + "         "
583            + "         "
584            + "         "
585            + " </TAB>"
586            + " "
587            + " <TAB title = \"SLCC Employees\">"
588            + "         "
589            + "         "
590            + "         "
591            + " </TAB>"
592            + ""
593            + "</CARDFILE>";
594    private static final String oneincidentXML =
595            "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>"
596            + ""
597            + "<TMC_SCRIPT title=\"One Incident Simulation\">"
598            + ""
599            + " <SCRIPT_EVENT>"
600            + "         <TIME_INDEX>00:00:00</TIME_INDEX>"
601            + "         <INCIDENT LogNum=\"100\">Media Log</INCIDENT>           "
602            + "         "
603            + "         <CAD_DATA>"
604            + "                 <HEADER_INFO>"
605            + "                         <Type>Media</Type>"
606            + "                         <Beat>"
607            + "                         </Beat>"
608            + "                         <TruncLoc>"
609            + "                         </TruncLoc>"
610            + "                         <FullLoc>"
611            + "                         </FullLoc>"
612            + "                 </HEADER_INFO>                  "
613            + "                 "
614            + "                 <CAD_INCIDENT_EVENT>    "
615            + "                 </CAD_INCIDENT_EVENT>           "
616            + "                 "
617            + "         </CAD_DATA>                             "
618            + "         "
619            + " </SCRIPT_EVENT> "
620            + "</TMC_SCRIPT>";
621}
Note: See TracBrowser for help on using the repository browser.