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 NativeEventPool class.
033: */
034: public class TestNativeEventPool extends TestCase {
035:
036: /**
037: * Tests putting multiple events into the pool.
038: */
039: void testMultiPut() {
040: final int NUM_EVENTS = 3;
041: assertTrue(NUM_EVENTS < NativeEventPool.DEFAULT_SIZE);
042:
043: NativeEventPool pool = new NativeEventPool();
044: NativeEvent evts[] = new NativeEvent[NUM_EVENTS];
045: for (int i = 0; i < NUM_EVENTS; i++) {
046: evts[i] = new NativeEvent();
047: pool.putBack(evts[i]);
048: }
049:
050: assertEquals(NUM_EVENTS, pool.eventsInPool);
051: for (int i = 0; i < NUM_EVENTS; i++) {
052: assertSame(evts[i], pool.eventStack[i]);
053: }
054: }
055:
056: /**
057: * Tests filling up the event pool.
058: */
059: void testFillPool() {
060: NativeEventPool pool = new NativeEventPool();
061:
062: for (int i = 0; i < NativeEventPool.DEFAULT_SIZE; i++) {
063: pool.putBack(new NativeEvent());
064: }
065:
066: assertEquals(NativeEventPool.DEFAULT_SIZE, pool.eventsInPool);
067: pool.putBack(new NativeEvent());
068: assertEquals(NativeEventPool.DEFAULT_SIZE, pool.eventsInPool);
069: }
070:
071: /**
072: * Tests that the event is cleared when it is returned to the pool. This
073: * is at least as much a test of NativeEvent.clear() as it is of the
074: * NativeEventPool's clearing function.
075: */
076: void testClear() {
077: NativeEventPool pool = new NativeEventPool();
078: NativeEvent evt = new NativeEvent(83);
079:
080: evt.intParam1 = 1;
081: evt.intParam2 = 2;
082: evt.intParam3 = 3;
083: evt.intParam4 = 4;
084:
085: evt.stringParam1 = "one";
086: evt.stringParam2 = "two";
087: evt.stringParam3 = "three";
088: evt.stringParam4 = "four";
089: evt.stringParam5 = "five";
090: evt.stringParam6 = "six";
091:
092: pool.putBack(evt);
093:
094: assertEquals(0, evt.type);
095: assertEquals(0, evt.intParam1);
096: assertEquals(0, evt.intParam2);
097: assertEquals(0, evt.intParam3);
098: assertEquals(0, evt.intParam4);
099: assertEquals(null, evt.stringParam1);
100: assertEquals(null, evt.stringParam2);
101: assertEquals(null, evt.stringParam3);
102: assertEquals(null, evt.stringParam4);
103: assertEquals(null, evt.stringParam5);
104: assertEquals(null, evt.stringParam6);
105: }
106:
107: /**
108: * Tests a simple get-put-get sequence.
109: */
110: void testGetPutGet() {
111: NativeEventPool pool = new NativeEventPool();
112: NativeEvent evt = pool.get();
113: assertEquals(0, pool.eventsInPool);
114:
115: pool.putBack(evt);
116: assertEquals(1, pool.eventsInPool);
117: assertSame(evt, pool.eventStack[0]);
118:
119: NativeEvent evt2 = pool.get();
120: assertSame(evt, evt2);
121: assertEquals(0, pool.eventsInPool);
122: }
123:
124: /**
125: * Tests basic creation of a native event pool.
126: */
127: void testCreate() {
128: NativeEventPool pool = new NativeEventPool();
129: assertEquals(0, pool.eventsInPool);
130: assertNotNull(pool.eventStack);
131: }
132:
133: /**
134: * Runs all tests.
135: */
136: public void runTests() throws Throwable {
137: declare("testCreate");
138: testCreate();
139: declare("testGetPutGet");
140: testGetPutGet();
141: declare("testClear");
142: testClear();
143: declare("testFillPool");
144: testFillPool();
145: declare("testMultiPut");
146: testMultiPut();
147: }
148:
149: }
|