1 package org.apache.commons.cli; 2 3 import junit.framework.Test; 4 import junit.framework.TestCase; 5 import junit.framework.TestSuite; 6 7 import junit.textui.TestRunner; 8 9 public class OptionBuilderTest extends TestCase { 10 11 public OptionBuilderTest( String name ) { 12 super( name ); 13 } 14 15 public static Test suite() { 16 return new TestSuite( OptionBuilderTest.class ); 17 } 18 19 public static void main( String args[] ) { 20 TestRunner.run( suite() ); 21 } 22 23 public void testCompleteOption( ) { 24 Option simple = OptionBuilder.withLongOpt( "simple option") 25 .hasArg( ) 26 .isRequired( ) 27 .hasArgs( ) 28 .withType( new Float ( 10 ) ) 29 .withDescription( "this is a simple option" ) 30 .create( 's' ); 31 32 assertEquals( "s", simple.getOpt() ); 33 assertEquals( "simple option", simple.getLongOpt() ); 34 assertEquals( "this is a simple option", simple.getDescription() ); 35 assertEquals( simple.getType().getClass(), Float .class ); 36 assertTrue( simple.hasArg() ); 37 assertTrue( simple.isRequired() ); 38 assertTrue( simple.hasArgs() ); 39 } 40 41 public void testTwoCompleteOptions( ) { 42 Option simple = OptionBuilder.withLongOpt( "simple option") 43 .hasArg( ) 44 .isRequired( ) 45 .hasArgs( ) 46 .withType( new Float ( 10 ) ) 47 .withDescription( "this is a simple option" ) 48 .create( 's' ); 49 50 assertEquals( "s", simple.getOpt() ); 51 assertEquals( "simple option", simple.getLongOpt() ); 52 assertEquals( "this is a simple option", simple.getDescription() ); 53 assertEquals( simple.getType().getClass(), Float .class ); 54 assertTrue( simple.hasArg() ); 55 assertTrue( simple.isRequired() ); 56 assertTrue( simple.hasArgs() ); 57 58 simple = OptionBuilder.withLongOpt( "dimple option") 59 .hasArg( ) 60 .withDescription( "this is a dimple option" ) 61 .create( 'd' ); 62 63 assertEquals( "d", simple.getOpt() ); 64 assertEquals( "dimple option", simple.getLongOpt() ); 65 assertEquals( "this is a dimple option", simple.getDescription() ); 66 assertNull( simple.getType() ); 67 assertTrue( simple.hasArg() ); 68 assertTrue( !simple.isRequired() ); 69 assertTrue( !simple.hasArgs() ); 70 } 71 72 public void testBaseOptionCharOpt() { 73 Option base = OptionBuilder.withDescription( "option description") 74 .create( 'o' ); 75 76 assertEquals( "o", base.getOpt() ); 77 assertEquals( "option description", base.getDescription() ); 78 assertTrue( !base.hasArg() ); 79 } 80 81 public void testBaseOptionStringOpt() { 82 Option base = OptionBuilder.withDescription( "option description") 83 .create( "o" ); 84 85 assertEquals( "o", base.getOpt() ); 86 assertEquals( "option description", base.getDescription() ); 87 assertTrue( !base.hasArg() ); 88 } 89 90 public void testSpecialOptChars() { 91 92 try { 94 Option opt = OptionBuilder.withDescription( "help options" ) 95 .create( '?' ); 96 assertEquals( "?", opt.getOpt() ); 97 } 98 catch( IllegalArgumentException arg ) { 99 fail( "IllegalArgumentException caught" ); 100 } 101 102 try { 104 Option opt = OptionBuilder.withDescription( "read from stdin" ) 105 .create( '@' ); 106 assertEquals( "@", opt.getOpt() ); 107 } 108 catch( IllegalArgumentException arg ) { 109 fail( "IllegalArgumentException caught" ); 110 } 111 } 112 113 public void testOptionArgNumbers() { 114 Option opt = OptionBuilder.withDescription( "option description" ) 115 .hasArgs( 2 ) 116 .create( 'o' ); 117 assertEquals( 2, opt.getArgs() ); 118 } 119 120 public void testIllegalOptions() { 121 try { 123 Option opt = OptionBuilder.withDescription( "option description" ) 124 .create( '"' ); 125 fail( "IllegalArgumentException not caught" ); 126 } 127 catch( IllegalArgumentException exp ) { 128 } 130 131 try { 133 Option opt = OptionBuilder.create( "opt`" ); 134 fail( "IllegalArgumentException not caught" ); 135 } 136 catch( IllegalArgumentException exp ) { 137 } 139 140 try { 142 Option opt = OptionBuilder.create( null ); 143 fail( "IllegalArgumentException not caught" ); 144 } 145 catch( IllegalArgumentException exp ) { 146 } 148 149 try { 151 Option opt = OptionBuilder.create( "opt" ); 152 } 154 catch( IllegalArgumentException exp ) { 155 fail( "IllegalArgumentException caught" ); 156 } 157 } 158 } | Popular Tags |