| 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 | |
|---|
| 19 | /** |
|---|
| 20 | * Test of CADSimulator GUI |
|---|
| 21 | * |
|---|
| 22 | * @author jdalbey |
|---|
| 23 | */ |
|---|
| 24 | public class CADSimulatorGUITest extends UISpecTestCase |
|---|
| 25 | { |
|---|
| 26 | |
|---|
| 27 | static final String configData = |
|---|
| 28 | "CADClientPort = 4444 \n" |
|---|
| 29 | + "CoordinatorRMIPort = 4445 \n" |
|---|
| 30 | + "CADRmiPort = 4446 \n" |
|---|
| 31 | + "UserInterface = tmcsim.cadsimulator.viewer.CADSimulatorViewer\n" |
|---|
| 32 | + "ParamicsProperties = pconfig.txt\n" |
|---|
| 33 | + "ATMSProperties = empty.txt\n" |
|---|
| 34 | + "MediaProperties = empty.txt\n"; |
|---|
| 35 | |
|---|
| 36 | public CADSimulatorGUITest(String testName) |
|---|
| 37 | { |
|---|
| 38 | super(testName); |
|---|
| 39 | } |
|---|
| 40 | |
|---|
| 41 | @Override |
|---|
| 42 | public void tearDown() throws java.io.IOException |
|---|
| 43 | { |
|---|
| 44 | File removeMe = new File("config.txt"); |
|---|
| 45 | removeMe.delete(); |
|---|
| 46 | removeMe = new File("pconfig.txt"); |
|---|
| 47 | removeMe.delete(); |
|---|
| 48 | removeMe = new File("empty.txt"); |
|---|
| 49 | removeMe.delete(); |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | /** |
|---|
| 53 | * Test of getCADTime method, of class CADSimulator. |
|---|
| 54 | */ |
|---|
| 55 | public void testGetCADTime() |
|---|
| 56 | { |
|---|
| 57 | System.out.println("getCADTime"); |
|---|
| 58 | String result = CADSimulator.getCADTime(); |
|---|
| 59 | // Just test length for now |
|---|
| 60 | assertEquals(4, result.length()); |
|---|
| 61 | } |
|---|
| 62 | |
|---|
| 63 | /** |
|---|
| 64 | * Test of getCADDate method, of class CADSimulator. |
|---|
| 65 | */ |
|---|
| 66 | public void testGetCADDate() |
|---|
| 67 | { |
|---|
| 68 | System.out.println("getCADDate"); |
|---|
| 69 | String result = CADSimulator.getCADDate(); |
|---|
| 70 | // Just test length for now |
|---|
| 71 | assertEquals(6, result.length()); |
|---|
| 72 | } |
|---|
| 73 | /** |
|---|
| 74 | * Test of main method, of class CADSimulator. |
|---|
| 75 | */ |
|---|
| 76 | CADSimulator app; |
|---|
| 77 | |
|---|
| 78 | public void testConstructor() throws SimulationException, RemoteException, ScriptException |
|---|
| 79 | { |
|---|
| 80 | CADSimulatorFixture.writeConfigData(); |
|---|
| 81 | CADSimulatorFixture.writedata("config.txt", configData); |
|---|
| 82 | System.out.println("CADSimulator constructor"); |
|---|
| 83 | System.setProperty("CAD_SIM_PROPERTIES", "config.txt"); |
|---|
| 84 | Window cadwindow = null; |
|---|
| 85 | if (System.getProperty("CAD_SIM_PROPERTIES") != null) |
|---|
| 86 | { |
|---|
| 87 | cadwindow = WindowInterceptor.run(new Trigger() |
|---|
| 88 | { |
|---|
| 89 | public void run() |
|---|
| 90 | { |
|---|
| 91 | try |
|---|
| 92 | { |
|---|
| 93 | app = new CADSimulator(System.getProperty("CAD_SIM_PROPERTIES")); |
|---|
| 94 | } catch (Exception e) |
|---|
| 95 | { |
|---|
| 96 | fail("Couldn't launch CADSimulator" + e.getMessage()); |
|---|
| 97 | } |
|---|
| 98 | } |
|---|
| 99 | }); |
|---|
| 100 | } else |
|---|
| 101 | { |
|---|
| 102 | fail("CAD_SIM_PROPERTIES system property not defined."); |
|---|
| 103 | } |
|---|
| 104 | assertEquals("CAD Simulator", cadwindow.getTitle()); |
|---|
| 105 | Panel mainPanel = cadwindow.getPanel("contentPane"); |
|---|
| 106 | TextBox txtStatus = mainPanel.getTextBox("simulationStatus"); |
|---|
| 107 | assertEquals("No Script", txtStatus.getText()); |
|---|
| 108 | TextBox terminals = mainPanel.getTextBox("termConnectedTF"); |
|---|
| 109 | assertEquals("0", terminals.getText().trim()); |
|---|
| 110 | assertEquals("No", mainPanel.getTextBox("managerConnectedTF").getText().trim()); |
|---|
| 111 | assertEquals("0:00:00", mainPanel.getTextBox("simulationClockLabel").getText()); |
|---|
| 112 | |
|---|
| 113 | CADClientInterface ci = new FakeClient(); |
|---|
| 114 | app.theCoordinator.registerForCallback(ci); |
|---|
| 115 | assertEquals("1", terminals.getText().trim()); |
|---|
| 116 | app.theCoordinator.registerForCallback(ci); |
|---|
| 117 | assertEquals("2", terminals.getText().trim()); |
|---|
| 118 | |
|---|
| 119 | SimulationManagerInterface si = mock(SimulationManagerInterface.class); |
|---|
| 120 | app.theCoordinator.registerForCallback(si); |
|---|
| 121 | assertEquals("Yes", mainPanel.getTextBox("managerConnectedTF").getText().trim()); |
|---|
| 122 | |
|---|
| 123 | Logger cadSimLogger = Logger.getLogger("tmcsim.cadsimulator"); |
|---|
| 124 | cadSimLogger.logp(Level.INFO, "", "", "Sample Info Message."); |
|---|
| 125 | |
|---|
| 126 | Panel infoPane = mainPanel.getPanel("infoMessagesPane"); |
|---|
| 127 | TextBox infoText = infoPane.getTextBox("infoMessagesTA"); |
|---|
| 128 | assertEquals(". = Sample Info Message.", infoText.getText().trim()); |
|---|
| 129 | |
|---|
| 130 | cadSimLogger.logp(Level.SEVERE, "", "", "Sample Error Message."); |
|---|
| 131 | |
|---|
| 132 | Panel errPane = mainPanel.getPanel("errorMessagesPane"); |
|---|
| 133 | TextBox errText = errPane.getTextBox("errorMessagesTA"); |
|---|
| 134 | assertEquals(". = Sample Error Message.", errText.getText().trim()); |
|---|
| 135 | |
|---|
| 136 | app.theCoordinator.setParamicsStatus(CADEnums.PARAMICS_STATUS.CONNECTED); |
|---|
| 137 | CADSimulatorFixture.pause(500); |
|---|
| 138 | assertEquals("Yes", mainPanel.getTextBox("paramicsConnectedTF").getText().trim()); |
|---|
| 139 | assertEquals("None", mainPanel.getTextBox("networkLoadedTF").getText().trim()); |
|---|
| 140 | |
|---|
| 141 | // app.theCoordinator.setScriptStatus(CADEnums.SCRIPT_STATUS.SCRIPT_RUNNING); |
|---|
| 142 | // assertEquals("Running", mainPanel.getTextBox("simulationStatus").getText().trim()); |
|---|
| 143 | |
|---|
| 144 | // Load a script file |
|---|
| 145 | String autoloadScriptname = "scripts/one-incident.xml"; |
|---|
| 146 | app.theCoordinator.loadScriptFile(new File(autoloadScriptname)); |
|---|
| 147 | // The status should now say Ready |
|---|
| 148 | assertEquals("Ready", mainPanel.getTextBox("simulationStatus").getText().trim()); |
|---|
| 149 | |
|---|
| 150 | app.theCoordinator.startSimulation(); |
|---|
| 151 | CADSimulatorFixture.pause(500); |
|---|
| 152 | assertEquals("Running", mainPanel.getTextBox("simulationStatus").getText().trim()); |
|---|
| 153 | assertEquals("0:00:01", mainPanel.getTextBox("simulationClockLabel").getText()); |
|---|
| 154 | |
|---|
| 155 | ParamicsSimulationManager psm = mock(ParamicsSimulationManager.class); |
|---|
| 156 | when(psm.isConnected()).thenReturn(Boolean.TRUE); |
|---|
| 157 | app.theParamicsSimMgr = psm; |
|---|
| 158 | app.theCoordinator.loadParamicsNetwork(1); |
|---|
| 159 | |
|---|
| 160 | // app.theViewer.dispose(); |
|---|
| 161 | // Window confirmPopup = null; |
|---|
| 162 | // confirmPopup = WindowInterceptor.run(new Trigger() |
|---|
| 163 | // { |
|---|
| 164 | // public void run() |
|---|
| 165 | // { |
|---|
| 166 | // app.theViewer.closeViewer(); |
|---|
| 167 | // } |
|---|
| 168 | // }); |
|---|
| 169 | // confirmPopup.getButton("OK").click(); |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | class FakeClient implements CADClientInterface |
|---|
| 173 | { |
|---|
| 174 | |
|---|
| 175 | @Override |
|---|
| 176 | public void refresh() throws RemoteException |
|---|
| 177 | { |
|---|
| 178 | } |
|---|
| 179 | } |
|---|
| 180 | } |
|---|