1 19 20 package org.netbeans.core.output2; 21 22 import java.awt.BorderLayout ; 23 import java.io.File ; 24 import javax.swing.JFrame ; 25 import javax.swing.SwingUtilities ; 26 import junit.framework.TestCase; 27 28 32 public class LifecycleTest extends TestCase { 33 34 public LifecycleTest(String testName) { 35 super(testName); 36 } 37 38 private OutputWindow win; 39 private NbIO io; 40 private OutWriter out = null; 41 JFrame jf = null; 42 43 OutputTab tab = null; 44 OutputPane pane = null; 45 protected void setUp() throws java.lang.Exception { 46 49 jf = new JFrame (); 50 win = new OutputWindow(); 51 OutputWindow.DEFAULT = win; 52 jf.getContentPane().setLayout (new BorderLayout ()); 53 jf.getContentPane().add (win, BorderLayout.CENTER); 54 jf.setBounds (20, 20, 700, 300); 55 io = (NbIO) new NbIOProvider().getIO ("Test", false); 56 SwingUtilities.invokeAndWait (new Shower()); 57 io.select(); 58 sleep(); 59 sleep(); 60 tab = (OutputTab) win.getSelectedTab(); 61 if (tab == null) { 62 fail ("Failed in setup - selected tab was null"); 63 } 64 pane = (OutputPane) tab.getOutputPane(); 65 sleep(); 66 } 67 68 protected void tearDown() { 69 tab = null; 70 pane = null; 71 out = null; 72 if (jf != null) { 73 jf.dispose(); 74 } 75 jf = null; 76 if (io != null) { 77 NbIOProvider.dispose(io); 78 } 79 io = null; 80 win = null; 81 OutputWindow.DEFAULT = null; 82 sleep(); 83 } 84 85 private final void sleep() { 86 dosleep(); 87 dosleep(); 88 dosleep(); 89 int ct = 0; 90 while (IOEvent.pendingCount > 0) { 91 dosleep(); 92 ct++; 93 if (ct > 1000) { 94 fail ("After 1000 cycles on the event queue, there is still some IOEvent which was not processed"); 95 } 96 } 97 } 98 99 private final void dosleep() { 100 try { 101 Thread.currentThread().sleep(200); 102 SwingUtilities.invokeAndWait (new Runnable () { 103 public void run() { 104 System.currentTimeMillis(); 105 } 106 }); 107 Thread.currentThread().sleep(200); 108 } catch (Exception e) { 109 fail (e.getMessage()); 110 } 111 } 112 113 public class Shower implements Runnable { 114 public void run() { 115 jf.setVisible(true); 116 } 117 } 118 119 public void testGetErr() throws Exception { 120 System.out.println("testGetOut"); 121 ErrWriter err = io.writer().err(); 122 assertNull ("Error output should not be created yet", err); 123 err = (ErrWriter) io.writer().getErr(); 124 assertNotNull ("Error output should never be null from getErr()", err); 125 assertTrue ("Error output should initially be closed", err.isClosed()); 126 err.println ("Hello"); 127 assertFalse ("Error output should not be closed after writing to it", err.isClosed()); 128 err.close(); 129 assertTrue ("Error output be closed after calling close()", err.isClosed()); 130 } 131 132 public void testClose() throws Exception { 133 System.out.println("testClose"); 134 NbWriter writer = (NbWriter) io.getOut(); 135 ErrWriter err = (ErrWriter) writer.getErr(); 136 OutWriter out = (OutWriter) writer.out(); 137 138 writer.reset(); 139 sleep(); 140 141 err.println ("hello"); 142 sleep(); 143 writer.println ("world"); 144 sleep(); 145 146 149 assertFalse ("Err should not be closed", err.isClosed()); 150 assertFalse ("Writer should not be closed", writer.isClosed()); 151 153 err.close(); 154 sleep(); 155 assertFalse ("Out is open, err is closed, writer should return false from isClosed()", writer.isClosed()); 156 157 writer.close(); 158 sleep(); 159 assertTrue ("Out should be closed after calling close() on it", out.isClosed()); 160 assertTrue ("Out and err are closed, but writer says it is not", writer.isClosed()); 161 162 assertTrue ("Output's storage is not closed", writer.out().getStorage().isClosed()); 163 164 writer.reset(); 165 sleep(); 166 167 assertTrue ("After reset, err should be closed", err.isClosed()); 168 assertTrue ("After reset, writer should be closed", writer.isClosed()); 169 assertTrue ("After reset, out should be closed", out.isClosed()); 170 171 err.println ("goodbye"); 172 writer.println ("world"); 173 sleep(); 174 175 assertFalse ("Err should not be closed", err.isClosed()); 176 assertFalse ("Writer should not be closed", writer.isClosed()); 177 179 writer.close(); 181 sleep(); 182 assertFalse ("Out should not be closed after calling close() on it but not the error out", writer.isClosed()); 183 assertFalse (writer.isClosed()); 184 185 err.close(); 186 sleep(); 187 assertTrue ("Out is closed, err is closed, writer should return true from isClosed()", writer.isClosed()); 188 assertTrue ("Out and err are closed, but writer says it is not", writer.isClosed()); 189 190 assertTrue ("Output's storage is not closed", writer.out().getStorage().isClosed()); 191 192 err.println("I should be reopened now"); 193 sleep(); 194 195 assertFalse ("Err should be open", err.isClosed()); 196 } 197 198 public void testReset() throws Exception { 199 System.out.println("testReset"); 200 ErrWriter err = (ErrWriter) io.writer().getErr(); 201 OutWriter out = (OutWriter) io.writer().out(); 202 NbWriter writer = io.writer(); 203 204 OutputDocument doc = (OutputDocument) pane.getDocument(); 205 assertNotNull ("Document should not be null", doc); 206 207 err.println ("hello"); 208 writer.println ("world"); 209 sleep(); 210 writer.reset(); 211 sleep(); 212 213 assertTrue ("Same writer object should be used after a reset", io.writer() == writer); 214 assertTrue ("Same err object should be used after a reset", io.writer().err() == err); 215 assertTrue ("Different output should be used afer a reset", out != io.writer().out()); 216 217 assertNull ("Old document's Lines object not disposed - that means neither was its writer", doc.getLines()); 218 219 Exception e = null; 220 try { 221 out.getStorage(); 222 } catch (Exception exc) { 223 e = exc; 224 } 225 assertNotNull ("OutWriter should have thrown an exception on trying to " + 226 "fetch its storage after it was disposed. It appears it wasn't disposed.", e); 227 } 228 229 public void testCloseInputOutput() throws Exception { 230 231 System.out.println("testCloseInputOutput"); 232 ErrWriter err = (ErrWriter) io.writer().getErr(); 233 OutWriter out = (OutWriter) io.writer().out(); 234 NbWriter writer = io.writer(); 235 236 err.println ("joy to the world"); 237 writer.println ("all the boys and girls"); 238 err.close(); 239 sleep(); 240 writer.close(); 241 sleep(); 242 243 io.closeInputOutput(); 244 sleep(); 245 246 assertNull ("Should be no selected tab after closeInputOutput", win.getSelectedTab()); 247 } 248 249 public void testFilesCleanedUp() throws Exception { 250 System.out.println("testFilesCleanedUp"); 251 NbWriter writer = io.writer(); 252 ErrWriter err = (ErrWriter) writer.getErr(); 253 OutWriter out = (OutWriter) writer.out(); 254 255 err.println ("hello"); 256 writer.println ("world"); 257 sleep(); 258 259 assertTrue ("Output should not have changed - was " + out + " now " + io.writer().out(), io.writer().out() == out); 260 FileMapStorage storage = (FileMapStorage) writer.out().getStorage(); 261 String fname = storage.toString(); 262 assertTrue ("FileMapStorage should be returning a file name", fname.indexOf("[") == -1); 263 assertTrue ("FileMapStorage should be pointing to an existing file", new File (fname).exists()); 264 265 err.close(); 266 sleep(); 267 writer.close(); 268 sleep(); 269 io.closeInputOutput(); 270 sleep(); 271 272 assertTrue (out.isDisposed()); 273 sleep(); 274 } 276 277 public void testMultipleResetsAreHarmless() throws Exception { 278 System.out.println("testMultipleResetsAreHarmless"); 279 NbWriter writer = io.writer(); 280 ErrWriter err = (ErrWriter) writer.getErr(); 281 OutWriter out = (OutWriter) writer.out(); 282 283 assertTrue ("Before any writes, out should be empty", out.isEmpty()); 284 285 writer.reset(); 286 sleep(); 287 assertTrue ("Reset on an unused writer should not replace its output", writer.out() == out); 288 289 writer.reset(); 290 writer.reset(); 291 writer.reset(); 292 sleep(); 293 assertTrue ("Reset on an unused writer should not replace its output", writer.out() == out); 294 295 writer.println ("Now there is data"); 296 writer.reset(); 297 sleep(); 298 299 assertFalse ("Reset on a used writer should replace its underlying output", writer.out() == out); 300 301 } 302 303 } 304 | Popular Tags |