Package test

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


      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

   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

      assertTrue(m_timer.isEmpty());
   }

   public void testSendPastNotifications3() throws Exception
   {
      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();

      // This periodic notification started in the past, sendPastNotifications is false
      // so only some notification must be emitted
      long occurrences = 10;
      long skip = 4;
      Date date = new Date(now - Timer.ONE_SECOND * skip);
      m_timer.setSendPastNotifications(false);
      m_timer.addNotification("notif-type", "notif-message", "notif-data", date, Timer.ONE_SECOND, occurrences);
      m_timer.start();

      // Wait for the notifications to happen
      sleep(Timer.ONE_SECOND * (occurrences + 1));

      // Sometimes we loose one notification because we're not that fast, it's ok.
      long expected = occurrences - skip;
      if (count.get() != expected && count.get() != expected - 1)
         fail("Expected notifications not emitted: expecting " + expected + " got " + count.get());
      assertTrue(m_timer.isEmpty());
   }
View Full Code Here

   public void testGetAttributeDefault() throws Exception
   {
      ObjectName name = new ObjectName(":type=test");

      MutableInteger counter = new MutableInteger(0);
      ModelMBeanTarget bean = new ModelMBeanTarget(counter);

      String attrName = "FixedContent";

      String[] names = new String[]{"name", "descriptorType", "value", "iterable", "displayName", "default", "currencyTimeLimit"};
      // changed to match the actual behaviour indicated in the specs about currencyTimeLimit
      // currencyTimeLimit is now -1
      Object[] values = new Object[]{attrName, "attribute", null, "false", "", "DEFAULT", "-1"};
      DescriptorSupport attrDescr = new DescriptorSupport(names, values);
      ModelMBeanAttributeInfo attrInfo = new ModelMBeanAttributeInfo(attrName, String.class.getName(), "", true, false, false, attrDescr);
      ModelMBeanInfoSupport info = new ModelMBeanInfoSupport(ModelMBeanTarget.class.getName(), "", new ModelMBeanAttributeInfo[]{attrInfo}, null, null, null);

      RequiredModelMBean rmmb = new RequiredModelMBean();
      rmmb.setModelMBeanInfo(info);
      rmmb.setManagedResource(bean, "ObjectReference");
      m_server.registerMBean(rmmb, name);

      // No get method, should always get the default value back
      int num = 5;
      for (int i = 0; i < num; ++i)
      {
         String value = (String)m_server.getAttribute(name, attrName);
         assertEquals("Returned value is not the default", value, "DEFAULT");
      }

      assertEquals("Wrong staleness algorithm", 0, counter.get());
   }
View Full Code Here

   public void testGetAttributeAlwaysStale() throws Exception
   {
      ObjectName name = new ObjectName(":type=test");

      MutableInteger counter = new MutableInteger(0);
      ModelMBeanTarget bean = new ModelMBeanTarget(counter);

      String attrName = "FixedContent";

      String[] names = new String[]{"name", "descriptorType", "value", "iterable", "displayName", "default", "getMethod", "currencyTimeLimit"};
      // changed to match the actual behaviour indicated in the specs about currencyTimeLimit
      // if currencyTimeLimit is -1 then the value is always stale
      // fix for bug #794313
      Object[] values = new Object[]{attrName, "attribute", null, "false", "", "DEFAULT", "get" + attrName, "-1"};
      DescriptorSupport attrDescr = new DescriptorSupport(names, values);
      ModelMBeanAttributeInfo attrInfo = new ModelMBeanAttributeInfo(attrName, String.class.getName(), "", true, false, false, attrDescr);
      ModelMBeanInfoSupport info = new ModelMBeanInfoSupport(ModelMBeanTarget.class.getName(), "", new ModelMBeanAttributeInfo[]{attrInfo}, null, null, null);

      RequiredModelMBean rmmb = new RequiredModelMBean();
      rmmb.setModelMBeanInfo(info);
      rmmb.setManagedResource(bean, "ObjectReference");
      m_server.registerMBean(rmmb, name);

      // We set the staleness to 0 (-> always stale) so test if the bean method is always called
      String fixed = bean.getFixedContent();
      counter.set(0);
      int num = 5;
      for (int i = 0; i < num; ++i)
      {
         String value = (String)m_server.getAttribute(name, attrName);
         assertEquals("Method returned different value", value, fixed);
      }

      assertEquals("Wrong staleness algorithm: " + counter.get(), counter.get(), num);
   }
