KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > output2 > LifecycleTest


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
20 package org.netbeans.core.output2;
21
22 import java.awt.BorderLayout JavaDoc;
23 import java.io.File JavaDoc;
24 import javax.swing.JFrame JavaDoc;
25 import javax.swing.SwingUtilities JavaDoc;
26 import junit.framework.TestCase;
27
28 /**
29  *
30  * @author tim
31  */

32 public class LifecycleTest extends TestCase {
33
34     public LifecycleTest(String JavaDoc testName) {
35         super(testName);
36     }
37
38     private OutputWindow win;
39     private NbIO io;
40     private OutWriter out = null;
41     JFrame JavaDoc jf = null;
42
43     OutputTab tab = null;
44     OutputPane pane = null;
45     protected void setUp() throws java.lang.Exception JavaDoc {
46 // Controller.logStdOut = true;
47
// Controller.log = true;
48

49         jf = new JFrame JavaDoc();
50         win = new OutputWindow();
51         OutputWindow.DEFAULT = win;
52         jf.getContentPane().setLayout (new BorderLayout JavaDoc());
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 JavaDoc() {
103                 public void run() {
104                     System.currentTimeMillis();
105                 }
106             });
107             Thread.currentThread().sleep(200);
108         } catch (Exception JavaDoc e) {
109             fail (e.getMessage());
110         }
111     }
112     
113     public class Shower implements Runnable JavaDoc {
114         public void run() {
115             jf.setVisible(true);
116         }
117     }
118     
119     public void testGetErr() throws Exception JavaDoc {
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 JavaDoc {
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 // assertTrue("Text in container not correct:\"" + pane.getTextView().getText() +"\"",
147
// pane.getTextView().getText().equals ("hello\nworld\n\n\n"));
148

149         assertFalse ("Err should not be closed", err.isClosed());
150         assertFalse ("Writer should not be closed", writer.isClosed());
151 // assertFalse ("Out should not be closed", out.isClosed());
152

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 // assertFalse ("Out should not be closed", out.isClosed());
178

179         //Now close err & out in the opposite order
180
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 JavaDoc {
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 JavaDoc e = null;
220         try {
221             out.getStorage();
222         } catch (Exception JavaDoc 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 JavaDoc {
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 JavaDoc {
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 JavaDoc 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 JavaDoc(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 // assertFalse ("FileMapStorage's file should have been deleted", new File(fname).exists());
275
}
276     
277     public void testMultipleResetsAreHarmless() throws Exception JavaDoc {
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