Examples of MutableInteger


Examples of test.MutableInteger

      MonitorTarget target = new MonitorTarget();
      target.setString(reference);
      server.registerMBean(target, name);

      final MutableInteger times = new MutableInteger(0);
      final MutableObject holder = new MutableObject(null);
      NotificationListener listener = new NotificationListener()
      {
         public void handleNotification(Notification notification, Object handback)
         {
            times.set(times.get() + 1);
            holder.set(notification);
         }
      };
      server.addNotificationListener(monitorName, listener, null, null);

      monitor.start();

      try
      {
         sleep(period * 3);
         assertEquals(times.get(), 1);
         MonitorNotification notification = (MonitorNotification)holder.get();
         assertEquals(notification.getType(), MonitorNotification.STRING_TO_COMPARE_VALUE_MATCHED);

         times.set(0);
         holder.set(null);
         target.setString("xx");

         sleep(period * 3);
         assertEquals(times.get(), 1);
         notification = (MonitorNotification)holder.get();
         assertEquals(notification.getType(), MonitorNotification.STRING_TO_COMPARE_VALUE_DIFFERED);

         times.set(0);
         holder.set(null);
         target.setString(reference);

         sleep(period * 3);
         assertEquals(times.get(), 1);
         notification = (MonitorNotification)holder.get();
         assertEquals(notification.getType(), MonitorNotification.STRING_TO_COMPARE_VALUE_MATCHED);

         times.set(0);
         holder.set(null);
         target.setString("yyyy");

         sleep(period * 3);
         assertEquals(times.get(), 1);
         notification = (MonitorNotification)holder.get();
         assertEquals(notification.getType(), MonitorNotification.STRING_TO_COMPARE_VALUE_DIFFERED);

         times.set(0);
         holder.set(null);
         target.setString("zzzzz");

         sleep(period * 3);
         assertEquals(times.get(), 0);
         assertNull(holder.get());
      }
      finally
      {
         monitor.stop();
View Full Code Here

Examples of test.MutableInteger

      NotificationBroadcasterSupport mbean2 = new NotificationSupport.Emitter();

      server.registerMBean(mbean1, name1);
      server.registerMBean(mbean2, name2);

      final MutableInteger integer = new MutableInteger(0);
      NotificationListener listener = new NotificationListener()
      {
         public void handleNotification(Notification notification, Object handback)
         {
            integer.set(integer.get() + 1);
         }
      };

      server.addNotificationListener(name1, listener, null, null);
      server.addNotificationListener(name2, listener, null, null);

      Notification notification = new Notification("test", mbean1, 1);
      mbean1.sendNotification(notification);

      // Be sure the listener is called
      assertEquals("Listener is not called", integer.get(), 1);

      mbean2.sendNotification(notification);

      // Be sure the listener is called
      assertEquals("Listener is not called", integer.get(), 2);

      // Remove one listener
      server.removeNotificationListener(name1, listener);

      // Be sure it is not called
      mbean1.sendNotification(notification);
      assertEquals("Listener is called", integer.get(), 2);

      // Be sure it is called
      mbean2.sendNotification(notification);
      assertEquals("Listener is not called", integer.get(), 3);

      try
      {
         server.removeNotificationListener(name1, listener);
         fail("Listener has been removed");
      }
      catch (ListenerNotFoundException ignored)
      {
      }

      // Remove also the second listener
      server.removeNotificationListener(name2, listener);

      // Be sure it is not called
      mbean2.sendNotification(notification);
      assertEquals("Listener is called", integer.get(), 3);
   }
View Full Code Here

Examples of test.MutableInteger

      NotificationBroadcasterSupport mbean2 = new NotificationSupport.Emitter();

      server.registerMBean(mbean1, name1);
      server.registerMBean(mbean2, name2);

      final MutableInteger integer = new MutableInteger(0);
      NotificationListener listener = new NotificationListener()
      {
         public void handleNotification(Notification notification, Object handback)
         {
            integer.set(integer.get() + 1);
         }
      };

      server.addNotificationListener(name1, listener, null, null);
      server.addNotificationListener(name2, listener, null, null);
      mbean2.addNotificationListener(listener, null, null);

      Notification notification = new Notification("test", mbean1, 1);
      mbean1.sendNotification(notification);

      // Be sure the listener is called
      assertEquals("Listener is not called", integer.get(), 1);

      mbean2.sendNotification(notification);

      // Be sure the listeners are called
      assertEquals("Listeners are not called", integer.get(), 3);

      // Remove one listener
      server.removeNotificationListener(name2, listener);

      // Be sure the listener is called
      mbean2.sendNotification(notification);
      assertEquals("Listener is not called", integer.get(), 4);

      // Be sure it is called
      mbean1.sendNotification(notification);
      assertEquals("Listener is not called", integer.get(), 5);

      server.removeNotificationListener(name1, listener);

      // Be sure it is not called
      mbean1.sendNotification(notification);
      assertEquals("Listener is called", integer.get(), 5);

      // Be sure it is called
      mbean2.sendNotification(notification);
      assertEquals("Listener is not called", integer.get(), 6);

      try
      {
         server.removeNotificationListener(name2, listener);
         fail("Listener has been removed");
      }
      catch (ListenerNotFoundException ignored)
      {
      }

      // Remove also the second listener
      mbean2.removeNotificationListener(listener);

      // Be sure it is not called
      mbean2.sendNotification(notification);
      assertEquals("Listener is called", integer.get(), 6);
   }
