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 @ 345

Revision 345, 21.1 KB checked in by jdalbey, 7 years ago (diff)

minor changes to output messages to get unit tests to pass. All passing now.

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