geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jdil...@apache.org
Subject cvs commit: incubator-geronimo/modules/common/src/test/org/apache/geronimo/common/jmx MBeanProxyHandlerTest.java MockObject.java MBeanProxyFactoryTest.java
Date Mon, 01 Sep 2003 19:18:47 GMT
jdillon     2003/09/01 12:18:47

  Modified:    modules/common/src/java/org/apache/geronimo/common/jmx
                        MBeanProxyHandler.java
               modules/common/src/test/org/apache/geronimo/common/jmx
                        MBeanProxyFactoryTest.java
  Added:       modules/common/src/test/org/apache/geronimo/common/jmx
                        MBeanProxyHandlerTest.java MockObject.java
  Log:
   o Added MBeanProxyHandlerTest to verify that Tasks are being cached
     properly
   o Moved MockObject to a top-level class as MBeanProxyHandlerTest and
     MBeanProxyFactoryTest use it.
   o Generating MBean interface for MockObject with XDoclet
  
  Revision  Changes    Path
  1.5       +12 -19    incubator-geronimo/modules/common/src/java/org/apache/geronimo/common/jmx/MBeanProxyHandler.java
  
  Index: MBeanProxyHandler.java
  ===================================================================
  RCS file: /home/cvs/incubator-geronimo/modules/common/src/java/org/apache/geronimo/common/jmx/MBeanProxyHandler.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- MBeanProxyHandler.java	1 Sep 2003 15:11:57 -0000	1.4
  +++ MBeanProxyHandler.java	1 Sep 2003 19:18:47 -0000	1.5
  @@ -60,7 +60,6 @@
   import java.lang.reflect.Method;
   
   import java.util.Map;
  -import java.util.HashMap;
   
   import javax.management.MBeanServer;
   import javax.management.ObjectName;
  @@ -69,10 +68,8 @@
   import javax.management.MBeanAttributeInfo;
   import javax.management.MBeanOperationInfo;
   import javax.management.MBeanException;
  -import javax.management.ReflectionException;
  -import javax.management.RuntimeOperationsException;
  -import javax.management.RuntimeMBeanException;
  -import javax.management.RuntimeErrorException;
  +
  +import org.apache.commons.collections.ReferenceMap;
   
   import org.apache.geronimo.common.NullArgumentException;
   
  @@ -89,10 +86,6 @@
       protected Map attributeMap;
       protected Map taskCache;
       
  -    //
  -    // TODO: Replace cache map with a backing which will not eat memory
  -    //
  -    
       public MBeanProxyHandler(final MBeanServer server,
                                final ObjectName target)
       {
  @@ -232,7 +225,8 @@
       {
           assert method != null;
           
  -        if (taskCache == null) {
  +        // Lazy init the attribute map
  +        if (attributeMap == null) {
               // Allow sub-class overrides
               ObjectName target = getObjectName();
               
  @@ -240,14 +234,11 @@
               MBeanInfo info = server.getMBeanInfo(target);
               
               // Load up the attribute mapping
  -            attributeMap = new HashMap();
  +            attributeMap = new ReferenceMap(ReferenceMap.HARD, ReferenceMap.SOFT);
               MBeanAttributeInfo[] attributes = info.getAttributes();
               for (int i=0; i<attributes.length; i++) {
                   attributeMap.put(attributes[i].getName(), attributes[i]);
               }
  -            
  -            // Initialize the task cache
  -            taskCache = new HashMap();
           }
           
           String methodName = method.getName();
  @@ -297,11 +288,13 @@
       protected Task getTask(final Method method, final Object[] args)
           throws Exception
       {
  -        // Check if there is a cached task
  -        Task task = null;
  -        if (taskCache != null) {
  -            task = (Task)taskCache.get(method);
  +        // Lazy init the cache
  +        if (taskCache == null) {
  +            taskCache = new ReferenceMap(ReferenceMap.SOFT, ReferenceMap.SOFT);
           }
  +        
  +        // Check if there is a cached task
  +        Task task = (Task)taskCache.get(method);
           
           // If not create one and cache it
           if (task == null) {
  
  
  
  1.5       +1 -76     incubator-geronimo/modules/common/src/test/org/apache/geronimo/common/jmx/MBeanProxyFactoryTest.java
  
  Index: MBeanProxyFactoryTest.java
  ===================================================================
  RCS file: /home/cvs/incubator-geronimo/modules/common/src/test/org/apache/geronimo/common/jmx/MBeanProxyFactoryTest.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- MBeanProxyFactoryTest.java	1 Sep 2003 15:15:03 -0000	1.4
  +++ MBeanProxyFactoryTest.java	1 Sep 2003 19:18:47 -0000	1.5
  @@ -166,79 +166,4 @@
           assertEquals(target, ctx.getObjectName());
           assertNotNull(ctx.getMBeanServer());
       }
  -    
  -    //
  -    // Mock MBean
  -    //
  -    
  -    public static interface MockObjectMBean
  -    {
  -        String getString();
  -        
  -        boolean isSomething();
  -        
  -        void setString(String value);
  -        
  -        String doIt();
  -        
  -        String setPoorlyNameOperation();
  -        
  -        String someOperation();
  -        
  -        String someOperation(Object arg);
  -        
  -        String someOperation(boolean arg);
  -    }
  -    
  -    public static class MockObject
  -        implements MockObjectMBean
  -    {
  -        String string = "MyString";
  -        boolean something;
  -        
  -        public void setString(String value)
  -        {
  -            this.string = value;
  -        }
  -        
  -        public String getString()
  -        {
  -            return string;
  -        }
  -        
  -        public void setSomething(boolean flag)
  -        {
  -            something = flag;
  -        }
  -        
  -        public boolean isSomething()
  -        {
  -            return something;
  -        }
  -        
  -        public String doIt()
  -        {
  -            return "done";
  -        }
  -        
  -        public String setPoorlyNameOperation()
  -        {
  -            return "bad";
  -        }
  -        
  -        public String someOperation()
  -        {
  -            return "someop";
  -        }
  -        
  -        public String someOperation(Object arg)
  -        {
  -            return "someop" + arg;
  -        }
  -        
  -        public String someOperation(boolean arg)
  -        {
  -            return "somebooleanop" + arg;
  -        }
  -    }
   }
  
  
  
  1.1                  incubator-geronimo/modules/common/src/test/org/apache/geronimo/common/jmx/MBeanProxyHandlerTest.java
  
  Index: MBeanProxyHandlerTest.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Geronimo" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Geronimo", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * ====================================================================
   */
  
  package org.apache.geronimo.common.jmx;
  
  import java.lang.reflect.Method;
  
  import java.util.Map;
  
  import javax.management.MBeanServer;
  import javax.management.MBeanServerFactory;
  import javax.management.ObjectName;
  
  import junit.framework.TestCase;
  
  /**
   * Unit test for {@link MBeanProxyHandler} class.
   *
   * @version $Revision: 1.1 $ $Date: 2003/09/01 19:18:47 $
   */
  public class MBeanProxyHandlerTest
      extends TestCase
  {
      protected MBeanServer server;
      protected ObjectName target;
      protected MockObject targetObject;
      protected MyMBeanProxyHandler handler;
      
      protected void setUp() throws Exception
      {
          server = MBeanServerFactory.createMBeanServer("geronimo.test");
          
          target = new ObjectName("geronimo.test:bean=test");
          targetObject = new MockObject();
          server.registerMBean(targetObject, target);
          
          handler = new MyMBeanProxyHandler(server, target);
      }
      
      protected void tearDown() throws Exception
      {
          MBeanServerFactory.releaseMBeanServer(server);
          handler = null;
          server = null;
      }
      
      public void testHandlerCreateTask() throws Exception
      {
          Class type = MockObjectMBean.class;
          Method method = type.getMethod("someOperation", new Class[0]);
          Object[] args = new Object[0];
          
          Object task = handler.createTask(method, args);
          assertNotNull(task);
      }
      
      public void testHandlerGetTask() throws Exception
      {
          Class type = MockObjectMBean.class;
          Method method = type.getMethod("someOperation", new Class[0]);
          Object[] args = new Object[0];
          Object task1 = handler.getTask(method, args);
          assertNotNull(task1);
          
          Map taskCache = handler.getTaskCache();
          assertNotNull(taskCache);
          assertEquals(1, taskCache.size());
          
          Object task2 = handler.getTask(method, args);
          assertNotNull(task2);
          assertEquals(task1, task2);
          assertEquals(1, taskCache.size());
      }
      
      //
      // Test MBeanProxyHandler to get access to protected bits
      //
      
      protected class MyMBeanProxyHandler
          extends MBeanProxyHandler
      {
          public MyMBeanProxyHandler(final MBeanServer server,
                                     final ObjectName target)
          {
              super(server, target);
          }
          
          public Map getTaskCache()
          {
              return taskCache;
          }
          
          public Map getAttributeMap()
          {
              return attributeMap;
          }
          
          public Task createTask(final Method method, final Object[] args)
              throws Exception
          {
              return super.createTask(method, args);
          }
          
          public Task getTask(final Method method, final Object[] args)
              throws Exception
          {
              return super.getTask(method, args);
          }
      }
  }
  
  
  
  1.1                  incubator-geronimo/modules/common/src/test/org/apache/geronimo/common/jmx/MockObject.java
  
  Index: MockObject.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution,
   *    if any, must include the following acknowledgment:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgment may appear in the software itself,
   *    if and wherever such third-party acknowledgments normally appear.
   *
   * 4. The names "Apache" and "Apache Software Foundation" and
   *    "Apache Geronimo" must not be used to endorse or promote products
   *    derived from this software without prior written permission. For
   *    written permission, please contact apache@apache.org.
   *
   * 5. Products derived from this software may not be called "Apache",
   *    "Apache Geronimo", nor may "Apache" appear in their name, without
   *    prior written permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   * ====================================================================
   */
  
  package org.apache.geronimo.common.jmx;
  
  import javax.management.MBeanServer;
  import javax.management.MBeanServerFactory;
  import javax.management.ObjectName;
  
  import junit.framework.TestCase;
  
  /**
   * A mock MBean for testing.
   *
   * @jmx:mbean
   *
   * @version $Revision: 1.1 $ $Date: 2003/09/01 19:18:47 $
   */
  public class MockObject
      implements MockObjectMBean
  {
      protected String string = "MyString";
      protected boolean something;
      
      /**
       * @jmx:managed-attribute
       */
      public void setString(String value)
      {
          this.string = value;
      }
      
      /**
       * @jmx:managed-attribute
       */
      public String getString()
      {
          return string;
      }
      
      /**
       * @jmx:managed-attribute
       */
      public void setSomething(boolean flag)
      {
          something = flag;
      }
      
      /**
       * @jmx:managed-attribute
       */
      public boolean isSomething()
      {
          return something;
      }
      
      /**
       * @jmx:managed-operation
       */
      public String doIt()
      {
          return "done";
      }
      
      /**
       * @jmx:managed-operation
       */
      public String setPoorlyNameOperation()
      {
          return "bad";
      }
      
      /**
       * @jmx:managed-operation
       */
      public String someOperation()
      {
          return "someop";
      }
      
      /**
       * @jmx:managed-operation
       */
      public String someOperation(Object arg)
      {
          return "someop" + arg;
      }
      
      /**
       * @jmx:managed-operation
       */
      public String someOperation(boolean arg)
      {
          return "somebooleanop" + arg;
      }
  }
  
  
  

Mime
View raw message