Changeset 60 in tmcsimulator-scriptbuilder for trunk/test/scriptbuilder/structures


Ignore:
Timestamp:
08/14/2017 03:44:12 PM (9 years ago)
Author:
bmcguffin
Message:

Filled out test cases for SimulationScript?.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/scriptbuilder/structures/SimulationScriptTest.java

    r56 r60  
    55import java.nio.file.Files; 
    66import java.util.ArrayList; 
     7import java.util.Observable; 
     8import java.util.Observer; 
    79import java.util.logging.Level; 
    810import java.util.logging.Logger; 
     11import static junit.framework.Assert.assertFalse; 
     12import static junit.framework.Assert.assertTrue; 
     13import static junit.framework.Assert.fail; 
    914import junit.framework.TestCase; 
     15import org.apache.commons.io.FileUtils; 
     16import scriptbuilder.structures.events.ATMSEvaluationEvent; 
     17import scriptbuilder.structures.events.AudioEvent; 
     18import scriptbuilder.structures.events.CHPRadioEvent; 
     19import scriptbuilder.structures.events.TMTRadioEvent; 
    1020 
    1121/** 
     
    1323 * @author Bryan McGuffin 
    1424 */ 
    15 public class SimulationScriptTest extends TestCase 
     25public class SimulationScriptTest extends TestCase implements Observer 
    1626{ 
    1727 
     28    private boolean updateFlag = false; 
     29 
    1830    public SimulationScriptTest(String testName) 
    1931    { 
     
    2840        System.out.println("update"); 
    2941        SimulationScript instance = new SimulationScript(); 
     42 
     43        updateFlag = false; 
     44        assertFalse(updateFlag); 
     45 
     46        instance.addObserver(this); 
    3047        instance.update(); 
    31         // TODO review the generated test code and remove the default call to fail. 
    32         fail("The test case is a prototype."); 
     48        assertTrue(updateFlag); 
    3349    } 
    3450 
     
    3955    { 
    4056        System.out.println("broadcastEvent"); 
    41         ScriptIncident.SliceChangedEvent e = null; 
    42         SimulationScript instance = new SimulationScript(); 
    43         instance.broadcastEvent(e); 
    44         // TODO review the generated test code and remove the default call to fail. 
    45         fail("The test case is a prototype."); 
     57        ScriptIncident.SliceChangedEvent ev = new ScriptIncident.SliceChangedEvent(null); 
     58        SimulationScript instance = new SimulationScript(); 
     59 
     60        updateFlag = false; 
     61        assertFalse(updateFlag); 
     62 
     63        instance.addObserver(this); 
     64        instance.broadcastEvent(ev); 
     65        assertTrue(updateFlag); 
    4666    } 
    4767 
     
    5272    { 
    5373        System.out.println("broadcastEvent"); 
    54         ScriptIncident.IncidentFocusedEvent e = null; 
    55         SimulationScript instance = new SimulationScript(); 
    56         instance.broadcastEvent(e); 
    57         // TODO review the generated test code and remove the default call to fail. 
    58         fail("The test case is a prototype."); 
     74        ScriptIncident.IncidentFocusedEvent ev = new ScriptIncident.IncidentFocusedEvent(null); 
     75        SimulationScript instance = new SimulationScript(); 
     76 
     77        updateFlag = false; 
     78        assertFalse(updateFlag); 
     79 
     80        instance.addObserver(this); 
     81        instance.broadcastEvent(ev); 
     82        assertTrue(updateFlag); 
    5983    } 
    6084 
     
    6589    { 
    6690        System.out.println("loadScriptFromFile"); 
    67         File f = null; 
    68         SimulationScript instance = new SimulationScript(); 
    69         instance.loadScriptFromFile(f); 
    70         // TODO review the generated test code and remove the default call to fail. 
    71         fail("The test case is a prototype."); 
     91        File inFile = new File("test/scriptbuilder/structures/test_input_file.xml"); 
     92        SimulationScript instance = new SimulationScript(); 
     93        assertEquals(0, instance.incidentCount()); 
     94 
     95        instance.loadScriptFromFile(inFile); 
     96        assertEquals(6, instance.incidentCount()); 
    7297    } 
    7398 
     
    77102    public void testSaveScriptToFile() 
    78103    { 
     104        //This is basically identical to the integration test for saving and loading 
    79105        System.out.println("saveScriptToFile"); 
    80         File f = null; 
    81         SimulationScript instance = new SimulationScript(); 
    82         instance.saveScriptToFile(f); 
    83         // TODO review the generated test code and remove the default call to fail. 
    84         fail("The test case is a prototype."); 
     106        try 
     107        { 
     108            File inFile = new File("test/scriptbuilder/structures/test_input_file.xml"); 
     109            assertTrue(inFile.exists()); 
     110 
     111            File outFile = new File("test/scriptbuilder/structures/test_output_file.xml"); 
     112            if (outFile.exists()) 
     113            { 
     114                outFile.delete(); 
     115            } 
     116            assertFalse(outFile.exists()); 
     117 
     118            outFile.createNewFile(); 
     119 
     120            assertTrue(outFile.exists()); 
     121 
     122            SimulationScript script = new SimulationScript(); 
     123 
     124            //Read in from test_input_file.xml 
     125            script.loadScriptFromFile(inFile); 
     126 
     127            //Save to test_output_file.xml 
     128            script.saveScriptToFile(outFile); 
     129 
     130            assertTrue(inFile.exists()); 
     131            assertTrue(outFile.exists()); 
     132 
     133            //Check to see if the two files are identical 
     134            boolean result = FileUtils.contentEquals(inFile, outFile); 
     135 
     136            assertTrue(result); 
     137            //If so, data model does in fact read in all data and write it back in the correct format 
     138        } 
     139        catch (IOException ex) 
     140        { 
     141            fail("There was an IO exception. Check file names and locations."); 
     142        } 
    85143    } 
    86144 
     
    109167        catch (IOException ex) 
    110168        { 
    111             Logger.getLogger(SimulationScriptTest.class.getName()).log(Level.SEVERE, null, ex); 
     169            fail("There was an IO exception. Check file names and locations."); 
    112170        } 
    113171    } 
     
    120178        System.out.println("allSlices"); 
    121179        SimulationScript instance = new SimulationScript(); 
    122         ArrayList<TimeSlice> expResult = null; 
     180 
     181        instance.incidents.set(0, new ScriptIncident(121, null, null, instance)); 
     182 
     183        instance.incidents.set(1, new ScriptIncident(101, null, null, instance)); 
     184 
     185        ScriptEvent ev1_101 = new AudioEvent(); 
     186 
     187        ScriptEvent ev2_101 = new CHPRadioEvent(); 
     188 
     189        ScriptEvent ev1_121 = new TMTRadioEvent(); 
     190 
     191        ScriptEvent ev2_121 = new ATMSEvaluationEvent(); 
     192 
     193        instance.incidents.get(1).addNewEvent(ev1_101, 25);//Fourth 
     194        instance.incidents.get(1).addNewEvent(ev2_101, 0);//Earliest; higher index. Second 
     195 
     196        instance.incidents.get(0).addNewEvent(ev1_121, 0);//Earliest; lowest index. First 
     197        instance.incidents.get(0).addNewEvent(ev2_121, 10);//Third 
     198 
    123199        ArrayList<TimeSlice> result = instance.allSlices(); 
    124         assertEquals(expResult, result); 
    125         // TODO review the generated test code and remove the default call to fail. 
    126         fail("The test case is a prototype."); 
     200        assertEquals(4, result.size()); 
     201        assertEquals(ev1_121, result.get(0).events.get(0)); 
     202        assertEquals(ev2_101, result.get(1).events.get(0)); 
     203        assertEquals(ev2_121, result.get(2).events.get(0)); 
     204        assertEquals(ev1_101, result.get(3).events.get(0)); 
     205 
    127206    } 
    128207 
     
    134213        System.out.println("absoluteLength"); 
    135214        SimulationScript instance = new SimulationScript(); 
    136         int expResult = 0; 
    137215        int result = instance.absoluteLength(); 
    138         assertEquals(expResult, result); 
    139         // TODO review the generated test code and remove the default call to fail. 
    140         fail("The test case is a prototype."); 
     216        assertEquals(0, result); 
     217 
     218        instance.incidents.set(0, new ScriptIncident(101, null, null, instance)); 
     219        instance.incidents.get(0).updateLength(); 
     220        assertEquals(0, instance.incidents.get(0).offset); 
     221        assertEquals(0, instance.incidents.get(0).length); 
     222        result = instance.absoluteLength(); 
     223        assertEquals(0, result); 
     224 
     225        instance.incidents.get(0).addNewEvent(new AudioEvent(), 0); 
     226        instance.incidents.get(0).updateLength(); 
     227        assertEquals(0, instance.incidents.get(0).offset); 
     228        assertEquals(1, instance.incidents.get(0).length); 
     229        result = instance.absoluteLength(); 
     230        assertEquals(1, result); 
     231 
     232        instance.incidents.get(0).setOffset(10); 
     233        instance.incidents.get(0).updateLength(); 
     234        assertEquals(10, instance.incidents.get(0).offset); 
     235        assertEquals(1, instance.incidents.get(0).length); 
     236        result = instance.absoluteLength(); 
     237        assertEquals(11, result); 
     238 
     239        ((ScriptEvent) instance.incidents.get(0).slices.get(0).events.get(0)).length = 45; 
     240        instance.incidents.get(0).updateLength(); 
     241        assertEquals(10, instance.incidents.get(0).offset); 
     242        assertEquals(45, instance.incidents.get(0).length); 
     243        result = instance.absoluteLength(); 
     244        assertEquals(55, result); 
     245    } 
     246 
     247    @Override 
     248    public void update(Observable o, Object arg) 
     249    { 
     250        updateFlag = true; 
     251    } 
     252 
     253    /** 
     254     * Test of incidentCount method, of class SimulationScript. 
     255     */ 
     256    public void testIncidentCount() 
     257    { 
     258        System.out.println("incidentCount"); 
     259        SimulationScript instance = new SimulationScript(); 
     260        int result = instance.incidentCount(); 
     261        assertEquals(0, result); 
     262        instance.incidents.set(0, new ScriptIncident(101, null, null, instance)); 
     263        result = instance.incidentCount(); 
     264        assertEquals(1, result); 
     265 
    141266    } 
    142267 
Note: See TracChangeset for help on using the changeset viewer.