File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\test\java\hudson\util\ArgumentListBuilderTest.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi, Yahoo! Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.util;
25
26 import java.util.HashMap;
27 import java.util.HashSet;
28 import java.util.Map;
29 import java.util.Set;
30 import org.junit.Assert;
31 import org.junit.Test;
32
33 public class ArgumentListBuilderTest extends Assert {
34
35 public static void assertArrayEquals(String msg, boolean[] expected, boolean[] actual) {
36 assertArrayEquals(msg,box(expected),box(actual));
37 }
38
39 private static Boolean[] box(boolean[] a) {
40 if(a==null) return null;
41 Boolean[] r = new Boolean[a.length];
42 for (int i = 0; i < a.length; i++)
43 r[i] = a[i];
44 return r;
45 }
46
47
48 @Test
49 public void assertEmptyMask() {
50 ArgumentListBuilder builder = new ArgumentListBuilder();
51 builder.add("arg");
52 builder.add("other", "arguments");
53
54 assertFalse("There shouldnt be any masked arguments", builder.hasMaskedArguments());
55 boolean[] array = builder.toMaskArray();
56 assertNotNull("The mask array should not be null", array);
57 assertArrayEquals("The mask array was incorrect", new boolean[]{false,false,false}, array);
58 }
59
60 @Test
61 public void assertLastArgumentIsMasked() {
62 ArgumentListBuilder builder = new ArgumentListBuilder();
63 builder.add("arg");
64 builder.addMasked("ismasked");
65
66 assertTrue("There should be masked arguments", builder.hasMaskedArguments());
67 boolean[] array = builder.toMaskArray();
68 assertNotNull("The mask array should not be null", array);
69 assertArrayEquals("The mask array was incorrect", new boolean[]{false,true}, array);
70 }
71
72 @Test
73 public void assertSeveralMaskedArguments() {
74 ArgumentListBuilder builder = new ArgumentListBuilder();
75 builder.add("arg");
76 builder.addMasked("ismasked");
77 builder.add("non masked arg");
78 builder.addMasked("ismasked2");
79
80 assertTrue("There should be masked arguments", builder.hasMaskedArguments());
81 boolean[] array = builder.toMaskArray();
82 assertNotNull("The mask array should not be null", array);
83 assertArrayEquals("The mask array was incorrect", new boolean[]{false,true, false, true}, array);
84 }
85
86 @Test
87 public void assertPrependAfterAddingMasked() {
88 ArgumentListBuilder builder = new ArgumentListBuilder();
89 builder.addMasked("ismasked");
90 builder.add("arg");
91 builder.prepend("first", "second");
92
93 assertTrue("There should be masked arguments", builder.hasMaskedArguments());
94 boolean[] array = builder.toMaskArray();
95 assertNotNull("The mask array should not be null", array);
96 assertArrayEquals("The mask array was incorrect", new boolean[]{false,false,true,false}, array);
97 }
98
99 @Test
100 public void assertPrependBeforeAddingMasked() {
101 ArgumentListBuilder builder = new ArgumentListBuilder();
102 builder.prepend("first", "second");
103 builder.addMasked("ismasked");
104 builder.add("arg");
105
106 assertTrue("There should be masked arguments", builder.hasMaskedArguments());
107 boolean[] array = builder.toMaskArray();
108 assertNotNull("The mask array should not be null", array);
109 assertArrayEquals("The mask array was incorrect", new boolean[]{false,false,true,false}, array);
110 }
111
112 @Test
113 public void testToWindowsCommand() {
114 ArgumentListBuilder builder = new ArgumentListBuilder(
115 "ant.bat", "-Dfoo1=abc", // nothing special, no quotes
116 "-Dfoo2=foo bar", "-Dfoo3=/u*r", "-Dfoo4=/us?", // add quotes
117 "-Dfoo10=bar,baz",
118 "-Dfoo5=foo;bar^baz", "-Dfoo6=<xml>&here;</xml>", // add quotes
119 "-Dfoo7=foo|bar\"baz", // add quotes and "" for "
120 "-Dfoo8=% %QED% %comspec% %-%(%.%", // add quotes, and extra quotes for %Q and %c
121 "-Dfoo9=%'''%%@%"); // no quotes as none of the % are followed by a letter
122 // By default, does not escape %VAR%
123 assertArrayEquals(new String[] { "cmd.exe", "/C",
124 "\"ant.bat -Dfoo1=abc \"-Dfoo2=foo bar\""
125 + " \"-Dfoo3=/u*r\" \"-Dfoo4=/us?\" \"-Dfoo10=bar,baz\" \"-Dfoo5=foo;bar^baz\""
126 + " \"-Dfoo6=<xml>&here;</xml>\" \"-Dfoo7=foo|bar\"\"baz\""
127 + " \"-Dfoo8=% %QED% %comspec% %-%(%.%\""
128 + " -Dfoo9=%'''%%@% && exit %%ERRORLEVEL%%\"" },
129 builder.toWindowsCommand().toCommandArray());
130 // Pass flag to escape %VAR%
131 assertArrayEquals(new String[] { "cmd.exe", "/C",
132 "\"ant.bat -Dfoo1=abc \"-Dfoo2=foo bar\""
133 + " \"-Dfoo3=/u*r\" \"-Dfoo4=/us?\" \"-Dfoo10=bar,baz\" \"-Dfoo5=foo;bar^baz\""
134 + " \"-Dfoo6=<xml>&here;</xml>\" \"-Dfoo7=foo|bar\"\"baz\""
135 + " \"-Dfoo8=% %\"Q\"ED% %\"c\"omspec% %-%(%.%\""
136 + " -Dfoo9=%'''%%@% && exit %%ERRORLEVEL%%\"" },
137 builder.toWindowsCommand(true).toCommandArray());
138 }
139
140 @Test
141 public void assertMaskOnClone() {
142 ArgumentListBuilder builder = new ArgumentListBuilder();
143 builder.add("arg1");
144 builder.addMasked("masked1");
145 builder.add("arg2");
146
147 ArgumentListBuilder clone = builder.clone();
148 assertTrue("There should be masked arguments", clone.hasMaskedArguments());
149 boolean[] array = clone.toMaskArray();
150 assertNotNull("The mask array should not be null", array);
151 assertArrayEquals("The mask array was incorrect", builder.toMaskArray(), array);
152 }
153
154 private static final Map<String, String> KEY_VALUES = new HashMap<String, String>() {{
155 put("key1", "value1");
156 put("key2", "value2");
157 put("key3", "value3");
158 }};
159
160 private static final Set<String> MASKS = new HashSet<String>() {{
161 add("key2");
162 }};
163
164 @Test
165 public void assertKeyValuePairsWithMask() {
166 ArgumentListBuilder builder = new ArgumentListBuilder();
167 builder.addKeyValuePairs(null, KEY_VALUES, MASKS);
168
169 assertTrue("There should be masked arguments", builder.hasMaskedArguments());
170 boolean[] array = builder.toMaskArray();
171 assertNotNull("The mask array should not be null", array);
172 assertArrayEquals("The mask array was incorrect", new boolean[]{false,true,false}, array);
173
174 }
175
176 @Test
177 public void assertKeyValuePairs() {
178 ArgumentListBuilder builder = new ArgumentListBuilder();
179 builder.addKeyValuePairs(null, KEY_VALUES);
180
181 assertFalse("There shouldnt be any masked arguments", builder.hasMaskedArguments());
182 boolean[] array = builder.toMaskArray();
183 assertNotNull("The mask array should not be null", array);
184 assertArrayEquals("The mask array was incorrect", new boolean[]{false,false,false}, array);
185 }
186 }
187