Esempio n. 1
0
  public void testClose() throws Exception {
    Participant provider = new Participant("prov");
    Participant consumer = new Participant("cons");
    final Observer obs = newObserver(consumer, InterfaceA.class);
    final ObserverListener listener = new ObserverListener(consumer);
    obs.registerListener(listener);
    provider.createA(A);
    waitForEvent(listener, provider, A);

    ProxyBusObject pbo = obs.getFirst();
    assertNotNull(pbo);

    obs.close();
    obs.close();
    obs.registerListener(listener);
    assertNull(obs.getFirst());
    assertNull(obs.get(pbo.getBusName(), pbo.getObjPath()));
    assertNull(obs.getNext(pbo));
    obs.unregisterListener(listener);
    obs.unregisterAllListeners();
    obs.close();
    Thread.sleep(100);
    listener.expectInvocations(0);
  }
Esempio n. 2
0
  public void testInvalidArgs() throws Exception {
    Participant test = new Participant("invArgs");
    Observer obs = null;
    /* null bus pointer is invalid expect a NullPointerException */
    try {
      obs = newObserver(null, InterfaceA.class);
      fail("bus can't be null");
    } catch (NullPointerException rt) {
        /* OK */
      assertNull(obs);
    }
    /* must pass in a valid interface Class<?>[] array */
    try {
      obs = new Observer(test.bus, null);
      fail("null array value is not allowed");
    } catch (NullPointerException rt) {
        /* OK */
      assertNull(obs);
    }
    /* array can not contain a null value. */
    try {
      obs = newObserver(test, new Class<?>[] {null});
      fail("Array with null value is not allowed");
    } catch (NullPointerException rt) {
        /* OK */
      assertNull(obs);
    }
    /* empty array is not allowed */
    try {
      obs = newObserver(test);
      fail("Empty array is not allowed");
    } catch (IllegalArgumentException iae) {
        /* OK */
      assertNull(obs);
    }
    /* class must be a BusInterface */
    try {
      obs = newObserver(test, String.class);
      fail("Class is not allowed");
    } catch (IllegalArgumentException iae) {
        /* OK */
      assertNull(obs);
    }

    /* all classes must be BusInterfaces */
    try {
      obs =
          new Observer(test.bus, new Class<?>[] {InterfaceA.class}, new Class<?>[] {String.class});
      fail("Class is not allowed");
    } catch (IllegalArgumentException iae) {
        /* OK */
      assertNull(obs);
    }
    assertNull(obs);
    obs = new Observer(test.bus, new Class<?>[] {InterfaceA.class}, null); // null
    // allowed.

    try {
      obs.registerListener(null);
      fail("null not allowed");
    } catch (IllegalArgumentException rt) {
        /* OK */
      assertNotNull(obs);
    }

    final ObserverListener listener = new ObserverListener(test);
    obs.registerListener(listener);
    listener.expectInvocations(1);
    test.createA(A);
    test.registerObject(A);
    assertTrue(waitForLambda(3000, new SingleLambda(listener)));

    // null values are harmless for the object.
    try {
      obs.get(null, A);
      fail("Expected Observer.get(null, path) to throw a NullPointerException.");
    } catch (NullPointerException npe) {
        /* OK */
      assertNotNull(obs);
    }
    try {
      obs.get(test.bus.getUniqueName(), null);
      fail("Expected Observer.get(bus, null) to throw a NullPointerException.");
    } catch (NullPointerException npe) {
        /* OK */
      assertNotNull(obs);
    }
    assertNull(obs.get(test.bus.getUniqueName(), AB));
    assertNull(obs.get(A, A));
    assertNotNull(obs.get(test.bus.getUniqueName(), makePath(A)));

    Method finalize = obs.getClass().getDeclaredMethod("finalize");
    finalize.setAccessible(true);
    finalize.invoke(obs);
    finalize.invoke(obs);
    finalize.invoke(obs);
  }
