[jboss-cvs] JBossAS SVN: r67399 - in projects/microcontainer/trunk/container/src: tests/org/jboss/test/beaninfo/support and 1 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Fri Nov 23 07:45:53 EST 2007


Author: alesj
Date: 2007-11-23 07:45:53 -0500 (Fri, 23 Nov 2007)
New Revision: 67399

Added:
   projects/microcontainer/trunk/container/src/main/org/jboss/beans/info/plugins/BeanInfoUtil.java
   projects/microcontainer/trunk/container/src/tests/org/jboss/test/beaninfo/support/NestedBean.java
   projects/microcontainer/trunk/container/src/tests/org/jboss/test/beaninfo/test/BeanInfoUtilTestCase.java
Modified:
   projects/microcontainer/trunk/container/src/tests/org/jboss/test/beaninfo/test/BeanInfoTestSuite.java
Log:
Nested property support - JBMICROCONT-220.

Added: projects/microcontainer/trunk/container/src/main/org/jboss/beans/info/plugins/BeanInfoUtil.java
===================================================================
--- projects/microcontainer/trunk/container/src/main/org/jboss/beans/info/plugins/BeanInfoUtil.java	                        (rev 0)
+++ projects/microcontainer/trunk/container/src/main/org/jboss/beans/info/plugins/BeanInfoUtil.java	2007-11-23 12:45:53 UTC (rev 67399)
@@ -0,0 +1,138 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.beans.info.plugins;
+
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+import java.util.Arrays;
+
+import org.jboss.config.spi.Configuration;
+import org.jboss.config.plugins.property.PropertyConfiguration;
+import org.jboss.util.propertyeditor.PropertyEditors;
+import org.jboss.beans.info.spi.BeanInfo;
+
+/**
+ * Bean info helper.
+ * Handles nested property names.
+ *
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public class BeanInfoUtil
+{
+   /** The bean configurator */
+   private static Configuration configuration;
+
+   static
+   {
+      configuration = AccessController.doPrivileged(new PrivilegedAction<Configuration>()
+      {
+         public Configuration run()
+         {
+            return new PropertyConfiguration(System.getProperties());
+         }
+      });
+      PropertyEditors.init();
+   }
+
+   /**
+    * Get the value from target.
+    *
+    * @param beanInfo the bean info
+    * @param target the target
+    * @param propertys the property names
+    * @return getter value
+    * @throws Throwable for any error
+    */
+   protected static Object getNestedTarget(BeanInfo beanInfo, Object target, String[] propertys)
+         throws Throwable
+   {
+      for(int i = 0; i < propertys.length; i++)
+      {
+         if (beanInfo == null)
+            throw new IllegalArgumentException("Null bean info");
+
+         Object result = beanInfo.getProperty(target, propertys[i]);
+         if (i < propertys.length - 1)
+         {
+            if (result == null)
+               throw new IllegalArgumentException("Null target in nested property (" + Arrays.asList(propertys) + "): " + target + "." + propertys[i]);
+            beanInfo = configuration.getBeanInfo(result.getClass());
+         }
+         target = result;
+      }
+      return target;
+   }
+
+   /**
+    * Get the value from target.
+    *
+    * @param beanInfo the bean info
+    * @param target the target
+    * @param name the property name, can be nested
+    * @return getter value
+    * @throws Throwable for any error
+    */
+   public static Object get(BeanInfo beanInfo, Object target, String name) throws Throwable
+   {
+      if (target == null)
+         throw new IllegalArgumentException("Null target");
+      if (name == null)
+         throw new IllegalArgumentException("Null property name");
+
+      String[] propertys = name.split("\\.");
+      return getNestedTarget(beanInfo, target, propertys);
+   }
+
+   /**
+    * Set the value on target.
+    *
+    * @param beanInfo the bean info
+    * @param target the target
+    * @param name the property name, can be nested
+    * @param value the value
+    * @throws Throwable for any error
+    */
+   public static void set(BeanInfo beanInfo, Object target, String name, Object value) throws Throwable
+   {
+      if (target == null)
+         throw new IllegalArgumentException("Null target");
+      if (name == null)
+         throw new IllegalArgumentException("Null property name");
+
+      String[] propertys = name.split("\\.");
+      int size = propertys.length - 1;
+      if (size > 0)
+      {
+         String[] allButLast = new String[size];
+         System.arraycopy(propertys, 0, allButLast, 0, size);
+         Object result = getNestedTarget(beanInfo, target, allButLast);
+         if (result == null)
+            throw new IllegalArgumentException("Cannot set value on null target: " + target + "." + name);
+         target = result;
+         beanInfo = configuration.getBeanInfo(target.getClass());
+      }
+      else if (beanInfo == null)
+         throw new IllegalArgumentException("Null bean info.");
+      
+      beanInfo.setProperty(target, propertys[size], value);
+   }
+}
\ No newline at end of file

