FileDocCategorySizeDatePackage
TestListener.javaAPI DocphoneME MR2 API (J2ME)8863Wed May 02 18:00:44 BST 2007com.sun.midp.content

TestListener

public class TestListener extends ExtendedTestCase implements javax.microedition.content.RequestListener, javax.microedition.content.ResponseListener
Test that the ContentListenerImpl class correctly notified when Invocations are present.

Fields Summary
private int
requestNotified
Incremented when the invocationRequestNotify method is called.
private int
responseNotified
Incremented when the invocationResponseNotify method is called.
RegistryImpl
registry
Registry in use for the tests.
Constructors Summary
Methods Summary
private voidassertEmpty()
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 voidinvocationRequestNotify(javax.microedition.content.ContentHandlerServer handler)
Notified when an invocation request is present.

param
handler the ContentHandlerServer with the request

	requestNotified++;
    
public voidinvocationResponseNotify(javax.microedition.content.Registry handler)
Notified when an invocation response is present.

param
handler the ContentHandlerServer with the response

	responseNotified++;
    
public voidrunTests()
Run the tests of the Listener.

	test001();
    
private voidtest001()
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);