예제 #1
0
  /** Test notifying job state to a NotificationListener. */
  public void testNotifyState() throws Exception {
    /** Fixture Stateful */
    class MyStateful extends MockStateful {
      StateListener jsl;

      public void addStateListener(StateListener listener) {
        jsl = listener;
        listener.jobStateChange(new StateEvent(this, JobState.READY, null));
      }

      public void removeStateListener(StateListener listener) {}

      public void foo() {
        jsl.jobStateChange(new StateEvent(this, JobState.COMPLETE, null));
      }
    };
    MyStateful myJob = new MyStateful();

    MyNotLis myNotLis = new MyNotLis();

    ServerContext serverContext = new ServerContextImpl(myJob, sm, new OurHierarchicalRegistry());

    // create and register MBean.
    OddjobMBean ojmb =
        new OddjobMBean(
            myJob, OddjobMBeanFactory.objectName(0), new OurServerSession(), serverContext);

    ObjectName on = OddjobMBeanFactory.objectName(0);

    MBeanServer mbs = MBeanServerFactory.createMBeanServer();
    mbs.registerMBean(ojmb, on);

    // add notification listener.
    mbs.addNotificationListener(on, myNotLis, null, null);

    // check null state to begin with
    assertEquals("number", 0, myNotLis.getNum());

    // change state.
    myJob.foo();

    // check state
    assertEquals("number", 1, myNotLis.getNum());
    assertEquals("source", on, myNotLis.getNotification(0).getSource());
    assertEquals(
        "type",
        StatefulHandlerFactory.STATE_CHANGE_NOTIF_TYPE,
        myNotLis.getNotification(0).getType());

    mbs.unregisterMBean(on);
  }
예제 #2
0
  /** Test notification of structural change. */
  public void testNotifyStructure() throws JMException {
    final Object myChild =
        new Object() {
          public String toString() {
            return "my child";
          }
        };

    class MyStructural implements Structural {
      StructuralListener jsl;
      boolean foo = false;

      public void addStructuralListener(StructuralListener listener) {
        jsl = listener;
      }

      public void removeStructuralListener(StructuralListener listener) {}

      public void foo() {
        if (foo) {
          jsl.childRemoved(new StructuralEvent(this, myChild, 0));
        } else {
          jsl.childAdded(new StructuralEvent(this, myChild, 0));
        }
        foo = !foo;
      }
    };
    MyStructural myJob = new MyStructural();

    MyNotLis myNotLis = new MyNotLis();

    MBeanServer mbs = MBeanServerFactory.createMBeanServer();
    OddjobMBeanFactory f = new OddjobMBeanFactory(mbs, new StandardArooaSession());

    ServerContext serverContext = new ServerContextImpl(myJob, sm, new OurHierarchicalRegistry());

    ObjectName on = f.createMBeanFor(myJob, serverContext);

    mbs.addNotificationListener(on, myNotLis, null, null);

    Notification[] notifications =
        (Notification[]) mbs.invoke(on, "structuralSynchronize", new Object[0], new String[0]);
    assertEquals(1, notifications.length);

    myJob.foo();
    assertEquals("notifications", 1, myNotLis.getNum());
    assertEquals("source", on, myNotLis.getNotification(0).getSource());
    assertEquals(
        "type",
        StructuralHandlerFactory.STRUCTURAL_NOTIF_TYPE,
        myNotLis.getNotification(0).getType());

    myJob.foo();
    assertEquals("notifications", 2, myNotLis.getNum());
    assertEquals("source", on, myNotLis.getNotification(1).getSource());
    assertEquals(
        "type",
        StructuralHandlerFactory.STRUCTURAL_NOTIF_TYPE,
        myNotLis.getNotification(1).getType());

    mbs.unregisterMBean(on);
  }