1 7 8 package org.jdesktop.swing.event; 9 10 import java.util.logging.Level ; 11 12 import junit.framework.Test; 13 import junit.framework.TestCase; 14 import junit.framework.TestSuite; 15 16 20 public class MessageTest extends TestCase { 21 22 private MListener[] listeners; 23 private PListener[] plisteners; 24 25 private static final int COUNT = 10; 26 27 protected void setUp() { 28 listeners = new MListener[COUNT]; 29 plisteners = new PListener[COUNT]; 30 31 for (int i = 0; i < COUNT; i++) { 32 listeners[i] = new MListener(); 33 plisteners[i] = new PListener(); 34 } 35 } 36 37 protected void tearDown() { 38 for (int i = 0; i < COUNT; i++) { 39 listeners[i] = null; 40 plisteners[i] = null; 41 } 42 listeners = null; 43 plisteners = null; 44 } 45 46 public void testMessageRegistration() { 47 MessageSource source = new MessageSourceSupport(this); 48 MessageListener[] ls = source.getMessageListeners(); 49 50 assertNotNull(ls); 51 assertEquals("Number of listeners not 0", 0, ls.length); 52 53 55 for (int i = 0; i < listeners.length; i++) { 57 source.addMessageListener(listeners[i]); 58 } 59 60 ls = source.getMessageListeners(); 61 assertEquals(listeners.length, ls.length); 62 63 for (int i = 0; i < listeners.length; i++) { 65 source.removeMessageListener(listeners[i]); 66 } 67 68 ls = source.getMessageListeners(); 69 assertEquals(0, ls.length); 70 71 for (int i = 0; i < COUNT; i++) { 73 source.addMessageListener(null); 74 ls = source.getMessageListeners(); 75 assertEquals(0, ls.length); 76 } 77 78 for (int i = 0; i < listeners.length; i++) { 80 source.addMessageListener(listeners[i]); 81 } 82 83 for (int i = 0; i < COUNT; i++) { 84 source.removeMessageListener(null); 85 ls = source.getMessageListeners(); 86 assertEquals(listeners.length, ls.length); 87 } 88 } 89 90 93 public void testMessageDispatch() { 94 MessageSourceSupport source = new MessageSourceSupport(this); 95 for (int i = 0; i < listeners.length; i++) { 96 source.addMessageListener(listeners[i]); 97 } 98 99 String message = "This is a simple message"; 101 Level level = Level.INFO; 102 long when = System.currentTimeMillis(); 103 MessageEvent evt = null; 104 105 source.fireMessage(message); 107 for (int i = 0; i < listeners.length; i++) { 108 evt = listeners[i].getLastEvent(); 109 assertNotNull(evt); 110 assertEquals(message, evt.getMessage()); 111 } 112 113 source.fireMessage(message, level); 114 for (int i = 0; i < listeners.length; i++) { 115 evt = listeners[i].getLastEvent(); 116 assertNotNull(evt); 117 assertEquals(message, evt.getMessage()); 118 assertEquals(level, evt.getLevel()); 119 } 120 121 source.fireMessage(message, level, when); 122 for (int i = 0; i < listeners.length; i++) { 123 evt = listeners[i].getLastEvent(); 124 assertNotNull(evt); 125 assertEquals(message, evt.getMessage()); 126 assertEquals(level, evt.getLevel()); 127 assertEquals(when, evt.getWhen()); 128 } 129 130 evt = new MessageEvent(this, message, level, when); 131 source.fireMessage(evt); 132 for (int i = 0; i < listeners.length; i++) { 133 assertEquals(evt, listeners[i].getLastEvent()); 134 } 135 } 136 137 140 public void testExceptions() { 141 MessageSourceSupport source = new MessageSourceSupport(this); 142 for (int i = 0; i < listeners.length; i++) { 143 source.addMessageListener(listeners[i]); 144 } 145 146 Exception ex = new Exception ("This is a message"); 147 MessageEvent evt = null; 148 149 source.fireException(ex); 150 for (int i = 0; i < listeners.length; i++) { 151 evt = listeners[i].getLastEvent(); 152 assertNotNull(evt); 153 assertEquals(ex.getMessage(), evt.getMessage()); 155 assertEquals(Level.SEVERE, evt.getLevel()); 157 } 158 159 160 } 161 162 165 public void testDeterminteProgress() { 166 MessageSourceSupport source = new MessageSourceSupport(this); 167 ProgressEvent evt = null; 168 169 for (int i = 0; i < plisteners.length; i++) { 171 source.addProgressListener(plisteners[i]); 172 } 173 174 source.fireProgressStarted(0, 100); 176 for (int i = 0; i < plisteners.length; i++) { 177 evt = plisteners[i].getLastEvent(); 178 assertFalse(evt.isIndeterminate()); 179 assertTrue(plisteners[i].begin); 180 assertFalse(plisteners[i].end); 181 } 182 183 source.fireProgressIncremented(50); 184 for (int i = 0; i < plisteners.length; i++) { 185 evt = plisteners[i].getLastEvent(); 186 assertFalse(evt.isIndeterminate()); 187 assertFalse(plisteners[i].begin); 188 assertFalse(plisteners[i].end); 189 } 190 191 source.fireProgressEnded(); 192 for (int i = 0; i < plisteners.length; i++) { 193 evt = plisteners[i].getLastEvent(); 194 assertTrue(evt.isIndeterminate()); 195 assertFalse(plisteners[i].begin); 196 assertTrue(plisteners[i].end); 197 } 198 } 199 200 public void testIndeterminteProgress() { 201 MessageSourceSupport source = new MessageSourceSupport(this); 202 ProgressEvent evt = null; 203 204 for (int i = 0; i < plisteners.length; i++) { 206 source.addProgressListener(plisteners[i]); 207 } 208 209 source.fireProgressStarted(0, 0); 211 for (int i = 0; i < plisteners.length; i++) { 212 evt = plisteners[i].getLastEvent(); 213 assertTrue(evt.isIndeterminate()); 214 assertTrue(plisteners[i].begin); 215 assertFalse(plisteners[i].end); 216 } 217 } 218 219 222 private class MListener 223 implements MessageListener { 224 225 private MessageEvent evt; 226 227 public void message(MessageEvent evt) { 228 this.evt = evt; 229 } 230 231 public void exception(MessageEvent evt) { 232 this.evt = evt; 233 } 234 235 public MessageEvent getLastEvent() { 236 return evt; 237 } 238 } 239 240 private class PListener 241 implements ProgressListener { 242 243 private ProgressEvent evt; 244 245 public boolean begin = false; 246 public boolean end = false; 247 248 public void progressStarted(ProgressEvent e) { 249 this.evt = e; 250 begin = true; 251 end = false; 252 } 253 254 public void progressIncremented(ProgressEvent e) { 255 this.evt = e; 256 begin = false; 257 end = false; 258 } 259 260 public void progressEnded(ProgressEvent e) { 261 this.evt = e; 262 begin = false; 263 end = true; 264 } 265 266 public void exception(ProgressEvent e) { 267 268 } 269 270 public ProgressEvent getLastEvent() { 271 return evt; 272 } 273 } 274 } 275 | Popular Tags |