View Full Code Here

   public void testGetAttributeNeverStale() throws Exception
   {
      ObjectName name = new ObjectName(":type=test");

      MutableInteger counter = new MutableInteger(0);
      ModelMBeanTarget bean = new ModelMBeanTarget(counter);

      String attrName = "FixedContent";

      String[] names = new String[]{"name", "descriptorType", "value", "iterable", "displayName", "default", "getMethod", "currencyTimeLimit", "value"};
      // changed to match the actual behaviour indicated in the specs about currencyTimeLimit
      // if currencyTimeLimit is 0 then the value is never stale
      // fix for bug #794313
      Object[] values = new Object[]{attrName, "attribute", null, "false", "", "DEFAULT", "get" + attrName, "0", "NEVER"};
      DescriptorSupport attrDescr = new DescriptorSupport(names, values);
      ModelMBeanAttributeInfo attrInfo = new ModelMBeanAttributeInfo(attrName, String.class.getName(), "", true, false, false, attrDescr);
      ModelMBeanInfoSupport info = new ModelMBeanInfoSupport(ModelMBeanTarget.class.getName(), "", new ModelMBeanAttributeInfo[]{attrInfo}, null, null, null);

      RequiredModelMBean rmmb = new RequiredModelMBean();
      rmmb.setModelMBeanInfo(info);
      rmmb.setManagedResource(bean, "ObjectReference");
      m_server.registerMBean(rmmb, name);

      // We set the staleness to 0 (-> never stale) so test that the bean method is never called
      int num = 5;
      for (int i = 0; i < num; ++i)
      {
         String value = (String)m_server.getAttribute(name, attrName);
         assertEquals("Method returned different value", value, "NEVER");
      }

      assertEquals("Wrong staleness algorithm", counter.get(), 0);
   }
