KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > types > CommandlineTest


1 /*
2  * Copyright 2000-2002,2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */

17
18 package org.apache.tools.ant.types;
19
20 import org.apache.tools.ant.BuildException;
21
22 import junit.framework.TestCase;
23 import junit.framework.AssertionFailedError;
24
25 import java.io.File JavaDoc;
26
27 /**
28  * JUnit 3 testcases for org.apache.tools.ant.CommandLine
29  *
30  */

31 public class CommandlineTest extends TestCase {
32
33     public CommandlineTest(String JavaDoc name) {
34         super(name);
35     }
36
37     public void testTokenizer() {
38         String JavaDoc[] s = Commandline.translateCommandline("1 2 3");
39         assertEquals("Simple case", 3, s.length);
40         for (int i=0; i<3; i++) {
41             assertEquals(""+(i+1), s[i]);
42         }
43
44         s = Commandline.translateCommandline("");
45         assertEquals("empty string", 0, s.length);
46
47         s = Commandline.translateCommandline(null);
48         assertEquals("null", 0, s.length);
49
50         s = Commandline.translateCommandline("1 \'2\' 3");
51         assertEquals("Simple case with single quotes", 3, s.length);
52         assertEquals("Single quotes have been stripped", "2", s[1]);
53
54         s = Commandline.translateCommandline("1 \"2\" 3");
55         assertEquals("Simple case with double quotes", 3, s.length);
56         assertEquals("Double quotes have been stripped", "2", s[1]);
57
58         s = Commandline.translateCommandline("1 \"2 3\" 4");
59         assertEquals("Case with double quotes and whitespace", 3, s.length);
60         assertEquals("Double quotes stripped, space included", "2 3", s[1]);
61
62         s = Commandline.translateCommandline("1 \"2\'3\" 4");
63         assertEquals("Case with double quotes around single quote", 3, s.length);
64         assertEquals("Double quotes stripped, single quote included", "2\'3",
65                      s[1]);
66
67         s = Commandline.translateCommandline("1 \'2 3\' 4");
68         assertEquals("Case with single quotes and whitespace", 3, s.length);
69         assertEquals("Single quotes stripped, space included", "2 3", s[1]);
70
71         s = Commandline.translateCommandline("1 \'2\"3\' 4");
72         assertEquals("Case with single quotes around double quote", 3, s.length);
73         assertEquals("Single quotes stripped, double quote included", "2\"3",
74                      s[1]);
75
76         // \ doesn't have a special meaning anymore - this is different from
77
// what the Unix sh does but causes a lot of problems on DOS
78
// based platforms otherwise
79
s = Commandline.translateCommandline("1 2\\ 3 4");
80         assertEquals("case with quoted whitespace", 4, s.length);
81         assertEquals("backslash included", "2\\", s[1]);
82
83         // "" should become a single empty argument, same for ''
84
// PR 5906
85
s = Commandline.translateCommandline("\"\" a");
86         assertEquals("Doublequoted null arg prepend", 2, s.length);
87         assertEquals("Doublequoted null arg prepend", "", s[0]);
88         assertEquals("Doublequoted null arg prepend", "a", s[1]);
89         s = Commandline.translateCommandline("a \"\"");
90         assertEquals("Doublequoted null arg append", 2, s.length);
91         assertEquals("Doublequoted null arg append", "a", s[0]);
92         assertEquals("Doublequoted null arg append", "", s[1]);
93         s = Commandline.translateCommandline("\"\"");
94         assertEquals("Doublequoted null arg", 1, s.length);
95         assertEquals("Doublequoted null arg", "", s[0]);
96
97         s = Commandline.translateCommandline("\'\' a");
98         assertEquals("Singlequoted null arg prepend", 2, s.length);
99         assertEquals("Singlequoted null arg prepend", "", s[0]);
100         assertEquals("Singlequoted null arg prepend", "a", s[1]);
101         s = Commandline.translateCommandline("a \'\'");
102         assertEquals("Singlequoted null arg append", 2, s.length);
103         assertEquals("Singlequoted null arg append", "a", s[0]);
104         assertEquals("Singlequoted null arg append", "", s[1]);
105         s = Commandline.translateCommandline("\'\'");
106         assertEquals("Singlequoted null arg", 1, s.length);
107         assertEquals("Singlequoted null arg", "", s[0]);
108
109         // now to the expected failures
110

111         try {
112             s = Commandline.translateCommandline("a \'b c");
113             fail("unbalanced single quotes undetected");
114         } catch (BuildException be) {
115             assertEquals("unbalanced quotes in a \'b c", be.getMessage());
116         }
117
118         try {
119             s = Commandline.translateCommandline("a \"b c");
120             fail("unbalanced double quotes undetected");
121         } catch (BuildException be) {
122             assertEquals("unbalanced quotes in a \"b c", be.getMessage());
123         }
124     }
125
126     public void testToString() {
127         assertEquals("", Commandline.toString(new String JavaDoc[0]));
128         assertEquals("", Commandline.toString(null));
129         assertEquals("1 2 3", Commandline.toString(new String JavaDoc[] {"1", "2", "3"}));
130         assertEquals("1 \"2 3\"", Commandline.toString(new String JavaDoc[] {"1", "2 3"}));
131         assertEquals("1 \"2\'3\"", Commandline.toString(new String JavaDoc[] {"1", "2\'3"}));
132         assertEquals("1 \'2\"3\'", Commandline.toString(new String JavaDoc[] {"1", "2\"3"}));
133     }
134
135     public void testAwkCommand(){
136         Commandline c = new Commandline();
137         c.setExecutable("awk");
138         c.createArgument().setValue("'NR == 2 { print $NF }'");
139         String JavaDoc[] s = c.getCommandline();
140         assertNotNull(s);
141         assertEquals(2, s.length);
142         assertEquals("awk", s[0]);
143         assertEquals("'NR == 2 { print $NF }'", s[1]);
144     }
145 }
146
Popular Tags