Esempio n. 3
0
  public void simpleScenario(Participant provider, Participant consumer) {
    provider.createA("justA");
    provider.createB("justB");
    provider.createAB("both");

    final Observer obsA = newObserver(consumer, InterfaceA.class);
    final ObserverListener listenerA = new ObserverListener(consumer);
    obsA.registerListener(listenerA);
    final Observer obsB = newObserver(consumer, InterfaceB.class);
    final ObserverListener listenerB = new ObserverListener(consumer);
    obsB.registerListener(listenerB);
    final Observer obsAB = newObserver(consumer, InterfaceA.class, InterfaceB.class);
    final ObserverListener listenerAB = new ObserverListener(consumer);
    obsAB.registerListener(listenerAB);

    Lambda allDone =
        new Lambda() {
          @Override
          public boolean func() {
            return listenerA.getCounter() == 0
                && listenerB.getCounter() == 0
                && listenerAB.getCounter() == 0;
          }
        };
    /* let provider publish objects on the bus */
    listenerA.expectInvocations(2);
    listenerB.expectInvocations(2);
    listenerAB.expectInvocations(1);

    provider.registerObject("justA");
    provider.registerObject("justB");
    provider.registerObject("both");
    assertTrue(waitForLambda(3000, allDone));

    /* remove justA from the bus */
    listenerA.expectInvocations(1);
    listenerB.expectInvocations(0);
    listenerAB.expectInvocations(0);

    provider.unregisterObject("justA");
    assertTrue(waitForLambda(3000, allDone));

    /* remove "both" from the bus */
    listenerA.expectInvocations(1);
    listenerB.expectInvocations(1);
    listenerAB.expectInvocations(1);

    provider.unregisterObject("both");
    assertTrue(waitForLambda(3000, allDone));

    /* count the number of proxies left in the Observers.
     * There should be 0 in A, 1 in B, 0 in AB */
    assertEquals(0, countProxies(obsA));
    assertEquals(1, countProxies(obsB));
    assertEquals(0, countProxies(obsAB));

    /* remove all listeners */
    obsA.unregisterAllListeners();
    obsB.unregisterAllListeners();
    obsAB.unregisterListener(listenerAB);

    /* remove "justB" and reinstate the other objects */
    listenerA.expectInvocations(0);
    listenerB.expectInvocations(0);
    listenerAB.expectInvocations(0);
    provider.unregisterObject("justB");
    provider.registerObject("justA");
    provider.registerObject("both");

    /* busy-wait for a second at most */
    assertTrue(
        waitForLambda(
            1000,
            new Lambda() {
              @Override
              public boolean func() {
                return 2 == countProxies(obsA)
                    && 1 == countProxies(obsB)
                    && 1 == countProxies(obsAB);
              }
            }));

    /* reinstate listeners & test triggerOnExisting functionality */
    listenerA.expectInvocations(2);
    listenerB.expectInvocations(1);
    listenerAB.expectInvocations(1);
    obsA.registerListener(listenerA);
    obsB.registerListener(listenerB);
    obsAB.registerListener(listenerAB);

    assertTrue(waitForLambda(3000, allDone));

    /* test multiple listeners for same observer */
    final ObserverListener listenerB2 = new ObserverListener(consumer);
    listenerB2.expectInvocations(0);
    obsB.registerListener(listenerB2, false);

    Lambda allDone2 =
        new Lambda() {
          @Override
          public boolean func() {
            return listenerA.getCounter() == 0
                && listenerB.getCounter() == 0
                && listenerB2.getCounter() == 0
                && listenerAB.getCounter() == 0;
          }
        };

    listenerA.expectInvocations(0);
    listenerB.expectInvocations(1);
    listenerB2.expectInvocations(1);
    listenerAB.expectInvocations(0);
    provider.registerObject("justB");
    assertTrue(waitForLambda(1000, allDone2));

    /* are all objects back where they belong? */
    assertEquals(2, countProxies(obsA));
    assertEquals(2, countProxies(obsB));
    assertEquals(1, countProxies(obsAB));

    /* test multiple observers for the same set of interfaces */
    final Observer obsB2 = newObserver(consumer, InterfaceB.class);
    obsB.unregisterListener(
        listenerB2); /* unregister listenerB2 from obsB so we can reuse it here */
    listenerA.expectInvocations(0);
    listenerB.expectInvocations(0);
    listenerB2.expectInvocations(2);
    listenerAB.expectInvocations(0);
    obsB2.registerListener(listenerB2);
    assertTrue(waitForLambda(1000, allDone2));

    /* test Observer::Get() and the proxy creation functionality */
    ProxyBusObject proxy;
    proxy = obsA.get(provider.bus.getUniqueName(), makePath("justA"));
    assertTrue(null != proxy);

    proxy = obsA.get(provider.bus.getUniqueName(), makePath("both"));
    assertTrue(null != proxy);

    /* verify that we can indeed perform method calls */
    InterfaceA intfA = proxy.getInterface(InterfaceA.class);
    try {
      String id = intfA.methodA();
      assertEquals(provider.bus.getUniqueName() + "@" + makePath("both"), id);
    } catch (BusException e) {
      fail("method call failed: " + e);
    }
  }
Esempio n. 4
0
 private void checkGet(ProxyBusObject obj, Observer obs) {
   assertSame(obj, obs.get(obj.getBusName(), obj.getObjPath()));
 }