KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > NbClipboardTest


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.core;
20
21 import java.awt.KeyboardFocusManager JavaDoc;
22 import java.awt.Window JavaDoc;
23 import java.lang.ref.WeakReference JavaDoc;
24 import java.util.logging.Level JavaDoc;
25 import java.util.logging.Logger JavaDoc;
26 import javax.swing.JFrame JavaDoc;
27 import javax.swing.SwingUtilities JavaDoc;
28 import org.netbeans.junit.NbTestCase;
29 import org.openide.windows.TopComponent;
30
31 /** Basic tests on NbClipboard
32  *
33  * @author Jaroslav Tulach
34  */

35 public class NbClipboardTest extends NbTestCase {
36
37     public NbClipboardTest(String JavaDoc testName) {
38         super(testName);
39     }
40     
41     protected void setUp() throws Exception JavaDoc {
42         System.getProperties().remove("netbeans.slow.system.clipboard.hack");
43     }
44
45     protected void tearDown() throws Exception JavaDoc {
46     }
47
48     public void testDefaultOnJDK15AndLater() {
49         if (System.getProperty("java.version").startsWith("1.4")) {
50             return;
51         }
52         
53         NbClipboard ec = new NbClipboard();
54         assertTrue("By default we still do use slow hacks", ec.slowSystemClipboard);
55     }
56     public void testPropOnJDK15AndLater() {
57         if (System.getProperty("java.version").startsWith("1.4")) {
58             return;
59         }
60         
61         System.setProperty("netbeans.slow.system.clipboard.hack", "false");
62         
63         NbClipboard ec = new NbClipboard();
64         assertFalse("Property overrides default", ec.slowSystemClipboard);
65         assertEquals("sun.awt.datatransfer.timeout is now 1000", "1000", System.getProperty("sun.awt.datatransfer.timeout"));
66     }
67     
68     public void testMemoryLeak89844() throws Exception JavaDoc {
69         class Safe implements Runnable JavaDoc {
70             WeakReference JavaDoc<Object JavaDoc> ref;
71             Window JavaDoc w;
72             TopComponent tc;
73             
74             
75             public void beforeAWT() throws InterruptedException JavaDoc {
76                 NbClipboard ec = new NbClipboard();
77                 
78                 tc = new TopComponent();
79                 tc.open();
80                 
81                 for(;;) {
82                     w = SwingUtilities.getWindowAncestor(tc);
83                     if (w != null && w.isVisible()) {
84                         break;
85                     }
86                     Thread.sleep(100);
87                 }
88                 
89                 tc.close();
90                 w.dispose();
91                 
92                 // opening new frame shall clear all the AWT references to previous frame
93
JFrame JavaDoc f = new JFrame JavaDoc("Focus stealer");
94                 f.setVisible(true);
95                 f.pack();
96                 f.toFront();
97                 f.requestFocus();
98                 f.requestFocusInWindow();
99             }
100             
101             public void run() {
102                 KeyboardFocusManager.getCurrentKeyboardFocusManager().clearGlobalFocusOwner();
103                 
104                 ref = new WeakReference JavaDoc<Object JavaDoc>(w);
105                 w = null;
106                 tc = null;
107             }
108 }
109         
110         Safe safe = new Safe();
111         
112         safe.beforeAWT();
113         SwingUtilities.invokeAndWait(safe);
114         
115         try {
116             assertGC("Top component can disappear", safe.ref);
117         } catch (junit.framework.AssertionFailedError ex) {
118             if (ex.getMessage().indexOf("NbClipboard") >= 0) {
119                 throw ex;
120             }
121             Logger.getAnonymousLogger().log(Level.WARNING, "Cannot do GC, but not due to NbClipboard itself", ex);
122         }
123     }
124     
125     private static void waitEQ(final Window JavaDoc w) throws Exception JavaDoc {
126         class R implements Runnable JavaDoc {
127             boolean visible;
128             
129             public void run() {
130                 visible = w.isShowing();
131             }
132         }
133         R r = new R();
134         while (!r.visible) {
135             SwingUtilities.invokeAndWait(r);
136         }
137     }
138 }
139
Popular Tags