| 1 | package tmcsim.cadsimulator; |
|---|
| 2 | |
|---|
| 3 | import java.io.File; |
|---|
| 4 | import java.rmi.RemoteException; |
|---|
| 5 | import java.util.logging.Level; |
|---|
| 6 | import java.util.logging.Logger; |
|---|
| 7 | import static junit.framework.Assert.assertEquals; |
|---|
| 8 | import static junit.framework.Assert.fail; |
|---|
| 9 | import static org.mockito.Mockito.*; |
|---|
| 10 | import org.uispec4j.*; |
|---|
| 11 | import org.uispec4j.interception.WindowInterceptor; |
|---|
| 12 | import tmcsim.cadsimulator.managers.ParamicsSimulationManager; |
|---|
| 13 | import tmcsim.common.CADEnums; |
|---|
| 14 | import tmcsim.common.ScriptException; |
|---|
| 15 | import tmcsim.common.SimulationException; |
|---|
| 16 | import tmcsim.interfaces.CADClientInterface; |
|---|
| 17 | import tmcsim.interfaces.SimulationManagerInterface; |
|---|
| 18 | import tmcsim.paramicscommunicator.ParamicsCommunicator; |
|---|
| 19 | |
|---|
| 20 | /** |
|---|
| 21 | * Test of CADSimulator GUI |
|---|
| 22 | * |
|---|
| 23 | * @author jdalbey |
|---|
| 24 | */ |
|---|
| 25 | public class CADSimulatorNetworkTest extends UISpecTestCase |
|---|
| 26 | { |
|---|
| 27 | |
|---|
| 28 | static final String configData = |
|---|
| 29 | "CADClientPort = 4444 \n" |
|---|
| 30 | + "CoordinatorRMIPort = 4445 \n" |
|---|
| 31 | + "CADRmiPort = 4446 \n" |
|---|
| 32 | + "UserInterface = tmcsim.cadsimulator.viewer.CADSimulatorViewer\n" |
|---|
| 33 | + "ParamicsProperties = pconfig.txt\n" |
|---|
| 34 | + "ATMSProperties = empty.txt\n" |
|---|
| 35 | + "MediaProperties = empty.txt\n"; |
|---|
| 36 | |
|---|
| 37 | public CADSimulatorNetworkTest(String testName) |
|---|
| 38 | { |
|---|
| 39 | super(testName); |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | @Override |
|---|
| 43 | public void tearDown() throws java.io.IOException |
|---|
| 44 | { |
|---|
| 45 | File removeMe = new File("config.txt"); |
|---|
| 46 | removeMe.delete(); |
|---|
| 47 | removeMe = new File("pconfig.txt"); |
|---|
| 48 | removeMe.delete(); |
|---|
| 49 | removeMe = new File("empty.txt"); |
|---|
| 50 | removeMe.delete(); |
|---|
| 51 | removeMe = new File("pmcprops.txt"); |
|---|
| 52 | removeMe.delete(); |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | /** |
|---|
| 56 | * Test of getCADTime method, of class CADSimulator. |
|---|
| 57 | */ |
|---|
| 58 | public void testGetCADTime() |
|---|
| 59 | { |
|---|
| 60 | System.out.println("getCADTime"); |
|---|
| 61 | String result = CADSimulator.getCADTime(); |
|---|
| 62 | // Just test length for now |
|---|
| 63 | assertEquals(4, result.length()); |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | /** |
|---|
| 67 | * Test of getCADDate method, of class CADSimulator. |
|---|
| 68 | */ |
|---|
| 69 | public void testGetCADDate() |
|---|
| 70 | { |
|---|
| 71 | System.out.println("getCADDate"); |
|---|
| 72 | String result = CADSimulator.getCADDate(); |
|---|
| 73 | // Just test length for now |
|---|
| 74 | assertEquals(6, result.length()); |
|---|
| 75 | } |
|---|
| 76 | /** |
|---|
| 77 | * Test of main method, of class CADSimulator. |
|---|
| 78 | */ |
|---|
| 79 | CADSimulator app; |
|---|
| 80 | |
|---|
| 81 | public void testConstructor() throws SimulationException, RemoteException, ScriptException |
|---|
| 82 | { |
|---|
| 83 | CADSimulatorFixture.writeConfigData(); |
|---|
| 84 | CADSimulatorFixture.writedata("config.txt", configData); |
|---|
| 85 | System.out.println("CADSimulator constructor"); |
|---|
| 86 | System.setProperty("CAD_SIM_PROPERTIES", "config.txt"); |
|---|
| 87 | Window cadwindow = null; |
|---|
| 88 | if (System.getProperty("CAD_SIM_PROPERTIES") != null) |
|---|
| 89 | { |
|---|
| 90 | cadwindow = WindowInterceptor.run(new Trigger() |
|---|
| 91 | { |
|---|
| 92 | public void run() |
|---|
| 93 | { |
|---|
| 94 | try |
|---|
| 95 | { |
|---|
| 96 | app = new CADSimulator(System.getProperty("CAD_SIM_PROPERTIES")); |
|---|
| 97 | } catch (Exception e) |
|---|
| 98 | { |
|---|
| 99 | fail("Couldn't launch CADSimulator" + e.getMessage()); |
|---|
| 100 | } |
|---|
| 101 | } |
|---|
| 102 | }); |
|---|
| 103 | } else |
|---|
| 104 | { |
|---|
| 105 | fail("CAD_SIM_PROPERTIES system property not defined."); |
|---|
| 106 | } |
|---|
| 107 | assertEquals("CAD Simulator", cadwindow.getTitle()); |
|---|
| 108 | Panel mainPanel = cadwindow.getPanel("contentPane"); |
|---|
| 109 | TextBox txtStatus = mainPanel.getTextBox("simulationStatus"); |
|---|
| 110 | assertEquals("No Script", txtStatus.getText()); |
|---|
| 111 | TextBox terminals = mainPanel.getTextBox("termConnectedTF"); |
|---|
| 112 | assertEquals("0", terminals.getText().trim()); |
|---|
| 113 | assertEquals("No", mainPanel.getTextBox("managerConnectedTF").getText().trim()); |
|---|
| 114 | assertEquals("0:00:00", mainPanel.getTextBox("simulationClockLabel").getText()); |
|---|
| 115 | |
|---|
| 116 | CADClientInterface ci = mock (CADClientInterface.class); |
|---|
| 117 | app.theCoordinator.registerForCallback(ci); |
|---|
| 118 | assertEquals("1", terminals.getText().trim()); |
|---|
| 119 | app.theCoordinator.registerForCallback(ci); |
|---|
| 120 | assertEquals("2", terminals.getText().trim()); |
|---|
| 121 | |
|---|
| 122 | SimulationManagerInterface si = mock(SimulationManagerInterface.class); |
|---|
| 123 | app.theCoordinator.registerForCallback(si); |
|---|
| 124 | assertEquals("Yes", mainPanel.getTextBox("managerConnectedTF").getText().trim()); |
|---|
| 125 | |
|---|
| 126 | |
|---|
| 127 | app.theCoordinator.setParamicsStatus(CADEnums.PARAMICS_STATUS.CONNECTED); |
|---|
| 128 | CADSimulatorFixture.pause(500); |
|---|
| 129 | assertEquals("Yes", mainPanel.getTextBox("paramicsConnectedTF").getText().trim()); |
|---|
| 130 | assertEquals("None", mainPanel.getTextBox("networkLoadedTF").getText().trim()); |
|---|
| 131 | |
|---|
| 132 | String loadedXML = "<Paramics>\n" |
|---|
| 133 | + "<Network_Status>LOADED</Network_Status>" |
|---|
| 134 | + "<Network_ID>1</Network_ID>" |
|---|
| 135 | + "</Paramics>"; |
|---|
| 136 | |
|---|
| 137 | // Thread pmc = new Thread(new ParamicsCommunicator("pmcprops.txt")); |
|---|
| 138 | // pmc.start(); |
|---|
| 139 | // pmc.stop(); |
|---|
| 140 | /* |
|---|
| 141 | * Start Paramics Controller. |
|---|
| 142 | * app.theCoordinator.connectToParamics(); |
|---|
| 143 | CADSimulatorFixture.pause(500); |
|---|
| 144 | assertEquals("Yes", mainPanel.getTextBox("paramicsConnectedTF").getText().trim()); |
|---|
| 145 | theCoorInt.loadParamicsNetwork(networkID); |
|---|
| 146 | * Optional: assert Status is "Sending Network ID" in Sim Mgr |
|---|
| 147 | * When " |
|---|
| 148 | * paramics_status.xml becomes |
|---|
| 149 | <Paramics> |
|---|
| 150 | <Network_Status>WARMING</Network_Status> |
|---|
| 151 | <Network_ID>1</Network_ID> |
|---|
| 152 | </Paramics> |
|---|
| 153 | * then Sim Mgr status becomes "Warming up" |
|---|
| 154 | When paramics_status.xml becomes loadedXML |
|---|
| 155 | CADSimulatorFixture.writedata(loadedXML, "paramics_status.xml"); |
|---|
| 156 | pause() |
|---|
| 157 | then Sim MGr status becomes Network Loaded and |
|---|
| 158 | CADSimulator Netowrk Loaded field says "1". |
|---|
| 159 | * |
|---|
| 160 | */ |
|---|
| 161 | ParamicsSimulationManager psm = mock(ParamicsSimulationManager.class); |
|---|
| 162 | when(psm.isConnected()).thenReturn(Boolean.TRUE); |
|---|
| 163 | app.theParamicsSimMgr = psm; |
|---|
| 164 | app.theCoordinator.loadParamicsNetwork(1); |
|---|
| 165 | // Load a script file |
|---|
| 166 | String autoloadScriptname = "scripts/one-incident.xml"; |
|---|
| 167 | app.theCoordinator.loadScriptFile(new File(autoloadScriptname)); |
|---|
| 168 | // The status should now say Ready |
|---|
| 169 | assertEquals("Ready", mainPanel.getTextBox("simulationStatus").getText().trim()); |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | } |
|---|