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

Revision 47, 20.6 KB checked in by jdalbey, 10 years ago (diff)

Merge 305 modifications into trunk.

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 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 CADSimulator 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 CADSimulator("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            + "MediaProperties        = empty.txt\n";
274    static final String paramicsData = "ParamicsCommHost = 127.0.0.1\n"
275            + "ParamicsCommPort       = 4450\n"
276            + "IncidentUpdateInterval = 30\n"
277            + "IncidentUpdateFile     = exchange.xml\n"
278            + "ParamicsStatusInterval = 15\n"
279            + "ParamicsStatusFile     = paramics_status.xml\n"
280            + "CameraStatusInterval   = 30\n"
281            + "CameraStatusFile       = camera_status.xml\n";
282    static final String cardfileURL = "http://pastebin.com/raw/Yr26nfp7";
283    static final String smallXMLURL = "http://pastebin.com/raw/Eqj2N5qD";
284    /*
285     * Creating instance of app must be done only once or you get registry
286     * bind problems, and code Written in Constructor is Executed
287     * before each Test Method
288     */
289
290    private void writeConfigData()
291    {
292        // Declare a stream to the output
293//        bos = new ByteArrayOutputStream();
294//        ps = new PrintStream(bos);
295        // Redirect the standard output
296//        System.setOut(ps);
297        writedata("config.txt", configData);
298        writedata("pconfig.txt", paramicsData);
299        writedata("empty.txt", "");
300        writeScriptfiles();
301    }
302
303    private void writeScriptfiles()
304    {
305        new File("scripts").mkdir();
306        writedata("scripts/Cardfile.xml", cardfileData);
307        writedata("scripts/one-incident.xml", oneincidentXML);
308    }
309
310    // Write the test data to a file
311    private void writedata(String filename, String data)
312    {
313        File cardFile = new File(filename);
314        // If a cardfile exists, leave it
315        if (!cardFile.exists())
316        {
317            PrintWriter writer = null;
318            try
319            {
320                writer = new PrintWriter(new FileWriter(filename));
321                writer.println(data);
322                writer.close();
323            } catch (Exception ex)
324            {
325                ex.printStackTrace();
326            }
327        }
328    }
329    private static final String cardfileData =
330            "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
331            + "<!--Please do not modify titles. Note that these titles are not"
332            + "sent into the actual screen. If titles are to be modified,"
333            + "they need to be changed in CardfileHandler.java  -->"
334            + ""
335            + ""
336            + ""
337            + "<CARDFILE> "
338            + " <TAB title = \"Coastal Division Units\">"
339            + "         <CARDFILE_OBJ name = \"Name\" >"
340            + "                 <ADDRESS>Address</ADDRESS>"
341            + "                 <CITY>City</CITY>"
342            + "                 <STATE>State</STATE>"
343            + "                 <ZIP>Zip</ZIP>"
344            + "                 <PHONE1>Phone1</PHONE1>"
345            + "                 <PHONE2>Phone2</PHONE2>"
346            + "                 <FAX>Fax</FAX>"
347            + "         </CARDFILE_OBJ>"
348            + "         "
349            + "         <CARDFILE_OBJ  name = \"Name2\" >"
350            + "                 <ADDRESS>Address2</ADDRESS>"
351            + "                 <CITY>City2</CITY>"
352            + "                 <STATE>State2</STATE>"
353            + "                 <ZIP>Zip2</ZIP>"
354            + "                 <PHONE1>Phone12</PHONE1>"
355            + "                 <PHONE2>Phone22</PHONE2>"
356            + "                 <FAX>Fax2</FAX>"
357            + "         </CARDFILE_OBJ>"
358            + "         "
359            + " </TAB>"
360            + " "
361            + " <TAB title = \"Police/Sheriff/Coroner\">"
362            + " "
363            + "         <CARDFILE_OBJ  name = \"Name\" >"
364            + "                 <ADDRESS>Address</ADDRESS>"
365            + "                 <CITY>City</CITY>"
366            + "                 <STATE>State</STATE>"
367            + "                 <ZIP>Zip</ZIP>"
368            + "                 <PHONE1>Phone1</PHONE1>"
369            + "                 <PHONE2>Phone2</PHONE2>"
370            + "                 <FAX>Fax</FAX>"
371            + "         </CARDFILE_OBJ>"
372            + "         "
373            + " </TAB>"
374            + " "
375            + " <TAB title = \"Courts\">"
376            + "         "
377            + "         "
378            + "         "
379            + " </TAB>"
380            + " "
381            + " <TAB title = \"Public Transportation\">"
382            + "         "
383            + "         "
384            + "         "
385            + " </TAB>"
386            + " "
387            + " <TAB title = \"GG Other\">"
388            + "         "
389            + "         "
390            + "         "
391            + " </TAB>"
392            + " "
393            + " <TAB title = \"MY Misc\">"
394            + "         "
395            + "         "
396            + "         "
397            + " </TAB>"
398            + " "
399            + " <TAB title = \"SL Misc\">"
400            + "         "
401            + "         "
402            + "         "
403            + " </TAB>"
404            + " "
405            + " <TAB title = \"VT Misc\">"
406            + "         "
407            + "         "
408            + "         "
409            + " </TAB>"
410            + " "
411            + " <TAB title = \"CHP Offices\">"
412            + "         "
413            + "         "
414            + "         "
415            + " </TAB>"
416            + " "
417            + " <TAB title = \"State Agencies/Facilities\">"
418            + "         "
419            + "         "
420            + "         "
421            + " </TAB>"
422            + " "
423            + " <TAB title = \"Government Officials\">"
424            + "         "
425            + "         "
426            + "         "
427            + " </TAB>"
428            + " "
429            + " <TAB title = \"Federal Agencies\">"
430            + "         "
431            + "         "
432            + "         "
433            + " </TAB>"
434            + " "
435            + " <TAB title = \"Fire/EMS\">"
436            + "         "
437            + "         "
438            + "         "
439            + " </TAB>"
440            + " "
441            + " <TAB title = \"Jails\">"
442            + "         "
443            + "         "
444            + "         "
445            + " </TAB>"
446            + " "
447            + " <TAB title = \"Hospitals/Med Centers\">"
448            + "         "
449            + "         "
450            + "         "
451            + " </TAB>"
452            + " "
453            + " <TAB title = \"Tow Companies\">"
454            + "         "
455            + "         "
456            + "         "
457            + " </TAB>"
458            + " "
459            + " <TAB title = \"CalTrans\">"
460            + "         "
461            + "         "
462            + "         "
463            + " </TAB>"
464            + " "
465            + " <TAB title = \"County Roads\">"
466            + "         "
467            + "         "
468            + "         "
469            + " </TAB>"
470            + " "
471            + " <TAB title = \"Utilities\">"
472            + "         "
473            + "         "
474            + "         "
475            + " </TAB>"
476            + " "
477            + " <TAB title = \"Animal Control\">"
478            + "         "
479            + "         "
480            + "         "
481            + " </TAB>"
482            + " "
483            + " <TAB title = \"Airports\">"
484            + "         "
485            + "         "
486            + "         "
487            + " </TAB>"
488            + " "
489            + " <TAB title = \"Credit Cards\">"
490            + "         "
491            + "         "
492            + "         "
493            + " </TAB>"
494            + " "
495            + " <TAB title = \"GG Crisis Shelters\">"
496            + "         "
497            + "         "
498            + "         "
499            + " </TAB>"
500            + " "
501            + " <TAB title = \"Ranges\">"
502            + "         "
503            + "         "
504            + "         "
505            + " </TAB>"
506            + " "
507            + " <TAB title = \"Hotlines\">"
508            + "         "
509            + "         "
510            + "         "
511            + " </TAB>"
512            + " "
513            + " <TAB title = \"Hwy Patrols OOS\">"
514            + "         "
515            + "         "
516            + "         "
517            + " </TAB>"
518            + " "
519            + " <TAB title = \"Parks/Recreation\">"
520            + "         "
521            + "         "
522            + "         "
523            + " </TAB>"
524            + " "
525            + " <TAB title = \"Shelters\">"
526            + "         "
527            + "         "
528            + "         "
529            + " </TAB>"
530            + " "
531            + " <TAB title = \"SL County Services\">"
532            + "         "
533            + "         "
534            + "         "
535            + " </TAB>"
536            + " "
537            + " <TAB title = \"SL Resources\">"
538            + "         "
539            + "         "
540            + "         "
541            + " </TAB>"
542            + " "
543            + " <TAB title = \"Truck/Tire Repair\">"
544            + "         "
545            + "         "
546            + "         "
547            + " </TAB>"
548            + " "
549            + " <TAB title = \"MCC Employees\">"
550            + "         "
551            + "         "
552            + "         "
553            + " </TAB>"
554            + " "
555            + " <TAB title = \"Gate Access Codes\">"
556            + "         "
557            + "         "
558            + "         "
559            + " </TAB>"
560            + " "
561            + " <TAB title = \"VT Call Signs\">"
562            + "         "
563            + "         "
564            + "         "
565            + " </TAB>"
566            + " "
567            + " <TAB title = \"SLCC Employees\">"
568            + "         "
569            + "         "
570            + "         "
571            + " </TAB>"
572            + ""
573            + "</CARDFILE>";
574    private static final String oneincidentXML =
575            "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>"
576            + ""
577            + "<TMC_SCRIPT title=\"One Incident Simulation\">"
578            + ""
579            + " <SCRIPT_EVENT>"
580            + "         <TIME_INDEX>00:00:00</TIME_INDEX>"
581            + "         <INCIDENT LogNum=\"100\">Media Log</INCIDENT>           "
582            + "         "
583            + "         <CAD_DATA>"
584            + "                 <HEADER_INFO>"
585            + "                         <Type>Media</Type>"
586            + "                         <Beat>"
587            + "                         </Beat>"
588            + "                         <TruncLoc>"
589            + "                         </TruncLoc>"
590            + "                         <FullLoc>"
591            + "                         </FullLoc>"
592            + "                 </HEADER_INFO>                  "
593            + "                 "
594            + "                 <CAD_INCIDENT_EVENT>    "
595            + "                 </CAD_INCIDENT_EVENT>           "
596            + "                 "
597            + "         </CAD_DATA>                             "
598            + "         "
599            + " </SCRIPT_EVENT> "
600            + "</TMC_SCRIPT>";
601}
Note: See TracBrowser for help on using the repository browser.