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.upgrade.launcher;
029:
030: import java.io.File;
031: import java.io.IOException;
032: import java.io.InputStream;
033: import java.io.RandomAccessFile;
034: import java.net.URL;
035: import java.nio.channels.FileChannel;
036: import java.util.Arrays;
037: import java.util.LinkedList;
038: import java.util.List;
039: import org.netbeans.junit.NbTestCase;
040: import org.openide.util.Lookup;
041: import org.openide.util.Utilities;
042:
043: /**
044: *
045: * @author Jaroslav Tulach, Tomas Hurka
046: */
047: public class PlatformWithAbsolutePathTest extends NbTestCase {
048: public PlatformWithAbsolutePathTest(String name) {
049: super (name);
050: }
051:
052: @Override
053: protected void setUp() throws Exception {
054: clearWorkDir();
055: }
056:
057: public void testPlatformWithAbsolutePath() throws Exception {
058: File wd = new File(getWorkDir(), "currentdir");
059: wd.mkdirs();
060: URL u = Lookup.class.getProtectionDomain().getCodeSource()
061: .getLocation();
062: File utilFile = new File(u.toURI());
063: assertTrue("file found: " + utilFile, utilFile.exists());
064: File root = utilFile.getParentFile().getParentFile()
065: .getParentFile();
066: File bin = new File(root, "bin");
067: File newBin = new File(wd, "bin");
068: newBin.mkdirs();
069: File newEtc = new File(wd, "etc");
070: newEtc.mkdirs();
071: File[] binFiles = bin.listFiles();
072: for (File f : binFiles) {
073: File newFile = new File(newBin, f.getName());
074: FileChannel newChannel = new RandomAccessFile(newFile, "rw")
075: .getChannel();
076: new RandomAccessFile(f, "r").getChannel().transferTo(0,
077: f.length(), newChannel);
078: newChannel.close();
079: }
080: RandomAccessFile netbeansCluster = new RandomAccessFile(
081: new File(newEtc, "netbeans.clusters"), "rw");
082: netbeansCluster.writeBytes(utilFile.getParentFile().getParent()
083: + "\n");
084: netbeansCluster.close();
085: String str = "1 * * * *";
086: run(wd, str);
087:
088: String[] args = MainCallback.getArgs(getWorkDir());
089: assertNotNull("args passed in", args);
090: List<String> a = Arrays.asList(args);
091: if (!a.contains(str)) {
092: fail(str + " should be there: " + a);
093: }
094: }
095:
096: private void run(File workDir, String... args) throws Exception {
097: File bin = new File(workDir, "bin");
098: File nbexec = Utilities.isWindows() ? new File(bin,
099: "netbeans.exe") : new File(bin, "netbeans");
100: assertTrue("nbexec found: " + nbexec, nbexec.exists());
101:
102: URL tu = MainCallback.class.getProtectionDomain()
103: .getCodeSource().getLocation();
104: File testf = new File(tu.toURI());
105: assertTrue("file found: " + testf, testf.exists());
106:
107: LinkedList<String> allArgs = new LinkedList<String>(Arrays
108: .asList(args));
109: allArgs.addFirst("-J-Dnetbeans.mainclass="
110: + MainCallback.class.getName());
111: allArgs.addFirst(System.getProperty("java.home"));
112: allArgs.addFirst("--jdkhome");
113: allArgs.addFirst(getWorkDirPath());
114: allArgs.addFirst("--userdir");
115: allArgs.addFirst(testf.getPath());
116: allArgs.addFirst("-cp:p");
117:
118: if (!Utilities.isWindows()) {
119: allArgs.addFirst(nbexec.getPath());
120: allArgs.addFirst("-x");
121: allArgs.addFirst("/bin/sh");
122: } else {
123: allArgs.addFirst(nbexec.getPath());
124: }
125:
126: StringBuffer sb = new StringBuffer();
127: Process p = Runtime.getRuntime().exec(
128: allArgs.toArray(new String[0]), new String[0], workDir);
129: int res = readOutput(sb, p);
130:
131: String output = sb.toString();
132:
133: assertEquals("Execution is ok: " + output, 0, res);
134: }
135:
136: private static int readOutput(final StringBuffer sb, Process p)
137: throws Exception {
138: class Read extends Thread {
139: private InputStream is;
140:
141: public Read(String name, InputStream is) {
142: super (name);
143: this .is = is;
144: setDaemon(true);
145: }
146:
147: @Override
148: public void run() {
149: byte[] arr = new byte[4096];
150: try {
151: for (;;) {
152: int len = is.read(arr);
153: if (len == -1) {
154: return;
155: }
156: sb.append(new String(arr, 0, len));
157: }
158: } catch (IOException ex) {
159: ex.printStackTrace();
160: }
161: }
162: }
163:
164: Read out = new Read("out", p.getInputStream());
165: Read err = new Read("err", p.getErrorStream());
166: out.start();
167: err.start();
168:
169: int res = p.waitFor();
170:
171: out.interrupt();
172: err.interrupt();
173: out.join();
174: err.join();
175:
176: return res;
177: }
178:
179: }
|