| 1 | package scriptbuilder.structures; |
|---|
| 2 | |
|---|
| 3 | import java.io.File; |
|---|
| 4 | import java.io.IOException; |
|---|
| 5 | import java.nio.file.Files; |
|---|
| 6 | import java.util.ArrayList; |
|---|
| 7 | import java.util.Observable; |
|---|
| 8 | import java.util.Observer; |
|---|
| 9 | import static junit.framework.Assert.assertFalse; |
|---|
| 10 | import static junit.framework.Assert.assertTrue; |
|---|
| 11 | import static junit.framework.Assert.fail; |
|---|
| 12 | import junit.framework.TestCase; |
|---|
| 13 | import org.apache.commons.io.FileUtils; |
|---|
| 14 | import scriptbuilder.structures.events.ATMSEvaluationEvent; |
|---|
| 15 | import scriptbuilder.structures.events.AudioEvent; |
|---|
| 16 | import scriptbuilder.structures.events.CHPRadioEvent; |
|---|
| 17 | import scriptbuilder.structures.events.TMTRadioEvent; |
|---|
| 18 | |
|---|
| 19 | /** |
|---|
| 20 | * |
|---|
| 21 | * @author Bryan McGuffin |
|---|
| 22 | */ |
|---|
| 23 | public class SimulationScriptTest extends TestCase implements Observer |
|---|
| 24 | { |
|---|
| 25 | |
|---|
| 26 | private boolean updateFlag = false; |
|---|
| 27 | |
|---|
| 28 | public SimulationScriptTest(String testName) |
|---|
| 29 | { |
|---|
| 30 | super(testName); |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | /** |
|---|
| 34 | * Test of update method, of class SimulationScript. |
|---|
| 35 | */ |
|---|
| 36 | public void testUpdate() |
|---|
| 37 | { |
|---|
| 38 | System.out.println("update"); |
|---|
| 39 | SimulationScript instance = new SimulationScript(); |
|---|
| 40 | |
|---|
| 41 | updateFlag = false; |
|---|
| 42 | assertFalse(updateFlag); |
|---|
| 43 | |
|---|
| 44 | instance.addObserver(this); |
|---|
| 45 | instance.update(); |
|---|
| 46 | assertTrue(updateFlag); |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | /** |
|---|
| 50 | * Test of broadcastEvent method, of class SimulationScript. |
|---|
| 51 | */ |
|---|
| 52 | public void testBroadcastEvent_ScriptIncidentSliceChangedEvent() |
|---|
| 53 | { |
|---|
| 54 | System.out.println("broadcastEvent"); |
|---|
| 55 | ScriptIncident.SliceChangedEvent ev = new ScriptIncident.SliceChangedEvent(null); |
|---|
| 56 | SimulationScript instance = new SimulationScript(); |
|---|
| 57 | |
|---|
| 58 | updateFlag = false; |
|---|
| 59 | assertFalse(updateFlag); |
|---|
| 60 | |
|---|
| 61 | instance.addObserver(this); |
|---|
| 62 | instance.broadcastEvent(ev); |
|---|
| 63 | assertTrue(updateFlag); |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | /** |
|---|
| 67 | * Test of broadcastEvent method, of class SimulationScript. |
|---|
| 68 | */ |
|---|
| 69 | public void testBroadcastEvent_ScriptIncidentIncidentFocusedEvent() |
|---|
| 70 | { |
|---|
| 71 | System.out.println("broadcastEvent"); |
|---|
| 72 | ScriptIncident.IncidentFocusedEvent ev = new ScriptIncident.IncidentFocusedEvent(null); |
|---|
| 73 | SimulationScript instance = new SimulationScript(); |
|---|
| 74 | |
|---|
| 75 | updateFlag = false; |
|---|
| 76 | assertFalse(updateFlag); |
|---|
| 77 | |
|---|
| 78 | instance.addObserver(this); |
|---|
| 79 | instance.broadcastEvent(ev); |
|---|
| 80 | assertTrue(updateFlag); |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | /** |
|---|
| 84 | * Test of loadScriptFromFile method, of class SimulationScript. |
|---|
| 85 | */ |
|---|
| 86 | public void testLoadScriptFromFile() |
|---|
| 87 | { |
|---|
| 88 | System.out.println("loadScriptFromFile"); |
|---|
| 89 | File inFile = new File("test/scriptbuilder/structures/test_input_file.xml"); |
|---|
| 90 | SimulationScript instance = new SimulationScript(); |
|---|
| 91 | assertEquals(0, instance.incidentCount()); |
|---|
| 92 | |
|---|
| 93 | instance.loadScriptFromFile(inFile); |
|---|
| 94 | assertEquals(6, instance.incidentCount()); |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | /** |
|---|
| 98 | * Test of saveScriptToFile method, of class SimulationScript. |
|---|
| 99 | */ |
|---|
| 100 | public void testSaveScriptToFile() |
|---|
| 101 | { |
|---|
| 102 | //This is basically identical to the integration test for saving and loading |
|---|
| 103 | System.out.println("saveScriptToFile"); |
|---|
| 104 | try |
|---|
| 105 | { |
|---|
| 106 | File inFile = new File("test/scriptbuilder/structures/test_input_file.xml"); |
|---|
| 107 | assertTrue(inFile.exists()); |
|---|
| 108 | |
|---|
| 109 | File outFile = new File("test/scriptbuilder/structures/test_output_file.xml"); |
|---|
| 110 | if (outFile.exists()) |
|---|
| 111 | { |
|---|
| 112 | outFile.delete(); |
|---|
| 113 | } |
|---|
| 114 | assertFalse(outFile.exists()); |
|---|
| 115 | |
|---|
| 116 | outFile.createNewFile(); |
|---|
| 117 | |
|---|
| 118 | assertTrue(outFile.exists()); |
|---|
| 119 | |
|---|
| 120 | SimulationScript script = new SimulationScript(); |
|---|
| 121 | |
|---|
| 122 | //Read in from test_input_file.xml |
|---|
| 123 | script.loadScriptFromFile(inFile); |
|---|
| 124 | |
|---|
| 125 | //Save to test_output_file.xml |
|---|
| 126 | script.saveScriptToFile(outFile); |
|---|
| 127 | |
|---|
| 128 | assertTrue(inFile.exists()); |
|---|
| 129 | assertTrue(outFile.exists()); |
|---|
| 130 | |
|---|
| 131 | //Check to see if the two files are identical |
|---|
| 132 | boolean result = FileUtils.contentEquals(inFile, outFile); |
|---|
| 133 | |
|---|
| 134 | assertTrue(result); |
|---|
| 135 | //If so, data model does in fact read in all data and write it back in the correct format |
|---|
| 136 | } |
|---|
| 137 | catch (IOException ex) |
|---|
| 138 | { |
|---|
| 139 | fail("There was an IO exception. Check file names and locations."); |
|---|
| 140 | } |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | /** |
|---|
| 144 | * Test of toXML method, of class SimulationScript. |
|---|
| 145 | */ |
|---|
| 146 | public void testToXML() |
|---|
| 147 | { |
|---|
| 148 | try |
|---|
| 149 | { |
|---|
| 150 | System.out.println("toXML"); |
|---|
| 151 | File inFile = new File("test/scriptbuilder/structures/test_input_file.xml"); |
|---|
| 152 | assertTrue(inFile.exists()); |
|---|
| 153 | SimulationScript instance = new SimulationScript(); |
|---|
| 154 | |
|---|
| 155 | instance.loadScriptFromFile(inFile); |
|---|
| 156 | |
|---|
| 157 | String expResult = ""; |
|---|
| 158 | |
|---|
| 159 | byte[] data = Files.readAllBytes(inFile.toPath()); |
|---|
| 160 | |
|---|
| 161 | expResult = new String(data); |
|---|
| 162 | String result = instance.toXML(); |
|---|
| 163 | assertEquals(expResult, result); |
|---|
| 164 | } |
|---|
| 165 | catch (IOException ex) |
|---|
| 166 | { |
|---|
| 167 | fail("There was an IO exception. Check file names and locations."); |
|---|
| 168 | } |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | /** |
|---|
| 172 | * Test of arrangeAllSlices method, of class SimulationScript. |
|---|
| 173 | */ |
|---|
| 174 | public void testAllSlices() |
|---|
| 175 | { |
|---|
| 176 | System.out.println("allSlices"); |
|---|
| 177 | SimulationScript instance = new SimulationScript(); |
|---|
| 178 | |
|---|
| 179 | instance.incidents.set(0, new ScriptIncident(121, null, null, instance)); |
|---|
| 180 | |
|---|
| 181 | instance.incidents.set(1, new ScriptIncident(101, null, null, instance)); |
|---|
| 182 | |
|---|
| 183 | ScriptEvent ev1_101 = new AudioEvent(); |
|---|
| 184 | |
|---|
| 185 | ScriptEvent ev2_101 = new CHPRadioEvent(); |
|---|
| 186 | |
|---|
| 187 | ScriptEvent ev1_121 = new TMTRadioEvent(); |
|---|
| 188 | |
|---|
| 189 | ScriptEvent ev2_121 = new ATMSEvaluationEvent(); |
|---|
| 190 | |
|---|
| 191 | instance.incidents.get(1).addNewEvent(ev1_101, 25);//Fourth |
|---|
| 192 | instance.incidents.get(1).addNewEvent(ev2_101, 0);//Earliest; higher index. Second |
|---|
| 193 | |
|---|
| 194 | instance.incidents.get(0).addNewEvent(ev1_121, 0);//Earliest; lowest index. First |
|---|
| 195 | instance.incidents.get(0).addNewEvent(ev2_121, 10);//Third |
|---|
| 196 | |
|---|
| 197 | ArrayList<TimeSlice> result = instance.arrangeAllSlices(); |
|---|
| 198 | assertEquals(4, result.size()); |
|---|
| 199 | assertEquals(ev1_121, result.get(0).events.get(0)); |
|---|
| 200 | assertEquals(ev2_101, result.get(1).events.get(0)); |
|---|
| 201 | assertEquals(ev2_121, result.get(2).events.get(0)); |
|---|
| 202 | assertEquals(ev1_101, result.get(3).events.get(0)); |
|---|
| 203 | |
|---|
| 204 | } |
|---|
| 205 | |
|---|
| 206 | /** |
|---|
| 207 | * Test of absoluteLength method, of class SimulationScript. |
|---|
| 208 | */ |
|---|
| 209 | public void testAbsoluteLength() |
|---|
| 210 | { |
|---|
| 211 | System.out.println("absoluteLength"); |
|---|
| 212 | SimulationScript instance = new SimulationScript(); |
|---|
| 213 | int result = instance.absoluteLength(); |
|---|
| 214 | assertEquals(0, result); |
|---|
| 215 | |
|---|
| 216 | instance.incidents.set(0, new ScriptIncident(101, null, null, instance)); |
|---|
| 217 | instance.incidents.get(0).updateLength(); |
|---|
| 218 | assertEquals(0, instance.incidents.get(0).offset); |
|---|
| 219 | assertEquals(0, instance.incidents.get(0).length); |
|---|
| 220 | result = instance.absoluteLength(); |
|---|
| 221 | assertEquals(0, result); |
|---|
| 222 | |
|---|
| 223 | instance.incidents.get(0).addNewEvent(new AudioEvent(), 0); |
|---|
| 224 | instance.incidents.get(0).updateLength(); |
|---|
| 225 | assertEquals(0, instance.incidents.get(0).offset); |
|---|
| 226 | assertEquals(1, instance.incidents.get(0).length); |
|---|
| 227 | result = instance.absoluteLength(); |
|---|
| 228 | assertEquals(1, result); |
|---|
| 229 | |
|---|
| 230 | instance.incidents.get(0).setOffset(10); |
|---|
| 231 | instance.incidents.get(0).updateLength(); |
|---|
| 232 | assertEquals(10, instance.incidents.get(0).offset); |
|---|
| 233 | assertEquals(1, instance.incidents.get(0).length); |
|---|
| 234 | result = instance.absoluteLength(); |
|---|
| 235 | assertEquals(11, result); |
|---|
| 236 | |
|---|
| 237 | ((ScriptEvent) instance.incidents.get(0).slices.get(10).events.get(0)).length = 45; |
|---|
| 238 | instance.incidents.get(0).updateLength(); |
|---|
| 239 | assertEquals(10, instance.incidents.get(0).offset); |
|---|
| 240 | assertEquals(45, instance.incidents.get(0).length); |
|---|
| 241 | result = instance.absoluteLength(); |
|---|
| 242 | assertEquals(55, result); |
|---|
| 243 | } |
|---|
| 244 | |
|---|
| 245 | @Override |
|---|
| 246 | public void update(Observable o, Object arg) |
|---|
| 247 | { |
|---|
| 248 | updateFlag = true; |
|---|
| 249 | } |
|---|
| 250 | |
|---|
| 251 | /** |
|---|
| 252 | * Test of incidentCount method, of class SimulationScript. |
|---|
| 253 | */ |
|---|
| 254 | public void testIncidentCount() |
|---|
| 255 | { |
|---|
| 256 | System.out.println("incidentCount"); |
|---|
| 257 | SimulationScript instance = new SimulationScript(); |
|---|
| 258 | int result = instance.incidentCount(); |
|---|
| 259 | assertEquals(0, result); |
|---|
| 260 | instance.incidents.set(0, new ScriptIncident(101, null, null, instance)); |
|---|
| 261 | result = instance.incidentCount(); |
|---|
| 262 | assertEquals(1, result); |
|---|
| 263 | |
|---|
| 264 | } |
|---|
| 265 | |
|---|
| 266 | } |
|---|