View Full Code Here

   public void testGetAttributeStale() throws Exception
   {
      ObjectName name = new ObjectName(":type=test");

      MutableInteger counter = new MutableInteger(0);
      ModelMBeanTarget bean = new ModelMBeanTarget(counter);

      String attrName = "MutableContent";

      String[] names = new String[]{"name", "descriptorType", "value", "iterable", "displayName", "default", "getMethod", "currencyTimeLimit"};
View Full Code Here

      String attrName1 = "FixedContent";
      String attrName2 = "MutableContent";

      ObjectName name = new ObjectName(":type=test");

      MutableInteger counter = new MutableInteger(0);
      ModelMBeanTarget bean = new ModelMBeanTarget(counter);

      String[] names1 = new String[]{"name", "descriptorType", "value", "iterable", "displayName", "getMethod", "currencyTimeLimit"};
      // changed to match the actual behaviour indicated in the specs about currencyTimeLimit
      // currencyTimeLimit is now -1
View Full Code Here

   {
      String attrName1 = "MutableContent";

      ObjectName name = new ObjectName(":type=test");

      MutableInteger counter = new MutableInteger(0);
      ModelMBeanTarget bean = new ModelMBeanTarget(counter);

      String[] names1 = new String[]{"name", "descriptorType", "value", "iterable", "displayName", "setMethod", "getMethod", "currencyTimeLimit"};
      // changed to match the actual behaviour indicated in the specs about currencyTimeLimit
      // currencyTimeLimit is now -1
      Object[] values1 = new Object[]{attrName1, "attribute", null, "false", "", "set" + attrName1, "get" + attrName1, "-1"};
      DescriptorSupport attrDescr1 = new DescriptorSupport(names1, values1);

      ModelMBeanAttributeInfo attrInfo1 = new ModelMBeanAttributeInfo(attrName1, String.class.getName(), "", true, true, false, attrDescr1);
      ModelMBeanInfoSupport info = new ModelMBeanInfoSupport(ModelMBeanTarget.class.getName(), "", new ModelMBeanAttributeInfo[]{attrInfo1}, null, null, null);

      final MutableBoolean storeTester = new MutableBoolean(false);
      RequiredModelMBean rmmb = new StoreTesterRMMB(storeTester);
      rmmb.setModelMBeanInfo(info);
      rmmb.setManagedResource(bean, "ObjectReference");
      m_server.registerMBean(rmmb, name);

      // Adding a attribute change notification listener
      final MutableInteger listenerCount = new MutableInteger(0);
      rmmb.addAttributeChangeNotificationListener(new NotificationListener()
      {
         public void handleNotification(Notification notification, Object handback)
         {
            listenerCount.set(listenerCount.get() + 1);
         }
      }, attrName1, null);

      String value = "SET_FIRST_TIME";
      Attribute attribute = new Attribute(attrName1, value);
      m_server.setAttribute(name, attribute);

      // check that has really been set
      assertEquals(bean.getMutableContent(), value);
      // check through MBeanServer
      assertEquals(m_server.getAttribute(name, attrName1), value);
      // check that listener has been called
      assertEquals(listenerCount.get(), 1);
      // There is no persistence settings, check that store was not called
      assertFalse("Store should not have been called", storeTester.get());

      // Adding a attribute change notification listener with
      // null as attribute. test for bug #742389
      NotificationListener dummyListener = new NotificationListener()
      {
         public void handleNotification(Notification notification, Object handback)
         {
         }
      };

      rmmb.addAttributeChangeNotificationListener(dummyListener, null, null);
      rmmb.removeAttributeChangeNotificationListener(dummyListener, null);

      // Change the persist policy - have to unregeister to call setModelMBeanInfo
      m_server.unregisterMBean(name);
      attrDescr1.setField("persistPolicy", "OnUpdate");
      info.setDescriptor(attrDescr1, "attribute");
      rmmb.setModelMBeanInfo(info);
      storeTester.set(false);
      m_server.registerMBean(rmmb, name);

      value = "SET_SECOND_TIME";
      attribute = new Attribute(attrName1, value);
      m_server.setAttribute(name, attribute);

      // check that listener has been called
      assertEquals(listenerCount.get(), 2);
      // There are persistence settings, check that store was called
      assertTrue("Store should have been called", storeTester.get());

      // Now remove setMethod - again we have to unregister
      m_server.unregisterMBean(name);
      names1 = new String[]{"name", "descriptorType", "value", "iterable", "displayName", "getMethod", "currencyTimeLimit"};
      // changed to match the actual behaviour indicated in the specs about currencyTimeLimit
      // currencyTimeLimit is now -1
      values1 = new Object[]{attrName1, "attribute", null, "false", "", "get" + attrName1, "-1"};
      attrDescr1 = new DescriptorSupport(names1, values1);
      attrDescr1.setField("persistPolicy", "OnUpdate");
      info.setDescriptor(attrDescr1, "attribute");
      rmmb.setModelMBeanInfo(info);
      storeTester.set(false);
      m_server.registerMBean(rmmb, name);

      value = "SET_THIRD_TIME";
      attribute = new Attribute(attrName1, value);
      m_server.setAttribute(name, attribute);

      // check that listener has been called
      assertEquals(listenerCount.get(), 3);
      // There are persistence settings, check that store was called
      assertTrue("Store should have been called", storeTester.get());
      // Check the attribute value
      if (bean.getMutableContent().equals(value))
      {
View Full Code Here

TOP

Related Classes of test.MutableInteger

Copyright © 2018 www.massapicom. 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.