001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package com.sun.midp.events;
028:
029: import com.sun.midp.i3test.*;
030:
031: /**
032: * Unit tests for the EventQueue class.
033: */
034: public class TestEventQueue extends TestCase {
035:
036: /**
037: * Test simple creation of an event queue.
038: */
039: void testCreate() {
040: EventQueue eq = new EventQueue();
041:
042: assertFalse(eq.alive);
043: assertNotNull(eq.dispatchTable);
044: assertTrue(eq.dispatchTable.length > 0);
045: assertNotNull(eq.pool);
046: assertEquals(-1, eq.nativeEventQueueHandle);
047: assertNotNull(eq.eventQueueThread);
048: assertNotNull(eq.eventMonitorThread);
049: assertFalse(eq.eventQueueThread.isAlive());
050: assertFalse(eq.eventMonitorThread.isAlive());
051: }
052:
053: /**
054: * Tests the ability to register an event.
055: */
056: void testRegister() {
057: final int EVENT_TYPE = 7;
058: EventQueue eq = new EventQueue();
059: InstrumentedEventListener iel = new InstrumentedEventListener();
060: eq.registerEventListener(EVENT_TYPE, iel);
061:
062: DispatchData dd = eq.dispatchTable[EVENT_TYPE - 1];
063: assertNotNull(dd);
064: assertEquals(iel, dd.listener);
065: }
066:
067: /**
068: * Tests whether the dispatch table is grown properly.
069: */
070: void testGrowDispatchTable() {
071: final int EVENT_TYPE_A = 4;
072: final int EVENT_TYPE_B = 97;
073: // must be larger than the dispatch table default size
074:
075: EventQueue eq = new EventQueue();
076:
077: assertTrue(EVENT_TYPE_B > eq.dispatchTable.length);
078:
079: InstrumentedEventListener iela = new InstrumentedEventListener();
080: InstrumentedEventListener ielb = new InstrumentedEventListener();
081:
082: eq.registerEventListener(EVENT_TYPE_A, iela);
083: eq.registerEventListener(EVENT_TYPE_B, ielb);
084:
085: DispatchData dda = eq.dispatchTable[EVENT_TYPE_A - 1];
086: assertNotNull(dda);
087: assertEquals(iela, dda.listener);
088:
089: DispatchData ddb = eq.dispatchTable[EVENT_TYPE_B - 1];
090: assertNotNull(ddb);
091: assertEquals(ielb, ddb.listener);
092: }
093:
094: /**
095: * Tests posting of an event.
096: */
097: void testPost1() {
098: final int EVENT_TYPE = 14;
099: EventQueue eq = new EventQueue();
100:
101: InstrumentedEventListener iel = new InstrumentedEventListener();
102: eq.registerEventListener(EVENT_TYPE, iel);
103:
104: Event ev = new Event(EVENT_TYPE);
105: eq.post(ev);
106:
107: // assertions on the event queue
108:
109: assertSame("nextEvent should be ev", ev, eq.nextEvent);
110: assertSame("lastEvent should be ev", ev, eq.lastEvent);
111:
112: // assertions from the event listener
113:
114: Event[] arr;
115:
116: arr = iel.getProcessedEvents();
117: assertEquals("processed should be length 0", 0, arr.length);
118:
119: arr = iel.getPreprocessedEvents();
120: assertEquals("preprocessed should be length 1", 1, arr.length);
121: assertSame("preprocessed[0] should be ev", ev, arr[0]);
122:
123: arr = iel.getWaitingEvents();
124: assertEquals("waiting should be length 1", 1, arr.length);
125: assertNull("waiting[0] should be null", arr[0]);
126: }
127:
128: /**
129: * Tests posting of three events.
130: */
131: void testPost3() {
132: final int EVENT_TYPE_A = 5;
133: final int EVENT_TYPE_B = 7;
134:
135: EventQueue eq = new EventQueue();
136:
137: InstrumentedEventListener iel = new InstrumentedEventListener();
138: eq.registerEventListener(EVENT_TYPE_A, iel);
139: eq.registerEventListener(EVENT_TYPE_B, iel);
140:
141: Event ev0 = new Event(EVENT_TYPE_A);
142: Event ev1 = new Event(EVENT_TYPE_B);
143: Event ev2 = new Event(EVENT_TYPE_A);
144: eq.post(ev0);
145: eq.post(ev1);
146: eq.post(ev2);
147:
148: // assertions on the event queue
149:
150: assertSame("nextEvent should be ev0", ev0, eq.nextEvent);
151: assertSame("lastEvent should be ev2", ev2, eq.lastEvent);
152: assertSame("ev0.next should be ev1", ev1, ev0.next);
153: assertSame("ev1.next should be ev2", ev2, ev1.next);
154: assertNull("ev2.next should be null", ev2.next);
155:
156: // assertions from the event listener
157:
158: Event[] arr;
159:
160: arr = iel.getProcessedEvents();
161: assertEquals("processed should be length 0", 0, arr.length);
162:
163: arr = iel.getPreprocessedEvents();
164: assertEquals("preprocessed should be length 3", 3, arr.length);
165: assertSame("preprocessed[0] should be ev0", ev0, arr[0]);
166: assertSame("preprocessed[1] should be ev1", ev1, arr[1]);
167: assertSame("preprocessed[2] should be ev2", ev2, arr[2]);
168:
169: arr = iel.getWaitingEvents();
170: assertEquals("waiting should be length 3", 3, arr.length);
171: assertNull("waiting[0] should be null", arr[0]);
172: assertNull("waiting[1] should be null", arr[1]);
173: assertEquals("waiting[2] should be ev0", ev0, arr[2]);
174: }
175:
176: /**
177: * Tests preprocessing of events.
178: */
179: void testPreprocess() {
180: EventQueue eq = new EventQueue();
181: InstrumentedEventListener iel = new InstrumentedEventListener(
182: true);
183: final int EVENT_TYPE = 10;
184:
185: eq.registerEventListener(EVENT_TYPE, iel);
186:
187: Event ev0 = new Event(EVENT_TYPE);
188: Event ev1 = new Event(EVENT_TYPE);
189: eq.post(ev0);
190: iel.setPreprocess(false);
191: eq.post(ev1);
192:
193: // assertions on the event queue
194:
195: assertSame("nextEvent should be ev0", ev0, eq.nextEvent);
196: assertSame("lastEvent should be ev0", ev0, eq.lastEvent);
197: assertNull("ev0.next should be null", ev0.next);
198:
199: // assertions from the event listener
200:
201: Event[] arr;
202:
203: arr = iel.getProcessedEvents();
204: assertEquals("processed should be length 0", 0, arr.length);
205:
206: arr = iel.getPreprocessedEvents();
207: assertEquals("preprocessed should be length 2", 2, arr.length);
208: assertSame("preprocessed[0] should be ev0", ev0, arr[0]);
209: assertSame("preprocessed[1] should be ev1", ev1, arr[1]);
210:
211: arr = iel.getWaitingEvents();
212: assertEquals("waiting should be length 2", 2, arr.length);
213: assertNull("waiting[0] should be null", arr[0]);
214: assertEquals("waiting[1] should be ev0", ev0, arr[1]);
215: }
216:
217: /**
218: * Runs all tests.
219: */
220: public void runTests() throws Throwable {
221: declare("testCreate");
222: testCreate();
223: declare("testRegister");
224: testRegister();
225: declare("testGrowDispatchTable");
226: testGrowDispatchTable();
227: declare("testPost1");
228: testPost1();
229: declare("testPost3");
230: testPost3();
231: declare("testPreprocess");
232: testPreprocess();
233: }
234:
235: }
|