Coverage Report - com.jeantessier.commandline.TestTextPrinter
 
Classes in this File Line Coverage Branch Coverage Complexity
TestTextPrinter
100%
113/113
N/A
1
 
 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 java.io.*;
 36  
 
 37  
 import junit.framework.*;
 38  
 
 39  12
 public class TestTextPrinter extends TestCase {
 40  
     private CommandLine commandLine;
 41  
     private TextPrinter printer;
 42  
 
 43  
     protected void setUp() throws Exception {
 44  12
         super.setUp();
 45  
 
 46  12
         commandLine = new CommandLine(false);
 47  12
         printer = new TextPrinter(getName());
 48  12
     }
 49  
 
 50  
     public void testNoArgs() throws CommandLineException, IOException {
 51  1
         commandLine.addToggleSwitch("switch1");
 52  1
         commandLine.addToggleSwitch("switch2");
 53  1
         commandLine.parse(new String[0]);
 54  1
         commandLine.accept(printer);
 55  
 
 56  1
         BufferedReader in = new BufferedReader(new StringReader(printer.toString()));
 57  1
         int i = 1;
 58  1
         assertEquals("line " + i++, getName(), in.readLine());
 59  1
         assertEquals("line " + i++, null, in.readLine());
 60  1
     }
 61  
 
 62  
     public void testPartialSwitches() throws CommandLineException, IOException {
 63  1
         commandLine.addToggleSwitch("switch1");
 64  1
         commandLine.addToggleSwitch("switch2");
 65  1
         commandLine.parse(new String[] {"-switch1"});
 66  1
         commandLine.accept(printer);
 67  
 
 68  1
         BufferedReader in = new BufferedReader(new StringReader(printer.toString()));
 69  1
         int i = 1;
 70  1
         assertEquals("line " + i++, getName(), in.readLine());
 71  1
         assertEquals("line " + i++, "    -switch1", in.readLine());
 72  1
         assertEquals("line " + i++, null, in.readLine());
 73  1
     }
 74  
 
 75  
     public void testToggleSwitch() throws CommandLineException, IOException {
 76  1
         commandLine.addToggleSwitch("switch1");
 77  1
         commandLine.parse(new String[] {"-switch1"});
 78  1
         commandLine.accept(printer);
 79  
 
 80  1
         BufferedReader in = new BufferedReader(new StringReader(printer.toString()));
 81  1
         int i = 1;
 82  1
         assertEquals("line " + i++, getName(), in.readLine());
 83  1
         assertEquals("line " + i++, "    -switch1", in.readLine());
 84  1
         assertEquals("line " + i++, null, in.readLine());
 85  1
     }
 86  
 
 87  
     public void testSingleValueSwitch() throws CommandLineException, IOException {
 88  1
         commandLine.addSingleValueSwitch("switch1");
 89  1
         commandLine.parse(new String[] {"-switch1", "value"});
 90  1
         commandLine.accept(printer);
 91  
 
 92  1
         BufferedReader in = new BufferedReader(new StringReader(printer.toString()));
 93  1
         int i = 1;
 94  1
         assertEquals("line " + i++, getName(), in.readLine());
 95  1
         assertEquals("line " + i++, "    -switch1 value", in.readLine());
 96  1
         assertEquals("line " + i++, null, in.readLine());
 97  1
     }
 98  
 
 99  
     public void testOptionalValueSwitchWithNoValue() throws CommandLineException, IOException {
 100  1
         commandLine.addOptionalValueSwitch("switch1");
 101  1
         commandLine.parse(new String[] {"-switch1"});
 102  1
         commandLine.accept(printer);
 103  
 
 104  1
         BufferedReader in = new BufferedReader(new StringReader(printer.toString()));
 105  1
         int i = 1;
 106  1
         assertEquals("line " + i++, getName(), in.readLine());
 107  1
         assertEquals("line " + i++, "    -switch1", in.readLine());
 108  1
         assertEquals("line " + i++, null, in.readLine());
 109  1
     }
 110  
 
 111  
     public void testOptionalValueSwitchWithOneValue() throws CommandLineException, IOException {
 112  1
         commandLine.addOptionalValueSwitch("switch1");
 113  1
         commandLine.parse(new String[] {"-switch1", "value"});
 114  1
         commandLine.accept(printer);
 115  
 
 116  1
         BufferedReader in = new BufferedReader(new StringReader(printer.toString()));
 117  1
         int i = 1;
 118  1
         assertEquals("line " + i++, getName(), in.readLine());
 119  1
         assertEquals("line " + i++, "    -switch1 value", in.readLine());
 120  1
         assertEquals("line " + i++, null, in.readLine());
 121  1
     }
 122  
 
 123  
     public void testMultipleValuesSwitchWithOneValue() throws CommandLineException, IOException {
 124  1
         commandLine.addMultipleValuesSwitch("switch1");
 125  1
         commandLine.parse(new String[] {"-switch1", "value"});
 126  1
         commandLine.accept(printer);
 127  
 
 128  1
         BufferedReader in = new BufferedReader(new StringReader(printer.toString()));
 129  1
         int i = 1;
 130  1
         assertEquals("line " + i++, getName(), in.readLine());
 131  1
         assertEquals("line " + i++, "    -switch1 value", in.readLine());
 132  1
         assertEquals("line " + i++, null, in.readLine());
 133  1
     }
 134  
 
 135  
     public void testMultipleValuesSwitchWithMultipleValues() throws CommandLineException, IOException {
 136  1
         commandLine.addMultipleValuesSwitch("switch1");
 137  1
         commandLine.parse(new String[] {"-switch1", "value1", "-switch1", "value2"});
 138  1
         commandLine.accept(printer);
 139  
 
 140  1
         BufferedReader in = new BufferedReader(new StringReader(printer.toString()));
 141  1
         int i = 1;
 142  1
         assertEquals("line " + i++, getName(), in.readLine());
 143  1
         assertEquals("line " + i++, "    -switch1 value1", in.readLine());
 144  1
         assertEquals("line " + i++, "    -switch1 value2", in.readLine());
 145  1
         assertEquals("line " + i++, null, in.readLine());
 146  1
     }
 147  
 
 148  
     public void testUnknownSwitch() throws CommandLineException, IOException {
 149  1
         commandLine.parse(new String[] {"-switch1"});
 150  1
         commandLine.accept(printer);
 151  
 
 152  1
         BufferedReader in = new BufferedReader(new StringReader(printer.toString()));
 153  1
         int i = 1;
 154  1
         assertEquals("line " + i++, getName(), in.readLine());
 155  1
         assertEquals("line " + i++, "    -switch1", in.readLine());
 156  1
         assertEquals("line " + i++, null, in.readLine());
 157  1
     }
 158  
 
 159  
     public void testOneParameter() throws CommandLineException, IOException {
 160  1
         commandLine.parse(new String[] {"param"});
 161  1
         commandLine.accept(printer);
 162  
 
 163  1
         BufferedReader in = new BufferedReader(new StringReader(printer.toString()));
 164  1
         int i = 1;
 165  1
         assertEquals("line " + i++, getName(), in.readLine());
 166  1
         assertEquals("line " + i++, "    param", in.readLine());
 167  1
         assertEquals("line " + i++, null, in.readLine());
 168  1
     }
 169  
 
 170  
     public void testMultipleParameter() throws CommandLineException, IOException {
 171  1
         commandLine.parse(new String[] {"param1", "param2"});
 172  1
         commandLine.accept(printer);
 173  
 
 174  1
         BufferedReader in = new BufferedReader(new StringReader(printer.toString()));
 175  1
         int i = 1;
 176  1
         assertEquals("line " + i++, getName(), in.readLine());
 177  1
         assertEquals("line " + i++, "    param1", in.readLine());
 178  1
         assertEquals("line " + i++, "    param2", in.readLine());
 179  1
         assertEquals("line " + i++, null, in.readLine());
 180  1
     }
 181  
 
 182  
     public void testSingleValueSwitchAndParameter() throws CommandLineException, IOException {
 183  1
         commandLine.parse(new String[] {"-switch", "value", "param"});
 184  1
         commandLine.accept(printer);
 185  
 
 186  1
         BufferedReader in = new BufferedReader(new StringReader(printer.toString()));
 187  1
         int i = 1;
 188  1
         assertEquals("line " + i++, getName(), in.readLine());
 189  1
         assertEquals("line " + i++, "    -switch value", in.readLine());
 190  1
         assertEquals("line " + i++, "    param", in.readLine());
 191  1
         assertEquals("line " + i++, null, in.readLine());
 192  1
     }
 193  
 }