KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openide > awt > ToolbarPoolTest


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.openide.awt;
21
22 import java.awt.Component JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.ObjectOutputStream JavaDoc;
25 import java.util.logging.Level JavaDoc;
26 import javax.swing.JComponent JavaDoc;
27 import javax.swing.JLabel JavaDoc;
28 import junit.framework.TestCase;
29 import org.netbeans.junit.NbTestCase;
30 import org.openide.cookies.InstanceCookie;
31 import org.openide.filesystems.FileLock;
32 import org.openide.filesystems.FileObject;
33 import org.openide.filesystems.FileSystem;
34 import org.openide.filesystems.FileUtil;
35 import org.openide.filesystems.Repository;
36 import org.openide.loaders.DataFolder;
37 import org.openide.loaders.DataObject;
38 import org.openide.loaders.InstanceDataObject;
39
40 /** Mostly to test the correct behaviour of AWTTask.waitFinished.
41  *
42  * @author Jaroslav Tulach
43  */

44 public class ToolbarPoolTest extends NbTestCase {
45     FileObject toolbars;
46     DataFolder toolbarsFolder;
47     
48     public ToolbarPoolTest (String JavaDoc testName) {
49         super (testName);
50     }
51
52     protected Level JavaDoc logLevel() {
53         return Level.FINE;
54     }
55     
56     protected void setUp() throws Exception JavaDoc {
57         FileObject root = Repository.getDefault ().getDefaultFileSystem ().getRoot ();
58         toolbars = FileUtil.createFolder (root, "Toolbars");
59         toolbarsFolder = DataFolder.findFolder (toolbars);
60         FileObject[] arr = toolbars.getChildren ();
61         for (int i = 0; i < arr.length; i++) {
62             arr[i].delete ();
63         }
64         
65         ToolbarPool tp = ToolbarPool.getDefault ();
66         tp.waitFinished ();
67     }
68
69     public void testGetConf () throws Exception JavaDoc {
70         ToolbarPool tp = ToolbarPool.getDefault ();
71         
72         String JavaDoc conf = tp.getConfiguration ();
73         assertEquals ("By default there is no config", "", conf);
74         
75     }
76     
77
78     
79     public void testCreateConf () throws Exception JavaDoc {
80         JLabel JavaDoc conf = new JLabel JavaDoc ();
81         conf.setName ("testCreateConf");
82         
83         conf = (JLabel JavaDoc)writeInstance (toolbars, "conf1.ser", conf);
84         
85         ToolbarPool tp = ToolbarPool.getDefault ();
86         
87         tp.waitFinished ();
88         String JavaDoc[] myConfs = tp.getConfigurations ();
89         assertEquals ("One", 1, myConfs.length);
90         assertEquals ("By default there is the one", "testCreateConf", myConfs[0]);
91         
92     }
93
94     public void testCreateFolderTlbs () throws Exception JavaDoc {
95         FileUtil.createFolder (toolbars, "tlb2");
96         
97         ToolbarPool tp = ToolbarPool.getDefault ();
98         
99         tp.waitFinished ();
100         Toolbar[] myTlbs = tp.getToolbars ();
101         assertEquals ("One", 1, myTlbs.length);
102         assertEquals ("By default there is the one", "tlb2", myTlbs[0].getName ());
103         
104     }
105     
106     public void testWaitsForToolbars () throws Exception JavaDoc {
107         FileObject tlb = FileUtil.createFolder (toolbars, "tlbx");
108         DataFolder f = DataFolder.findFolder (tlb);
109         InstanceDataObject.create (f, "test1", JLabel JavaDoc.class);
110         
111         ToolbarPool tp = ToolbarPool.getDefault ();
112         
113         tp.waitFinished ();
114         Toolbar[] myTlbs = tp.getToolbars ();
115         assertEquals ("One", 1, myTlbs.length);
116         assertEquals ("By default there is the one", "tlbx", myTlbs[0].getName ());
117         
118         assertLabels ("One subcomponent", 1, myTlbs[0]);
119         
120         InstanceDataObject.create (f, "test2", JLabel JavaDoc.class);
121         
122         tp.waitFinished ();
123         
124         assertLabels ("Now there are two", 2, myTlbs[0]);
125     }
126     
127     private static Object JavaDoc writeInstance (final FileObject folder, final String JavaDoc name, final Object JavaDoc inst) throws IOException JavaDoc {
128         class W implements FileSystem.AtomicAction {
129             public Object JavaDoc create;
130             
131             public void run () throws IOException JavaDoc {
132                 FileObject fo = FileUtil.createData (folder, name);
133                 FileLock lock = fo.lock ();
134                 ObjectOutputStream JavaDoc oos = new ObjectOutputStream JavaDoc (fo.getOutputStream (lock));
135                 oos.writeObject (inst);
136                 oos.close ();
137                 lock.releaseLock ();
138                 
139                 DataObject obj = DataObject.find (fo);
140                 InstanceCookie ic =
141                     (InstanceCookie)
142                     obj.getCookie (InstanceCookie.class);
143                 
144                 assertNotNull ("Cookie created", ic);
145                 try {
146                     create = ic.instanceCreate ();
147                     assertEquals ("The same instance class", inst.getClass(), create.getClass ());
148                 } catch (ClassNotFoundException JavaDoc ex) {
149                     fail (ex.getMessage ());
150                 }
151             }
152         }
153         W w = new W ();
154         folder.getFileSystem ().runAtomicAction (w);
155         return w.create;
156     }
157     
158     private static void assertLabels (String JavaDoc msg, int cnt, Component JavaDoc c) {
159         int real = countLabels (c);
160         assertEquals (msg, cnt, real);
161     }
162     
163     private static int countLabels (Component JavaDoc c) {
164         if (c instanceof JLabel JavaDoc) return 1;
165         if (! (c instanceof JComponent JavaDoc)) return 0;
166         int cnt = 0;
167         Component JavaDoc[] arr = ((JComponent JavaDoc)c).getComponents ();
168         for (int i = 0; i < arr.length; i++) {
169             cnt += countLabels (arr[i]);
170         }
171         return cnt;
172     }
173 }
174
Popular Tags