1 19 20 package org.netbeans.spi.looks; 21 22 import java.util.Enumeration ; 23 import java.util.TooManyListenersException ; 24 import javax.swing.event.ChangeListener ; 25 import org.netbeans.api.nodes2looks.Nodes; 26 import org.netbeans.junit.NbTestCase; 27 import org.openide.nodes.Node; 28 import org.openide.util.Enumerations; 29 30 34 public class ProxyLookTest extends NbTestCase { 35 36 38 public ProxyLookTest( String testName ) { 39 super(testName); 40 } 41 42 44 public void testThrowNoException() { 45 46 Look composite = Looks.composite( "TC", new Look[] { 47 new ExceptionLook( "sl1", null ), 48 new ExceptionLook( "sl2", null ) 49 } ); 50 51 Look filter = Looks.filter( "TF", new ExceptionLook( "fd1", null ), ProxyLook.ALL_METHODS ); 52 53 try { 54 org.netbeans.modules.looks.Accessor.DEFAULT.addLookListener( composite, "RO", null ); 55 org.netbeans.modules.looks.Accessor.DEFAULT.addLookListener( filter, "RO", null ); 56 } 57 catch( Exception e ) { 58 fail( "No exception should be thrown" ); 59 } 60 61 } 62 63 public void testThrowClassCastException() { 64 65 Look composite = Looks.composite( "TC", new Look[] { 66 new ExceptionLook( "sl1", null ), 67 new ExceptionLook( "sl2", ClassCastException .class ) 68 } ); 69 70 Look filter = Looks.filter( "TF", new ExceptionLook( "fd1", ClassCastException .class ), ProxyLook.ALL_METHODS ); 71 72 try { 73 org.netbeans.modules.looks.Accessor.DEFAULT.addLookListener( composite, "RO", null ); 74 org.netbeans.modules.looks.Accessor.DEFAULT.addLookListener( filter, "RO", null ); 75 fail( "ClassCastException should be thrown, but it was not" ); 76 } 77 catch( Exception e ) { 78 assertTrue( "ClassCastException should be thrown. Was : " + e.getClass(), e instanceof ClassCastException ); 79 } 80 81 } 82 83 public void testThrowIllegalArgumentException() { 84 Look composite = Looks.composite( "TC", new Look[] { 85 new ExceptionLook( "sl1", null ), 86 new ExceptionLook( "sl2", IllegalArgumentException .class ) 87 } ); 88 89 Look filter = Looks.filter( "TF", new ExceptionLook( "fd1", IllegalArgumentException .class ), ProxyLook.ALL_METHODS ); 90 91 try { 92 org.netbeans.modules.looks.Accessor.DEFAULT.addLookListener( composite, "RO", null ); 93 org.netbeans.modules.looks.Accessor.DEFAULT.addLookListener( filter, "RO", null ); 94 fail( "IllegalArgumentException should be thrown, but it was not" ); 95 } 96 catch( Exception e ) { 97 assertTrue( "IllegalArgumentException should be thrown. Was : " + e.getClass(), e instanceof IllegalArgumentException ); 98 } 99 100 } 101 102 public void testDetachWhenExceptionWasThrown() { 103 104 ExceptionLook sl1 = new ExceptionLook( "sl1", null ); 105 ExceptionLook sl2 = new ExceptionLook( "sl2", null ); 106 ExceptionLook sl3 = new ExceptionLook( "sl2", IllegalArgumentException .class ); 107 ExceptionLook sl4 = new ExceptionLook( "sl2", ClassCastException .class ); 108 109 Look composite = Looks.composite( "TC", new Look[] { sl1, sl2, sl3, sl4 } ); 110 111 try { 112 org.netbeans.modules.looks.Accessor.DEFAULT.addLookListener( composite, "RO", null ); 113 } 114 catch ( IllegalArgumentException e ) { 115 assertEquals( "Sl1 attachTo", 1, sl1.getAttachToCount() ); 116 assertEquals( "Sl1 detachFrom", 1, sl1.getDetachFromCount() ); 117 assertEquals( "Sl2 attachTo", 1, sl2.getAttachToCount() ); 118 assertEquals( "Sl2 detachFrom", 1, sl2.getDetachFromCount() ); 119 assertEquals( "Sl3 attachTo", 0, sl3.getAttachToCount() ); 120 assertEquals( "Sl3 detachFrom", 0, sl3.getDetachFromCount() ); 121 assertEquals( "Sl4 attachTo", 0, sl4.getAttachToCount() ); 122 assertEquals( "Sl4 detachFrom", 0, sl4.getDetachFromCount() ); 123 return; 124 } 125 126 fail( "Exception not thrown" ); 127 } 128 129 public void testAttachDetachAfterSeletorChange( ) { 130 131 AttachDetachProvider provider = new AttachDetachProvider(); 132 LookSelector selector = Selectors.selector( provider ); 133 Look proxy = new AttachDetachProxyLook( "TEST_AD", selector ); 134 135 Node n = Nodes.node( "KAREL", null, Selectors.singleton( proxy ) ); 136 137 assertEquals( "AD_LOOK_1 attachTo", 1, provider.AD_LOOK[0].getAttachCount() ); 138 assertEquals( "AD_LOOK_1 detachFrom", 0, provider.AD_LOOK[0].getDetachCount() ); 139 assertEquals( "AD_LOOK_2 attachTo", 0, provider.AD_LOOK[1].getAttachCount() ); 140 assertEquals( "AD_LOOK_2 detachFrom", 0, provider.AD_LOOK[1].getDetachCount() ); 141 142 provider.switchLook(); 143 144 assertEquals( "AD_LOOK_1 attachTo", 0, provider.AD_LOOK[0].getAttachCount() ); 145 assertEquals( "AD_LOOK_1 detachFrom", 1, provider.AD_LOOK[0].getDetachCount() ); 146 assertEquals( "AD_LOOK_2 attachTo", 1, provider.AD_LOOK[1].getAttachCount() ); 147 assertEquals( "AD_LOOK_2 detachFrom", 0, provider.AD_LOOK[1].getDetachCount() ); 148 } 149 150 152 153 public static class ExceptionLook extends Look { 154 155 private int attachTo; 156 private int detachFrom; 157 158 private Class exClass; 159 160 public ExceptionLook( String name, Class exClass ) { 161 super( name ); 162 163 if ( exClass != null && !( RuntimeException .class.isAssignableFrom( exClass ) ) ) { 164 fail( "Bad usage of the test " + exClass + " is not runtimeException" ); 165 } 166 167 this.exClass = exClass; 168 } 169 170 public void attachTo( Object representedObject ) { 171 if ( exClass != null) { 172 try { 173 System.err.println("exClass=" + exClass); throw (RuntimeException )exClass.newInstance(); 175 } 176 catch ( InstantiationException e ) { 177 fail( "Bad usage of the test " + e ); 178 } 179 catch ( IllegalAccessException e ) { 180 fail( "Bad usage of the test " + e ); 181 } 182 } 183 184 attachTo++; 185 } 186 187 public void detachFrom( Object representedObject ) { 188 detachFrom++; 189 } 190 191 public int getAttachToCount() { 192 return attachTo; 193 } 194 195 public int getDetachFromCount() { 196 return detachFrom; 197 } 198 199 } 200 201 private static class AttachDetachProvider implements ChangeableLookProvider { 202 203 private final AttachDetachLook AD_LOOK[] = new AttachDetachLook[] { 204 new AttachDetachLook( "AD_LOOK_1" ), 205 new AttachDetachLook( "AD_LOOK_2" ) 206 }; 207 208 private int current = 0; 209 210 private ChangeListener listener; 211 212 public Enumeration getLooksForObject( Object representedObject ) { 213 return Enumerations.singleton(AD_LOOK[current]); 214 } 215 216 public void switchLook() { 217 current = current == 0 ? 1 : 0; 218 219 if ( listener != null ) { 220 listener.stateChanged( null ); 221 } 222 } 223 224 public synchronized void addChangeListener(ChangeListener listener) throws TooManyListenersException { 225 if ( this.listener != null ) { 226 throw new TooManyListenersException (); 227 } 228 else { 229 this.listener = listener; 230 } 231 } 232 233 public Object getKeyForObject(Object representedObject) { 234 return String .class; 235 } 236 237 public Enumeration getLooksForKey(Object key) { 238 return Enumerations.singleton(AD_LOOK[current]); 239 } 240 241 } 242 243 private static class AttachDetachProxyLook extends ProxyLook { 244 245 public AttachDetachProxyLook( String name, LookSelector selector ) { 246 super( name, selector ); 247 } 248 249 250 } 251 252 private static class AttachDetachLook extends Look { 253 254 private int attachCount; 255 private int detachCount; 256 257 public AttachDetachLook( String name ) { 258 super( name ); 259 } 260 261 protected void attachTo(Object representedObject) { 262 attachCount ++; 263 } 264 265 266 267 protected void detachFrom(Object representedObject) { 268 detachCount ++; 269 } 270 271 272 public int getAttachCount() { 273 int c = attachCount; 274 attachCount = 0; 275 return c; 276 } 277 278 public int getDetachCount() { 279 int c = detachCount; 280 detachCount = 0; 281 return c; 282 } 283 284 } 285 286 287 } 288 | Popular Tags |