KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > CLIWhatHappensWhenSecondVMExistsTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans;
20
21 import java.io.File JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Arrays JavaDoc;
27 import java.util.Collection JavaDoc;
28 import java.util.Map JavaDoc;
29 import java.util.Set JavaDoc;
30 import java.util.logging.Level JavaDoc;
31 import java.util.logging.Logger JavaDoc;
32 import org.fakepkg.FakeHandler;
33 import org.netbeans.junit.NbTestCase;
34
35 /** Tests that handler can set netbeans.mainclass property in its constructor.
36  *
37  * @author Jaroslav Tulach
38  */

39 public class CLIWhatHappensWhenSecondVMExistsTest extends NbTestCase
40 implements Map JavaDoc {
41     private boolean called;
42     private Exception JavaDoc e;
43     private int howMuchOut;
44     private boolean open;
45     static Logger JavaDoc LOG;
46     
47     public CLIWhatHappensWhenSecondVMExistsTest(String JavaDoc testName) {
48         super(testName);
49     }
50     
51     protected Level JavaDoc logLevel() {
52         return Level.FINEST;
53     }
54
55     protected void setUp() throws Exception JavaDoc {
56         clearWorkDir();
57         LOG = Logger.getLogger("TEST." + getName());
58         called = false;
59         
60         System.setProperty("netbeans.mainclass", CLIWhatHappensWhenSecondVMExistsTest.class.getName());
61         
62         FakeHandler.chained = this;
63     }
64
65     public static void main(String JavaDoc[] args) {
66         // ok, ready to work
67
LOG.info("We are in main, finishInitialization now");
68         CLIHandler.finishInitialization(false);
69         LOG.info("finishInitialization done");
70     }
71     
72     private int cli(CLIHandler.Args a) {
73         called = true;
74         
75         
76         String JavaDoc[] args = a.getArguments();
77         LOG.info(" cli: args: " + Arrays.asList(args));
78         
79         boolean yes = false;
80         for (int i = 0; i < args.length; i++) {
81             if ("--userdir".equals(args[i])) {
82                 args[i] = null;
83                 System.setProperty("netbeans.user", args[i + 1]);
84                 args[i + 1] = null;
85             }
86             if ("--generate".equals(args[i])) {
87                 yes = true;
88                 args[i] = null;
89             }
90         }
91         
92         LOG.info(" yes: " + yes);
93         if (yes) {
94             this.open = a.isOpen();
95             assertTrue("We are open at begining", this.open);
96             try {
97                 OutputStream JavaDoc os = a.getOutputStream();
98                 os.write("123\n".getBytes());
99                 LOG.info("send 123 to the output stream");
100                 for (howMuchOut = 0; howMuchOut < 1000; howMuchOut++) {
101                     try {
102                         Thread.sleep(10);
103                     } catch (InterruptedException JavaDoc ex) {
104                         this.e = ex;
105                     }
106                     LOG.info(" howMuchOut " + howMuchOut);
107                     
108                     if (!a.isOpen()) {
109                         LOG.info("a is closed, break");
110                         break;
111                     }
112                 }
113                 
114             } catch (IOException JavaDoc ex) {
115                 this.e = ex;
116                 LOG.log(Level.WARNING, "Exception while writing", ex);
117             } finally {
118                 synchronized (this) {
119                     this.open = a.isOpen();
120                     notifyAll();
121                 }
122                 LOG.info("open assigned " + this.open + " all notified");
123             }
124         }
125         
126         LOG.info("Exit cli");
127         return 0;
128     }
129     
130
131     public void testGet1000AndExit() throws Exception JavaDoc {
132         LOG.info("testGet1000AndExit starts");
133         org.netbeans.MainImpl.main(new String JavaDoc[] { "--userdir", getWorkDirPath() });
134         LOG.log(Level.INFO, "main finished with userdir {0}", getWorkDirPath());
135         assertEquals("Called", true, called);
136
137         called = false;
138         Process JavaDoc p = exec(new String JavaDoc[] { "--userdir", getWorkDirPath(), "--generate" });
139         
140         byte[] arr = new byte[4];
141         int offset = 0;
142         int time = 10;
143         InputStream JavaDoc is = p.getInputStream();
144         while(offset < 4 && time-- > 0) {
145             int l = arr.length - offset;
146             LOG.log(Level.FINEST, "Reading at {0} length {1}", new Object JavaDoc[] { offset, l });
147             int read = is.read(arr, offset, l);
148             LOG.log(Level.FINEST, "Read {0} bytes", read);
149             if (read == -1) {
150                 LOG.log(Level.WARNING, "this is not good, why there is no data in the stream?"); // NOI18N
151
Thread.sleep(1000);
152                 continue;
153             }
154             offset += read;
155         }
156         assertEquals("Ofset is 4", 4, offset);
157         
158         String JavaDoc s = new String JavaDoc(arr);
159         assertEquals("123\n", s);
160         
161         assertEquals("Our main method called once more", true, called);
162
163         try {
164             int r = p.exitValue();
165             fail("We should be still running: " + r);
166         } catch (IllegalThreadStateException JavaDoc ex) {
167             // ok
168
}
169         // destroy the
170
p.destroy();
171
172         // wait for it to be killed
173
int result = p.waitFor();
174         
175         synchronized (this) {
176             int cnt = 10;
177             while (this.open && cnt-- > 0) {
178                 this.wait(1000);
179             }
180         }
181         if (this.open) {
182             fail("We should not be open: " + howMuchOut + " open: " + this.open);
183         }
184         if (e instanceof InterruptedException JavaDoc) {
185             // ok
186
e = null;
187         }
188
189         
190         if (e != null) {
191             throw e;
192         }
193     }
194     
195     private static Process JavaDoc exec(String JavaDoc[] args) throws IOException JavaDoc {
196         String JavaDoc s = System.getProperty("java.home");
197         assertNotNull(s);
198         String JavaDoc cp = System.getProperty("java.class.path");
199         assertNotNull(cp);
200         
201         ArrayList JavaDoc<String JavaDoc> l = new ArrayList JavaDoc<String JavaDoc>();
202         l.add(s + File.separator + "bin" + File.separator + "java");
203         l.add("-cp");
204         l.add(cp);
205         l.add("org.netbeans.Main");
206         l.addAll(Arrays.asList(args));
207         
208 // System.err.println("exec: " + l);
209

210         args = l.toArray(args);
211         
212         return Runtime.getRuntime().exec(args);
213     }
214     
215     //
216
// To allow callback from FakeHandler
217
//
218

219     public int size() {
220         fail("Not implemented");
221         return 0;
222     }
223
224     public boolean isEmpty() {
225         fail("Not implemented");
226         return true;
227     }
228
229     public boolean containsKey(Object JavaDoc key) {
230         fail("Not implemented");
231         return true;
232     }
233
234     public boolean containsValue(Object JavaDoc value) {
235         fail("Not implemented");
236         return true;
237     }
238
239     public Object JavaDoc get(Object JavaDoc key) {
240         CLIHandler.Args a = (CLIHandler.Args)key;
241         return new Integer JavaDoc(cli(a));
242     }
243
244     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
245         fail("Not implemented");
246         return null;
247     }
248
249     public Object JavaDoc remove(Object JavaDoc key) {
250         fail("Not implemented");
251         return null;
252     }
253
254     public void putAll(Map JavaDoc t) {
255         fail("Not implemented");
256     }
257
258     public void clear() {
259         fail("Not implemented");
260     }
261
262     public Set JavaDoc keySet() {
263         fail("Not implemented");
264         return null;
265     }
266
267     public Collection JavaDoc values() {
268         fail("Not implemented");
269         return null;
270     }
271
272     public Set JavaDoc entrySet() {
273         fail("Not implemented");
274         return null;
275     }
276 }
277
Popular Tags