Added: projects/microcontainer/trunk/container/src/tests/org/jboss/test/beaninfo/support/NestedBean.java
===================================================================
--- projects/microcontainer/trunk/container/src/tests/org/jboss/test/beaninfo/support/NestedBean.java	                        (rev 0)
+++ projects/microcontainer/trunk/container/src/tests/org/jboss/test/beaninfo/support/NestedBean.java	2007-11-23 12:45:53 UTC (rev 67399)
@@ -0,0 +1,54 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.test.beaninfo.support;
+
+/**
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public class NestedBean
+{
+   private NestedBean bean;
+
+   public NestedBean()
+   {
+   }
+
+   public NestedBean(NestedBean bean)
+   {
+      this.bean = bean;
+   }
+
+   public NestedBean getNestedBean()
+   {
+      return bean;
+   }
+
+   public void setNestedBean(NestedBean bean)
+   {
+      this.bean = bean;
+   }
+
+   public NestedBean getDifferentGetter()
+   {
+      return null;
+   }
+}

Modified: projects/microcontainer/trunk/container/src/tests/org/jboss/test/beaninfo/test/BeanInfoTestSuite.java
===================================================================
--- projects/microcontainer/trunk/container/src/tests/org/jboss/test/beaninfo/test/BeanInfoTestSuite.java	2007-11-23 12:34:59 UTC (rev 67398)
+++ projects/microcontainer/trunk/container/src/tests/org/jboss/test/beaninfo/test/BeanInfoTestSuite.java	2007-11-23 12:45:53 UTC (rev 67399)
@@ -43,7 +43,8 @@
       TestSuite suite = new TestSuite("BeanInfo Tests");
 
       suite.addTest(BeanInfoUnitTestCase.suite());
-      
+      suite.addTest(BeanInfoUtilTestCase.suite());
+
       return suite;
    }
 }

Added: projects/microcontainer/trunk/container/src/tests/org/jboss/test/beaninfo/test/BeanInfoUtilTestCase.java
===================================================================
--- projects/microcontainer/trunk/container/src/tests/org/jboss/test/beaninfo/test/BeanInfoUtilTestCase.java	                        (rev 0)
+++ projects/microcontainer/trunk/container/src/tests/org/jboss/test/beaninfo/test/BeanInfoUtilTestCase.java	2007-11-23 12:45:53 UTC (rev 67399)
@@ -0,0 +1,117 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.test.beaninfo.test;
+
+import junit.framework.Test;
+import org.jboss.test.beaninfo.support.NestedBean;
+import org.jboss.beans.info.plugins.BeanInfoUtil;
+import org.jboss.beans.info.spi.BeanInfo;
+
+/**
+ * BeanInfoUtil Test Case.
+ *
+ * @author <a href="ales.justin at jboss.com">Ales Justin</a>
+ */
+public class BeanInfoUtilTestCase extends AbstractBeanInfoTest
+{
+   public static Test suite()
+   {
+      return suite(BeanInfoUtilTestCase.class);
+   }
+
+   public BeanInfoUtilTestCase(String name)
+   {
+      super(name);
+   }
+
+   protected BeanInfo getBeanInfo() throws Throwable
+   {
+      return getBeanInfo(NestedBean.class);
+   }
+
+   public void testSimpleGet() throws Throwable
+   {
+      NestedBean child = new NestedBean();
+      NestedBean parent = new NestedBean(child);
+      assertSame(child, BeanInfoUtil.get(getBeanInfo(), parent, "nestedBean"));
+   }
+
+   public void testSimpleSet() throws Throwable
+   {
+      NestedBean child = new NestedBean();
+      NestedBean parent = new NestedBean();
+      BeanInfoUtil.set(getBeanInfo(), parent, "nestedBean", child);
+      assertSame(child, parent.getNestedBean());
+   }
+
+   public void testNestedGet() throws Throwable
+   {
+      NestedBean grandchild = new NestedBean();
+      NestedBean child = new NestedBean(grandchild);
+      NestedBean parent = new NestedBean(child);
+      assertSame(grandchild, BeanInfoUtil.get(getBeanInfo(), parent, "nestedBean.nestedBean"));
+   }
+
+   public void testNestedSet() throws Throwable
+   {
+      NestedBean grandchild = new NestedBean();
+      NestedBean child = new NestedBean();
+      NestedBean parent = new NestedBean(child);
+      BeanInfoUtil.set(getBeanInfo(), parent, "nestedBean.nestedBean", grandchild);
+      assertSame(child, parent.getNestedBean());
+      assertSame(grandchild, child.getNestedBean());
+   }
+
+   public void testNestedGetFail() throws Throwable
+   {
+      try
+      {
+         NestedBean grandchild = new NestedBean();
+         NestedBean child = new NestedBean(grandchild);
+         NestedBean parent = new NestedBean(child);
+         assertSame(grandchild, BeanInfoUtil.get(getBeanInfo(), parent, "differentGetter.nestedBean"));
+         fail("Should not be here.");
+      }
+      catch (Throwable t)
+      {
+         assertInstanceOf(t, IllegalArgumentException.class);
+      }
+   }
+
+   public void testNestedSetFail() throws Throwable
+   {
+      try
+      {
+         NestedBean grandchild = new NestedBean();
+         NestedBean child = new NestedBean();
+         NestedBean parent = new NestedBean(child);
+         BeanInfoUtil.set(getBeanInfo(), parent, "differentGetter.nestedBean", grandchild);
+         assertSame(child, parent.getNestedBean());
+         assertSame(grandchild, child.getNestedBean());
+         fail("Should not be here.");
+      }
+      catch (Throwable t)
+      {
+         assertInstanceOf(t, IllegalArgumentException.class);
+      }
+   }
+}




More information about the jboss-cvs-commits mailing list