001    package org.hackystat.dailyprojectdata.resource.commit;
002    
003    import static org.junit.Assert.assertEquals;
004    import static org.junit.Assert.assertFalse;
005    import static org.junit.Assert.assertTrue;
006    import static org.junit.Assert.fail;
007    
008    import java.util.Date;
009    
010    import javax.xml.datatype.XMLGregorianCalendar;
011    
012    import org.hackystat.dailyprojectdata.resource.coverage.CoverageData;
013    import org.hackystat.sensorbase.resource.sensordata.jaxb.Properties;
014    import org.hackystat.sensorbase.resource.sensordata.jaxb.Property;
015    import org.hackystat.sensorbase.resource.sensordata.jaxb.SensorData;
016    import org.hackystat.utilities.tstamp.Tstamp;
017    import org.junit.Before;
018    import org.junit.Test;
019    
020    /**
021     * Tests if the CoverageData wraps a SensorData instance as intended.
022     * @author aito
023     * 
024     */
025    public class TestCommitData {
026      /** The fields that are tested in this test class. */
027      private CommitData commitData = null;
028      private SensorData sensorData = null;
029    
030      /** Setup this test class. */
031      @Before
032      public void setUp() {
033        XMLGregorianCalendar runtime = Tstamp.makeTimestamp(new Date().getTime());
034        this.sensorData = createData(runtime.toString(), runtime.toString(), "austen@hawaii.edu",
035            "C:\\foo.java", "4", "10");
036        this.commitData = new CommitData(this.sensorData);
037      }
038    
039      /** Tests if the commit data returns the correct amount of lines added. */
040      @Test
041      public void testGetLinesAdded() {
042        assertEquals("The amount of lines added is incorrect.", 4, this.commitData
043            .getLinesAdded());
044      }
045    
046      /** Tests if the commit data returns the correct amount of lines deleted. */
047      @Test
048      public void testGetLinesDeleted() {
049        assertEquals("The amount of lines deleted is incorrect.", 10, this.commitData
050            .getLinesDeleted());
051      }
052    
053    
054      /** Tests the overridden .equals method returns the correct values. */
055      @Test
056      public void testEquals() {
057        // First, test equal instances.
058        CommitData newCoverageData = new CommitData(this.sensorData);
059        assertTrue("Instances with the same SensorData are not equal.", this.commitData
060            .equals(newCoverageData));
061    
062        // Then, test if the same instance returns true.
063        assertTrue("The same instances are equal.", this.commitData.equals(this.commitData));
064    
065        // Next, test instances with different SensorData objects.
066        XMLGregorianCalendar runtime = Tstamp.makeTimestamp(new Date().getTime() + 10);
067        SensorData sensorData = createData(runtime.toString(), runtime.toString(),
068            "austen@hawaii.edu", "C:\\foo.java", "1", "2");
069        assertFalse("Instances with the differnt SensorData are not equal.", this.commitData
070            .equals(new CoverageData(sensorData)));
071    
072        // Finally, test if different object types are not equal.
073        assertFalse("Instances with the different SensorData are not equal.", this.commitData
074            .equals("Foo String"));
075      }
076    
077      /**
078       * A helper method used to create the SensorData instances used to by this
079       * test class.
080       * @param timestamp the timestamp of the created sensor data instance.
081       * @param runtime the runtime of the SensorData instance.
082       * @param owner the specified owner.
083       * @param resource the specified resource.
084       * @param linesAdded the total lines added in returned data instance.
085       * @param linesDeleted the total lines deleted in returned data instance.
086       * @return the populated SensorData instance.
087       */
088      public static SensorData createData(String timestamp, String runtime, String owner,
089          String resource, String linesAdded, String linesDeleted) {
090        try {
091          SensorData data = new SensorData();
092          data.setOwner(owner);
093          data.setTimestamp(Tstamp.makeTimestamp(timestamp));
094          data.setRuntime(Tstamp.makeTimestamp(runtime));
095          data.setSensorDataType("Commit");
096          data.setTool("Subversion");
097          data.setResource(resource);
098    
099          // Sets the lines added property.
100          Properties props = new Properties();
101          Property linesAddedProperty = new Property();
102          linesAddedProperty.setKey("linesAdded");
103          linesAddedProperty.setValue(linesAdded);
104          props.getProperty().add(linesAddedProperty);
105    
106          // Sets the lines removed property.
107          Property linesDeletedProperty = new Property();
108          linesDeletedProperty.setKey("linesDeleted");
109          linesDeletedProperty.setValue(linesDeleted);
110          props.getProperty().add(linesDeletedProperty);
111    
112          data.setProperties(props);
113          return data;
114        }
115        catch (Exception e) {
116          fail("Failed to create test data. " + e.getMessage());
117        }
118        return null;
119      }
120    }