[jboss-cvs] JBossAS SVN: r72270 - in projects/microcontainer/trunk/kernel/src: tests/org/jboss/test/kernel/deployment/support and 1 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Apr 16 05:34:18 EDT 2008


Author: alesj
Date: 2008-04-16 05:34:18 -0400 (Wed, 16 Apr 2008)
New Revision: 72270

Added:
   projects/microcontainer/trunk/kernel/src/resources/tests/org/jboss/test/kernel/deployment/test/BeanContainerStaticTestCase.xml
   projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/deployment/support/StaticHolder.java
   projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/deployment/support/StaticInjector.java
   projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/deployment/test/BeanContainerStaticTestCase.java
Modified:
   projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/deployment/test/DeploymentTestSuite.java
Log:
Bean container static usage.

Copied: projects/microcontainer/trunk/kernel/src/resources/tests/org/jboss/test/kernel/deployment/test/BeanContainerStaticTestCase.xml (from rev 72266, projects/microcontainer/trunk/kernel/src/resources/tests/org/jboss/test/kernel/deployment/test/BeanContainerInjectionTestCase.xml)
===================================================================
--- projects/microcontainer/trunk/kernel/src/resources/tests/org/jboss/test/kernel/deployment/test/BeanContainerStaticTestCase.xml	                        (rev 0)
+++ projects/microcontainer/trunk/kernel/src/resources/tests/org/jboss/test/kernel/deployment/test/BeanContainerStaticTestCase.xml	2008-04-16 09:34:18 UTC (rev 72270)
@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<deployment xmlns="urn:jboss:bean-deployer:2.0">
+
+  <bean name="StaticInjector" class="org.jboss.test.kernel.deployment.support.StaticInjector" />
+
+</deployment>

