TestListenerpublic class TestListener extends ExtendedTestCase implements javax.microedition.content.RequestListener, javax.microedition.content.ResponseListenerTest that the ContentListenerImpl class correctly notified
when Invocations are present. |
Fields Summary |
---|
private int | requestNotifiedIncremented when the invocationRequestNotify method is called. | private int | responseNotifiedIncremented when the invocationResponseNotify method is called. | RegistryImpl | registryRegistry in use for the tests. |
Methods Summary |
---|
private void | assertEmpty()Check that there are no Invocations pending;
none should be.
InvocationImpl get;
do {
get = InvocationStore.
getRequest(registry.application.getStorageId(),
registry.application.getClassname(),
false);
assertNull("Verify no request pending", get);
} while (get != null);
do {
get = InvocationStore.getResponse(new InvocationImpl(),
registry.application.getStorageId(),
registry.application.getClassname(),
false);
assertNull("Verify no response pending", get);
} while (get != null);
assertEquals("verify invocation queue is empty",
0, InvocationStore.size());
| public void | invocationRequestNotify(javax.microedition.content.ContentHandlerServer handler)Notified when an invocation request is present.
requestNotified++;
| public void | invocationResponseNotify(javax.microedition.content.Registry handler)Notified when an invocation response is present.
responseNotified++;
| public void | runTests()Run the tests of the Listener.
test001();
| private void | test001()Test that the listener is notified when a new Invocation
is queued.
declare("Listener called when invocation available");
// Registry that will be doing the listening
registry = getRegistry();
// ContentHandlerImpl doing the listening
ContentHandlerImpl handler = new ReqListener(this);
handler.storageId = registry.application.getStorageId();
handler.classname = registry.application.getClassname();
InvocationImpl put = new InvocationImpl();
put.suiteId = registry.application.getStorageId();
put.classname = registry.application.getClassname();
put.invokingSuiteId = put.suiteId;
put.invokingClassname = put.classname;
put.status = Invocation.INIT;
put.responseRequired = true;
requestNotified = 0;
responseNotified = 0;
InvocationStore.put(put);
// Verify that there is no notification if no listener
sleep(200); // wait a bit
assertEquals("listener should not be notified", 0, requestNotified);
assertEquals("listener should not be notified", 0, responseNotified);
// Now create the monitor threads so the notification will occur
RequestListenerImpl requestImpl =
new RequestListenerImpl(handler, this);
ResponseListenerImpl responseImpl =
new ResponseListenerImpl(registry, this);
sleep(200L); // wait a bit
assertEquals("request listener should be notified",
1, requestNotified);
assertEquals("response listener should not be notified",
0, responseNotified);
// Remove the pending request
InvocationImpl get =
InvocationStore.getRequest(registry.application.getStorageId(),
registry.application.getClassname(),
true);
assertEquals("verify request invocation", put, get);
// Requeue as a response
requestNotified = 0;
responseNotified = 0;
get.status = Invocation.OK;
InvocationStore.setStatus(get);
sleep(200L); // wait a bit
assertEquals("request listener should not be notified",
0, requestNotified);
assertEquals("response listener should be notified",
1, responseNotified);
// Remove the pending notification
get = InvocationStore.getResponse(new InvocationImpl(),
registry.application.getStorageId(),
registry.application.getClassname(),
true);
assertEquals("verify invocation", put, get);
// Now stop the listener thread; same as setListener(null).
requestImpl.setListener(null);
responseImpl.setListener(null);
/*
* With listeners reset put another request and verify no
* notifications.
*/
requestNotified = 0;
responseNotified = 0;
InvocationStore.put(put);
sleep(200L);
assertEquals("request listener should not be notified",
0, requestNotified);
assertEquals("response listener should not be notified",
0, responseNotified);
// Remove the pending request
get = InvocationStore.getRequest(registry.application.getStorageId(),
registry.application.getClassname(),
true);
assertEquals("verify request invocation", put, get);
// Requeue as a response
get.status = Invocation.OK;
InvocationStore.setStatus(get);
sleep(200L); // wait a bit
assertEquals("request listener should not be notified",
0, requestNotified);
assertEquals("response listener should not be notified",
0, responseNotified);
// Remove the pending notification
get = InvocationStore.getResponse(new InvocationImpl(),
registry.application.getStorageId(),
registry.application.getClassname(),
true);
assertEquals("verify invocation", put, get);
/*
* Verify that setting a new listener restarts the monitors.
* Put another Invocation and verify the notification does occur
*/
requestNotified = 0;
responseNotified = 0;
requestImpl.setListener(this);
responseImpl.setListener(this);
InvocationStore.put(put);
sleep(200L);
assertEquals("listener should be notified", 1, requestNotified);
assertEquals("listener should not be notified", 0, responseNotified);
// Remove the pending request
get = InvocationStore.getRequest(registry.application.getStorageId(),
registry.application.getClassname(),
true);
assertEquals("verify request invocation", put, get);
// Requeue as a response
requestNotified = 0;
responseNotified = 0;
get.status = Invocation.OK;
InvocationStore.setStatus(get);
sleep(200L);
assertEquals("request listener should not be notified",
0, requestNotified);
assertEquals("response listener should be notified",
1, responseNotified);
// Remove the pending notification
get = InvocationStore.getResponse(new InvocationImpl(),
registry.application.getStorageId(),
registry.application.getClassname(),
true);
assertEquals("verify invocation", put, get);
// Now stop the listener thread; same as setListener(null).
requestImpl.setListener(null);
responseImpl.setListener(null);
|
|