001    package org.hackystat.sensor.xmldata.option;
002    
003    import java.util.ArrayList;
004    import java.util.List;
005    
006    import org.hackystat.sensor.xmldata.XmlDataController;
007    import org.junit.Assert;
008    import org.junit.Before;
009    import org.junit.Test;
010    
011    /**
012     * Tests if the option handler stores, validates, and executes options as
013     * intended.
014     * @author aito
015     * 
016     */
017    public class TestOptionHandler {
018      /** The handler class that is tested. */
019      private OptionHandler handler = null;
020      /** The controller instance. */
021      private XmlDataController controller = null;
022    
023      /** Sets up each test case. */
024      @Before
025      public void setUp() {
026        this.controller = new XmlDataController();
027        this.handler = new OptionHandler(this.controller);
028      }
029    
030      /** Tests if the options are validated correctly. */
031      @Test
032      public void testValidateOptions() {
033        // Tests if a valid sdt option is validated.
034        List<String> parameters = new ArrayList<String>();
035        parameters.add("DevEvent");
036        Option sdtOption = new SdtOption(controller, parameters);
037        this.handler.addOption(sdtOption);
038        Assert.assertTrue("The sdt option was not validated.", this.handler.isOptionsValid());
039    
040        // Tests if a duplicate option is validated.
041        this.handler.addOption(sdtOption);
042        Assert.assertFalse("A duplicate sdt option was validated.", this.handler.isOptionsValid());
043    
044        // Tests if an invalid file option is validated.
045        parameters = new ArrayList<String>();
046        parameters.add("FooPath");
047        Option fileOption = new FileOption(controller, parameters);
048        this.handler.addOption(fileOption);
049        Assert.assertFalse("The file option was validated.", this.handler.isOptionsValid());
050      }
051    
052      /** Tests if all of the required options have been added. */
053      @Test
054      public void testHasRequiredOptions() {
055        // First, test if no options fails.
056        Assert.assertFalse("No options returned true.", this.handler.hasRequiredOptions());
057    
058        // Then, test if only the sdt options fails.
059        Option sdtOption = new SdtOption(controller, new ArrayList<String>());
060        this.handler.addOption(sdtOption);
061        Assert.assertFalse("Only an sdt option returned true.", this.handler.hasRequiredOptions());
062    
063        // Finally, test if all of the required attributes passes.
064        Option fileOption = new FileOption(controller, new ArrayList<String>());
065        this.handler.addOption(fileOption);
066        Assert.assertTrue("All the correct options returned false.", this.handler
067            .hasRequiredOptions());
068      }
069    
070      /**
071       * Tests if the specified option strings return true if they are indeed an
072       * option rather than a parameter.
073       */
074      @Test
075      public void testIsOption() {
076        Assert.assertTrue("-sdt failed the test to be an option.", this.handler.isOption("-sdt"));
077        Assert.assertFalse("nohyphen passed the test to be an option.", this.handler
078            .isOption("nohyphen"));
079        Assert.assertFalse("An empty string passed the test to be an option", this.handler
080            .isOption(""));
081        Assert.assertFalse("Null passed the test to be an option", this.handler.isOption(null));
082      }
083    }