1 19 20 package org.openide.awt; 21 22 import java.awt.Component ; 23 import java.io.IOException ; 24 import java.io.ObjectOutputStream ; 25 import java.util.logging.Level ; 26 import javax.swing.JComponent ; 27 import javax.swing.JLabel ; 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 44 public class ToolbarPoolTest extends NbTestCase { 45 FileObject toolbars; 46 DataFolder toolbarsFolder; 47 48 public ToolbarPoolTest (String testName) { 49 super (testName); 50 } 51 52 protected Level logLevel() { 53 return Level.FINE; 54 } 55 56 protected void setUp() throws Exception { 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 { 70 ToolbarPool tp = ToolbarPool.getDefault (); 71 72 String conf = tp.getConfiguration (); 73 assertEquals ("By default there is no config", "", conf); 74 75 } 76 77 78 79 public void testCreateConf () throws Exception { 80 JLabel conf = new JLabel (); 81 conf.setName ("testCreateConf"); 82 83 conf = (JLabel )writeInstance (toolbars, "conf1.ser", conf); 84 85 ToolbarPool tp = ToolbarPool.getDefault (); 86 87 tp.waitFinished (); 88 String [] 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 { 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 { 107 FileObject tlb = FileUtil.createFolder (toolbars, "tlbx"); 108 DataFolder f = DataFolder.findFolder (tlb); 109 InstanceDataObject.create (f, "test1", JLabel .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 .class); 121 122 tp.waitFinished (); 123 124 assertLabels ("Now there are two", 2, myTlbs[0]); 125 } 126 127 private static Object writeInstance (final FileObject folder, final String name, final Object inst) throws IOException { 128 class W implements FileSystem.AtomicAction { 129 public Object create; 130 131 public void run () throws IOException { 132 FileObject fo = FileUtil.createData (folder, name); 133 FileLock lock = fo.lock (); 134 ObjectOutputStream oos = new ObjectOutputStream (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 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 msg, int cnt, Component c) { 159 int real = countLabels (c); 160 assertEquals (msg, cnt, real); 161 } 162 163 private static int countLabels (Component c) { 164 if (c instanceof JLabel ) return 1; 165 if (! (c instanceof JComponent )) return 0; 166 int cnt = 0; 167 Component [] arr = ((JComponent )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 |