1 19 package org.netbeans; 20 21 import java.io.File ; 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.io.OutputStream ; 25 import java.util.ArrayList ; 26 import java.util.Arrays ; 27 import java.util.Collection ; 28 import java.util.Map ; 29 import java.util.Set ; 30 import java.util.logging.Level ; 31 import java.util.logging.Logger ; 32 import org.fakepkg.FakeHandler; 33 import org.netbeans.junit.NbTestCase; 34 35 39 public class CLIWhatHappensWhenSecondVMExistsTest extends NbTestCase 40 implements Map { 41 private boolean called; 42 private Exception e; 43 private int howMuchOut; 44 private boolean open; 45 static Logger LOG; 46 47 public CLIWhatHappensWhenSecondVMExistsTest(String testName) { 48 super(testName); 49 } 50 51 protected Level logLevel() { 52 return Level.FINEST; 53 } 54 55 protected void setUp() throws Exception { 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 [] args) { 66 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 [] 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 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 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 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 { 132 LOG.info("testGet1000AndExit starts"); 133 org.netbeans.MainImpl.main(new String [] { "--userdir", getWorkDirPath() }); 134 LOG.log(Level.INFO, "main finished with userdir {0}", getWorkDirPath()); 135 assertEquals("Called", true, called); 136 137 called = false; 138 Process p = exec(new String [] { "--userdir", getWorkDirPath(), "--generate" }); 139 140 byte[] arr = new byte[4]; 141 int offset = 0; 142 int time = 10; 143 InputStream 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 [] { 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?"); Thread.sleep(1000); 152 continue; 153 } 154 offset += read; 155 } 156 assertEquals("Ofset is 4", 4, offset); 157 158 String s = new String (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 ex) { 167 } 169 p.destroy(); 171 172 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 ) { 185 e = null; 187 } 188 189 190 if (e != null) { 191 throw e; 192 } 193 } 194 195 private static Process exec(String [] args) throws IOException { 196 String s = System.getProperty("java.home"); 197 assertNotNull(s); 198 String cp = System.getProperty("java.class.path"); 199 assertNotNull(cp); 200 201 ArrayList <String > l = new ArrayList <String >(); 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 210 args = l.toArray(args); 211 212 return Runtime.getRuntime().exec(args); 213 } 214 215 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 key) { 230 fail("Not implemented"); 231 return true; 232 } 233 234 public boolean containsValue(Object value) { 235 fail("Not implemented"); 236 return true; 237 } 238 239 public Object get(Object key) { 240 CLIHandler.Args a = (CLIHandler.Args)key; 241 return new Integer (cli(a)); 242 } 243 244 public Object put(Object key, Object value) { 245 fail("Not implemented"); 246 return null; 247 } 248 249 public Object remove(Object key) { 250 fail("Not implemented"); 251 return null; 252 } 253 254 public void putAll(Map t) { 255 fail("Not implemented"); 256 } 257 258 public void clear() { 259 fail("Not implemented"); 260 } 261 262 public Set keySet() { 263 fail("Not implemented"); 264 return null; 265 } 266 267 public Collection values() { 268 fail("Not implemented"); 269 return null; 270 } 271 272 public Set entrySet() { 273 fail("Not implemented"); 274 return null; 275 } 276 } 277 | Popular Tags |