Added: projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/deployment/support/StaticHolder.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/deployment/support/StaticHolder.java	                        (rev 0)
+++ projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/deployment/support/StaticHolder.java	2008-04-16 09:34:18 UTC (rev 72270)
@@ -0,0 +1,58 @@
+/*
+* 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.kernel.deployment.support;
+
+/**
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public class StaticHolder
+{
+   private static Object privField;
+   protected static Object protField;
+   public static Object pubField;
+
+   public static Object getPrivField()
+   {
+      return privField;
+   }
+
+   public static Object getProtField()
+   {
+      return protField;
+   }
+
+   @SuppressWarnings("unused")
+   private static void privMain(Object object)
+   {
+      privField = object;
+   }
+
+   protected static void protMain(Object object)
+   {
+      protField = object;
+   }
+
+   public static void pubMain(Object object)
+   {
+      pubField = object;
+   }
+}

Added: projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/deployment/support/StaticInjector.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/deployment/support/StaticInjector.java	                        (rev 0)
+++ projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/deployment/support/StaticInjector.java	2008-04-16 09:34:18 UTC (rev 72270)
@@ -0,0 +1,87 @@
+/*
+* 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.kernel.deployment.support;
+
+import java.lang.reflect.Method;
+
+import org.jboss.beans.metadata.api.annotations.Constructor;
+import org.jboss.beans.metadata.api.annotations.Inject;
+import org.jboss.joinpoint.plugins.Config;
+import org.jboss.kernel.plugins.bootstrap.basic.KernelConstants;
+import org.jboss.kernel.spi.config.KernelConfigurator;
+import org.jboss.reflect.plugins.introspection.ReflectMethodInfoImpl;
+import org.jboss.reflect.spi.ClassInfo;
+import org.jboss.reflect.spi.FieldInfo;
+import org.jboss.reflect.spi.MethodInfo;
+
+/**
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public class StaticInjector
+{
+   private KernelConfigurator configurator;
+
+   @Constructor
+   public StaticInjector(@Inject(bean = KernelConstants.KERNEL_CONFIGURATOR_NAME) KernelConfigurator configurator)
+   {
+      if (configurator == null)
+         throw new IllegalArgumentException("Null configurator");
+
+      this.configurator = configurator;
+   }
+
+   public void injectToMethod(Class<?> clazz, String method, Object value, Class<?> signature) throws Throwable
+   {
+      injectToMethod(clazz, method, value, signature, true);
+   }
+
+   public void injectToNonPublicMethod(Class<?> clazz, String method, Object value, Class<?> signature) throws Throwable
+   {
+      injectToMethod(clazz, method, value, signature, false);
+   }
+
+   private void injectToMethod(Class<?> clazz, String method, Object value, Class<?> signature, boolean isPublic) throws Throwable
+   {
+      ClassInfo classInfo = configurator.getClassInfo(clazz);
+      MethodInfo mi = Config.findMethodInfo(classInfo, method, new String[]{signature.getName()}, true, isPublic);
+      if (isPublic == false)
+      {
+         // TODO - move this into Reflection?
+         if (mi instanceof ReflectMethodInfoImpl)
+         {
+            ReflectMethodInfoImpl rmi = (ReflectMethodInfoImpl)mi;
+            Method  m = rmi.getMethod();
+            m.setAccessible(true);
+         }
+         else
+            throw new IllegalArgumentException("Cannot set accessible on method info: " + mi);
+      }
+      mi.invoke(null, new Object[]{value});
+   }
+
+   public void injectToField(Class<?> clazz, String name, Object value) throws Throwable
+   {
+      ClassInfo classInfo = configurator.getClassInfo(clazz);
+      FieldInfo fi = Config.findFieldInfo(classInfo, name);
+      fi.set(null, value);
+   }
+}
\ No newline at end of file

Copied: projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/deployment/test/BeanContainerStaticTestCase.java (from rev 72266, projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/deployment/test/BeanContainerInjectionTestCase.java)
===================================================================
--- projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/deployment/test/BeanContainerStaticTestCase.java	                        (rev 0)
+++ projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/deployment/test/BeanContainerStaticTestCase.java	2008-04-16 09:34:18 UTC (rev 72270)
@@ -0,0 +1,72 @@
+/*
+* 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.kernel.deployment.test;
+
+import junit.framework.Test;
+import org.jboss.test.kernel.deployment.support.StaticHolder;
+import org.jboss.test.kernel.deployment.support.StaticInjector;
+
+/**
+ * @author <a href="mailto:ales.justin at jboss.com">Ales Justin</a>
+ */
+public class BeanContainerStaticTestCase extends AbstractDeploymentTest
+{
+   public BeanContainerStaticTestCase(String name) throws Throwable
+   {
+      super(name);
+   }
+
+   public static Test suite()
+   {
+      return suite(BeanContainerStaticTestCase.class);
+   }
+
+   public void testStaticInjection() throws Throwable
+   {
+      StaticInjector injector = assertBean("StaticInjector", StaticInjector.class);
+      Class<StaticHolder> clazz = StaticHolder.class;
+      String string = "foobar";
+      Integer number = 123;
+
+      SecurityManager sm = suspendSecurity();
+      try
+      {
+         injector.injectToNonPublicMethod(clazz, "privMain", string, Object.class);
+         assertSame(string, StaticHolder.getPrivField());
+         injector.injectToNonPublicMethod(clazz, "protMain", string, Object.class);
+         assertSame(string, StaticHolder.getProtField());
+         injector.injectToMethod(clazz, "pubMain", string, Object.class);
+         assertSame(string, StaticHolder.pubField);
+
+         injector.injectToField(clazz, "privField", number);
+         assertSame(number, StaticHolder.getPrivField());
+         injector.injectToField(clazz, "protField", number);
+         assertSame(number, StaticHolder.getProtField());
+         injector.injectToField(clazz, "pubField", number);
+         assertSame(number, StaticHolder.pubField);
+      }
+      finally
+      {
+         resumeSecurity(sm);
+      }
+   }
+}
\ No newline at end of file

Modified: projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/deployment/test/DeploymentTestSuite.java
===================================================================
--- projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/deployment/test/DeploymentTestSuite.java	2008-04-16 09:31:32 UTC (rev 72269)
+++ projects/microcontainer/trunk/kernel/src/tests/org/jboss/test/kernel/deployment/test/DeploymentTestSuite.java	2008-04-16 09:34:18 UTC (rev 72270)
@@ -70,6 +70,7 @@
       suite.addTest(BeanContainerUsageMDTestCase.suite());
       suite.addTest(BeanContainerScopingTestCase.suite());
       suite.addTest(BeanContainerInjectionTestCase.suite());
+      suite.addTest(BeanContainerStaticTestCase.suite());
 
       return suite;
    }




More information about the jboss-cvs-commits mailing list