connectioncontrollertest.java
来自「This is a resource based on j2me embedde」· Java 代码 · 共 1,253 行 · 第 1/4 页
JAVA
1,253 行
createConnectionController(store, lifecycleAdapter); cc.registerConnection(midletSuiteId, midlet1, descriptor1); cc.registerConnection(midletSuiteId, midlet2, descriptor2); final DelayableThread t1 = descriptor1.connectionReservation .pingThread(); final DelayableThread t2 = descriptor2.connectionReservation .pingThread(); synchronized (t1.lock) { synchronized (t2.lock) { // start the threads first... t1.start(); t2.start(); // ...but before listeners starts, uninstall the suite... cc.removeSuiteConnections(midletSuiteId); // ...now let listeners proceed } } try { t1.join(); t2.join(); } catch (InterruptedException ie) { fail("Unexpected InterruptedException: " + ie); } assertTrue(lifecycleAdapter.hasNotBeenInvoked()); } private void assertSetsEqualDeep( final Object [] expected, final Object [] actual) { /** * IMPL_NOTE: haven't found better way yet. Technically * speaking it's not even quite correct: imho there is * no guarantees that "same" hash sets will produce same * toArray's, but chances are high, really */ final HashSet e = new HashSet(Arrays.asList(expected)); final HashSet a = new HashSet(Arrays.asList(actual)); Arrays.equals(e.toArray(), a.toArray()); } public void testStateAfterDispose() throws IOException { final int midletSuiteId1 = 123; final String midlet1 = "com.sun.Foo"; final String connection1 = "foo://bar"; final String filter1 = "*.123"; final MockReservationDescriptor descriptor1 = new MockReservationDescriptor(connection1, filter1); final String midlet2 = "com.sun.Bar"; final String connection2 = "foo://qux"; final String filter2 = "*.*"; final MockReservationDescriptor descriptor2 = new MockReservationDescriptor(connection2, filter2); final int midletSuiteId2 = midletSuiteId1 + 17; final String midlet3 = "com.sun.Qux"; final String connection3 = "foo4://bar"; final String filter3 = "4.*.123"; final MockReservationDescriptor descriptor3 = new MockReservationDescriptor(connection3, filter3); final Store store = createStore(); final ConnectionController cc = createConnectionController(store); cc.registerConnection(midletSuiteId1, midlet1, descriptor1); cc.registerConnection(midletSuiteId1, midlet2, descriptor2); cc.registerConnection(midletSuiteId2, midlet3, descriptor3); cc.dispose(); // Check that reservations have been canceled assertTrue(descriptor1.connectionReservation.isCancelled); assertTrue(descriptor2.connectionReservation.isCancelled); assertTrue(descriptor3.connectionReservation.isCancelled); // Check that there is nothing registered assertNull(cc.getMIDlet(midletSuiteId1, connection1)); assertNull(cc.getFilter(midletSuiteId1, connection1)); assertNull(cc.getMIDlet(midletSuiteId1, connection2)); assertNull(cc.getFilter(midletSuiteId1, connection2)); assertEquals(0, cc.listConnections(midletSuiteId1, false).length); assertNull(cc.getMIDlet(midletSuiteId2, connection3)); assertNull(cc.getFilter(midletSuiteId2, connection3)); assertEquals(0, cc.listConnections(midletSuiteId2, false).length); // But check that all the connections reside in the persistent store store.listConnections(new Store.ConnectionsConsumer() { public void consume( final int id, final ConnectionInfo [] infos) { switch (id) { case midletSuiteId1: assertSetsEqualDeep(new ConnectionInfo [] { new ConnectionInfo(midlet1, connection1, filter1), new ConnectionInfo(midlet2, connection2, filter2), }, infos); break; case midletSuiteId2: assertSetsEqualDeep(new ConnectionInfo [] { new ConnectionInfo(midlet3, connection3, filter3), }, infos); break; default: fail("Unexpected suite id"); } } }); } public void testDisposeCancellation() throws IOException { final int midletSuiteId1 = 123; final String midlet1 = "com.sun.Foo"; final String connection1 = "foo://bar"; final String filter1 = "*.123"; final MockReservationDescriptor descriptor1 = new MockReservationDescriptor(connection1, filter1); final String midlet2 = "com.sun.Bar"; final String connection2 = "foo://qux"; final String filter2 = "*.*"; final MockReservationDescriptor descriptor2 = new MockReservationDescriptor(connection2, filter2); final int midletSuiteId2 = midletSuiteId1 + 17; final String midlet3 = "com.sun.Qux"; final String connection3 = "foo4://bar"; final String filter3 = "4.*.123"; final MockReservationDescriptor descriptor3 = new MockReservationDescriptor(connection3, filter3); final Store store = createStore(); final ListingLifecycleAdapter lifecycleAdapter = new ListingLifecycleAdapter(); final ConnectionController cc = createConnectionController(store, lifecycleAdapter); cc.registerConnection(midletSuiteId1, midlet1, descriptor1); cc.registerConnection(midletSuiteId1, midlet2, descriptor2); cc.registerConnection(midletSuiteId2, midlet3, descriptor3); final DelayableThread t1 = descriptor1.connectionReservation .pingThread(); final DelayableThread t2 = descriptor2.connectionReservation .pingThread(); final DelayableThread t3 = descriptor3.connectionReservation .pingThread(); synchronized (t1.lock) { synchronized (t2.lock) { synchronized (t3.lock) { // start the threads first... t1.start(); t2.start(); t3.start(); // ...but before listeners starts, dispose the controller... cc.dispose(); // ...now let listeners proceed } } } try { t1.join(); t2.join(); t3.join(); } catch (InterruptedException ie) { fail("Unexpected InterruptedException: " + ie); } // Check that nothing has been launched assertTrue(lifecycleAdapter.hasNotBeenInvoked()); } private static final class Registration { private final MIDPApp app; private final String connection; private final String filter; Registration(final int midletSuiteId, final String midlet, final String connection, final String filter) { this.app = new MIDPApp(midletSuiteId, midlet); this.connection = connection; this.filter = filter; } } public void testStartup() throws IOException { final int suiteId1 = 123; final int suiteId2 = 321; final Registration [] registrations = { new Registration(suiteId1, "com.sun.Foo", "foo://bar", "*.123"), new Registration(suiteId2, "com.sun.Foo", "foo:qux", "*.*.*"), new Registration(suiteId1, "com.sun.Qux", "qux:123", "*"), }; final Store store = createStore(); for (int i = 0; i < registrations.length; i++) { final Registration r = registrations[i]; final ConnectionInfo info = new ConnectionInfo( r.connection, r.app.midlet, r.filter); store.addConnection(r.app.midletSuiteID, info); } final ListingLifecycleAdapter lifecycleAdapter = new ListingLifecycleAdapter(); final ConnectionController cc = createConnectionController(store, lifecycleAdapter); assertSetsEqual( new String [] { registrations[0].connection, registrations[2].connection, }, cc.listConnections(suiteId1, false)); assertSetsEqual( new String [] { registrations[1].connection, }, cc.listConnections(suiteId2, false)); // And now check both MIDlets and filters for (int i = 0; i < registrations.length; i++) { final Registration r = registrations[i]; final int id = r.app.midletSuiteID; final String c = r.connection; assertEquals(r.app.midlet, cc.getMIDlet(id, c)); assertEquals(r.filter, cc.getFilter(id, c)); } } public void testAccessorsForAnotherSuite() throws IOException { final int midletSuiteId = 123; final String midlet = "com.sun.Foo"; final String connection = "foo://bar"; final String filter = "*.123"; final int midletSuiteId2 = 1001 - midletSuiteId; final ConnectionController cc = createConnectionController(createStore()); cc.registerConnection(midletSuiteId, midlet, new MockReservationDescriptor(connection, filter)); assertNull(cc.getMIDlet(midletSuiteId2, connection)); assertNull(cc.getFilter(midletSuiteId2, connection)); } public void testAccessorsForUnregistered() throws IOException { final int midletSuiteId = 123; final String connection = "foo://bar"; final ConnectionController cc = createConnectionController(createStore()); assertNull(cc.getMIDlet(midletSuiteId, connection)); assertNull(cc.getFilter(midletSuiteId, connection)); } private void pingDescriptor(final MockReservationDescriptor descriptor) { final Thread t = descriptor.connectionReservation.pingThread(); t.start(); try { t.join(); } catch (InterruptedException ie) { fail("Unexpected InterruptedException: " + ie); } } public void testThrowingLifecycleAdapter() throws IOException { final int midletSuiteId = 123; final String midlet = "com.sun.Foo"; final String connection = "foo://bar"; final String filter = "*.123"; final Store store = createStore(); final MockReservationDescriptor descriptor = new MockReservationDescriptor(connection, filter); final ListingLifecycleAdapter listingLifecycleAdapter = new ListingLifecycleAdapter(); final LifecycleAdapter throwingLifecycleAdapter = new ThrowingLifecycleAdapter(); final ProxyLifecycleAdapter lifecycleAdapter = new ProxyLifecycleAdapter(); final ConnectionController cc = createConnectionController(store, lifecycleAdapter); cc.registerConnection(midletSuiteId, midlet, descriptor); lifecycleAdapter.setProxy(throwingLifecycleAdapter); pingDescriptor(descriptor); lifecycleAdapter.setProxy(listingLifecycleAdapter); pingDescriptor(descriptor); assertTrue(listingLifecycleAdapter .hasBeenInvokedOnceFor(midletSuiteId, midlet)); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?