KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cobertura > util > CommandLineBuilderTest


1 /*
2  * Cobertura - http://cobertura.sourceforge.net/
3  *
4  * Copyright (C) 2005 Grzegorz Lukasik
5  *
6  * Note: This file is dual licensed under the GPL and the Apache
7  * Source License (so that it can be used from both the main
8  * Cobertura classes and the ant tasks).
9  *
10  * Cobertura is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published
12  * by the Free Software Foundation; either version 2 of the License,
13  * or (at your option) any later version.
14  *
15  * Cobertura is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Cobertura; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23  * USA
24  */

25 package net.sourceforge.cobertura.util;
26
27 import java.io.File JavaDoc;
28 import java.io.IOException JavaDoc;
29
30 import junit.framework.TestCase;
31
32 /**
33  * @author Grzegorz Lukasik
34  */

35 public class CommandLineBuilderTest extends TestCase {
36
37     private String JavaDoc[] testArguments( String JavaDoc[] args) throws Exception JavaDoc {
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 JavaDoc cmdFile = new File JavaDoc(builder.getCommandLineFile());
44         assertTrue( cmdFile.isAbsolute());
45         assertTrue( cmdFile.isFile());
46         
47         String JavaDoc[] result =
48         CommandLineBuilder.preprocessCommandLineArguments(
49                 new String JavaDoc[] { "--commandsfile", builder.getCommandLineFile()});
50         builder.dispose();
51         
52         return result;
53     }
54     
55     public void testExample() throws Exception JavaDoc {
56         CommandLineBuilder builder = new CommandLineBuilder();
57         builder.addArg("--someoption");
58         builder.addArg("optionValue");
59         builder.saveArgs();
60         
61         String JavaDoc[] args =
62         CommandLineBuilder.preprocessCommandLineArguments(
63                 new String JavaDoc[] { "--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 JavaDoc {
72         CommandLineBuilder builder = new CommandLineBuilder();
73         builder.addArg("--someoption", "optionValue");
74         builder.saveArgs();
75         
76         String JavaDoc[] args =
77         CommandLineBuilder.preprocessCommandLineArguments(
78                 new String JavaDoc[] { "--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 JavaDoc[] first, String JavaDoc[] 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 JavaDoc {
96         String JavaDoc[] options = new String JavaDoc[100000];
97         for( int i=0; i<options.length; i++) {
98             options[i] = "myOption" + i;
99         }
100
101         String JavaDoc[] args = testArguments( options);
102         assertEquals( options, args);
103     }
104
105     public void testVariousOptions() throws Exception JavaDoc {
106         String JavaDoc[] options = { "hello", " one", "two ", " three , ", "\"'xx", " ", "file .java", "f.java",
107                 "#@()39340*(@0$#&%^@#&4098353856_*(_@735/896_udsknbfdvzxvkasd DSFWBXfqw']][.,=---3\\]];",
108                 "null", "!@#$%^&*()_+-={}|[]\\:\";'<>?,./'"
109         };
110         String JavaDoc[] args = testArguments( options);
111         assertEquals( options, args);
112     }
113
114     public void testEmptyOptions() throws Exception JavaDoc {
115         String JavaDoc[] args = testArguments( new String JavaDoc[0]);
116         assertEquals( new String JavaDoc[0], args);
117     }
118
119     public void testInvalidArguments() throws Exception JavaDoc {
120         CommandLineBuilder builder = new CommandLineBuilder();
121         try {
122             builder.addArg(null);
123             fail( "NullPointerException expected");
124         } catch( NullPointerException JavaDoc ex) {}
125         try {
126             builder.addArg( "someArgument", null);
127             fail( "NullPointerException expected");
128         } catch( NullPointerException JavaDoc ex) {}
129         try {
130             builder.addArg( null, "someValue");
131             fail( "NullPointerException expected");
132         } catch( NullPointerException JavaDoc ex) {}
133         try {
134             CommandLineBuilder.preprocessCommandLineArguments(null);
135             fail( "NullPointerException expected");
136         } catch( NullPointerException JavaDoc ex) {}
137
138         try {
139             CommandLineBuilder.preprocessCommandLineArguments(new String JavaDoc[] { "Hello", null });
140             fail( "NullPointerException expected");
141         } catch( NullPointerException JavaDoc ex) {}
142
143         try {
144             CommandLineBuilder.preprocessCommandLineArguments(new String JavaDoc[] { "--commandsfile", "hello", null });
145             fail( "NullPointerException expected");
146         } catch( NullPointerException JavaDoc ex) {}
147     }
148
149     public void testCommandsFileOption() throws Exception JavaDoc {
150         String JavaDoc[] args = { "Hello", "world" };
151         String JavaDoc[] result = CommandLineBuilder.preprocessCommandLineArguments( args);
152         assertSame( args, result);
153         
154         try {
155             args = new String JavaDoc[]{ "Hello", "--commandsfile" };
156             CommandLineBuilder.preprocessCommandLineArguments( args);
157             fail( "IllegalArgumentException expected");
158         } catch( IllegalArgumentException JavaDoc ex) {}
159
160         try {
161             args = new String JavaDoc[]{ "Hello", "--commandsfile", "hello.cmd" };
162             CommandLineBuilder.preprocessCommandLineArguments( args);
163             fail( "IO Exception expected");
164         } catch( IOException JavaDoc ex) {}
165     }
166     
167 }
168
Popular Tags