1 19 20 package org.netbeans.spi.looks; 21 22 import java.lang.ref.WeakReference ; 23 import java.net.URL ; 24 import java.util.Enumeration ; 25 26 import org.netbeans.junit.*; 27 import junit.framework.*; 28 29 import org.netbeans.spi.looks.ProxyLook; 30 import org.netbeans.spi.looks.Look; 31 import org.netbeans.spi.looks.Looks; 32 import org.netbeans.spi.looks.LookSelector; 33 import org.netbeans.api.nodes2looks.Nodes; 34 35 import org.openide.cookies.InstanceCookie; 36 import org.openide.filesystems.FileObject; 37 import org.openide.filesystems.FileSystem; 38 import org.openide.filesystems.XMLFileSystem; 39 import org.openide.util.Lookup; 40 41 public class LooksTest extends NbTestCase { 42 43 private FileSystem fs; 44 private Look delegate; 45 private long mask = Look.ALL_METHODS; 46 private javax.swing.JTextField bean; 47 48 public LooksTest(java.lang.String testName) { 49 super(testName); 50 } 51 52 public static void main(java.lang.String [] args) { 53 junit.textui.TestRunner.run(suite()); 54 } 55 56 public static Test suite() { 57 TestSuite suite = new NbTestSuite(LooksTest.class); 58 59 return suite; 60 } 61 62 protected void setUp() throws Exception { 63 super.setUp(); 64 URL u = getClass ().getResource ("LooksTest.xml"); 65 66 fs = new XMLFileSystem (u); 67 68 org.netbeans.modules.looks.RegistryBridge.setDefault( fs.getRoot() ); 69 } 70 71 protected void tearDown() throws Exception { 72 super.tearDown(); 73 } 74 75 76 public void testNODES () throws Exception { 77 checkInstance (Nodes.nodeLook(), "Looks/NODES"); 79 } 80 81 public void testBEANS () throws Exception { 82 checkInstance (Looks.bean(), "Looks/BEANS"); 83 } 84 85 public void testFILTER_DEFAULT () throws Exception { 86 doFILTER_DEFAULT ("FILTER_DEFAULT"); 87 } 88 89 public void testFILTER_DEFAULT2 () throws Exception { 90 doFILTER_DEFAULT ("FILTER_DEFAULT2"); 91 } 92 93 private void doFILTER_DEFAULT (String name) throws Exception { 94 95 initFilterLookTest (); 98 99 Look filterRef = Looks.filter ( "TestFilter", delegate, mask); 101 filterRef.attachTo (bean); 102 103 Look filter = (Look)checkIfExists ("Looks/" + name); 105 filter.attachTo (bean); 106 107 assertTrue (bean.getName ().equals (filterRef.getName (bean, Lookup.EMPTY))); 108 assertEquals (filterRef.getName (bean, Lookup.EMPTY), filter.getName (bean, Lookup.EMPTY)); 109 } 110 111 public void testFILTER_ALL_METHODS () throws Exception { 112 doFILTER_ALL_METHODS ("FILTER_ALL_METHODS"); 113 } 114 115 public void testFILTER_ALL_METHODS2 () throws Exception { 116 doFILTER_ALL_METHODS ("FILTER_ALL_METHODS2"); 117 } 118 119 private void doFILTER_ALL_METHODS (String name) throws Exception { 120 121 initFilterLookTest (); 124 125 mask = Look.ALL_METHODS; 127 Look filterRef = Looks.filter ( "TestFilter", delegate, mask); 128 filterRef.attachTo (bean); 129 130 Look filter = (Look)checkIfExists ("Looks/" + name); 132 filter.attachTo (bean); 133 134 assertTrue (bean.getName ().equals (filterRef.getName (bean, Lookup.EMPTY))); 135 assertEquals (filterRef.getName (bean, Lookup.EMPTY), filter.getName (bean, Lookup.EMPTY)); 136 137 } 138 139 public void testFILTER_ALL_METHODS_MINUS_GET_NAME () throws Exception { 140 doFILTER_ALL_METHODS_MINUS_GET_NAME ("FILTER_ALL_METHODS_MINUS_GET_NAME"); 141 } 142 143 public void testFILTER_ALL_METHODS_MINUS_GET_NAME2 () throws Exception { 144 doFILTER_ALL_METHODS_MINUS_GET_NAME ("FILTER_ALL_METHODS_MINUS_GET_NAME2"); 145 } 146 147 private void doFILTER_ALL_METHODS_MINUS_GET_NAME (String name) throws Exception { 148 initFilterLookTest (); 151 152 mask = Look.ALL_METHODS & ~Look.GET_NAME; 154 Look filterRef = Looks.filter ("TestFilter", delegate, mask); 155 filterRef.attachTo (bean); 156 157 Look filter = (Look)checkIfExists ("Looks/" + name); 159 filter.attachTo (bean); 160 161 assertTrue (!bean.getName ().equals (filterRef.getName (bean, Lookup.EMPTY))); 162 assertEquals (filterRef.getName (bean, Lookup.EMPTY), filter.getName (bean, Lookup.EMPTY)); 163 164 } 165 166 public void testFILTER_NO_METHODS () throws Exception { 167 doFILTER_NO_METHODS ("FILTER_NO_METHODS"); 168 } 169 170 public void testFILTER_NO_METHODS2 () throws Exception { 171 doFILTER_NO_METHODS ("FILTER_NO_METHODS"); 172 } 173 174 private void doFILTER_NO_METHODS (String name) throws Exception { 175 initFilterLookTest (); 178 179 mask = Look.NO_METHODS; 181 Look filterRef = Looks.filter ("TestFilter", delegate, mask); 182 filterRef.attachTo (bean); 183 184 Look filter = (Look)checkIfExists ("Looks/" + name); 186 filter.attachTo (bean); 187 188 assertTrue (!bean.getName ().equals (filterRef.getName (bean, Lookup.EMPTY))); 189 assertEquals (filterRef.getName (bean, Lookup.EMPTY), filter.getName (bean, Lookup.EMPTY)); 190 } 191 192 public void testFILTER_NO_METHODS_PLUS_GET_NAME () throws Exception { 193 doFILTER_NO_METHODS_PLUS_GET_NAME ("FILTER_NO_METHODS_PLUS_GET_NAME"); 194 } 195 196 public void testFILTER_NO_METHODS_PLUS_GET_NAME2 () throws Exception { 197 doFILTER_NO_METHODS_PLUS_GET_NAME ("FILTER_NO_METHODS_PLUS_GET_NAME2"); 198 } 199 200 public void doFILTER_NO_METHODS_PLUS_GET_NAME (String name) throws Exception { 201 initFilterLookTest (); 204 205 mask = Look.NO_METHODS | Look.GET_NAME; 207 Look filterRef = Looks.filter ("TestFilter", delegate, mask); 208 filterRef.attachTo (bean); 209 210 Look filter = (Look)checkIfExists ("Looks/" + name ); 212 filter.attachTo (bean); 213 214 assertTrue (bean.getName ().equals (filterRef.getName (bean, Lookup.EMPTY))); 215 assertEquals (filterRef.getName (bean, Lookup.EMPTY), filter.getName (bean, Lookup.EMPTY)); 216 } 217 218 public void testNamespaceSelector () throws Exception { 219 LookSelector selectorRef = org.netbeans.spi.looks.Selectors.namespaceTypes ("Looks/Types/Beans/"); 221 LookSelector selector = (LookSelector)checkIfExists ("Looks/Selectors/NAMESPACE_SELECTOR"); 222 223 String s = new String (); 224 Enumeration enRef = selectorRef.getLooks (s); 225 Enumeration en = selector.getLooks (s); 226 227 int i = 0; 228 while (enRef.hasMoreElements () && en.hasMoreElements ()) { 229 assertSame ("Found looks are same.", enRef.nextElement (), en.nextElement ()); 230 i ++; 231 } 232 233 assertTrue( "Two looks should be found. Found " + i + ".", i == 2 ); 234 assertTrue ("No more items from reference selector.", !enRef.hasMoreElements ()); 235 assertTrue ("No more items from declared selector.", !en.hasMoreElements ()); 236 } 237 238 239 public void testNamespaceSelectorCaching() { 240 LookSelector selector = Selectors.namespaceTypes( "Looks/Types/Simple/" ); 241 242 assertNotNull( "Selector should exist.", selector ); 243 244 245 String ro = "REPRESENTED OBJECT"; 247 for( int i = 0; i < 100; i++ ) { 248 249 if ( i % 10 == 0 ) { 250 Object k = new Object (); 251 WeakReference wr = new WeakReference ( k ); 252 k = null; 253 System.gc(); 254 assertGC( "Reference should disapear", wr ); 255 } 256 257 Enumeration e = selector.getLooks( ro ); 258 assertTrue( "Should find instance counting look.", e.nextElement() instanceof InstanceCountingLook ); 259 assertTrue( "It should be the only look.", !e.hasMoreElements() ); 260 assertEquals( "There should be one instance of the look.", 1, InstanceCountingLook.getInstanceCount() ); 261 } 262 263 } 264 265 private static Look instanceCountingLook() { 266 return new InstanceCountingLook(); 267 } 268 269 public void testDecoratorSelector () throws Exception { 270 doDecoratorSelector ( 271 "DECORATOR_SELECTOR", 272 (Look)checkIfExists ("Looks/Bean/SimpleBeanLook"), 273 namespaceSelectorForLooksTypesBeans () 274 ); 275 } 276 277 public void testDecoratorSelector2 () throws Exception { 278 doDecoratorSelector ( 279 "DECORATOR_SELECTOR2", 280 Looks.bean(), 281 (LookSelector)checkIfExists ("Looks/Selectors/NAMESPACE_SELECTOR") 282 ); 283 } 284 285 public static LookSelector namespaceSelectorForLooksTypesBeans () { 286 return Selectors.namespaceTypes("Looks/Types/Properties"); 287 } 288 289 private void doDecoratorSelector (String name, Look decoratingLook, LookSelector selector) throws Exception { 290 291 LookSelector decoratorRef = Selectors.decorator ( selector, decoratingLook ); 293 294 LookSelector decorator = (LookSelector)checkIfExists ("Looks/Selectors/" + name); 295 296 String s = new String (); 297 Enumeration enRef = decoratorRef.getLooks (s); 298 Enumeration en = decorator.getLooks (s); 299 Enumeration enSelector = selector.getLooks (s); 300 while (enRef.hasMoreElements () && en.hasMoreElements () && enSelector.hasMoreElements ()) { 301 en.nextElement (); 303 enRef.nextElement (); 304 enSelector.nextElement (); 305 } 306 assertTrue ("No more items from docorated selector.", !enSelector.hasMoreElements ()); 307 assertTrue ("No more items from reference decorator.", !enRef.hasMoreElements ()); 308 assertTrue ("No more items from decorator.", !en.hasMoreElements ()); 309 310 311 } 312 313 315 private Object checkInstance (Object inst, String name) throws Exception { 316 Object instRef = checkIfExists( name ); 317 assertEquals ("Instance is the same", inst, instRef); 318 return instRef; 319 } 320 321 322 private Object checkIfExists (String name) throws Exception { 323 324 org.netbeans.modules.looks.RegistryBridge registryBridge = org.netbeans.modules.looks.RegistryBridge.getDefault( null ); 325 Object o = registryBridge.resolve( name ); 326 assertNotNull( "Name " + name + " not found.", o ); 327 return o; 328 329 } 330 331 private void initFilterLookTest () { 332 if (bean != null) 333 return ; 334 try { 335 delegate = (Look)checkInstance (Looks.bean(), "Looks/Bean/SimpleBeanLook"); assertNotNull (delegate); 337 bean = new javax.swing.JTextField (); 338 bean.setName ("dummy"); } catch (Exception e) { 340 fail (e.getMessage ()); 341 } 342 } 343 344 346 private static class InstanceCountingLook extends Look { 347 348 private static int count = 0; 349 350 public InstanceCountingLook() { 351 super( "ICL " + count++ ); 352 } 353 354 355 public static int getInstanceCount() { 356 return count; 357 } 358 359 } 360 361 } 362 363 364 365 | Popular Tags |