001    package org.hackystat.dailyprojectdata.resource.coverage;
002    
003    import static org.junit.Assert.assertEquals;
004    import static org.junit.Assert.assertFalse;
005    import static org.junit.Assert.assertNull;
006    import static org.junit.Assert.assertTrue;
007    
008    import java.util.Date;
009    
010    import javax.xml.datatype.XMLGregorianCalendar;
011    
012    import org.hackystat.sensorbase.resource.sensordata.jaxb.Properties;
013    import org.hackystat.sensorbase.resource.sensordata.jaxb.Property;
014    import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorData;
015    import org.hackystat.utilities.tstamp.Tstamp;
016    import org.junit.Before;
017    import org.junit.Test;
018    
019    /**
020     * Tests if the CoverageData wraps a SensorData instance as intended.
021     * 
022     * @author aito
023     * 
024     */
025    public class TestCoverageData {
026      /** The fields that are tested in this test class. */
027      private CoverageData coverageData = null;
028      private SensorData sensorData = null;
029      private XMLGregorianCalendar runtime = null;
030    
031      /**
032       * Setup this test class.
033       * 
034       * @throws Exception If problems occur.
035       */
036      @Before
037      public void setUp() throws Exception {
038        this.runtime = Tstamp.makeTimestamp(new Date().getTime());
039        this.sensorData = createData(this.runtime.toString(), this.runtime.toString(),
040            "austen@hawaii.edu", "C:\\foo.java");
041        this.coverageData = new CoverageData(this.sensorData);
042      }
043    
044      /** Tests if the correct runtime is returned. */
045      @Test
046      public void testGetRuntime() {
047        assertEquals("Returned runtime is incorrect.", this.runtime, this.coverageData.getRuntime());
048      }
049    
050      /** Tests if the correct amount of uncovered coverage entities is returned. */
051      @Test
052      public void testGetUncovered() {
053        assertEquals("The amount of 'line' uncovered data is incorrect.", 1, this.coverageData
054            .getUncovered(CoverageData.GRANULARITY_LINE));
055        assertEquals("The amount of 'method' uncovered data is incorrect.", 2, this.coverageData
056            .getUncovered(CoverageData.GRANULARITY_METHOD));
057        assertEquals("The amount of 'class' uncovered data is incorrect.", 3, this.coverageData
058            .getUncovered(CoverageData.GRANULARITY_CLASS));
059        assertEquals("The amount of 'block' uncovered data is incorrect.", 4, this.coverageData
060            .getUncovered(CoverageData.GRANULARITY_BLOCK));
061      }
062    
063      /** Tests if the correct amount of uncovered coverage entities is returned. */
064      @Test
065      public void testGetCovered() {
066        assertEquals("The amount of 'line' covered data is incorrect.", 5, this.coverageData
067            .getCovered(CoverageData.GRANULARITY_LINE));
068        assertEquals("The amount of 'method' covered data is incorrect.", 6, this.coverageData
069            .getCovered(CoverageData.GRANULARITY_METHOD));
070        assertEquals("The amount of 'class' covered data is incorrect.", 7, this.coverageData
071            .getCovered(CoverageData.GRANULARITY_CLASS));
072        assertEquals("The amount of 'block' covered data is incorrect.", 8, this.coverageData
073            .getCovered(CoverageData.GRANULARITY_BLOCK));
074      }
075    
076      /**
077       * Tests if the resource returned from the wrapper class is the same as the resource in the
078       * SensorData instance.
079       */
080      @Test
081      public void testGetResource() {
082        assertEquals("The returned resource is incorrect.", this.sensorData.getResource(),
083            this.coverageData.getResource());
084      }
085    
086      /**
087       * Tests if the correct Property is returned or that null is returned if the property does not
088       * exist.
089       */
090      @Test
091      public void testGetCoverageProperty() {
092        // First, let's test an existing property.
093        String propertyName = CoverageData.GRANULARITY_LINE + "_Covered";
094        Property coveredProperty = this.coverageData.getCoverageProperty(propertyName);
095        assertEquals("The Covered Property Name is incorrect.", propertyName, coveredProperty.getKey());
096        assertEquals("The Covered Property Value is incorrect.", "5", coveredProperty.getValue());
097    
098        // Next, let's test if a non-existent property returns null.
099        assertNull("Null was not returned for a non-existent property.", this.coverageData
100            .getCoverageProperty("Foo Property"));
101      }
102    
103      /**
104       * Tests the overridden .equals method returns the correct values.
105       * 
106       * @throws Exception if problems occur
107       */
108      @Test
109      public void testEquals() throws Exception {
110        // First, test equal instances.
111        CoverageData newCoverageData = new CoverageData(this.sensorData);
112        assertTrue("Instances with the same SensorData are equal.", this.coverageData
113            .equals(newCoverageData));
114    
115        // Then, test if the same instance returns true.
116        assertTrue("The same instances are equal.", this.coverageData.equals(this.coverageData));
117    
118        // Next, test instances with different SensorData objects.
119        XMLGregorianCalendar runtime = Tstamp.makeTimestamp(new Date().getTime() + 10);
120        SensorData sensorData = createData(runtime.toString(), runtime.toString(), "austen@hawaii.edu",
121            "C:\\foo.java");
122        assertFalse("Instances with the differnt SensorData are not equal.", this.coverageData
123            .equals(new CoverageData(sensorData)));
124    
125        // Finally, test if different object types are not equal.
126        assertFalse("Instances with the different SensorData are not equal.", this.coverageData
127            .equals("Foo String"));
128      }
129    
130      /**
131       * Tests if the correct coverage data is returned regardless of the granularity's case.
132       */
133      @Test
134      public void testGranularityCaseInsensitivity() {
135        // Tests the covered data case insensitivity.
136        assertEquals("Incorrect number of covered lines using 'Line'.", 5, this.coverageData
137            .getCovered("Line"));
138        assertEquals("Incorrect number of covered lines using 'LINE'.", 5, this.coverageData
139            .getCovered("LINE"));
140        assertEquals("Incorrect number of covered lines using 'lINE'.", 5, this.coverageData
141            .getCovered("lINE"));
142        assertEquals("Incorrect number of covered lines using 'line'.", 5, this.coverageData
143            .getCovered("line"));
144    
145        // Tests the uncovered data case insensitivity.
146        assertEquals("Incorrect number of uncovered lines using 'Line'.", 1, this.coverageData
147            .getUncovered("Line"));
148        assertEquals("Incorrect number of uncovered lines using 'LINE'.", 1, this.coverageData
149            .getUncovered("LINE"));
150        assertEquals("Incorrect number of uncovered lines using 'lINE'.", 1, this.coverageData
151            .getUncovered("lINE"));
152        assertEquals("Incorrect number of uncovered lines using 'line'.", 1, this.coverageData
153            .getUncovered("line"));
154    
155      }
156    
157      /**
158       * A helper method used to create the SensorData instances used to by this test class.
159       * 
160       * @param timestamp the timestamp of the created sensor data instance.
161       * @param runtime the runtime of the SensorData instance.
162       * @param owner the specified owner.
163       * @param resource the specified resource.
164       * @return the populated SensorData instance.
165       * @throws Exception if problems occur.
166       */
167      public static SensorData createData(String timestamp, String runtime, String owner,
168          String resource) throws Exception {
169        SensorData data = new SensorData();
170        data.setOwner(owner);
171        data.setTimestamp(Tstamp.makeTimestamp(timestamp));
172        data.setRuntime(Tstamp.makeTimestamp(runtime));
173        data.setSensorDataType("Coverage");
174        data.setTool("Emma");
175        data.setResource(resource);
176    
177        // Sets the uncovered values.
178        Properties props = new Properties();
179        Property lineUncoveredProperty = new Property();
180        lineUncoveredProperty.setKey("line_Uncovered");
181        lineUncoveredProperty.setValue("1");
182        props.getProperty().add(lineUncoveredProperty);
183    
184        Property methodUncoveredProperty = new Property();
185        methodUncoveredProperty.setKey("method_Uncovered");
186        methodUncoveredProperty.setValue("2");
187        props.getProperty().add(methodUncoveredProperty);
188    
189        Property classUncoveredProperty = new Property();
190        classUncoveredProperty.setKey("class_Uncovered");
191        classUncoveredProperty.setValue("3");
192        props.getProperty().add(classUncoveredProperty);
193    
194        Property blockUncoveredProperty = new Property();
195        blockUncoveredProperty.setKey("block_Uncovered");
196        blockUncoveredProperty.setValue("4");
197        props.getProperty().add(blockUncoveredProperty);
198    
199        // Sets the covered values.
200        Property lineCoveredProperty = new Property();
201        lineCoveredProperty.setKey("line_Covered");
202        lineCoveredProperty.setValue("5");
203        props.getProperty().add(lineCoveredProperty);
204    
205        Property methodCoveredProperty = new Property();
206        methodCoveredProperty.setKey("method_Covered");
207        methodCoveredProperty.setValue("6");
208        props.getProperty().add(methodCoveredProperty);
209    
210        Property classCoveredProperty = new Property();
211        classCoveredProperty.setKey("class_Covered");
212        classCoveredProperty.setValue("7");
213        props.getProperty().add(classCoveredProperty);
214    
215        Property blockCoveredProperty = new Property();
216        blockCoveredProperty.setKey("block_Covered");
217        blockCoveredProperty.setValue("8");
218        props.getProperty().add(blockCoveredProperty);
219    
220        data.setProperties(props);
221        return data;
222      }
223    }