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

Revision 656, 22.3 KB checked in by jdalbey, 4 years ago (diff)

!CADSimulatorConsoleTest.java: fix defect in verify() method.

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