1 25 package net.sourceforge.cobertura.util; 26 27 import java.io.File ; 28 import java.io.IOException ; 29 30 import junit.framework.TestCase; 31 32 35 public class CommandLineBuilderTest extends TestCase { 36 37 private String [] testArguments( String [] args) throws Exception { 38 CommandLineBuilder builder = new CommandLineBuilder(); 39 for( int i=0; i<args.length; i++) 40 builder.addArg( args[i]); 41 builder.saveArgs(); 42 43 File cmdFile = new File (builder.getCommandLineFile()); 44 assertTrue( cmdFile.isAbsolute()); 45 assertTrue( cmdFile.isFile()); 46 47 String [] result = 48 CommandLineBuilder.preprocessCommandLineArguments( 49 new String [] { "--commandsfile", builder.getCommandLineFile()}); 50 builder.dispose(); 51 52 return result; 53 } 54 55 public void testExample() throws Exception { 56 CommandLineBuilder builder = new CommandLineBuilder(); 57 builder.addArg("--someoption"); 58 builder.addArg("optionValue"); 59 builder.saveArgs(); 60 61 String [] args = 62 CommandLineBuilder.preprocessCommandLineArguments( 63 new String [] { "--commandsfile", builder.getCommandLineFile()}); 64 65 assertEquals( "--someoption", args[0]); 66 assertEquals( "optionValue", args[1]); 67 68 builder.dispose(); 69 } 70 71 public void testExample_2() throws Exception { 72 CommandLineBuilder builder = new CommandLineBuilder(); 73 builder.addArg("--someoption", "optionValue"); 74 builder.saveArgs(); 75 76 String [] args = 77 CommandLineBuilder.preprocessCommandLineArguments( 78 new String [] { "--commandsfile", builder.getCommandLineFile()}); 79 80 assertEquals( "--someoption", args[0]); 81 assertEquals( "optionValue", args[1]); 82 83 builder.dispose(); 84 } 85 86 private void assertEquals(String [] first, String [] second) 87 { 88 assertEquals(first.length, second.length); 89 for (int i = 0; i < first.length; i++) 90 { 91 assertEquals(first[i], second[i]); 92 } 93 } 94 95 public void testManyOptions() throws Exception { 96 String [] options = new String [100000]; 97 for( int i=0; i<options.length; i++) { 98 options[i] = "myOption" + i; 99 } 100 101 String [] args = testArguments( options); 102 assertEquals( options, args); 103 } 104 105 public void testVariousOptions() throws Exception { 106 String [] options = { "hello", " one", "two ", " three , ", "\"'xx", " ", "file .java", "f.java", 107 "#@()39340*(@0$#&%^@#&4098353856_*(_@735/896_udsknbfdvzxvkasd DSFWBXfqw']][.,=---3\\]];", 108 "null", "!@#$%^&*()_+-={}|[]\\:\";'<>?,./'" 109 }; 110 String [] args = testArguments( options); 111 assertEquals( options, args); 112 } 113 114 public void testEmptyOptions() throws Exception { 115 String [] args = testArguments( new String [0]); 116 assertEquals( new String [0], args); 117 } 118 119 public void testInvalidArguments() throws Exception { 120 CommandLineBuilder builder = new CommandLineBuilder(); 121 try { 122 builder.addArg(null); 123 fail( "NullPointerException expected"); 124 } catch( NullPointerException ex) {} 125 try { 126 builder.addArg( "someArgument", null); 127 fail( "NullPointerException expected"); 128 } catch( NullPointerException ex) {} 129 try { 130 builder.addArg( null, "someValue"); 131 fail( "NullPointerException expected"); 132 } catch( NullPointerException ex) {} 133 try { 134 CommandLineBuilder.preprocessCommandLineArguments(null); 135 fail( "NullPointerException expected"); 136 } catch( NullPointerException ex) {} 137 138 try { 139 CommandLineBuilder.preprocessCommandLineArguments(new String [] { "Hello", null }); 140 fail( "NullPointerException expected"); 141 } catch( NullPointerException ex) {} 142 143 try { 144 CommandLineBuilder.preprocessCommandLineArguments(new String [] { "--commandsfile", "hello", null }); 145 fail( "NullPointerException expected"); 146 } catch( NullPointerException ex) {} 147 } 148 149 public void testCommandsFileOption() throws Exception { 150 String [] args = { "Hello", "world" }; 151 String [] result = CommandLineBuilder.preprocessCommandLineArguments( args); 152 assertSame( args, result); 153 154 try { 155 args = new String []{ "Hello", "--commandsfile" }; 156 CommandLineBuilder.preprocessCommandLineArguments( args); 157 fail( "IllegalArgumentException expected"); 158 } catch( IllegalArgumentException ex) {} 159 160 try { 161 args = new String []{ "Hello", "--commandsfile", "hello.cmd" }; 162 CommandLineBuilder.preprocessCommandLineArguments( args); 163 fail( "IO Exception expected"); 164 } catch( IOException ex) {} 165 } 166 167 } 168 | Popular Tags |