Coverage Report - com.jeantessier.commandline.TestAliasSwitch
 
Classes in this File Line Coverage Branch Coverage Complexity
TestAliasSwitch
96%
54/56
N/A
1.083
TestAliasSwitch$1
100%
3/3
N/A
1.083
 
 1  
 /*
 2  
  *  Copyright (c) 2001-2009, Jean Tessier
 3  
  *  All rights reserved.
 4  
  *
 5  
  *  Redistribution and use in source and binary forms, with or without
 6  
  *  modification, are permitted provided that the following conditions
 7  
  *  are met:
 8  
  *
 9  
  *      * Redistributions of source code must retain the above copyright
 10  
  *        notice, this list of conditions and the following disclaimer.
 11  
  *
 12  
  *      * Redistributions in binary form must reproduce the above copyright
 13  
  *        notice, this list of conditions and the following disclaimer in the
 14  
  *        documentation and/or other materials provided with the distribution.
 15  
  *
 16  
  *      * Neither the name of Jean Tessier nor the names of his contributors
 17  
  *        may be used to endorse or promote products derived from this software
 18  
  *        without specific prior written permission.
 19  
  *
 20  
  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 21  
  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 22  
  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 23  
  *  A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR
 24  
  *  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 25  
  *  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 26  
  *  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 27  
  *  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 28  
  *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 29  
  *  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 30  
  *  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 31  
  */
 32  
 
 33  
 package com.jeantessier.commandline;
 34  
 
 35  
 import org.jmock.integration.junit3.*;
 36  
 import org.jmock.*;
 37  
 
 38  12
 public class TestAliasSwitch extends MockObjectTestCase {
 39  
     private static final String SWITCH_NAME = "switch";
 40  
     private static final String SWITCH_VALUE = "value";
 41  
 
 42  
     private CommandLineSwitch switch1;
 43  
     private CommandLineSwitch switch2;
 44  
 
 45  
     private AliasSwitch aliasSwitch;
 46  
 
 47  
     protected void setUp() throws Exception {
 48  11
         super.setUp();
 49  
 
 50  11
         switch1 = new SingleValueSwitch("switch1");
 51  11
         switch2 = new OptionalValueSwitch("switch2");
 52  
 
 53  11
         aliasSwitch = new AliasSwitch(SWITCH_NAME, switch1, switch2);
 54  11
     }
 55  
     
 56  
     public void testConstructor() {
 57  1
         assertEquals("Name", SWITCH_NAME, aliasSwitch.getName());
 58  1
         assertEquals("Nb switches", 2, aliasSwitch.getSwitches().size());
 59  1
         assertTrue("Missing switch1", aliasSwitch.getSwitches().contains(switch1));
 60  1
         assertTrue("Missing switch2", aliasSwitch.getSwitches().contains(switch2));
 61  1
     }
 62  
 
 63  
     public void testSetValueToNull() {
 64  1
         aliasSwitch.setValue(null);
 65  1
         assertEquals("Switch1 not default value", switch1.getDefaultValue(), switch1.getValue());
 66  1
         assertEquals("Switch2 not default value", switch2.getDefaultValue(), switch2.getValue());
 67  1
     }
 68  
 
 69  
     public void testSetValueToValue() {
 70  1
         aliasSwitch.setValue(SWITCH_VALUE);
 71  1
         assertEquals("Switch1 not new value", SWITCH_VALUE, switch1.getValue());
 72  1
         assertEquals("Switch2 not new value", SWITCH_VALUE, switch2.getValue());
 73  1
     }
 74  
 
 75  
     public void testIsPresent() {
 76  1
         aliasSwitch.setValue(SWITCH_VALUE);
 77  1
         assertTrue("Switch1 not present", switch1.isPresent());
 78  1
         assertTrue("Switch2 not present", switch2.isPresent());
 79  1
         assertTrue("Not present", aliasSwitch.isPresent());
 80  1
     }
 81  
 
 82  
     public void testIsPresentWithNoSwitches() {
 83  1
         aliasSwitch = new AliasSwitch(SWITCH_NAME);
 84  1
         aliasSwitch.setValue(SWITCH_VALUE);
 85  1
         assertFalse("Present", aliasSwitch.isPresent());
 86  1
     }
 87  
 
 88  
     public void testParseNull() throws CommandLineException {
 89  1
         aliasSwitch = new AliasSwitch(SWITCH_NAME, switch2);
 90  1
         int step = aliasSwitch.parse(null);
 91  1
         assertEquals("step", 1, step);
 92  1
         assertFalse("Switch1 was set", switch1.isPresent());
 93  1
         assertEquals("Switch2 not default value", switch2.getDefaultValue(), switch2.getValue());
 94  1
     }
 95  
 
 96  
     public void testParseNullWithError() throws CommandLineException {
 97  
         try {
 98  1
             aliasSwitch.parse(null);
 99  0
             fail("Alias with SingleValueSwitch parsed null");
 100  1
         } catch (CommandLineException e) {
 101  
             // Expected
 102  0
         }
 103  1
     }
 104  
 
 105  
     public void testParseValue() throws CommandLineException {
 106  1
         int step = aliasSwitch.parse(SWITCH_VALUE);
 107  1
         assertEquals("step", 2, step);
 108  1
         assertEquals("Switch1 not new value", SWITCH_VALUE, switch1.getValue());
 109  1
         assertEquals("Switch2 not new value", SWITCH_VALUE, switch2.getValue());
 110  1
     }
 111  
 
 112  
     public void testParseNullWithNoSwitches() throws CommandLineException {
 113  1
         aliasSwitch = new AliasSwitch(SWITCH_NAME);
 114  1
         int step = aliasSwitch.parse(null);
 115  1
         assertEquals("step", 1, step);
 116  1
     }
 117  
 
 118  
     public void testParseValueWithNoSwitches() throws CommandLineException {
 119  1
         aliasSwitch = new AliasSwitch(SWITCH_NAME);
 120  1
         int step = aliasSwitch.parse(SWITCH_VALUE);
 121  1
         assertEquals("step", 1, step);
 122  1
     }
 123  
 
 124  
     public void testAccept() {
 125  1
         final Visitor mockVisitor = mock(Visitor.class);
 126  
 
 127  1
         checking(new Expectations() {{
 128  1
             one (mockVisitor).visitAliasSwitch(aliasSwitch);
 129  1
         }});
 130  
 
 131  1
         aliasSwitch.accept(mockVisitor);
 132  1
     }
 133  
 }