Added
Link Here
|
1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
3 |
* |
4 |
* Copyright 2011 Oracle and/or its affiliates. All rights reserved. |
5 |
* |
6 |
* Oracle and Java are registered trademarks of Oracle and/or its affiliates. |
7 |
* Other names may be trademarks of their respective owners. |
8 |
* |
9 |
* The contents of this file are subject to the terms of either the GNU |
10 |
* General Public License Version 2 only ("GPL") or the Common |
11 |
* Development and Distribution License("CDDL") (collectively, the |
12 |
* "License"). You may not use this file except in compliance with the |
13 |
* License. You can obtain a copy of the License at |
14 |
* http://www.netbeans.org/cddl-gplv2.html |
15 |
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the |
16 |
* specific language governing permissions and limitations under the |
17 |
* License. When distributing the software, include this License Header |
18 |
* Notice in each file and include the License file at |
19 |
* nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this |
20 |
* particular file as subject to the "Classpath" exception as provided |
21 |
* by Oracle in the GPL Version 2 section of the License file that |
22 |
* accompanied this code. If applicable, add the following below the |
23 |
* License Header, with the fields enclosed by brackets [] replaced by |
24 |
* your own identifying information: |
25 |
* "Portions Copyrighted [year] [name of copyright owner]" |
26 |
* |
27 |
* If you wish your version of this file to be governed by only the CDDL |
28 |
* or only the GPL Version 2, indicate your decision by adding |
29 |
* "[Contributor] elects to include this software in this distribution |
30 |
* under the [CDDL or GPL Version 2] license." If you do not indicate a |
31 |
* single choice of license, a recipient has the option to distribute |
32 |
* your version of this file under either the CDDL, the GPL Version 2 or |
33 |
* to extend the choice of license to its licensees as provided above. |
34 |
* However, if you add GPL Version 2 code and therefore, elected the GPL |
35 |
* Version 2 license, then the option applies only if the new code is |
36 |
* made subject to such option by the copyright holder. |
37 |
* |
38 |
* Contributor(s): |
39 |
* |
40 |
* Portions Copyrighted 2011 Sun Microsystems, Inc. |
41 |
*/ |
42 |
package org.netbeans.api.extexecution; |
43 |
|
44 |
import java.io.IOException; |
45 |
import java.util.ArrayList; |
46 |
import java.util.Collections; |
47 |
import java.util.HashMap; |
48 |
import java.util.List; |
49 |
import java.util.Map; |
50 |
import org.netbeans.junit.NbTestCase; |
51 |
import org.netbeans.spi.extexecution.ProcessBuilderFactory; |
52 |
import org.netbeans.spi.extexecution.ProcessBuilderImplementation; |
53 |
|
54 |
/** |
55 |
* |
56 |
* @author Petr Hejl |
57 |
*/ |
58 |
public class ProcessBuilderTest extends NbTestCase { |
59 |
|
60 |
public ProcessBuilderTest(String name) { |
61 |
super(name); |
62 |
} |
63 |
|
64 |
public void testExecutable() throws IOException { |
65 |
TestProcessBuilder testBuilder = new TestProcessBuilder(); |
66 |
ProcessBuilder builder = ProcessBuilderFactory.createProcessBuilder(testBuilder, "Test builder"); |
67 |
|
68 |
try { |
69 |
builder.call(); |
70 |
fail("Empty executable does not throw exception"); |
71 |
} catch (IllegalStateException ex) { |
72 |
// expected |
73 |
} |
74 |
|
75 |
builder.setExecutable("ls"); |
76 |
builder.call(); |
77 |
assertEquals("ls", testBuilder.getExecutable()); |
78 |
|
79 |
builder.setExecutable("cd"); |
80 |
assertEquals("ls", testBuilder.getExecutable()); |
81 |
|
82 |
builder.call(); |
83 |
assertEquals("cd", testBuilder.getExecutable()); |
84 |
} |
85 |
|
86 |
public void testWorkingDirectory() throws IOException { |
87 |
TestProcessBuilder testBuilder = new TestProcessBuilder(); |
88 |
ProcessBuilder builder = ProcessBuilderFactory.createProcessBuilder(testBuilder, "Test builder"); |
89 |
builder.setExecutable("ls"); |
90 |
|
91 |
builder.call(); |
92 |
assertNull(testBuilder.getWorkingDirectory()); |
93 |
|
94 |
builder.setWorkingDirectory("test"); |
95 |
assertNull(testBuilder.getWorkingDirectory()); |
96 |
|
97 |
builder.call(); |
98 |
assertEquals("test", testBuilder.getWorkingDirectory()); |
99 |
} |
100 |
|
101 |
public void testArguments() throws IOException { |
102 |
TestProcessBuilder testBuilder = new TestProcessBuilder(); |
103 |
ProcessBuilder builder = ProcessBuilderFactory.createProcessBuilder(testBuilder, "Test builder"); |
104 |
builder.setExecutable("ls"); |
105 |
|
106 |
builder.call(); |
107 |
assertTrue(testBuilder.getArguments().isEmpty()); |
108 |
|
109 |
List<String> arguments = new ArrayList<String>(); |
110 |
Collections.addAll(arguments, "test1", "test2"); |
111 |
builder.setArguments(arguments); |
112 |
assertTrue(testBuilder.getArguments().isEmpty()); |
113 |
|
114 |
builder.call(); |
115 |
assertEquals(2, testBuilder.getArguments().size()); |
116 |
assertEquals("test1", testBuilder.getArguments().get(0)); |
117 |
assertEquals("test2", testBuilder.getArguments().get(1)); |
118 |
|
119 |
arguments.remove(0); |
120 |
assertEquals(2, testBuilder.getArguments().size()); |
121 |
assertEquals("test1", testBuilder.getArguments().get(0)); |
122 |
assertEquals("test2", testBuilder.getArguments().get(1)); |
123 |
|
124 |
builder.call(); |
125 |
assertEquals(2, testBuilder.getArguments().size()); |
126 |
assertEquals("test1", testBuilder.getArguments().get(0)); |
127 |
assertEquals("test2", testBuilder.getArguments().get(1)); |
128 |
|
129 |
builder.setArguments(arguments); |
130 |
builder.call(); |
131 |
assertEquals(1, testBuilder.getArguments().size()); |
132 |
assertEquals("test2", testBuilder.getArguments().get(0)); |
133 |
} |
134 |
|
135 |
public void testPaths() throws IOException { |
136 |
TestProcessBuilder testBuilder = new TestProcessBuilder(); |
137 |
ProcessBuilder builder = ProcessBuilderFactory.createProcessBuilder(testBuilder, "Test builder"); |
138 |
builder.setExecutable("ls"); |
139 |
|
140 |
builder.call(); |
141 |
assertTrue(testBuilder.getPaths().isEmpty()); |
142 |
|
143 |
List<String> paths = new ArrayList<String>(); |
144 |
Collections.addAll(paths, "test1", "test2"); |
145 |
builder.setPaths(paths); |
146 |
assertTrue(testBuilder.getPaths().isEmpty()); |
147 |
|
148 |
builder.call(); |
149 |
assertEquals(2, testBuilder.getPaths().size()); |
150 |
assertEquals("test1", testBuilder.getPaths().get(0)); |
151 |
assertEquals("test2", testBuilder.getPaths().get(1)); |
152 |
|
153 |
paths.remove(0); |
154 |
assertEquals(2, testBuilder.getPaths().size()); |
155 |
assertEquals("test1", testBuilder.getPaths().get(0)); |
156 |
assertEquals("test2", testBuilder.getPaths().get(1)); |
157 |
|
158 |
builder.call(); |
159 |
assertEquals(2, testBuilder.getPaths().size()); |
160 |
assertEquals("test1", testBuilder.getPaths().get(0)); |
161 |
assertEquals("test2", testBuilder.getPaths().get(1)); |
162 |
|
163 |
builder.setPaths(paths); |
164 |
builder.call(); |
165 |
assertEquals(1, testBuilder.getPaths().size()); |
166 |
assertEquals("test2", testBuilder.getPaths().get(0)); |
167 |
} |
168 |
|
169 |
public void testEnvironment() throws IOException { |
170 |
TestProcessBuilder testBuilder = new TestProcessBuilder(); |
171 |
ProcessBuilder builder = ProcessBuilderFactory.createProcessBuilder(testBuilder, "Test builder"); |
172 |
builder.setExecutable("ls"); |
173 |
|
174 |
builder.call(); |
175 |
assertTrue(testBuilder.getEnvironment().isEmpty()); |
176 |
|
177 |
Map<String, String> environment = new HashMap<String, String>(); |
178 |
environment.put("key1", "value1"); |
179 |
environment.put("key2", "value2"); |
180 |
builder.setEnvironmentVariables(environment); |
181 |
assertTrue(testBuilder.getEnvironment().isEmpty()); |
182 |
|
183 |
builder.call(); |
184 |
assertEquals(2, testBuilder.getEnvironment().size()); |
185 |
assertEquals("value1", testBuilder.getEnvironment().get("key1")); |
186 |
assertEquals("value2", testBuilder.getEnvironment().get("key2")); |
187 |
|
188 |
environment.remove("key1"); |
189 |
assertEquals(2, testBuilder.getEnvironment().size()); |
190 |
assertEquals("value1", testBuilder.getEnvironment().get("key1")); |
191 |
assertEquals("value2", testBuilder.getEnvironment().get("key2")); |
192 |
|
193 |
builder.call(); |
194 |
assertEquals(2, testBuilder.getEnvironment().size()); |
195 |
assertEquals("value1", testBuilder.getEnvironment().get("key1")); |
196 |
assertEquals("value2", testBuilder.getEnvironment().get("key2")); |
197 |
|
198 |
builder.setEnvironmentVariables(environment); |
199 |
builder.call(); |
200 |
assertEquals(1, testBuilder.getEnvironment().size()); |
201 |
assertEquals("value2", testBuilder.getEnvironment().get("key2")); |
202 |
} |
203 |
|
204 |
public void testRedirectErrorStream() throws IOException { |
205 |
TestProcessBuilder testBuilder = new TestProcessBuilder(); |
206 |
ProcessBuilder builder = ProcessBuilderFactory.createProcessBuilder(testBuilder, "Test builder"); |
207 |
builder.setExecutable("ls"); |
208 |
|
209 |
builder.call(); |
210 |
assertFalse(testBuilder.isRedirectErrorStream()); |
211 |
|
212 |
builder.setRedirectErrorStream(true); |
213 |
assertFalse(testBuilder.isRedirectErrorStream()); |
214 |
|
215 |
builder.call(); |
216 |
assertTrue(testBuilder.isRedirectErrorStream()); |
217 |
} |
218 |
|
219 |
private class TestProcessBuilder implements ProcessBuilderImplementation { |
220 |
|
221 |
private String executable; |
222 |
|
223 |
private String workingDirectory; |
224 |
|
225 |
private List<String> arguments; |
226 |
|
227 |
private List<String> paths; |
228 |
|
229 |
private Map<String, String> environment; |
230 |
|
231 |
private boolean redirectErrorStream; |
232 |
|
233 |
@Override |
234 |
public Process createProcess(String executable, String workingDirectory, |
235 |
List<String> arguments, List<String> paths, Map<String, String> environment, boolean redirectErrorStream) throws IOException { |
236 |
|
237 |
this.executable = executable; |
238 |
this.workingDirectory = workingDirectory; |
239 |
this.arguments = arguments; |
240 |
this.paths = paths; |
241 |
this.environment = environment; |
242 |
this.redirectErrorStream = redirectErrorStream; |
243 |
|
244 |
return null; |
245 |
} |
246 |
|
247 |
public String getExecutable() { |
248 |
return executable; |
249 |
} |
250 |
|
251 |
public List<String> getArguments() { |
252 |
return arguments; |
253 |
} |
254 |
|
255 |
public List<String> getPaths() { |
256 |
return paths; |
257 |
} |
258 |
|
259 |
public Map<String, String> getEnvironment() { |
260 |
return environment; |
261 |
} |
262 |
|
263 |
public boolean isRedirectErrorStream() { |
264 |
return redirectErrorStream; |
265 |
} |
266 |
|
267 |
public String getWorkingDirectory() { |
268 |
return workingDirectory; |
269 |
} |
270 |
|
271 |
} |
272 |
} |