001: /*
002: * $Id: MessageTest.java,v 1.2 2005/07/02 15:38:22 l2fprod Exp $
003: *
004: * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
005: * Santa Clara, California 95054, U.S.A. All rights reserved.
006: */
007:
008: package org.jdesktop.swingx.event;
009:
010: import java.util.logging.Level;
011:
012: import junit.framework.TestCase;
013:
014: /**
015: * implementation of the MessageSourceSupport class.
016: * A JUnit test case for the Message event set. Mostly this tests the
017: */
018: public class MessageTest extends TestCase {
019:
020: private MListener[] listeners;
021: private PListener[] plisteners;
022:
023: private static final int COUNT = 10;
024:
025: protected void setUp() {
026: listeners = new MListener[COUNT];
027: plisteners = new PListener[COUNT];
028:
029: for (int i = 0; i < COUNT; i++) {
030: listeners[i] = new MListener();
031: plisteners[i] = new PListener();
032: }
033: }
034:
035: protected void tearDown() {
036: for (int i = 0; i < COUNT; i++) {
037: listeners[i] = null;
038: plisteners[i] = null;
039: }
040: listeners = null;
041: plisteners = null;
042: }
043:
044: public void testMessageRegistration() {
045: MessageSource source = new MessageSourceSupport(this );
046: MessageListener[] ls = source.getMessageListeners();
047:
048: assertNotNull(ls);
049: assertEquals("Number of listeners not 0", 0, ls.length);
050:
051: // test add and remove methods.
052:
053: // Add listeners. Ensure that they have been added
054: for (int i = 0; i < listeners.length; i++) {
055: source.addMessageListener(listeners[i]);
056: }
057:
058: ls = source.getMessageListeners();
059: assertEquals(listeners.length, ls.length);
060:
061: // Remove all listenters.
062: for (int i = 0; i < listeners.length; i++) {
063: source.removeMessageListener(listeners[i]);
064: }
065:
066: ls = source.getMessageListeners();
067: assertEquals(0, ls.length);
068:
069: // test to ensure that null adds and removes have no effect.
070: for (int i = 0; i < COUNT; i++) {
071: source.addMessageListener(null);
072: ls = source.getMessageListeners();
073: assertEquals(0, ls.length);
074: }
075:
076: // repopulate the message source.
077: for (int i = 0; i < listeners.length; i++) {
078: source.addMessageListener(listeners[i]);
079: }
080:
081: for (int i = 0; i < COUNT; i++) {
082: source.removeMessageListener(null);
083: ls = source.getMessageListeners();
084: assertEquals(listeners.length, ls.length);
085: }
086: }
087:
088: /**
089: * Test to ensure that messages are dispatched correctly.
090: */
091: public void testMessageDispatch() {
092: MessageSourceSupport source = new MessageSourceSupport(this );
093: for (int i = 0; i < listeners.length; i++) {
094: source.addMessageListener(listeners[i]);
095: }
096:
097: // Test data.
098: String message = "This is a simple message";
099: Level level = Level.INFO;
100: long when = System.currentTimeMillis();
101: MessageEvent evt = null;
102:
103: // Simple test to ensure that all listeners get the same event.
104: source.fireMessage(message);
105: for (int i = 0; i < listeners.length; i++) {
106: evt = listeners[i].getLastEvent();
107: assertNotNull(evt);
108: assertEquals(message, evt.getMessage());
109: }
110:
111: source.fireMessage(message, level);
112: for (int i = 0; i < listeners.length; i++) {
113: evt = listeners[i].getLastEvent();
114: assertNotNull(evt);
115: assertEquals(message, evt.getMessage());
116: assertEquals(level, evt.getLevel());
117: }
118:
119: source.fireMessage(message, level, when);
120: for (int i = 0; i < listeners.length; i++) {
121: evt = listeners[i].getLastEvent();
122: assertNotNull(evt);
123: assertEquals(message, evt.getMessage());
124: assertEquals(level, evt.getLevel());
125: assertEquals(when, evt.getWhen());
126: }
127:
128: evt = new MessageEvent(this , message, level, when);
129: source.fireMessage(evt);
130: for (int i = 0; i < listeners.length; i++) {
131: assertEquals(evt, listeners[i].getLastEvent());
132: }
133: }
134:
135: /**
136: * Tests the exception passing mechanism.
137: */
138: public void testExceptions() {
139: MessageSourceSupport source = new MessageSourceSupport(this );
140: for (int i = 0; i < listeners.length; i++) {
141: source.addMessageListener(listeners[i]);
142: }
143:
144: Exception ex = new Exception("This is a message");
145: MessageEvent evt = null;
146:
147: source.fireException(ex);
148: for (int i = 0; i < listeners.length; i++) {
149: evt = listeners[i].getLastEvent();
150: assertNotNull(evt);
151: // The event message should be the exception message.
152: assertEquals(ex.getMessage(), evt.getMessage());
153: // By default, exceptions should be Level.SEVERE
154: assertEquals(Level.SEVERE, evt.getLevel());
155: }
156:
157: }
158:
159: /**
160: * Test to ensure that progress messages are correctly propagated.
161: */
162: public void testDeterminteProgress() {
163: MessageSourceSupport source = new MessageSourceSupport(this );
164: ProgressEvent evt = null;
165:
166: // Add listeners.
167: for (int i = 0; i < plisteners.length; i++) {
168: source.addProgressListener(plisteners[i]);
169: }
170:
171: // This is a determinate progress operation
172: source.fireProgressStarted(0, 100);
173: for (int i = 0; i < plisteners.length; i++) {
174: evt = plisteners[i].getLastEvent();
175: assertFalse(evt.isIndeterminate());
176: assertTrue(plisteners[i].begin);
177: assertFalse(plisteners[i].end);
178: }
179:
180: source.fireProgressIncremented(50);
181: for (int i = 0; i < plisteners.length; i++) {
182: evt = plisteners[i].getLastEvent();
183: assertFalse(evt.isIndeterminate());
184: assertFalse(plisteners[i].begin);
185: assertFalse(plisteners[i].end);
186: }
187:
188: source.fireProgressEnded();
189: for (int i = 0; i < plisteners.length; i++) {
190: evt = plisteners[i].getLastEvent();
191: assertTrue(evt.isIndeterminate());
192: assertFalse(plisteners[i].begin);
193: assertTrue(plisteners[i].end);
194: }
195: }
196:
197: public void testIndeterminteProgress() {
198: MessageSourceSupport source = new MessageSourceSupport(this );
199: ProgressEvent evt = null;
200:
201: // Add listeners.
202: for (int i = 0; i < plisteners.length; i++) {
203: source.addProgressListener(plisteners[i]);
204: }
205:
206: // This is a determinate progress operation
207: source.fireProgressStarted(0, 0);
208: for (int i = 0; i < plisteners.length; i++) {
209: evt = plisteners[i].getLastEvent();
210: assertTrue(evt.isIndeterminate());
211: assertTrue(plisteners[i].begin);
212: assertFalse(plisteners[i].end);
213: }
214: }
215:
216: /**
217: * A listener of MessageEvents.
218: */
219: private class MListener implements MessageListener {
220:
221: private MessageEvent evt;
222:
223: public void message(MessageEvent evt) {
224: this .evt = evt;
225: }
226:
227: public void exception(MessageEvent evt) {
228: this .evt = evt;
229: }
230:
231: public MessageEvent getLastEvent() {
232: return evt;
233: }
234: }
235:
236: private class PListener implements ProgressListener {
237:
238: private ProgressEvent evt;
239:
240: public boolean begin = false;
241: public boolean end = false;
242:
243: public void progressStarted(ProgressEvent e) {
244: this .evt = e;
245: begin = true;
246: end = false;
247: }
248:
249: public void progressIncremented(ProgressEvent e) {
250: this .evt = e;
251: begin = false;
252: end = false;
253: }
254:
255: public void progressEnded(ProgressEvent e) {
256: this .evt = e;
257: begin = false;
258: end = true;
259: }
260:
261: public void exception(ProgressEvent e) {
262: /**@todo what should we do here? */
263: }
264:
265: public ProgressEvent getLastEvent() {
266: return evt;
267: }
268: }
269: }
|