001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * Portions Copyrighted 2007 Sun Microsystems, Inc.
027: */
028: package org.netbeans.nbexec;
029:
030: import java.io.File;
031: import java.io.IOException;
032: import java.io.InputStream;
033: import java.net.URL;
034: import java.util.Arrays;
035: import java.util.LinkedList;
036: import java.util.List;
037: import org.netbeans.junit.NbTestCase;
038: import org.openide.util.Lookup;
039: import org.openide.util.Utilities;
040:
041: /**
042: *
043: * @author Jaroslav Tulach
044: */
045: public class NbExecPassesCorrectlyQuotedArgsTest extends NbTestCase {
046: public NbExecPassesCorrectlyQuotedArgsTest(String name) {
047: super (name);
048: }
049:
050: @Override
051: protected void setUp() throws Exception {
052: clearWorkDir();
053: }
054:
055: public void testArgsArePassed() throws Exception {
056: run(getWorkDir(), "ahoj");
057:
058: String[] args = MainCallback.getArgs(getWorkDir());
059: assertNotNull("args passed in", args);
060: List<String> a = Arrays.asList(args);
061: if (!a.contains("ahoj")) {
062: fail("Ahoj should be there: " + a);
063: }
064: }
065:
066: public void testStartsArePassedInUnparsed() throws Exception {
067: File wd = new File(getWorkDir(), "currentdir");
068: wd.mkdirs();
069: File f1 = new File(wd, "f1");
070: File f2 = new File(wd, "f2");
071: File f3 = new File(wd, "f3");
072: f1.createNewFile();
073: f2.createNewFile();
074: f3.createNewFile();
075:
076: String str = "1 * * * *";
077: run(wd, str);
078:
079: String[] args = MainCallback.getArgs(getWorkDir());
080: assertNotNull("args passed in", args);
081: List<String> a = Arrays.asList(args);
082: if (!a.contains(str)) {
083: fail(str + " should be there: " + a);
084: }
085: }
086:
087: private void run(File workDir, String... args) throws Exception {
088: URL u = Lookup.class.getProtectionDomain().getCodeSource()
089: .getLocation();
090: File f = new File(u.toURI());
091: assertTrue("file found: " + f, f.exists());
092: File nbexec = Utilities.isWindows() ? new File(f.getParent(),
093: "nbexec.exe") : new File(f.getParent(), "nbexec");
094: assertTrue("nbexec found: " + nbexec, nbexec.exists());
095:
096: URL tu = MainCallback.class.getProtectionDomain()
097: .getCodeSource().getLocation();
098: File testf = new File(tu.toURI());
099: assertTrue("file found: " + testf, testf.exists());
100:
101: LinkedList<String> allArgs = new LinkedList<String>(Arrays
102: .asList(args));
103: allArgs.addFirst("-J-Dnetbeans.mainclass="
104: + MainCallback.class.getName());
105: allArgs.addFirst(System.getProperty("java.home"));
106: allArgs.addFirst("--jdkhome");
107: allArgs.addFirst(getWorkDirPath());
108: allArgs.addFirst("--userdir");
109: allArgs.addFirst(testf.getPath());
110: allArgs.addFirst("-cp:p");
111:
112: if (!Utilities.isWindows()) {
113: allArgs.addFirst(nbexec.getPath());
114: allArgs.addFirst("-x");
115: allArgs.addFirst("/bin/sh");
116: } else {
117: allArgs.addFirst(nbexec.getPath());
118: }
119:
120: StringBuffer sb = new StringBuffer();
121: Process p = Runtime.getRuntime().exec(
122: allArgs.toArray(new String[0]), new String[0], workDir);
123: int res = readOutput(sb, p);
124:
125: String output = sb.toString();
126:
127: assertEquals("Execution is ok: " + output, 0, res);
128: }
129:
130: private static int readOutput(final StringBuffer sb, Process p)
131: throws Exception {
132: class Read extends Thread {
133: private InputStream is;
134:
135: public Read(String name, InputStream is) {
136: super (name);
137: this .is = is;
138: setDaemon(true);
139: }
140:
141: @Override
142: public void run() {
143: byte[] arr = new byte[4096];
144: try {
145: for (;;) {
146: int len = is.read(arr);
147: if (len == -1) {
148: return;
149: }
150: sb.append(new String(arr, 0, len));
151: }
152: } catch (IOException ex) {
153: ex.printStackTrace();
154: }
155: }
156: }
157:
158: Read out = new Read("out", p.getInputStream());
159: Read err = new Read("err", p.getErrorStream());
160: out.start();
161: err.start();
162:
163: int res = p.waitFor();
164:
165: out.interrupt();
166: err.interrupt();
167: out.join();
168: err.join();
169:
170: return res;
171: }
172:
173: }
|