package tmcsim.cadsimulator; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.PrintWriter; import java.io.StringWriter; import java.util.logging.Level; import java.util.logging.Logger; import junit.framework.TestCase; import static org.mockito.Mockito.mock; import tmcsim.cadsimulator.viewer.CADConsoleViewer; import tmcsim.common.CADEnums; import tmcsim.common.ScriptException; import tmcsim.common.SimulationException; import tmcsim.interfaces.CADClientInterface; import tmcsim.interfaces.SimulationManagerInterface; /** * Test of CADSimulator Console * * @author jdalbey */ public class CADSimulatorConsoleTest extends TestCase { private static CADServer app = null; private CADConsoleViewer console; private StringWriter sw; public CADSimulatorConsoleTest(String testName) { super(testName); } public void setUp() throws Exception { super.setUp(); writeConfigData(); // Since CADSimulator has registry, we musn't instantiate it more than once if (app == null) { try { app = new CADServer("config.txt"); } catch (Exception ex) { fail("Couldn't launch CADSimulator"); } } console = (CADConsoleViewer) app.theViewer; sw = new StringWriter(); console.setWriter(sw); } @Override public void tearDown() throws java.io.IOException { File removeMe = new File("config.txt"); removeMe.delete(); removeMe = new File("pconfig.txt"); removeMe.delete(); removeMe = new File("tconfig.txt"); removeMe.delete(); removeMe = new File("empty.txt"); removeMe.delete(); } /** * compare StringWriter contents against expected */ private void verify(String msg, String expect) { String result = sw.toString().trim(); result = result.replaceAll("\n", ","); String fullExpect = expect.trim(); fullExpect = fullExpect.replaceAll("\n", ","); // System.out.println(fullExpect); System.out.println(result.substring(result.length() - fullExpect.length(), result.length())); // String diff = StringUtils.difference(fullExpect, result); boolean match = result.endsWith(fullExpect); assertTrue(msg + ": " + result, match); } public static void pause(int millis) { try { Thread.sleep(millis); } catch (InterruptedException ex) { } } /** * The tests must be all run in one method, because the order matters. As * separate methods, we couldn't control which is executed first. * * @throws java.io.IOException * @throws SimulationException * @throws ScriptException */ public void testAll() throws java.io.IOException, SimulationException, ScriptException { CADConsoleViewer console = (CADConsoleViewer) app.theViewer; String expected1 = "--- CAD Simulator ---\n" + "Elapsed Simulation Time : 0:00:00\n" + "Status : No Script\n" + "Connected CAD Terminals : 0\n" + "Simulation Manager Connected: No\n" + "Connected to Paramics : No\n" + "Network Loaded : None\n" + "-- Info Messages --\n\n" + "-- Error Messages --"; System.out.println("setVisible"); console.setVisible(true); verify("Initial output incorrect: ", expected1); String expected2 = "--- CAD Simulator ---\n" + "Elapsed Simulation Time : 0:00:00\n" + "Status : No Script\n" + "Connected CAD Terminals : 1\n" + "Simulation Manager Connected: No\n" + "Connected to Paramics : No\n" + "Network Loaded : None\n" + "-- Info Messages --\n\n" + "-- Error Messages --\n\n"; System.out.println("connect one"); CADClientInterface ci = mock(CADClientInterface.class); app.theCoordinator.registerForCallback(ci); verify("connected 1 terminal output incorrect: ", expected2); String expected3 = "--- CAD Simulator ---\n" + "Elapsed Simulation Time : 0:00:00\n" + "Status : No Script\n" + "Connected CAD Terminals : 2\n" + "Simulation Manager Connected: No\n" + "Connected to Paramics : No\n" + "Network Loaded : None\n" + "-- Info Messages --\n\n" + "-- Error Messages --\n\n"; System.out.println("connect two"); app.theCoordinator.registerForCallback(ci); verify("connected 2 terminals output incorrect: ", expected3); System.out.println("disconnect"); app.theCoordinator.unregisterForCallback(ci); verify("disconnect terminal output incorrect: ", expected2); app.theCoordinator.unregisterForCallback(ci); String expected4 = "--- CAD Simulator ---\n" + "Elapsed Simulation Time : 0:00:00\n" + "Status : No Script\n" + "Connected CAD Terminals : 0\n" + "Simulation Manager Connected: Yes\n" + "Connected to Paramics : No\n" + "Network Loaded : None\n" + "-- Info Messages --\n\n" + "-- Error Messages --\n\n"; System.out.println("sim mgr connect"); SimulationManagerInterface si = mock(SimulationManagerInterface.class); app.theCoordinator.registerForCallback(si); verify("sim mgr connected output incorrect: ", expected4); String expected5 = "--- CAD Simulator ---\n" + "Elapsed Simulation Time : 0:00:00\n" + "Status : No Script\n" + "Connected CAD Terminals : 0\n" + "Simulation Manager Connected: Yes\n" + "Connected to Paramics : No\n" + "Network Loaded : None\n" + "-- Info Messages --\n" + ". = Console Info Message.\n" + "-- Error Messages --\n\n"; System.out.println("Info msg"); Logger cadSimLogger = Logger.getLogger("tmcsim.cadsimulator"); cadSimLogger.logp(Level.INFO, "", "", "Console Info Message."); verify("Info message output incorrect: ", expected5); String expected7 = "--- CAD Simulator ---\n" + "Elapsed Simulation Time : 0:00:00\n" + "Status : No Script\n" + "Connected CAD Terminals : 0\n" + "Simulation Manager Connected: Yes\n" + "Connected to Paramics : No\n" + "Network Loaded : None\n" + "-- Info Messages --\n" + ". = Console Info Message.\n" + "-- Error Messages --\n" + "Someclass.Somemethod = Something bad happened.\n"; System.out.println("Error msg"); cadSimLogger = Logger.getLogger("tmcsim.cadsimulator"); cadSimLogger.logp(Level.SEVERE, "Someclass", "Somemethod", "Something bad happened."); verify("Error message output incorrect: ", expected7); String expected6 = "--- CAD Simulator ---\n" + "Elapsed Simulation Time : 0:00:00\n" + "Status : No Script\n" + "Connected CAD Terminals : 0\n" + "Simulation Manager Connected: Yes\n" + "Connected to Paramics : Yes\n" + "Network Loaded : None\n" + "-- Info Messages --\n" + ". = Console Info Message.\n" + "-- Error Messages --\n" + "Someclass.Somemethod = Something bad happened.\n"; System.out.println("Paramics connect"); app.theCoordinator.setParamicsStatus(CADEnums.PARAMICS_STATUS.CONNECTED); pause(500); pause(500); verify("paramics connected should be yes", expected6); String expected8 = "--- CAD Simulator ---\n" + "Elapsed Simulation Time : 0:00:00\n" + "Status : Ready\n" + "Connected CAD Terminals : 0\n" + "Simulation Manager Connected: Yes\n" + "Connected to Paramics : Yes\n" + "Network Loaded : None\n" + "-- Info Messages --\n" + ". = Console Info Message.\n" + "-- Error Messages --\n" + "Someclass.Somemethod = Something bad happened.\n"; String expected9 = "--- CAD Simulator ---\n" + "Elapsed Simulation Time : 0:00:01\n" + "Status : Running\n" + "Connected CAD Terminals : 0\n" + "Simulation Manager Connected: Yes\n" + "Connected to Paramics : Yes\n" + "Network Loaded : None\n" + "-- Info Messages --\n" + ". = Console Info Message.\n" + "-- Error Messages --\n" + "Someclass.Somemethod = Something bad happened.\n"; System.out.println("Sim status"); // Load a script file - to put status at Ready String autoloadScriptname = "scripts/one-incident.xml"; app.theCoordinator.loadScriptFile(new File(autoloadScriptname)); pause(500); verify("Status should be 'ready'", expected8); // startSimulation to put status at Running app.theCoordinator.startSimulation(); pause(500); verify("Status should be 'running', time should be 0:01", expected9); } // public void testNetworkID() // { // String expected10 = // "--- CAD Simulator ---\n" // + "Elapsed Simulation Time : 0:00:00\n" // + "Status : No Script\n" // + "Connected CAD Terminals : 0\n" // + "Simulation Manager Connected: No\n" // + "Connected to Paramics : No\n" // + "Network Loaded : 17\n" // + "-- Info Messages --\n\n" // + "-- Error Messages --\n\n"; // // this will tell the model it has a new network ID // cadmodel.setParamicsNetworkLoaded("17"); // cadmodel.setParamicsStatus(CADEnums.PARAMICS_STATUS.LOADED); // pause(500); // verify("network id should be 17", expected10); // } // ByteArrayOutputStream bos; // PrintStream ps; static final String configData = "CADClientPort = 4444 \n" + "CoordinatorRMIPort = 4445 \n" + "CADRmiPort = 4446 \n" + "UserInterface = tmcsim.cadsimulator.viewer.CADConsoleViewer\n" + "ParamicsProperties = pconfig.txt\n" + "ATMSProperties = empty.txt\n" + "TrafficMgrProperties = tconfig.txt\n" + "MediaProperties = empty.txt\n" + "ElapsedTimeFile = webapps/dynamicdata/sim_elapsedtime.json"; static final String trafficMgrData = "Highways_Map_File = config/vds_data/postmile_coordinates.txt\n" +"Events_File = config/vds_data/atmsBatchEvents.txt\n" +"FEPSim_IP_addr = localhost\n" +"Output_Destination = Console\n" +"Highway_Status_File = /tmp/highway_status.json\n"; static final String paramicsData = "ParamicsCommHost = 127.0.0.1\n" + "ParamicsCommPort = 4450\n" + "IncidentUpdateInterval = 30\n" + "IncidentUpdateFile = exchange.xml\n" + "ParamicsStatusInterval = 15\n" + "ParamicsStatusFile = paramics_status.xml\n" + "CameraStatusInterval = 30\n" + "CameraStatusFile = camera_status.xml\n"; static final String cardfileURL = "http://pastebin.com/raw/Yr26nfp7"; static final String smallXMLURL = "http://pastebin.com/raw/Eqj2N5qD"; /* * Creating instance of app must be done only once or you get registry * bind problems, and code Written in Constructor is Executed * before each Test Method */ private void writeConfigData() { // Declare a stream to the output // bos = new ByteArrayOutputStream(); // ps = new PrintStream(bos); // Redirect the standard output // System.setOut(ps); File cf = new File ("config.txt"); cf.delete(); writedata("config.txt", configData); File tf = new File ("tconfig.txt"); tf.delete(); writedata("tconfig.txt", trafficMgrData); writedata("pconfig.txt", paramicsData); writedata("empty.txt", ""); writeScriptfiles(); } private void writeScriptfiles() { new File("scripts").mkdir(); writedata("scripts/Cardfile.xml", cardfileData); writedata("scripts/one-incident.xml", oneincidentXML); } // Write the test data to a file private void writedata(String filename, String data) { File cardFile = new File(filename); // If a cardfile exists, leave it if (!cardFile.exists()) { PrintWriter writer = null; try { writer = new PrintWriter(new FileWriter(filename)); writer.println(data); writer.close(); } catch (Exception ex) { ex.printStackTrace(); } } } private static final String cardfileData = "" + "" + "" + "" + "" + " " + " " + " " + "
Address
" + " City" + " State" + " Zip" + " Phone1" + " Phone2" + " Fax" + "
" + " " + " " + "
Address2
" + " City2" + " State2" + " Zip2" + " Phone12" + " Phone22" + " Fax2" + "
" + " " + "
" + " " + " " + " " + " " + "
Address
" + " City" + " State" + " Zip" + " Phone1" + " Phone2" + " Fax" + "
" + " " + "
" + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + "" + "
"; private static final String oneincidentXML = "" + "" + "" + "" + " " + " 00:00:00" + " Media Log " + " " + " " + " " + " Media" + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + " " + ""; }