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: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041: package org.netbeans;
042:
043: import java.io.File;
044: import java.io.IOException;
045: import java.io.InputStream;
046: import java.io.OutputStream;
047: import java.util.ArrayList;
048: import java.util.Arrays;
049: import java.util.Collection;
050: import java.util.Map;
051: import java.util.Set;
052: import java.util.logging.Level;
053: import java.util.logging.Logger;
054: import org.fakepkg.FakeHandler;
055: import org.netbeans.junit.NbTestCase;
056:
057: /** Tests that handler can set netbeans.mainclass property in its constructor.
058: *
059: * @author Jaroslav Tulach
060: */
061: public class CLIWhatHappensWhenSecondVMExistsTest extends NbTestCase
062: implements Map {
063: private boolean called;
064: private Exception e;
065: private int howMuchOut;
066: private boolean open;
067: static Logger LOG;
068:
069: public CLIWhatHappensWhenSecondVMExistsTest(String testName) {
070: super (testName);
071: }
072:
073: protected Level logLevel() {
074: return Level.FINEST;
075: }
076:
077: protected void setUp() throws Exception {
078: clearWorkDir();
079: LOG = Logger.getLogger("TEST." + getName());
080: called = false;
081:
082: System.setProperty("netbeans.mainclass",
083: CLIWhatHappensWhenSecondVMExistsTest.class.getName());
084:
085: FakeHandler.chained = this ;
086: }
087:
088: public static void main(String[] args) {
089: // ok, ready to work
090: LOG.info("We are in main, finishInitialization now");
091: CLIHandler.finishInitialization(false);
092: LOG.info("finishInitialization done");
093: }
094:
095: private int cli(CLIHandler.Args a) {
096: called = true;
097:
098: String[] args = a.getArguments();
099: LOG.info(" cli: args: " + Arrays.asList(args));
100:
101: boolean yes = false;
102: for (int i = 0; i < args.length; i++) {
103: if ("--userdir".equals(args[i])) {
104: args[i] = null;
105: System.setProperty("netbeans.user", args[i + 1]);
106: args[i + 1] = null;
107: }
108: if ("--generate".equals(args[i])) {
109: yes = true;
110: args[i] = null;
111: }
112: }
113:
114: LOG.info(" yes: " + yes);
115: if (yes) {
116: this .open = a.isOpen();
117: assertTrue("We are open at begining", this .open);
118: try {
119: OutputStream os = a.getOutputStream();
120: os.write("123\n".getBytes());
121: LOG.info("send 123 to the output stream");
122: for (howMuchOut = 0; howMuchOut < 1000; howMuchOut++) {
123: try {
124: Thread.sleep(10);
125: } catch (InterruptedException ex) {
126: this .e = ex;
127: }
128: LOG.info(" howMuchOut " + howMuchOut);
129:
130: if (!a.isOpen()) {
131: LOG.info("a is closed, break");
132: break;
133: }
134: }
135:
136: } catch (IOException ex) {
137: this .e = ex;
138: LOG.log(Level.WARNING, "Exception while writing", ex);
139: } finally {
140: synchronized (this ) {
141: this .open = a.isOpen();
142: notifyAll();
143: }
144: LOG
145: .info("open assigned " + this .open
146: + " all notified");
147: }
148: }
149:
150: LOG.info("Exit cli");
151: return 0;
152: }
153:
154: public void testGet1000AndExit() throws Exception {
155: LOG.info("testGet1000AndExit starts");
156: org.netbeans.MainImpl.main(new String[] { "--userdir",
157: getWorkDirPath() });
158: LOG.log(Level.INFO, "main finished with userdir {0}",
159: getWorkDirPath());
160: assertEquals("Called", true, called);
161:
162: called = false;
163: Process p = exec(new String[] { "--userdir", getWorkDirPath(),
164: "--generate" });
165:
166: byte[] arr = new byte[4];
167: int offset = 0;
168: int time = 10;
169: InputStream is = p.getInputStream();
170: while (offset < 4 && time-- > 0) {
171: int l = arr.length - offset;
172: LOG.log(Level.FINEST, "Reading at {0} length {1}",
173: new Object[] { offset, l });
174: int read = is.read(arr, offset, l);
175: LOG.log(Level.FINEST, "Read {0} bytes", read);
176: if (read == -1) {
177: LOG
178: .log(Level.WARNING,
179: "this is not good, why there is no data in the stream?"); // NOI18N
180: Thread.sleep(1000);
181: continue;
182: }
183: offset += read;
184: }
185: assertEquals("Offset is 4", 4, offset);
186:
187: String s = new String(arr);
188: assertEquals("123\n", s);
189:
190: assertEquals("Our main method called once more", true, called);
191:
192: try {
193: int r = p.exitValue();
194: fail("We should be still running: " + r);
195: } catch (IllegalThreadStateException ex) {
196: // ok
197: }
198: // destroy the
199: p.destroy();
200:
201: // wait for it to be killed
202: int result = p.waitFor();
203:
204: synchronized (this ) {
205: int cnt = 10;
206: while (this .open && cnt-- > 0) {
207: this .wait(1000);
208: }
209: }
210: if (this .open) {
211: fail("We should not be open: " + howMuchOut + " open: "
212: + this .open);
213: }
214: if (e instanceof InterruptedException) {
215: // ok
216: e = null;
217: }
218:
219: if (e != null) {
220: throw e;
221: }
222: }
223:
224: private static Process exec(String[] args) throws IOException {
225: String s = System.getProperty("java.home");
226: assertNotNull(s);
227: String cp = System.getProperty("java.class.path");
228: assertNotNull(cp);
229:
230: ArrayList<String> l = new ArrayList<String>();
231: l.add(s + File.separator + "bin" + File.separator + "java");
232: l.add("-cp");
233: l.add(cp);
234: l.add("org.netbeans.Main");
235: l.addAll(Arrays.asList(args));
236:
237: // System.err.println("exec: " + l);
238:
239: args = l.toArray(args);
240:
241: return Runtime.getRuntime().exec(args);
242: }
243:
244: //
245: // To allow callback from FakeHandler
246: //
247:
248: public int size() {
249: fail("Not implemented");
250: return 0;
251: }
252:
253: public boolean isEmpty() {
254: fail("Not implemented");
255: return true;
256: }
257:
258: public boolean containsKey(Object key) {
259: fail("Not implemented");
260: return true;
261: }
262:
263: public boolean containsValue(Object value) {
264: fail("Not implemented");
265: return true;
266: }
267:
268: public Object get(Object key) {
269: CLIHandler.Args a = (CLIHandler.Args) key;
270: return new Integer(cli(a));
271: }
272:
273: public Object put(Object key, Object value) {
274: fail("Not implemented");
275: return null;
276: }
277:
278: public Object remove(Object key) {
279: fail("Not implemented");
280: return null;
281: }
282:
283: public void putAll(Map t) {
284: fail("Not implemented");
285: }
286:
287: public void clear() {
288: fail("Not implemented");
289: }
290:
291: public Set keySet() {
292: fail("Not implemented");
293: return null;
294: }
295:
296: public Collection values() {
297: fail("Not implemented");
298: return null;
299: }
300:
301: public Set entrySet() {
302: fail("Not implemented");
303: return null;
304: }
305: }
|