Home » geronimo-2.2-source-release » org.apache.geronimo.gbean.runtime » [javadoc | source]

    1   /**
    2    *
    3    * Copyright 2004 The Apache Software Foundation
    4    *
    5    *  Licensed under the Apache License, Version 2.0 (the "License");
    6    *  you may not use this file except in compliance with the License.
    7    *  You may obtain a copy of the License at
    8    *
    9    *     http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    *  Unless required by applicable law or agreed to in writing, software
   12    *  distributed under the License is distributed on an "AS IS" BASIS,
   13    *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    *  See the License for the specific language governing permissions and
   15    *  limitations under the License.
   16    */
   17   package org.apache.geronimo.gbean.runtime;
   18   
   19   import javax.management.ObjectName;
   20   
   21   import junit.framework.TestCase;
   22   import org.apache.geronimo.gbean.GAttributeInfo;
   23   import org.apache.geronimo.gbean.GBeanData;
   24   import org.apache.geronimo.gbean.InvalidConfigurationException;
   25   import org.apache.geronimo.kernel.MockDynamicGBean;
   26   import org.apache.geronimo.kernel.MockGBean;
   27   import org.apache.geronimo.kernel.KernelFactory;
   28   import org.apache.geronimo.kernel.Kernel;
   29   
   30   /**
   31    * @version $Rev: 169154 $ $Date: 2005-05-08 12:35:23 -0700 (Sun, 08 May 2005) $
   32    */
   33   public class GBeanAttributeTest extends TestCase {
   34   
   35       private static final String attributeName = "Name";
   36   
   37       private static final String persistentPrimitiveAttributeName = "MutableInt";
   38   
   39       /**
   40        * Wraps GBean
   41        */
   42       private GBeanInstance gbeanInstance = null;
   43   
   44       /**
   45        * Wraps DynamicGBean
   46        */
   47       private GBeanInstance dynamicGBeanInstance = null;
   48   
   49       private MethodInvoker getInvoker = null;
   50   
   51       private MethodInvoker setInvoker = null;
   52   
   53       private GAttributeInfo persistentPrimitiveAttributeInfo = null;
   54       private GAttributeInfo attributeInfo = null;
   55       private Kernel kernel;
   56   //    private GAttributeInfo throwingExceptionAttributeInfo = null;
   57   
   58       public final void testGBeanAttributStringClassMethodInvokerMethodInvoker() {
   59           try {
   60               GBeanAttribute.createFrameworkAttribute(null, null, null, null);
   61               fail("IllegalArgumentException expected");
   62           } catch (IllegalArgumentException expected) {
   63           }
   64   //        try {
   65   //            GBeanAttribute.createFrameworkAttribute(gbeanInstance, attributeName, String.class, null);
   66   //            fail("InvalidConfigurationException expected");
   67   //        } catch (InvalidConfigurationException expected) {
   68   //        }
   69           GBeanAttribute attribute;
   70           attribute = GBeanAttribute.createFrameworkAttribute(gbeanInstance, attributeName, String.class, getInvoker);
   71           assertEquals(String.class, attribute.getType());
   72           assertEquals(attributeName, attribute.getName());
   73           assertTrue(attribute.isReadable());
   74           assertFalse(attribute.isWritable());
   75           assertFalse(attribute.isPersistent());
   76           attribute = GBeanAttribute.createFrameworkAttribute(gbeanInstance, attributeName, String.class, null, setInvoker, false, null);
   77           assertEquals(String.class, attribute.getType());
   78           assertEquals(attributeName, attribute.getName());
   79           assertFalse(attribute.isReadable());
   80           assertTrue(attribute.isWritable());
   81           assertFalse(attribute.isPersistent());
   82           attribute = GBeanAttribute.createFrameworkAttribute(gbeanInstance, attributeName, String.class, getInvoker, setInvoker, false, null);
   83           assertEquals(String.class, attribute.getType());
   84           assertEquals(attributeName, attribute.getName());
   85           assertTrue(attribute.isReadable());
   86           assertTrue(attribute.isWritable());
   87           assertFalse(attribute.isPersistent());
   88       }
   89   
   90       public final void testGBeanAttributeInfoClass() {
   91           try {
   92               new GBeanAttribute(null, null, false);
   93               fail("IllegalArgumentException expected");
   94           } catch (IllegalArgumentException expected) {
   95           }
   96   
   97           // 2. @todo BUG An attribute must be readable, writable, or persistent
   98           // GBeanAttribute ctor doesn't check if readable/writable are
   99           // null's
  100           try {
  101               new GBeanAttribute(gbeanInstance, attributeInfo, false);
  102               // till Dain sorts out the question of ctor
  103               // fail("InvalidConfigurationException expected");
  104           } catch (InvalidConfigurationException expected) {
  105           }
  106   
  107           try {
  108               GAttributeInfo invalidAttributeInfo = new GAttributeInfo(attributeName, String.class.getName(), false, null, null);
  109   
  110               new GBeanAttribute(gbeanInstance, invalidAttributeInfo, false);
  111               fail("InvalidConfigurationException expected");
  112           } catch (InvalidConfigurationException expected) {
  113           }
  114   
  115           {
  116               final GAttributeInfo attributeInfo = new GAttributeInfo(attributeName, String.class.getName(), false, true, false, null, null);
  117               GBeanAttribute attribute = new GBeanAttribute(gbeanInstance, attributeInfo, false);
  118               assertTrue(attribute.isReadable());
  119               assertFalse(attribute.isWritable());
  120           }
  121   
  122           {
  123               final GAttributeInfo attributeInfo = new GAttributeInfo(persistentPrimitiveAttributeName, int.class.getName(), false, false, true, null, null);
  124               GBeanAttribute attribute = new GBeanAttribute(gbeanInstance, attributeInfo, false);
  125               assertFalse(attribute.isReadable());
  126               assertTrue(attribute.isWritable());
  127           }
  128   
  129   //        // todo recreate this in the info builder tests since the compariason now happens there
  130   //        {
  131   //            final GAttributeInfo attributeInfo = new GAttributeInfo("AnotherFinalInt", int.class.getName(), false, true, true, null, null);
  132   //            try {
  133   //                new GBeanAttribute(gbeanInstance, attributeInfo, false);
  134   //                fail("Getter and setter methods do not have the same types; InvalidConfigurationException expected");
  135   //            } catch (InvalidConfigurationException expected) {
  136   //            }
  137   //        }
  138   
  139           {
  140               // the attribute name and getter name are different, yet both
  141               // exist.
  142               // getYetAnotherFinalInt doesn't exist
  143               final GAttributeInfo attributeInfo = new GAttributeInfo("YetAnotherFinalInt", int.class.getName(), true, "getFinalInt", null);
  144               GBeanAttribute attribute = new GBeanAttribute(gbeanInstance, attributeInfo, false);
  145               assertNotNull(attribute);
  146           }
  147   
  148           {
  149               final GAttributeInfo attributeInfo = new GAttributeInfo("YetAnotherFinalInt", int.class.getName(), true, null, "setCharAsYetAnotherFinalInt");
  150               try {
  151                   new GBeanAttribute(gbeanInstance, attributeInfo, false);
  152                   fail("Expected InvalidConfigurationException due to invalid setter parameter type");
  153               } catch (InvalidConfigurationException expected) {
  154               }
  155           }
  156   
  157           {
  158               final GAttributeInfo attributeInfo = new GAttributeInfo("YetAnotherFinalInt", int.class.getName(), true, null, "setBooleanAsYetAnotherFinalInt");
  159               try {
  160                   new GBeanAttribute(gbeanInstance, attributeInfo, false);
  161                   fail("Expected InvalidConfigurationException due to invalid setter parameter type");
  162               } catch (InvalidConfigurationException expected) {
  163               }
  164           }
  165   
  166           {
  167               final GAttributeInfo attributeInfo = new GAttributeInfo("YetAnotherFinalInt", int.class.getName(), true, null, "setByteAsYetAnotherFinalInt");
  168               try {
  169                   new GBeanAttribute(gbeanInstance, attributeInfo, false);
  170                   fail("Expected InvalidConfigurationException due to invalid setter parameter type");
  171               } catch (InvalidConfigurationException expected) {
  172               }
  173           }
  174   
  175           {
  176               final GAttributeInfo attributeInfo = new GAttributeInfo("YetAnotherFinalInt", int.class.getName(), true, null, "setShortAsYetAnotherFinalInt");
  177               try {
  178                   new GBeanAttribute(gbeanInstance, attributeInfo, false);
  179                   fail("Expected InvalidConfigurationException due to invalid setter parameter type");
  180               } catch (InvalidConfigurationException expected) {
  181               }
  182           }
  183   
  184           {
  185               final GAttributeInfo attributeInfo = new GAttributeInfo("YetAnotherFinalInt", int.class.getName(), true, null, "setLongAsYetAnotherFinalInt");
  186               try {
  187                   new GBeanAttribute(gbeanInstance, attributeInfo, false);
  188                   fail("Expected InvalidConfigurationException due to invalid setter parameter type");
  189               } catch (InvalidConfigurationException expected) {
  190               }
  191           }
  192   
  193           {
  194               final GAttributeInfo attributeInfo = new GAttributeInfo("YetAnotherFinalInt", int.class.getName(), true, null, "setFloatAsYetAnotherFinalInt");
  195               try {
  196                   new GBeanAttribute(gbeanInstance, attributeInfo, false);
  197                   fail("Expected InvalidConfigurationException due to invalid setter parameter type");
  198               } catch (InvalidConfigurationException expected) {
  199               }
  200           }
  201   
  202           {
  203               final GAttributeInfo attributeInfo = new GAttributeInfo("YetAnotherFinalInt", int.class.getName(), true, null, "setDoubleAsYetAnotherFinalInt");
  204               try {
  205                   new GBeanAttribute(gbeanInstance, attributeInfo, false);
  206                   fail("Expected InvalidConfigurationException due to invalid setter parameter type");
  207               } catch (InvalidConfigurationException expected) {
  208               }
  209           }
  210   
  211           {
  212               final GAttributeInfo attributeInfo = new GAttributeInfo("YetAnotherFinalInt", int.class.getName(), true, "getVoidGetterOfFinalInt", null);
  213               try {
  214                   new GBeanAttribute(gbeanInstance, attributeInfo, false);
  215                   fail("Getter method not found on target; InvalidConfigurationException expected");
  216               } catch (InvalidConfigurationException expected) {
  217               }
  218           }
  219   
  220           {
  221               final GAttributeInfo attributeInfo = new GAttributeInfo("YetAnotherFinalInt", int.class.getName(), true, null, "setThatDoesntExist");
  222               try {
  223                   new GBeanAttribute(gbeanInstance, attributeInfo, false);
  224                   fail("Setter method not found on target; InvalidConfigurationException expected");
  225               } catch (InvalidConfigurationException expected) {
  226               }
  227           }
  228       }
  229   
  230       public final void testGetValue() throws Exception {
  231           {
  232               // attribute that isn't readable and persistent
  233               final GBeanAttribute attribute = GBeanAttribute.createFrameworkAttribute(gbeanInstance, attributeName, String.class, null, setInvoker, false, null);
  234               try {
  235                   attribute.getValue(gbeanInstance);
  236                   fail("Only persistent attributes can be accessed while offline; exception expected");
  237               } catch (/* IllegalState */Exception expected) {
  238               }
  239           }
  240   
  241           {
  242               final GBeanAttribute attribute = GBeanAttribute.createFrameworkAttribute(gbeanInstance, attributeName, String.class, null, setInvoker, false, null);
  243   
  244               try {
  245                   gbeanInstance.start();
  246   
  247                   attribute.getValue(gbeanInstance);
  248                   fail("This attribute is not readable; exception expected");
  249               } catch (/* IllegalArgument */Throwable expected) {
  250               } finally {
  251                   gbeanInstance.stop();
  252               }
  253           }
  254       }
  255   
  256       public final void testSetValue() throws Exception {
  257   
  258           // 1. (offline) attribute that isn't readable and persistent
  259           {
  260               final GBeanAttribute attribute = GBeanAttribute.createFrameworkAttribute(gbeanInstance, attributeName, String.class, null, setInvoker, false, null);
  261               try {
  262                   attribute.setValue(gbeanInstance, null);
  263                   fail("Only persistent attributes can be modified while offline; exception expected");
  264               } catch (/* IllegalState */Exception expected) {
  265               }
  266           }
  267   
  268           // 2. (offline) attribute that is of primitive type, writable and
  269           // persistent, but not readable
  270           {
  271               final GBeanAttribute persistentAttribute = new GBeanAttribute(gbeanInstance, persistentPrimitiveAttributeInfo, false);
  272               try {
  273                   persistentAttribute.setValue(gbeanInstance, null);
  274                   fail("Cannot assign null to a primitive attribute; exception expected");
  275               } catch (/* IllegalArgument */Exception expected) {
  276               }
  277           }
  278   
  279           // 3. (online) attribute that is immutable and not persistent
  280           {
  281               final GBeanAttribute immutableAttribute = GBeanAttribute.createFrameworkAttribute(gbeanInstance, attributeName, String.class, getInvoker);
  282   
  283               try {
  284                   gbeanInstance.start();
  285   
  286                   immutableAttribute.setValue(gbeanInstance, null);
  287                   fail("This attribute is not writable; exception expected");
  288               } catch (/* IllegalArgument */Exception expected) {
  289               } finally {
  290                   gbeanInstance.stop();
  291               }
  292           }
  293   
  294           // 4. (online) attribute that is mutable and of primitive type
  295           {
  296               final GBeanAttribute mutablePersistentAttribute = new GBeanAttribute(gbeanInstance, persistentPrimitiveAttributeInfo, false);
  297   
  298               try {
  299                   gbeanInstance.start();
  300   
  301                   mutablePersistentAttribute.setValue(gbeanInstance, null);
  302                   fail("Cannot assign null to a primitive attribute; exception expected");
  303               } catch (/* IllegalArgument */Exception expected) {
  304               } finally {
  305                   gbeanInstance.stop();
  306               }
  307           }
  308   
  309           // 4a. @todo BUG: It's possible to set a value to a persistent
  310           // attribute while online; IllegalStateException expected
  311           {
  312               final GBeanAttribute mutablePersistentAttribute = new GBeanAttribute(gbeanInstance, persistentPrimitiveAttributeInfo, false);
  313   
  314               try {
  315                   gbeanInstance.start();
  316   
  317                   mutablePersistentAttribute.setValue(gbeanInstance, new Integer(4));
  318                   //fail("Cannot assign a value to a persistent attribute while
  319                   // online; exception expected");
  320               } catch (/* IllegalState */Exception expected) {
  321               } finally {
  322                   gbeanInstance.stop();
  323               }
  324           }
  325   
  326           // 5. Invoke setValue so that exception is thrown
  327           {
  328               final GBeanAttribute attribute = GBeanAttribute.createFrameworkAttribute(gbeanInstance,
  329                       attributeName,
  330                       int.class,
  331                       null,
  332                       setInvoker,
  333                       false,
  334                       null);
  335   
  336               try {
  337                   gbeanInstance.start();
  338   
  339                   attribute.setValue(gbeanInstance, new Integer(4));
  340                   fail("Exception expected upon setValue's call");
  341               } catch (/* IllegalState */Exception expected) {
  342               } finally {
  343                   gbeanInstance.stop();
  344               }
  345           }
  346       }
  347   
  348       protected void setUp() throws Exception {
  349           kernel = KernelFactory.newInstance().createKernel("test");
  350           kernel.boot();
  351   
  352           gbeanInstance = new GBeanInstance(new GBeanData(new ObjectName("test:MockGBean=normal"), MockGBean.getGBeanInfo()),
  353                   kernel,
  354                   kernel.getDependencyManager(),
  355                   new MyLifecycleBroadcaster(),
  356                   MockGBean.class.getClassLoader());
  357           dynamicGBeanInstance = new GBeanInstance(new GBeanData(new ObjectName("test:MockGBean=dynamic"), MockDynamicGBean.getGBeanInfo()),
  358                   kernel,
  359                   kernel.getDependencyManager(),
  360                   new MyLifecycleBroadcaster(),
  361                   MockGBean.class.getClassLoader());
  362           getInvoker = new MethodInvoker() {
  363   
  364               public Object invoke(Object target, Object[] arguments) throws Exception {
  365                   throw new UnsupportedOperationException("Throws exception to rise test coverage");
  366               }
  367           };
  368           setInvoker = new MethodInvoker() {
  369   
  370               public Object invoke(Object target, Object[] arguments) throws Exception {
  371                   throw new UnsupportedOperationException("Throws exception to rise test coverage");
  372               }
  373           };
  374           attributeInfo = new GAttributeInfo(attributeName, String.class.getName(), false, "getName", "setName");
  375           persistentPrimitiveAttributeInfo = new GAttributeInfo(persistentPrimitiveAttributeName, int.class.getName(), true, "getMutableInt", "setMutableInt");
  376       }
  377   
  378       protected void tearDown() throws Exception {
  379           kernel.shutdown();
  380           gbeanInstance = null;
  381       }
  382   
  383       private static class MyLifecycleBroadcaster implements LifecycleBroadcaster {
  384           public void fireLoadedEvent() {
  385           }
  386   
  387           public void fireStartingEvent() {
  388           }
  389   
  390           public void fireRunningEvent() {
  391           }
  392   
  393           public void fireStoppingEvent() {
  394           }
  395   
  396           public void fireStoppedEvent() {
  397           }
  398   
  399           public void fireFailedEvent() {
  400           }
  401   
  402           public void fireUnloadedEvent() {
  403           }
  404       }
  405   }

Home » geronimo-2.2-source-release » org.apache.geronimo.gbean.runtime » [javadoc | source]