View Full Code Here

Examples of test.MutableInteger

   public void testAddRemoveMBeanServerInterceptor() throws Exception
   {
      MBeanServer server = newMBeanServer();
      ObjectName configurator = new ObjectName(MBeanServerInterceptorConfigurator.OBJECT_NAME);

      MutableInteger integer = new MutableInteger(0);
      TestMBeanServerInterceptor tester = new TestMBeanServerInterceptor(integer);
      server.invoke(configurator, "addInterceptor", new Object[]{tester}, new String[]{MBeanServerInterceptor.class.getName()});

      server.getAttribute(configurator, "Running");
      if (integer.get() != 1) fail("Interceptor not installed");

      server.invoke(configurator, "clearInterceptors", null, null);
      server.getAttribute(configurator, "Running");

      // Be sure the interceptor is not anymore in the chain
      if (integer.get() != 1) fail("Interceptor not removed");
   }
View Full Code Here

Examples of test.MutableInteger

   public void testRegisterRemoveMBeanServerInterceptor() throws Exception
   {
      MBeanServer server = newMBeanServer();
      ObjectName configurator = new ObjectName(MBeanServerInterceptorConfigurator.OBJECT_NAME);

      MutableInteger integer = new MutableInteger(0);
      TestMBeanServerInterceptor tester = new TestMBeanServerInterceptor(integer);
      ObjectName name = new ObjectName("Interceptor:category=MBeanServer,type=Test");
      server.invoke(configurator, "registerInterceptor", new Object[]{tester, name}, new String[]{MBeanServerInterceptor.class.getName(), ObjectName.class.getName()});

      server.getMBeanInfo(configurator);
      if (integer.get() != 1) fail("Interceptor not installed");

      // Let's check if the interceptor is registered, let's change something in it
      server.setAttribute(name, new Attribute("Enabled", Boolean.FALSE));

      // Call again
      server.getMBeanInfo(configurator);
      if (integer.get() != 1) fail("Interceptor not registered");

      AttributeList list = new AttributeList();
      list.add(new Attribute("Enabled", Boolean.TRUE));
      server.setAttributes(name, list);

      server.getMBeanInfo(configurator);
      if (integer.get() != 2) fail("Interceptor not enabled");

      server.invoke(configurator, "clearInterceptors", null, null);
      server.getAttribute(configurator, "Running");

      // Be sure the interceptor is not anymore in the chain
      if (integer.get() != 2) fail("Interceptor not removed");
   }
View Full Code Here

Examples of test.MutableInteger

   public void testOneShotNotification() throws Exception
   {
      m_timer.start();

      final long now = System.currentTimeMillis();
      final MutableInteger mid = new MutableInteger(-1);
      final MutableInteger occurrencesCount = new MutableInteger(0);

      final String notifType = "timer-test";
      final long delay = 3 * Timer.ONE_SECOND;

      NotificationListener listener = new NotificationListener()
      {
         public void handleNotification(Notification notification, Object handback)
         {
            // Test that the listener has been called at the specified time
            long elapsed = System.currentTimeMillis() - now;
            assertTrue(elapsed >= delay);
            assertFalse(elapsed - delay > 50);

            assertTrue(notification instanceof TimerNotification);

            Integer id = ((TimerNotification)notification).getNotificationID();
            assertEquals(mid.get(), id.intValue());

            occurrencesCount.set(occurrencesCount.get() + 1);
         }
      };

      m_server.addNotificationListener(m_timerName, listener, new NotificationFilter()
      {
         public boolean isNotificationEnabled(Notification notification)
         {
            return notification.getType().equals(notifType);
         }
      }, null);

      // Notify after a while
      Date date = new Date(now + delay);
      // One shot notification at the specified time
      Integer id = m_timer.addNotification(notifType, "timer-message", "user-data", date);
      mid.set(id.intValue());

      // Sleep to wait for the notification to happen
      sleep(delay * 2);

      // Check notification arrived
      assertTrue(occurrencesCount.get() == 1);

      // Check that it won't be notified again
      assertTrue(m_timer.getNbNotifications() == 0);
   }
View Full Code Here

