KickJava   Java API By Example, From Geeks To Geeks.

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


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.awt.Point JavaDoc;
24 import java.awt.event.ActionEvent JavaDoc;
25 import javax.swing.JFrame JavaDoc;
26 import javax.swing.SwingUtilities JavaDoc;
27 import junit.framework.TestCase;
28 import org.netbeans.core.output2.ui.AbstractOutputPane;
29 import org.netbeans.core.output2.ui.AbstractOutputTab;
30 import org.openide.windows.OutputEvent;
31 import org.openide.windows.OutputListener;
32
33 /**
34  *
35  * @author Tim Boudreau
36  */

37 public class OutputWindowTest extends TestCase {
38
39     public OutputWindowTest(String JavaDoc testName) {
40         super(testName);
41     }
42     
43     private OutputWindow win;
44     private NbIO io;
45     private OutWriter out = null;
46     JFrame JavaDoc jf = null;
47     NbIOProvider provider = null;
48
49     protected void setUp() throws Exception JavaDoc {
50         jf = new JFrame JavaDoc();
51         win = new OutputWindow();
52         OutputWindow.DEFAULT = win;
53         jf.getContentPane().setLayout (new BorderLayout JavaDoc());
54         jf.getContentPane().add (win, BorderLayout.CENTER);
55         jf.setBounds (20, 20, 700, 300);
56         provider = new NbIOProvider();
57         io = (NbIO) provider.getIO ("Test", false);
58         SwingUtilities.invokeAndWait (new Shower());
59     }
60     
61     private final void sleep() {
62         try {
63             Thread.currentThread().sleep(200);
64             SwingUtilities.invokeAndWait (new Runnable JavaDoc() {
65                 public void run() {
66                     System.currentTimeMillis();
67                 }
68             });
69             Thread.currentThread().sleep(200);
70         } catch (Exception JavaDoc e) {
71             fail (e.getMessage());
72         }
73     }
74     
75     public class Shower implements Runnable JavaDoc {
76         public void run() {
77             jf.setVisible(true);
78         }
79     }
80
81     public void testTabsShownAndHidden() {
82         System.out.println ("testTabsShownAndHidden");
83         int n = win.getTabs().length;
84         assertTrue ("Number of tabs should be 1, not " + n, n == 1);
85         NbIO io2 = (NbIO) provider.getIO("Test2", true);
86         sleep();
87         n = win.getTabs().length;
88         assertTrue ("Number of tabs should be 2, not " + n, n == 2);
89
90         io2.closeInputOutput();
91         sleep();
92
93         n = win.getTabs().length;
94         assertTrue ("After closeInputOutput on second tab, number of tabs should be 1, not " + n, n == 1);
95     }
96     
97     public void testTabNamesUpdatedCorrectly() throws Exception JavaDoc {
98         System.out.println ("testTabNamesUpdatedCorrectly");
99         io.select();
100         for (int i=0; i < 100; i++) {
101             io.getOut().println ("Here is some text we can delete");
102         }
103         sleep();
104         sleep();
105         sleep();
106         sleep();
107         io.getOut().flush();
108         
109         assertTrue ("Tab name should be html but is " + win.getDisplayName(),
110             win.getHtmlDisplayName() != null && win.getHtmlDisplayName().indexOf("<") >= 0);
111
112         io.getOut().close();
113         sleep();
114         sleep();
115         assertFalse ("Tab name should not be html", win.getHtmlDisplayName() != null && win.getHtmlDisplayName().indexOf("<") >= 0);
116         
117         final OutputTab tab = (OutputTab) win.getSelectedTab();
118         
119         //Ensure the actions are paying attention
120
SwingUtilities.invokeLater (new Runnable JavaDoc() {
121             public void run() {
122                 win.getController().postPopupMenu(win, tab, new Point JavaDoc(0,0), tab);
123                 win.getController().clearAction.actionPerformed(new ActionEvent JavaDoc (tab, ActionEvent.ACTION_PERFORMED, "clear"));
124             }
125         });
126         sleep();
127         sleep();
128         assertFalse ("Tab name should not be html", win.getHtmlDisplayName() != null && win.getHtmlDisplayName().indexOf("<") >= 0);
129         
130         io.getOut().reset();
131         sleep();
132         io.getOut().println("And here is some more text");
133         sleep();
134         
135         assertTrue ("Tab name should be html", win.getHtmlDisplayName() != null && win.getHtmlDisplayName().indexOf("<") >= 0);
136
137         io.getOut().close();
138         sleep();
139         sleep();
140         assertFalse ("Tab name should not be html", win.getHtmlDisplayName() != null && win.getHtmlDisplayName().indexOf("<") >= 0);
141         
142     }
143
144 // mkleint: this test is non deterministic, teh sleep() calls are not a guarantee for reproducibility.
145

146 // public void testScrollToEnd() throws Exception {
147
// System.out.println("testScrollToEnd");
148
// NbIO io2 = (NbIO) provider.getIO("ScrollDown", true);
149
// io2.select();
150
// sleep();
151
// AbstractOutputTab tab = win.getSelectedTab();
152
// assertNotNull("Selected tab should not be null", tab);
153
//
154
// AbstractOutputPane pane = tab.getOutputPane();
155
// OutputDocument doc = (OutputDocument) pane.getDocument();
156
//
157
// for (int i = 0; i < 20; i++) {
158
// io2.getOut().println("Hellooo... ");
159
// }
160
// sleep();
161
// int end = doc.getEndPosition().getOffset();
162
// int carretPos = pane.getCaretPos();
163
// assertEquals("Caret position should be at the end (end: " + end
164
// + ", carretPos: " + carretPos + ")",
165
// end, carretPos);
166
// io2.closeInputOutput();
167
// sleep();
168
// }
169

170     public void testAbleToRetrieveSameInputOutputInstance() {
171         System.out.println ("testAbleToRetrieveSameInputOutputInstance");
172         NbIO io2 = (NbIO) provider.getIO("Test2", true);
173         sleep();
174
175         NbIO io3 = (NbIO) provider.getIO("Test", false);
176         assertSame ("Requesting a tab with a name already in use should return the original InputOutput", io3, io);
177
178         NbIO io4 = (NbIO) provider.getIO("Test2", false);
179         assertSame ("Requesting a second tab with a name already in use should return the same InputOutput", io4, io2);
180
181         NbIO io5 = (NbIO) provider.getIO("Test", true);
182         assertNotSame ("Requesting a new InputOutput with a name matching another tab should not return the other tab", io5, io);
183     }
184
185     public void testIOclosed() {
186         System.out.println ("testIOclosed");
187         assertFalse ("If a tab is showing, its InputOutput should not say it is closed", io.isClosed());
188         NbIO io2 = (NbIO) provider.getIO("Test2", true);
189         sleep();
190         assertFalse ("If a tab is showing, its InputOutput should not say it is closed", io2.isClosed());
191
192         assertFalse ("Adding another tab should not make the first tab think it has been closed, its InputOutput should not say it is closed", io.isClosed());
193
194         io2.closeInputOutput();
195         sleep();
196
197         assertTrue ("After programmatically closing a tab, its InputOutput should return true from isClosed()", io2.isClosed());
198
199         io2 = (NbIO) provider.getIO("Test3", true);
200         sleep();
201
202         NbWriter writer = (NbWriter) io.getOut();
203         writer.println ("Hello world");
204         sleep();
205         OutWriter outwriter = writer.out();
206
207         assertFalse ("After removing and adding another tab, the last tab added should not say it has been closed", io2.isClosed());
208         io.select();
209         sleep();
210
211         final OutputTab tab = (OutputTab) win.getSelectedTab();
212         assertSame ("After calling InputOutput.select(), a tab owing the IO it was requested on should be the selected tab", tab.getIO(), io);
213
214         //Ensure the actions are paying attention
215
SwingUtilities.invokeLater (new Runnable JavaDoc() {
216             public void run() {
217                 win.getController().postPopupMenu(win, tab, new Point JavaDoc(0,0), tab);
218                 win.getController().clearAction.actionPerformed(new ActionEvent JavaDoc (tab, ActionEvent.ACTION_PERFORMED, "clear"));
219             }
220         });
221         sleep();
222         sleep();
223
224         io.getOut().println ("Goodbye world");
225
226         assertSame ("OutputWriter should not be replaced by calling clearing/reset() on it", writer, io.getOut());
227         assertNotSame ("Underlying OutWriter should be replaced by calling reset() on a used NbWriter", outwriter, writer.out());
228     }
229
230
231     public void testListenersCleared() throws Exception JavaDoc {
232 // Controller.log = true;
233
// Controller.logStdOut = true;
234

235         System.out.println ("testListenersCleared");
236         io.select();
237         io.getOut().println ("Helloooooo....");
238         sleep();
239         L[] ls = new L[20];
240         for (int i=0; i < ls.length; i++) {
241             L l = new L();
242             io.getOut().println ("Hyperlink " + i, l);
243             ls[i] = l;
244         }
245         sleep();
246
247         for (int i=0; i < ls.length; i++) {
248             ls[i].assertNotCleared("Newly written listeners should be cleared");
249         }
250
251         sleep();
252         
253         final OutputTab tab = (OutputTab) win.getSelectedTab();
254         assertNotNull ("Selected tab should not be null", tab);
255         assertSame ("After calling InputOutput.select(), a tab owing the IO it was requested on should be the selected tab", tab.getIO(), io);
256
257         //Ensure the actions are paying attention
258
SwingUtilities.invokeLater (new Runnable JavaDoc() {
259             public void run() {
260                 win.getController().postPopupMenu(win, tab, new Point JavaDoc(0,0), tab);
261                 win.getController().clearAction.actionPerformed(new ActionEvent JavaDoc (tab, ActionEvent.ACTION_PERFORMED, "clear"));
262             }
263         });
264         sleep();
265         sleep();
266
267         for (int i=0; i < ls.length; i++) {
268             ls[i].assertCleared("After invoking the GUI's clear action, listeners should be cleared");
269         }
270
271         L[] ls2 = new L[20];
272         for (int i=0; i < ls2.length; i++) {
273             L l = new L();
274             io.getOut().println ("Second round of hyperlinks " + i, l);
275             ls2[i] = l;
276         }
277         sleep();
278
279         for (int i=0; i < ls2.length; i++) {
280             ls2[i].assertNotCleared("The second round of listeners were cleared prematurely");
281         }
282
283         //Make sure the old ones are untouched
284
for (int i=0; i < ls.length; i++) {
285             //These should not have been touched. The last assertCleared() cleared the cleared flag :-o
286
ls[i].assertNotCleared("Already cleared listeners should be unreferenced and should not be cleared a second time");
287         }
288
289         //Ensure the actions are paying attention
290
SwingUtilities.invokeLater (new Runnable JavaDoc() {
291             public void run() {
292                 win.getController().postPopupMenu(win, tab, new Point JavaDoc(0,0), tab);
293                 win.getController().clearAction.actionPerformed(new ActionEvent JavaDoc (tab, ActionEvent.ACTION_PERFORMED, "clear"));
294             }
295         });
296         sleep();
297         sleep();
298
299         for (int i=0; i < ls2.length; i++) {
300             ls2[i].assertCleared("After invoking the Clear Output action a second time, the newly written listeners were not cleared - " + i);
301         }
302
303         //Make sure the old ones are untouched again - clearing a new set of lines should not
304
//touch the old listeners - they are already forgotten
305
for (int i=0; i < ls.length; i++) {
306             //These should not have been touched. The last assertCleared() cleared the cleared flag :-o
307
ls[i].assertNotCleared("Already cleared listeners should not be touched by clearing or writing new data");
308         }
309
310         L[] ls3 = new L[20];
311         for (int i=0; i < ls3.length; i++) {
312             L l = new L();
313             io.getOut().println ("Third round of hyperlinks " + i, l);
314             ls3[i] = l;
315         }
316         sleep();
317
318         for (int i=0; i < ls3.length; i++) {
319             ls3[i].assertNotCleared("Third round of writes with listeners had those listeners prematurely cleared");
320         }
321
322         //This time we test it using reset(), not programmatically invoking the GUI call to do the same
323
io.reset();
324         sleep();
325         sleep();
326
327         for (int i=0; i < ls3.length; i++) {
328             ls3[i].assertCleared("InputOutput.reset() should cause all OutputListeners to be cleared");
329         }
330         
331         io = (NbIO) provider.getIO("Another tab", true);
332
333         L[] ls4 = new L[20];
334         for (int i=0; i < ls4.length; i++) {
335             L l = new L();
336             io.getOut().println ("Third round of hyperlinks " + i, l);
337             ls4[i] = l;
338         }
339         sleep();
340
341         for (int i=0; i < ls4.length; i++) {
342             ls4[i].assertNotCleared("Premature clear");
343         }
344
345         io.getOut().close(); //Close the stream
346
sleep();
347         //And this time with closeInputOutput()
348
io.closeInputOutput();
349         sleep();
350         sleep();
351         sleep();
352         sleep();
353         sleep();
354
355         for (int i=0; i < ls4.length; i++) {
356             ls4[i].assertCleared("CloseInputOutput should cause all OutputListeners to be cleared");
357         }
358     }
359
360
361     public class L implements OutputListener {
362         private OutputEvent clearedEvent = null;
363         public void assertCleared(String JavaDoc msg) {
364             assertNotNull (msg, clearedEvent);
365             clearedEvent = null;
366         }
367
368         public void assertNotCleared(String JavaDoc msg) {
369             assertNull (msg, clearedEvent);
370         }
371
372         public void outputLineSelected(OutputEvent ev) {
373         }
374
375         public void outputLineAction(OutputEvent ev) {
376         }
377
378         public void outputLineCleared(OutputEvent ev) {
379             clearedEvent = ev;
380         }
381     }
382     
383 }
384
Popular Tags