1 19 20 package org.netbeans.spi.looks; 21 22 import java.util.ArrayList ; 23 import java.util.ConcurrentModificationException ; 24 import org.netbeans.junit.*; 25 import org.netbeans.modules.looks.LookEvent; 26 27 import org.netbeans.modules.looks.LookListener; 28 29 31 public class LookListenersTest extends NbTestCase { 32 33 35 public LookListenersTest(java.lang.String testName) { 36 super(testName); 37 } 38 39 public static void main(java.lang.String [] args) { 40 junit.textui.TestRunner.run(suite()); 41 } 42 43 public static NbTest suite() { 44 NbTestSuite suite = new NbTestSuite(LookListenersTest.class); 45 return suite; 46 } 47 48 protected void setUp() throws Exception { 49 super.setUp(); 50 } 51 52 protected void tearDown() throws Exception { 53 super.tearDown(); 54 } 55 56 57 59 public void testAddObject() { 60 61 TestFiringLook look = new TestFiringLook(); 63 TestLookListener listener = new TestLookListener( look ); 64 65 reset(); 66 67 look.addLookListener( createObject(), listener ); 69 look.addLookListener( createObject(), listener ); 70 71 try { 72 look.fire( null, ADD_OBJECT ); 74 } 75 catch ( ConcurrentModificationException e ) { 76 fail( "CME thrown " + e ); 77 } 78 79 } 80 81 public void testRemoveObject() { 82 83 TestFiringLook look = new TestFiringLook(); 85 TestLookListener listener = new TestLookListener( look ); 86 87 reset(); 88 89 90 look.addLookListener( createObject(), listener ); 92 look.addLookListener( createObject(), listener ); 93 look.addLookListener( createObject(), listener ); 94 95 try { 96 look.fire( null, REMOVE_OBJECT ); 98 } 99 catch ( ConcurrentModificationException e ) { 100 fail( "CME thrown " + e ); 101 } 102 103 } 104 105 public void testAddListener() { 106 107 TestFiringLook look = new TestFiringLook(); 109 110 reset(); 111 createObject(); 112 look.addLookListener( getObject( counter ), new TestLookListener( look ) ); 114 look.addLookListener( getObject( counter ), new TestLookListener( look ) ); 115 116 try { 117 look.fire( getObject( counter ), ADD_LISTENER ); 119 } 120 catch ( ConcurrentModificationException e ) { 121 fail( "CME thrown " + e ); 122 } 123 124 } 125 126 public void testRemoveListener() { 127 128 TestFiringLook look = new TestFiringLook(); 130 131 reset(); 132 createObject(); 133 look.addLookListener( getObject( counter ), new TestLookListener( look ) ); 135 look.addLookListener( getObject( counter ), new TestLookListener( look ) ); 136 137 try { 138 look.fire( getObject( counter ), REMOVE_LISTENER ); 140 } 141 catch ( ConcurrentModificationException e ) { 142 fail( "CME thrown " + e ); 143 } 144 145 } 146 147 149 private static int counter = -1; 150 private static ArrayList objects = new ArrayList (); 151 152 private static void reset() { 153 counter = -1; 154 objects = new ArrayList (); 155 TestLookListener.listeners = new ArrayList (); 156 } 157 158 private static Integer createObject() { 159 objects.add( new Integer ( counter++ ) ); 160 return (Integer )objects.get( counter ); 161 } 162 163 private static Integer getObject( int index ) { 164 return (Integer )objects.get( index ); 165 } 166 167 private static final long ADD_OBJECT = Look.GET_NAME; 169 private static final long REMOVE_OBJECT = Look.GET_DISPLAY_NAME; 170 private static final long ADD_LISTENER = Look.GET_ICON; 171 private static final long REMOVE_LISTENER = Look.GET_OPENED_ICON; 172 173 private static class TestFiringLook extends Look { 174 175 TestFiringLook() { 176 super( "TEST_FIRING_LOOK" ); 177 } 178 179 public void fire( Object object, long mask ) { 180 fireChange( object, mask ); 181 } 182 183 } 184 185 private static class TestLookListener implements LookListener { 186 187 private static ArrayList listeners = new ArrayList (); 188 189 Look look; 190 191 TestLookListener( Look look ) { 192 this.look = look; 193 listeners.add( this ); 194 } 195 196 public void change( LookEvent evt ) { 197 if ( evt.getMask() == ADD_OBJECT ) { 198 look.addLookListener( createObject(), this ); 199 } 200 else if ( evt.getMask() == REMOVE_OBJECT ) { 201 look.removeLookListener( getObject( counter ), this ); 202 } 203 else if ( evt.getMask() == ADD_LISTENER ) { 204 look.addLookListener( getObject( counter ), this ); 205 } 206 else if ( evt.getMask() == REMOVE_LISTENER ) { 207 look.removeLookListener( getObject( counter ), (LookListener)listeners.get( counter ) ); 208 } 209 } 210 211 public void propertyChange( LookEvent evt ) { 212 213 } 214 215 } 216 217 218 } 219 | Popular Tags |