Examples of test.MutableInteger

      m_timer.start();

      final String notifType = "timer-test";
      final String periodicNotifType = "timer-test-periodic";

      final MutableInteger occurrencesCount = new MutableInteger(0);

      NotificationListener listener = new NotificationListener()
      {
         public void handleNotification(Notification notification, Object handback)
         {
            occurrencesCount.set(occurrencesCount.get() + 1);
         }
      };

      final MutableInteger periodicOccurrences = new MutableInteger(0);
      NotificationListener periodicListener = new NotificationListener()
      {
         public void handleNotification(Notification notification, Object handback)
         {
            periodicOccurrences.set(periodicOccurrences.get() + 1);
         }
      };

      m_server.addNotificationListener(m_timerName, listener, new NotificationFilter()
      {
         public boolean isNotificationEnabled(Notification notification)
         {
            return notification.getType().equals(notifType);
         }
      }, null);
      m_server.addNotificationListener(m_timerName, periodicListener, new NotificationFilter()
      {
         public boolean isNotificationEnabled(Notification notification)
         {
            return notification.getType().equals(periodicNotifType);
         }
      }, null);

      // Register to happen 3 times on the first listener
      long now = System.currentTimeMillis();
      // Notify in one second
      Date date = new Date(now + Timer.ONE_SECOND);
      String message = "timer-message";
      Integer id = m_timer.addNotification(notifType, message, "user-data", date, Timer.ONE_SECOND, 3L);

      // Register to happen periodically
      // Notify in one second
      date = new Date(now + Timer.ONE_SECOND);
      String userDataPeriodic = "user-data-periodic";
      Integer periodicID = m_timer.addNotification(periodicNotifType, "timer-message-periodic", userDataPeriodic, date, Timer.ONE_SECOND);

      // Sleep some time
      sleep(Timer.ONE_SECOND);

      Vector v = m_timer.getAllNotificationIDs();
      assertEquals(v.size(), 2);
      assertTrue(v.contains(id));
      assertTrue(v.contains(periodicID));

      v = m_timer.getNotificationIDs(periodicNotifType);
      assertEquals(v.size(), 1);
      assertTrue(v.contains(periodicID));

      assertEquals(m_timer.getNotificationMessage(id), message);

      assertEquals(m_timer.getNotificationUserData(periodicID), userDataPeriodic);

      // Sleep till the end of the three-time notification
      sleep(Timer.ONE_SECOND * 6);

      // Check that was called the right number of times
      assertEquals(occurrencesCount.get(), 3);

      // The three-time notification is expired now
      v = m_timer.getAllNotificationIDs();
      assertEquals(v.size(), 1);
      assertTrue(v.contains(periodicID));

      Long p = m_timer.getPeriod(periodicID);
      assertEquals(p.longValue(), Timer.ONE_SECOND);

      assertEquals(m_timer.getNotificationType(periodicID), periodicNotifType);

      // Removing non existing notification
      try
      {
         m_timer.removeNotifications("dummy");
         fail("Removed non-existing notification");
      }
      catch (InstanceNotFoundException ignored)
      {
      }

      // Should have already been removed, was the three-shot notification
      try
      {
         m_timer.removeNotification(id);
         fail("Removed non-existing notification");
      }
      catch (InstanceNotFoundException ignored)
      {
      }

      // Some more wait
      sleep(Timer.ONE_SECOND * 3);

      // Removing existing notification
      m_timer.removeNotification(periodicID);

      // Check that none are still present
      assertTrue(m_timer.isEmpty());

      // Wait some more to be sure the periodic listener is not notified anymore
      int periodTimes = periodicOccurrences.get();
      assertTrue(periodTimes > 0);

      sleep(Timer.ONE_SECOND * 5);

      assertEquals(periodicOccurrences.get(), periodTimes);
   }
View Full Code Here

Examples of test.MutableInteger

   public void testAddStopRemoveNotification() throws Exception
   {
      // Check that add + stop + remove behaves correctly

      final MutableInteger count = new MutableInteger(0);
      NotificationListener listener = new NotificationListener()
      {
         public void handleNotification(Notification notification, Object handback)
         {
            count.set(count.get() + 1);
         }
      };
      m_server.addNotificationListener(m_timerName, listener, null, null);

      long now = System.currentTimeMillis();
      Date date = new Date(now + Timer.ONE_SECOND);

      // Periodic notification
      Integer id = m_timer.addNotification("notif-type", "notif-message", "notif-data", date, Timer.ONE_SECOND);
      m_timer.start();

      // Wait for the notifications to arrive...
      sleep(Timer.ONE_SECOND * 2);

      m_timer.stop();

      int counted = count.get();

      assertEquals(m_timer.getNbNotifications(), 1);

      m_timer.removeNotification(id);
      assertTrue(m_timer.isEmpty());

      // Wait some more to be sure that there are no more notifications
      Thread.sleep(Timer.ONE_SECOND * 5);

      assertEquals(counted, count.get());
   }
View Full Code Here

Examples of us.rconner.util.MutableInteger

        public void remove()
        {
            if( !canRemove ) {
                throw new IllegalStateException();
            }
            MutableInteger mutableInt = (MutableInteger) entry.getValue();
            size--;
            mutableInt.value--;
            if( mutableInt.value == 0 ) {
                iter.remove();
            }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.