001: /* Licensed to the Apache Software Foundation (ASF) under one or more
002: * contributor license agreements. See the NOTICE file distributed with
003: * this work for additional information regarding copyright ownership.
004: * The ASF licenses this file to You under the Apache License, Version 2.0
005: * (the "License"); you may not use this file except in compliance with
006: * the License. You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.harmony.luni.tests.java.lang;
018:
019: import java.io.File;
020: import java.io.IOException;
021: import java.io.InputStream;
022: import java.util.ArrayList;
023: import java.util.Arrays;
024: import java.util.List;
025: import java.util.Map;
026:
027: import junit.framework.TestCase;
028:
029: public class ProcessBuilderTest extends TestCase {
030:
031: public void testProcessBuilderStringArray() {
032:
033: }
034:
035: public void testProcessBuilderListOfString() {
036: try {
037: new ProcessBuilder((List<String>) null);
038: fail("no null pointer exception");
039: } catch (NullPointerException e) {
040: }
041: }
042:
043: public void testCommand() {
044: ProcessBuilder pb = new ProcessBuilder("command");
045: assertEquals(1, pb.command().size());
046: assertEquals("command", pb.command().get(0));
047:
048: // Regression for HARMONY-2675
049: pb = new ProcessBuilder("AAA");
050: pb.command("BBB", "CCC");
051: List<String> list = pb.command();
052: list.add("DDD");
053: String[] command = new String[3];
054: list.toArray(command);
055: assertTrue(Arrays.equals(new String[] { "BBB", "CCC", "DDD" },
056: command));
057: }
058:
059: public void testCommandStringArray() {
060: ProcessBuilder pb = new ProcessBuilder("command");
061: ProcessBuilder pbReturn = pb.command("cmd");
062: assertSame(pb, pbReturn);
063: assertEquals(1, pb.command().size());
064: assertEquals("cmd", pb.command().get(0));
065: }
066:
067: public void testCommandListOfString() {
068: ProcessBuilder pb = new ProcessBuilder("command");
069: List<String> newCmd = new ArrayList<String>();
070: newCmd.add("cmd");
071: ProcessBuilder pbReturn = pb.command(newCmd);
072: assertSame(pb, pbReturn);
073: assertEquals(1, pb.command().size());
074: assertEquals("cmd", pb.command().get(0));
075:
076: newCmd.add("arg");
077: assertEquals(2, pb.command().size());
078: assertEquals("cmd", pb.command().get(0));
079: assertEquals("arg", pb.command().get(1));
080: }
081:
082: public void testDirectory() {
083: ProcessBuilder pb = new ProcessBuilder("command");
084: assertNull(pb.directory());
085: }
086:
087: public void testDirectoryFile() {
088: ProcessBuilder pb = new ProcessBuilder("command");
089: File dir = new File(System.getProperty("java.io.tmpdir"));
090: ProcessBuilder pbReturn = pb.directory(dir);
091: assertSame(pb, pbReturn);
092: assertEquals(dir, pb.directory());
093:
094: pbReturn = pb.directory(null);
095: assertSame(pb, pbReturn);
096: assertNull(pb.directory());
097: }
098:
099: public void testEnvironment() {
100: ProcessBuilder pb = new ProcessBuilder("command");
101: Map<String, String> env = pb.environment();
102: assertEquals(System.getenv(), env);
103: env.clear();
104: env = pb.environment();
105: assertTrue(env.isEmpty());
106: try {
107: env.put(null, "");
108: fail("should throw NPE.");
109: } catch (NullPointerException e) {
110: // expected;
111: }
112: try {
113: env.put("", null);
114: fail("should throw NPE.");
115: } catch (NullPointerException e) {
116: // expected;
117: }
118: try {
119: env.get(null);
120: fail("should throw NPE.");
121: } catch (NullPointerException e) {
122: // expected;
123: }
124: try {
125: env.get(new Object());
126: fail("should throw ClassCastException.");
127: } catch (ClassCastException e) {
128: // expected;
129: }
130: }
131:
132: public void testRedirectErrorStream() {
133: ProcessBuilder pb = new ProcessBuilder("command");
134: assertFalse(pb.redirectErrorStream());
135: }
136:
137: public void testRedirectErrorStreamBoolean() {
138: ProcessBuilder pb = new ProcessBuilder("command");
139: ProcessBuilder pbReturn = pb.redirectErrorStream(true);
140: assertSame(pb, pbReturn);
141: assertTrue(pb.redirectErrorStream());
142: }
143:
144: /**
145: * @throws IOException
146: * @tests {@link java.lang.ProcessBuilder#start()}
147: */
148: @SuppressWarnings("nls")
149: public void testStart() throws IOException {
150: ProcessBuilder pb = new ProcessBuilder("java", "-version");
151: pb.directory(new File(System.getProperty("java.home")
152: + File.separator + "bin"));
153:
154: // Call the test target
155: Process process = pb.start();
156: InputStream in = process.getInputStream();
157: InputStream err = process.getErrorStream();
158: byte[] buf = new byte[1024];
159: if (in.available() > 0) {
160: assertTrue(in.read(buf) > 0);
161: } else {
162: assertTrue(err.read(buf) > 0);
163: }
164: }
165: }
|