| 1 | package tmcsim.cadsimulator; |
|---|
| 2 | |
|---|
| 3 | import java.io.File; |
|---|
| 4 | import java.io.IOException; |
|---|
| 5 | import java.util.logging.Level; |
|---|
| 6 | import java.util.logging.Logger; |
|---|
| 7 | import junit.framework.TestCase; |
|---|
| 8 | import static org.mockito.Mockito.mock; |
|---|
| 9 | import static tmcsim.cadsimulator.CADSimulatorFixture.writedata; |
|---|
| 10 | import tmcsim.common.CADEnums; |
|---|
| 11 | import tmcsim.common.ScriptException; |
|---|
| 12 | import tmcsim.common.SimulationException; |
|---|
| 13 | import tmcsim.interfaces.CADClientInterface; |
|---|
| 14 | import tmcsim.interfaces.SimulationManagerInterface; |
|---|
| 15 | import static tmcsim.simulationmanager.SimulationManager.SCENARIOS_DIR; |
|---|
| 16 | |
|---|
| 17 | /** |
|---|
| 18 | * Driver for CADSimulator Console - output to stdout. |
|---|
| 19 | * |
|---|
| 20 | * @author jdalbey |
|---|
| 21 | */ |
|---|
| 22 | public class CADSimulatorConsoleDriver extends TestCase |
|---|
| 23 | { |
|---|
| 24 | |
|---|
| 25 | static CADServer app; |
|---|
| 26 | static final String configData = |
|---|
| 27 | "CADClientPort = 4444 \n" |
|---|
| 28 | + "CoordinatorRMIPort = 4445 \n" |
|---|
| 29 | + "CADRmiPort = 4446 \n" |
|---|
| 30 | + "UserInterface = tmcsim.cadsimulator.viewer.CADConsoleViewer\n" |
|---|
| 31 | + "ParamicsProperties = pconfig.txt\n" |
|---|
| 32 | + "ATMSProperties = empty.txt\n" |
|---|
| 33 | + "MediaProperties = empty.txt\n" |
|---|
| 34 | + "TrafficMgrProperties = config/traffic_model_config/traffic_model_config.properties\n" |
|---|
| 35 | + "ElapsedTimeFile = webapps/dynamicdata/sim_elapsedtime.json\n" |
|---|
| 36 | + "CADcommentsLog = webapps/dynamicdata/CADcomments.log\n"; |
|---|
| 37 | static final String paramicsData = "ParamicsCommHost = 192.168.251.45\n" |
|---|
| 38 | + "ParamicsCommPort = 4450\n" |
|---|
| 39 | + "IncidentUpdateInterval = 30\n" |
|---|
| 40 | + "IncidentUpdateFile = exchange.xml\n" |
|---|
| 41 | + "ParamicsStatusInterval = 15\n" |
|---|
| 42 | + "ParamicsStatusFile = paramics_status.xml\n" |
|---|
| 43 | + "CameraStatusInterval = 30\n" |
|---|
| 44 | + "CameraStatusFile = camera_status.xml\n"; |
|---|
| 45 | /* |
|---|
| 46 | * Creating instance of app must be done only once or you get registry |
|---|
| 47 | * bind problems, and code Written in Constructor is Executed |
|---|
| 48 | * before each Test Method |
|---|
| 49 | */ |
|---|
| 50 | |
|---|
| 51 | public static void main(String[] args) throws IOException, SimulationException, ScriptException |
|---|
| 52 | { |
|---|
| 53 | writedata("config.txt", configData); |
|---|
| 54 | writedata("pconfig.txt", paramicsData); |
|---|
| 55 | writedata("empty.txt", ""); |
|---|
| 56 | CADSimulatorFixture.writeScriptfiles(); |
|---|
| 57 | CADSimulatorFixture.startCADSim(); |
|---|
| 58 | (new CADSimulatorConsoleDriver()).testAll(); |
|---|
| 59 | System.exit(0); |
|---|
| 60 | } |
|---|
| 61 | // Write the test data to a file |
|---|
| 62 | |
|---|
| 63 | private void testAll() throws java.io.IOException, SimulationException, ScriptException |
|---|
| 64 | { |
|---|
| 65 | |
|---|
| 66 | CADClientInterface ci = mock(CADClientInterface.class); //new tmcsim.cadsimulator.CADSimulatorDriver.FakeClient(); |
|---|
| 67 | app.theCoordinator.registerForCallback(ci); |
|---|
| 68 | // Convert the output stream into a string we can test. |
|---|
| 69 | |
|---|
| 70 | app.theCoordinator.registerForCallback(ci); |
|---|
| 71 | |
|---|
| 72 | SimulationManagerInterface si = mock(SimulationManagerInterface.class); |
|---|
| 73 | |
|---|
| 74 | app.theCoordinator.registerForCallback(si); |
|---|
| 75 | |
|---|
| 76 | Logger cadSimLogger = Logger.getLogger("tmcsim.cadsimulator"); |
|---|
| 77 | cadSimLogger.logp(Level.INFO, "", "", "Sample Info Message."); |
|---|
| 78 | |
|---|
| 79 | app.theCoordinator.setParamicsStatus(CADEnums.PARAMICS_STATUS.CONNECTED); |
|---|
| 80 | CADSimulatorFixture.pause(500); |
|---|
| 81 | |
|---|
| 82 | // Show loaded, and ALSO update the current network id |
|---|
| 83 | app.theCoordinator.setParamicsStatus(CADEnums.PARAMICS_STATUS.LOADED); |
|---|
| 84 | CADSimulatorFixture.pause(500); |
|---|
| 85 | |
|---|
| 86 | app.theCoordinator.setScriptStatus(CADEnums.SCRIPT_STATUS.SCRIPT_RUNNING); |
|---|
| 87 | CADSimulatorFixture.pause(500); |
|---|
| 88 | |
|---|
| 89 | cadSimLogger.logp(Level.SEVERE, "Someclass", "Somemethod", "Sample error message."); |
|---|
| 90 | |
|---|
| 91 | // Load a script file |
|---|
| 92 | String autoloadScriptname = SCENARIOS_DIR+"/one-incident.xml"; |
|---|
| 93 | app.theCoordinator.loadScriptFile(new File(autoloadScriptname)); |
|---|
| 94 | // The status should now say Ready |
|---|
| 95 | |
|---|
| 96 | |
|---|
| 97 | // ParamicsSimulationManager psm = mock(ParamicsSimulationManager.class); |
|---|
| 98 | // when(psm.isConnected()).thenReturn(Boolean.TRUE, Boolean.FALSE); |
|---|
| 99 | // app.theParamicsSimMgr = psm; |
|---|
| 100 | // app.theCoordinator.connectToParamics(); |
|---|
| 101 | // app.theCoordinator.loadParamicsNetwork(1); |
|---|
| 102 | // CADSimulatorFixture.pause(500); |
|---|
| 103 | // app.theCoordinator.disconnectFromParamics(); |
|---|
| 104 | // CADSimulatorFixture.pause(500); |
|---|
| 105 | |
|---|
| 106 | app.theCoordinator.startSimulation(); |
|---|
| 107 | CADSimulatorFixture.pause(1500); |
|---|
| 108 | app.theCoordinator.pauseSimulation(); |
|---|
| 109 | app = null; |
|---|
| 110 | cleanup(); |
|---|
| 111 | } |
|---|
| 112 | |
|---|
| 113 | private void cleanup() |
|---|
| 114 | { |
|---|
| 115 | File removeMe = new File("config.txt"); |
|---|
| 116 | removeMe.delete(); |
|---|
| 117 | removeMe = new File("pconfig.txt"); |
|---|
| 118 | removeMe.delete(); |
|---|
| 119 | removeMe = new File("empty.txt"); |
|---|
| 120 | removeMe.delete(); |
|---|
| 121 | } |
|---|
| 122 | String expected1 = |
|---|
| 123 | "--- CAD Simulator ---\n" |
|---|
| 124 | + "Elapsed Simulation Time : 0:00:00\n" |
|---|
| 125 | + "Status : No Script\n" |
|---|
| 126 | + "Connected CAD Terminals : 0\n" |
|---|
| 127 | + "Simulation Manager Connected: No\n" |
|---|
| 128 | + "Connected to Paramics : No\n" |
|---|
| 129 | + "Network Loaded : \n" |
|---|
| 130 | + "-- Info Messages --\n\n" |
|---|
| 131 | + "-- Error Messages --\n\n"; |
|---|
| 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 : \n" |
|---|
| 140 | + "-- Info Messages --\n\n" |
|---|
| 141 | + "-- Error Messages --\n\n"; |
|---|
| 142 | String expected3 = |
|---|
| 143 | "--- CAD Simulator ---\n" |
|---|
| 144 | + "Elapsed Simulation Time : 0:00:00\n" |
|---|
| 145 | + "Status : No Script\n" |
|---|
| 146 | + "Connected CAD Terminals : 2\n" |
|---|
| 147 | + "Simulation Manager Connected: No\n" |
|---|
| 148 | + "Connected to Paramics : No\n" |
|---|
| 149 | + "Network Loaded : \n" |
|---|
| 150 | + "-- Info Messages --\n\n" |
|---|
| 151 | + "-- Error Messages --\n\n"; |
|---|
| 152 | String expected4 = |
|---|
| 153 | "--- CAD Simulator ---\n" |
|---|
| 154 | + "Elapsed Simulation Time : 0:00:00\n" |
|---|
| 155 | + "Status : No Script\n" |
|---|
| 156 | + "Connected CAD Terminals : 2\n" |
|---|
| 157 | + "Simulation Manager Connected: Yes\n" |
|---|
| 158 | + "Connected to Paramics : No\n" |
|---|
| 159 | + "Network Loaded : \n" |
|---|
| 160 | + "-- Info Messages --\n\n" |
|---|
| 161 | + "-- Error Messages --\n\n"; |
|---|
| 162 | String expected5 = |
|---|
| 163 | "--- CAD Simulator ---\n" |
|---|
| 164 | + "Elapsed Simulation Time : 0:00:00\n" |
|---|
| 165 | + "Status : No Script\n" |
|---|
| 166 | + "Connected CAD Terminals : 2\n" |
|---|
| 167 | + "Simulation Manager Connected: Yes\n" |
|---|
| 168 | + "Connected to Paramics : No\n" |
|---|
| 169 | + "Network Loaded : \n" |
|---|
| 170 | + "-- Info Messages --\n" |
|---|
| 171 | + ". = Sample Info Message.\n" |
|---|
| 172 | + "-- Error Messages --\n\n"; |
|---|
| 173 | String expected6 = |
|---|
| 174 | "--- CAD Simulator ---\n" |
|---|
| 175 | + "Elapsed Simulation Time : 0:00:00\n" |
|---|
| 176 | + "Status : No Script\n" |
|---|
| 177 | + "Connected CAD Terminals : 2\n" |
|---|
| 178 | + "Simulation Manager Connected: Yes\n" |
|---|
| 179 | + "Connected to Paramics : Yes\n" |
|---|
| 180 | + "Network Loaded : \n" |
|---|
| 181 | + "-- Info Messages --\n" |
|---|
| 182 | + ". = Sample Info Message.\n" |
|---|
| 183 | + "-- Error Messages --\n\n"; |
|---|
| 184 | String expected7 = |
|---|
| 185 | "--- CAD Simulator ---\n" |
|---|
| 186 | + "Elapsed Simulation Time : 0:00:00\n" |
|---|
| 187 | + "Status : No Script\n" |
|---|
| 188 | + "Connected CAD Terminals : 2\n" |
|---|
| 189 | + "Simulation Manager Connected: Yes\n" |
|---|
| 190 | + "Connected to Paramics : Yes\n" |
|---|
| 191 | + "Network Loaded : 3\n" |
|---|
| 192 | + "-- Info Messages --\n" |
|---|
| 193 | + ". = Sample Info Message.\n" |
|---|
| 194 | + "-- Error Messages --\n\n"; |
|---|
| 195 | } |
|---|