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.content;
028:
029: import com.sun.midp.i3test.TestCase;
030:
031: import javax.microedition.content.Invocation;
032: import javax.microedition.content.RequestListener;
033: import javax.microedition.content.ResponseListener;
034: import javax.microedition.content.Registry;
035: import javax.microedition.content.ContentHandlerServer;
036:
037: import java.util.Vector;
038:
039: /**
040: * Test that the ContentListenerImpl class correctly notified
041: * when Invocations are present.
042: */
043: public class TestListener extends ExtendedTestCase implements
044: ResponseListener, RequestListener {
045:
046: /** Incremented when the invocationRequestNotify method is called. */
047: private int requestNotified;
048:
049: /** Incremented when the invocationResponseNotify method is called. */
050: private int responseNotified;
051:
052: /** Registry in use for the tests. */
053: RegistryImpl registry;
054:
055: /**
056: * Run the tests of the Listener.
057: */
058: public void runTests() {
059: test001();
060: }
061:
062: /**
063: * Test that the listener is notified when a new Invocation
064: * is queued.
065: */
066: private void test001() {
067: declare("Listener called when invocation available");
068:
069: // Registry that will be doing the listening
070: registry = getRegistry();
071:
072: // ContentHandlerImpl doing the listening
073: ContentHandlerImpl handler = new ReqListener(this );
074: handler.storageId = registry.application.getStorageId();
075: handler.classname = registry.application.getClassname();
076:
077: InvocationImpl put = new InvocationImpl();
078: put.suiteId = registry.application.getStorageId();
079: put.classname = registry.application.getClassname();
080: put.invokingSuiteId = put.suiteId;
081: put.invokingClassname = put.classname;
082: put.status = Invocation.INIT;
083: put.responseRequired = true;
084:
085: requestNotified = 0;
086: responseNotified = 0;
087: InvocationStore.put(put);
088:
089: // Verify that there is no notification if no listener
090: sleep(200); // wait a bit
091: assertEquals("listener should not be notified", 0,
092: requestNotified);
093: assertEquals("listener should not be notified", 0,
094: responseNotified);
095:
096: // Now create the monitor threads so the notification will occur
097: RequestListenerImpl requestImpl = new RequestListenerImpl(
098: handler, this );
099: ResponseListenerImpl responseImpl = new ResponseListenerImpl(
100: registry, this );
101:
102: sleep(200L); // wait a bit
103: assertEquals("request listener should be notified", 1,
104: requestNotified);
105: assertEquals("response listener should not be notified", 0,
106: responseNotified);
107:
108: // Remove the pending request
109: InvocationImpl get = InvocationStore.getRequest(
110: registry.application.getStorageId(),
111: registry.application.getClassname(), true);
112: assertEquals("verify request invocation", put, get);
113:
114: // Requeue as a response
115: requestNotified = 0;
116: responseNotified = 0;
117: get.status = Invocation.OK;
118: InvocationStore.setStatus(get);
119:
120: sleep(200L); // wait a bit
121: assertEquals("request listener should not be notified", 0,
122: requestNotified);
123: assertEquals("response listener should be notified", 1,
124: responseNotified);
125:
126: // Remove the pending notification
127: get = InvocationStore.getResponse(new InvocationImpl(),
128: registry.application.getStorageId(),
129: registry.application.getClassname(), true);
130: assertEquals("verify invocation", put, get);
131:
132: // Now stop the listener thread; same as setListener(null).
133: requestImpl.setListener(null);
134: responseImpl.setListener(null);
135:
136: /*
137: * With listeners reset put another request and verify no
138: * notifications.
139: */
140: requestNotified = 0;
141: responseNotified = 0;
142: InvocationStore.put(put);
143: sleep(200L);
144: assertEquals("request listener should not be notified", 0,
145: requestNotified);
146: assertEquals("response listener should not be notified", 0,
147: responseNotified);
148:
149: // Remove the pending request
150: get = InvocationStore.getRequest(registry.application
151: .getStorageId(), registry.application.getClassname(),
152: true);
153: assertEquals("verify request invocation", put, get);
154:
155: // Requeue as a response
156: get.status = Invocation.OK;
157: InvocationStore.setStatus(get);
158:
159: sleep(200L); // wait a bit
160: assertEquals("request listener should not be notified", 0,
161: requestNotified);
162: assertEquals("response listener should not be notified", 0,
163: responseNotified);
164:
165: // Remove the pending notification
166: get = InvocationStore.getResponse(new InvocationImpl(),
167: registry.application.getStorageId(),
168: registry.application.getClassname(), true);
169: assertEquals("verify invocation", put, get);
170:
171: /*
172: * Verify that setting a new listener restarts the monitors.
173: * Put another Invocation and verify the notification does occur
174: */
175: requestNotified = 0;
176: responseNotified = 0;
177:
178: requestImpl.setListener(this );
179: responseImpl.setListener(this );
180:
181: InvocationStore.put(put);
182: sleep(200L);
183: assertEquals("listener should be notified", 1, requestNotified);
184: assertEquals("listener should not be notified", 0,
185: responseNotified);
186:
187: // Remove the pending request
188: get = InvocationStore.getRequest(registry.application
189: .getStorageId(), registry.application.getClassname(),
190: true);
191: assertEquals("verify request invocation", put, get);
192:
193: // Requeue as a response
194: requestNotified = 0;
195: responseNotified = 0;
196: get.status = Invocation.OK;
197: InvocationStore.setStatus(get);
198:
199: sleep(200L);
200: assertEquals("request listener should not be notified", 0,
201: requestNotified);
202: assertEquals("response listener should be notified", 1,
203: responseNotified);
204:
205: // Remove the pending notification
206: get = InvocationStore.getResponse(new InvocationImpl(),
207: registry.application.getStorageId(),
208: registry.application.getClassname(), true);
209: assertEquals("verify invocation", put, get);
210:
211: // Now stop the listener thread; same as setListener(null).
212: requestImpl.setListener(null);
213: responseImpl.setListener(null);
214: }
215:
216: /**
217: * Notified when an invocation request is present.
218: * @param handler the ContentHandlerServer with the request
219: */
220: public void invocationRequestNotify(ContentHandlerServer handler) {
221: requestNotified++;
222: }
223:
224: /**
225: * Notified when an invocation response is present.
226: * @param handler the ContentHandlerServer with the response
227: */
228: public void invocationResponseNotify(Registry handler) {
229: responseNotified++;
230: }
231:
232: /**
233: * Check that there are no Invocations pending;
234: * none should be.
235: */
236: private void assertEmpty() {
237: InvocationImpl get;
238: do {
239: get = InvocationStore.getRequest(registry.application
240: .getStorageId(), registry.application
241: .getClassname(), false);
242: assertNull("Verify no request pending", get);
243: } while (get != null);
244: do {
245: get = InvocationStore.getResponse(new InvocationImpl(),
246: registry.application.getStorageId(),
247: registry.application.getClassname(), false);
248: assertNull("Verify no response pending", get);
249: } while (get != null);
250: assertEquals("verify invocation queue is empty", 0,
251: InvocationStore.size());
252: }
253:
254: /**
255: * Shell class to be notified of available requests.
256: */
257: static class ReqListener extends ContentHandlerImpl implements
258: ContentHandlerServer {
259: /** Listener to be notified on request available. */
260: RequestListener listener;
261:
262: /**
263: * Constructor for test handler.
264: * @param l the listener to be notified
265: */
266: ReqListener(RequestListener l) {
267: listener = l;
268: }
269:
270: /**
271: * Overridden method to be notified.
272: */
273: protected void requestNotify() {
274: listener.invocationRequestNotify(this );
275: }
276:
277: public boolean finish(Invocation invocation, int status) {
278: return false;
279: }
280:
281: public Invocation getRequest(boolean wait) {
282: return null;
283: }
284:
285: }
286: }
|