001    package org.hackystat.sensor.xmldata.option;
002    
003    import java.util.ArrayList;
004    import java.util.Date;
005    
006    import javax.xml.datatype.XMLGregorianCalendar;
007    
008    import org.hackystat.sensor.xmldata.XmlDataController;
009    import org.hackystat.sensorshell.MultiSensorShell;
010    import org.hackystat.sensorshell.SensorShellProperties;
011    import org.hackystat.sensorshell.SensorShell;
012    import org.hackystat.sensorshell.Shell;
013    import org.hackystat.utilities.tstamp.Tstamp;
014    import org.hackystat.utilities.tstamp.TstampSet;
015    import org.junit.Assert;
016    import org.junit.Test;
017    
018    /**
019     * Tests if the option utility helper methods return the correct values.
020     * @author aito
021     * 
022     */
023    public class OptionUtilTest {
024      /**
025       * Tests if the correct long value in milliseconds is returned based on the
026       * timestamp.
027       */
028      @Test
029      public void testGetTimestampInMillis() {
030        // Test returning a timestamp long that is a string.
031        try {
032          long timestamp = new Date().getTime();
033          Assert.assertTrue("The returned timestamp long is wrong.", timestamp == OptionUtil
034              .getTimestampInMillis(String.valueOf(timestamp)));
035        }
036        catch (Exception e) {
037          Assert.fail("Failed to get the timestamp in milliseconds.");
038        }
039    
040        // Tests returning a timestamp long that is in the SimpleDateFormat:
041        // MM/dd/yyyy-hh:mm:ss
042        try {
043          String timestamp = "07/07/1977-07:07:07";
044          Assert.assertTrue("The returned timestamp long is wrong.", 237143227000L == OptionUtil
045              .getTimestampInMillis(timestamp));
046        }
047        catch (Exception e) {
048          Assert.fail("Failed to get the timestamp in milliseconds.");
049        }
050    
051        // Tests sending an invalid SimpleDateFormat.
052        try {
053          String timestamp = "07/07/1977-07:07";
054          OptionUtil.getTimestampInMillis(timestamp);
055          Assert.fail("The simple date format is incorrect and should thrown an exception.");
056        }
057        catch (Exception e) {
058          System.out.println("An invalid simple data format threw an exception.");
059        }
060      }
061    
062      /** Tests if the correct current timestamp is returned. */
063      @Test
064      public void testGetCurrentTimestamp() {
065        // Next, let's test if unique current timestamps are returned.
066        TstampSet tstampSet = new TstampSet();
067        XMLGregorianCalendar timestamp1 = OptionUtil.getCurrentTimestamp(true, tstampSet);
068        XMLGregorianCalendar timestamp2 = OptionUtil.getCurrentTimestamp(true, tstampSet);
069        Assert.assertNotSame("The two timestamps are the same.", timestamp1, timestamp2);
070      }
071    
072      /** Tests if the specified timestamp is massaged to valid values correctly. */
073      @Test
074      public void testMassageTimestamp() {
075        // Tests if the correct, non-unique, XmlGregorianCalendar is returned.
076        long timestamp = new Date().getTime();
077        Assert.assertEquals("The returned calendar instance is incorrect.", Tstamp
078            .makeTimestamp(timestamp), OptionUtil.massageTimestamp(false, new TstampSet(),
079            timestamp));
080    
081        // Tests if unique XmlGregorianCalendars are returned.
082        TstampSet tstampSet = new TstampSet();
083        Assert.assertNotSame("The returned calendar instance is incorrect.", tstampSet
084            .getUniqueTstamp(timestamp), OptionUtil.massageTimestamp(true, tstampSet, timestamp));
085      }
086    
087      /** Tests if the correct sensorshell instance is returned. */
088      @Test
089      public void testCreateShell() {
090        try {
091          // First, create the controller used to determine which shell to use.
092          XmlDataController controller = new XmlDataController();
093          Option option = OptionFactory.getInstance(controller, MultiShellOption.OPTION_NAME,
094              new ArrayList<String>());
095    
096          // Tests if a normal SensorShell is used.
097          Shell shell = OptionUtil.createShell(new SensorShellProperties(), controller);
098          Assert.assertTrue("The returned shell is not a SensorShell instance.",
099              shell instanceof SensorShell);
100    
101          // Tests if a MultiSensorShell is used when the option is set.
102          option.process();
103          shell = OptionUtil.createShell(new SensorShellProperties(), controller);
104          Assert.assertTrue("The returned shell is not a MultiSensorShell instance.",
105              shell instanceof MultiSensorShell);
106        }
107        catch (Exception e) {
108          e.printStackTrace();
109          Assert.fail("Failed to create a Shell instance.");
110        }
111      }
112    }