[jboss-svn-commits] JBoss Common SVN: r4551 - in arquillian/trunk: bundle/src/main/java/org/jboss/arquillian/osgi and 8 other directories.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Thu Jun 24 04:57:00 EDT 2010


Author: thomas.diesler at jboss.com
Date: 2010-06-24 04:56:59 -0400 (Thu, 24 Jun 2010)
New Revision: 4551

Added:
   arquillian/trunk/testenrichers/osgi/src/main/java/org/jboss/arquillian/testenricher/osgi/BundleContextHolder.java
   arquillian/trunk/testenrichers/osgi/src/main/resources/
   arquillian/trunk/testenrichers/osgi/src/main/resources/META-INF/
   arquillian/trunk/testenrichers/osgi/src/main/resources/META-INF/services/
   arquillian/trunk/testenrichers/osgi/src/main/resources/META-INF/services/org.jboss.arquillian.spi.TestEnricher
Removed:
   arquillian/trunk/bundle/src/main/java/org/jboss/arquillian/osgi/BundleContextHolder.java
   arquillian/trunk/bundle/src/main/java/org/jboss/arquillian/osgi/OSGiTestEnricher.java
   arquillian/trunk/bundle/src/main/resources/META-INF/services/org.jboss.arquillian.spi.TestEnricher
Modified:
   arquillian/trunk/bundle/pom.xml
   arquillian/trunk/bundle/src/main/java/org/jboss/arquillian/osgi/ArquillianBundleActivator.java
   arquillian/trunk/testenrichers/osgi/pom.xml
   arquillian/trunk/testenrichers/osgi/src/main/java/org/jboss/arquillian/testenricher/osgi/OSGiTestEnricher.java
   arquillian/trunk/testenrichers/pom.xml
Log:
osgi: Move OSGiTestEnricher to its own module

Modified: arquillian/trunk/bundle/pom.xml
===================================================================
--- arquillian/trunk/bundle/pom.xml	2010-06-24 08:55:49 UTC (rev 4550)
+++ arquillian/trunk/bundle/pom.xml	2010-06-24 08:56:59 UTC (rev 4551)
@@ -55,6 +55,7 @@
               arquillian-junit;inline=false,
               arquillian-protocol-jmx;inline=false,
               arquillian-spi;inline=false,
+              arquillian-testenricher-osgi;inline=false,
               shrinkwrap-api;inline=false,
               shrinkwrap-impl-base;inline=false,
               shrinkwrap-spi;inline=false,
@@ -96,6 +97,11 @@
       <version>${version}</version>
     </dependency>
     <dependency>
+      <groupId>org.jboss.arquillian.testenricher</groupId>
+      <artifactId>arquillian-testenricher-osgi</artifactId>
+      <version>${version}</version>
+    </dependency>
+    <dependency>
       <groupId>org.jboss.arquillian</groupId>
       <artifactId>arquillian-junit</artifactId>
       <version>${version}</version>

Modified: arquillian/trunk/bundle/src/main/java/org/jboss/arquillian/osgi/ArquillianBundleActivator.java
===================================================================
--- arquillian/trunk/bundle/src/main/java/org/jboss/arquillian/osgi/ArquillianBundleActivator.java	2010-06-24 08:55:49 UTC (rev 4550)
+++ arquillian/trunk/bundle/src/main/java/org/jboss/arquillian/osgi/ArquillianBundleActivator.java	2010-06-24 08:56:59 UTC (rev 4551)
@@ -23,16 +23,13 @@
 
 // $Id$
 
-import java.util.ArrayList;
-
-import javax.management.JMException;
 import javax.management.MBeanServer;
-import javax.management.MBeanServerFactory;
-import javax.management.MBeanServerInvocationHandler;
 import javax.management.ObjectName;
 import javax.management.StandardMBean;
 
 import org.jboss.arquillian.protocol.jmx.JMXTestRunner;
+import org.jboss.arquillian.testenricher.osgi.BundleContextHolder;
+import org.jboss.arquillian.testenricher.osgi.OSGiTestEnricher;
 import org.jboss.logging.Logger;
 import org.osgi.framework.BundleActivator;
 import org.osgi.framework.BundleContext;
@@ -82,47 +79,6 @@
       mbeanServer.unregisterMBean(new ObjectName(BundleContextHolder.OBJECT_NAME));
    }
 
-   static MBeanServer findOrCreateMBeanServer()
-   {
-      MBeanServer mbeanServer = null;
-
-      ArrayList<MBeanServer> serverArr = MBeanServerFactory.findMBeanServer(null);
-      if (serverArr.size() > 1)
-         log.warn("Multiple MBeanServer instances: " + serverArr);
-
-      if (serverArr.size() > 0)
-      {
-         mbeanServer = serverArr.get(0);
-         log.debug("Found MBeanServer: " + mbeanServer.getDefaultDomain());
-      }
-
-      if (mbeanServer == null)
-      {
-         log.debug("No MBeanServer, create one ...");
-         mbeanServer = MBeanServerFactory.createMBeanServer();
-      }
-
-      return mbeanServer;
-   }
-   
-   /**
-    * Get the BundleContext associated with the arquillian-bundle
-    */
-   static BundleContext getBundleContext()
-   {
-      try
-      {
-         MBeanServer mbeanServer = findOrCreateMBeanServer();
-         ObjectName oname = new ObjectName(BundleContextHolder.OBJECT_NAME);
-         BundleContextHolder holder = MBeanServerInvocationHandler.newProxyInstance(mbeanServer, oname, BundleContextHolder.class, false);
-         return holder.getBundleContext();
-      }
-      catch (JMException ex)
-      {
-         throw new IllegalStateException("Cannot obtain arquillian-bundle context", ex);
-      }
-   }
-   
    private MBeanServer getMBeanServer(BundleContext context)
    {
       // Check if the MBeanServer is registered as an OSGi service 
@@ -135,6 +91,6 @@
       }
       
       // Find or create the MBeanServer
-      return findOrCreateMBeanServer();
+      return OSGiTestEnricher.findOrCreateMBeanServer();
    }
 }
\ No newline at end of file

Deleted: arquillian/trunk/bundle/src/main/java/org/jboss/arquillian/osgi/BundleContextHolder.java
===================================================================
--- arquillian/trunk/bundle/src/main/java/org/jboss/arquillian/osgi/BundleContextHolder.java	2010-06-24 08:55:49 UTC (rev 4550)
+++ arquillian/trunk/bundle/src/main/java/org/jboss/arquillian/osgi/BundleContextHolder.java	2010-06-24 08:56:59 UTC (rev 4551)
@@ -1,33 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2009, Red Hat Middleware LLC, and individual contributors
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- * http://www.apache.org/licenses/LICENSE-2.0
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.jboss.arquillian.osgi;
-
-import org.osgi.framework.BundleContext;
-
-/**
- * BundleContextHolder
- * 
- * @author thomas.diesler at jboss.com
- * @version $Revision: $
- */
-public interface BundleContextHolder
-{
-   /** The ObjectName for this service: jboss.arquillian:service=bundle-context */
-   String OBJECT_NAME = "jboss.arquillian:service=bundle-context";
-   
-   BundleContext getBundleContext();
-}

Deleted: arquillian/trunk/bundle/src/main/java/org/jboss/arquillian/osgi/OSGiTestEnricher.java
===================================================================
--- arquillian/trunk/bundle/src/main/java/org/jboss/arquillian/osgi/OSGiTestEnricher.java	2010-06-24 08:55:49 UTC (rev 4550)
+++ arquillian/trunk/bundle/src/main/java/org/jboss/arquillian/osgi/OSGiTestEnricher.java	2010-06-24 08:56:59 UTC (rev 4551)
@@ -1,122 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source
- * Copyright 2009, Red Hat Middleware LLC, and individual contributors
- * by the @authors tag. See the copyright.txt in the distribution for a
- * full listing of individual contributors.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- * http://www.apache.org/licenses/LICENSE-2.0
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.jboss.arquillian.osgi;
-
-import java.lang.reflect.Field;
-import java.lang.reflect.Method;
-
-import javax.inject.Inject;
-
-import org.jboss.arquillian.spi.Context;
-import org.jboss.arquillian.spi.TestEnricher;
-import org.osgi.framework.Bundle;
-import org.osgi.framework.BundleContext;
-import org.osgi.framework.ServiceReference;
-import org.osgi.service.packageadmin.PackageAdmin;
-
-/**
- * The OSGi TestEnricher
- * 
- * The enricher supports the injection of the Framework and the Bundle under test.
- * 
- * <pre><code>
- *    @Inject
- *    BundleContext sysctx;
- * 
- *    @Inject
- *    Bundle bundle;
- * </code></pre>
- * 
- * @author thomas.diesler at jboss.com
- * @version $Revision: $
- */
-public class OSGiTestEnricher implements TestEnricher
-{
-   @Override
-   public void enrich(Context context, Object testCase)
-   {
-      Class<? extends Object> testClass = testCase.getClass();
-      for (Field field : testClass.getDeclaredFields())
-      {
-         if (field.isAnnotationPresent(Inject.class))
-         {
-            if (field.getType().isAssignableFrom(BundleContext.class))
-            {
-               injectBundleContext(context, testCase, field);
-            }
-            if (field.getType().isAssignableFrom(Bundle.class))
-            {
-               injectBundle(context, testCase, field);
-            }
-         }
-      }
-   }
-
-   @Override
-   public Object[] resolve(Context context, Method method)
-   {
-      return null;
-   }
-   
-   private void injectBundleContext(Context context, Object testCase, Field field) 
-   {
-      try
-      {
-         field.set(testCase, getBundleContext(context));
-      }
-      catch (IllegalAccessException ex)
-      {
-         throw new IllegalStateException("Cannot inject BundleContext", ex);
-      }
-   }
-
-   private void injectBundle(Context context, Object testCase, Field field) 
-   {
-      try
-      {
-         field.set(testCase, getTestBundle(context, testCase.getClass()));
-      }
-      catch (IllegalAccessException ex)
-      {
-         throw new IllegalStateException("Cannot inject Bundle", ex);
-      }
-   }
-
-   private BundleContext getBundleContext(Context context)
-   {
-      BundleContext bundleContext = context.get(BundleContext.class);
-      if (bundleContext == null)
-      {
-         bundleContext = ArquillianBundleActivator.getBundleContext();
-         bundleContext = bundleContext.getBundle(0).getBundleContext();
-      }
-      return bundleContext;
-   }
-
-   private Bundle getTestBundle(Context context, Class<?> testClass)
-   {
-      Bundle testbundle = context.get(Bundle.class);
-      if (testbundle == null)
-      {
-         BundleContext bundleContext = getBundleContext(context);
-         ServiceReference sref = bundleContext.getServiceReference(PackageAdmin.class.getName());
-         PackageAdmin pa = (PackageAdmin)bundleContext.getService(sref);
-         testbundle = pa.getBundle(testClass);
-      }
-      return testbundle;
-   }
-}

Deleted: arquillian/trunk/bundle/src/main/resources/META-INF/services/org.jboss.arquillian.spi.TestEnricher
===================================================================
--- arquillian/trunk/bundle/src/main/resources/META-INF/services/org.jboss.arquillian.spi.TestEnricher	2010-06-24 08:55:49 UTC (rev 4550)
+++ arquillian/trunk/bundle/src/main/resources/META-INF/services/org.jboss.arquillian.spi.TestEnricher	2010-06-24 08:56:59 UTC (rev 4551)
@@ -1 +0,0 @@
-org.jboss.arquillian.osgi.OSGiTestEnricher
\ No newline at end of file

Modified: arquillian/trunk/testenrichers/osgi/pom.xml
===================================================================
--- arquillian/trunk/testenrichers/osgi/pom.xml	2010-06-24 08:55:49 UTC (rev 4550)
+++ arquillian/trunk/testenrichers/osgi/pom.xml	2010-06-24 08:56:59 UTC (rev 4551)
@@ -37,6 +37,10 @@
       <groupId>javax.inject</groupId>
       <artifactId>javax.inject</artifactId>
     </dependency>
+    <dependency>
+      <groupId>org.jboss.logging</groupId>
+      <artifactId>jboss-logging-spi</artifactId>
+    </dependency>
     
     <dependency>
       <groupId>org.osgi</groupId>

Copied: arquillian/trunk/testenrichers/osgi/src/main/java/org/jboss/arquillian/testenricher/osgi/BundleContextHolder.java (from rev 4550, arquillian/trunk/bundle/src/main/java/org/jboss/arquillian/osgi/BundleContextHolder.java)
===================================================================
--- arquillian/trunk/testenrichers/osgi/src/main/java/org/jboss/arquillian/testenricher/osgi/BundleContextHolder.java	                        (rev 0)
+++ arquillian/trunk/testenrichers/osgi/src/main/java/org/jboss/arquillian/testenricher/osgi/BundleContextHolder.java	2010-06-24 08:56:59 UTC (rev 4551)
@@ -0,0 +1,33 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.jboss.arquillian.testenricher.osgi;
+
+import org.osgi.framework.BundleContext;
+
+/**
+ * BundleContextHolder
+ * 
+ * @author thomas.diesler at jboss.com
+ * @version $Revision: $
+ */
+public interface BundleContextHolder
+{
+   /** The ObjectName for this service: jboss.arquillian:service=bundle-context */
+   String OBJECT_NAME = "jboss.arquillian:service=bundle-context";
+   
+   BundleContext getBundleContext();
+}

Modified: arquillian/trunk/testenrichers/osgi/src/main/java/org/jboss/arquillian/testenricher/osgi/OSGiTestEnricher.java
===================================================================
--- arquillian/trunk/testenrichers/osgi/src/main/java/org/jboss/arquillian/testenricher/osgi/OSGiTestEnricher.java	2010-06-24 08:55:49 UTC (rev 4550)
+++ arquillian/trunk/testenrichers/osgi/src/main/java/org/jboss/arquillian/testenricher/osgi/OSGiTestEnricher.java	2010-06-24 08:56:59 UTC (rev 4551)
@@ -18,22 +18,31 @@
 
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
+import java.util.ArrayList;
 
 import javax.inject.Inject;
+import javax.management.JMException;
+import javax.management.MBeanServer;
+import javax.management.MBeanServerFactory;
+import javax.management.MBeanServerInvocationHandler;
+import javax.management.ObjectName;
 
 import org.jboss.arquillian.spi.Context;
 import org.jboss.arquillian.spi.TestEnricher;
+import org.jboss.logging.Logger;
 import org.osgi.framework.Bundle;
 import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.packageadmin.PackageAdmin;
 
 /**
  * The OSGi TestEnricher
  * 
- * The enricher supports the injection of the Framework and the Bundle under test.
+ * The enricher supports the injection of the system BundleContext and the test Bundle.
  * 
  * <pre><code>
  *    @Inject
- *    BundleContext sysctx;
+ *    BundleContext context;
  * 
  *    @Inject
  *    Bundle bundle;
@@ -44,23 +53,13 @@
  */
 public class OSGiTestEnricher implements TestEnricher
 {
+   // Provide logging
+   private static Logger log = Logger.getLogger(OSGiTestEnricher.class);
+   
    @Override
    public void enrich(Context context, Object testCase)
    {
       Class<? extends Object> testClass = testCase.getClass();
-      enrichInternal(context, testClass, testCase);
-   }
-
-   /**
-    * Enrich the static fields on the test case
-    */
-   public void enrich(Context context, Class<?> testClass)
-   {
-      enrichInternal(context, testClass, null);
-   }
-
-   private void enrichInternal(Context context, Class<?> testClass, Object testCase)
-   {
       for (Field field : testClass.getDeclaredFields())
       {
          if (field.isAnnotationPresent(Inject.class))
@@ -77,12 +76,17 @@
       }
    }
 
+   @Override
+   public Object[] resolve(Context context, Method method)
+   {
+      return null;
+   }
+   
    private void injectBundleContext(Context context, Object testCase, Field field) 
    {
       try
       {
-         BundleContext sysctx = context.get(BundleContext.class);
-         field.set(testCase, sysctx);
+         field.set(testCase, getSystemBundleContext(context));
       }
       catch (IllegalAccessException ex)
       {
@@ -94,8 +98,7 @@
    {
       try
       {
-         Bundle bundle = context.get(Bundle.class);
-         field.set(testCase, bundle);
+         field.set(testCase, getTestBundle(context, testCase.getClass()));
       }
       catch (IllegalAccessException ex)
       {
@@ -103,9 +106,73 @@
       }
    }
 
-   @Override
-   public Object[] resolve(Context context, Method method)
+   private BundleContext getSystemBundleContext(Context context)
    {
-      return null;
+      BundleContext bundleContext = context.get(BundleContext.class);
+      if (bundleContext == null)
+         bundleContext = getBundleContextFromHolder();
+      
+      // Make sure this is really the system context
+      bundleContext = bundleContext.getBundle(0).getBundleContext();
+      return bundleContext;
    }
+
+   private Bundle getTestBundle(Context context, Class<?> testClass)
+   {
+      Bundle testbundle = context.get(Bundle.class);
+      if (testbundle == null)
+      {
+         // Get the test bundle from PackageAdmin with the test class as key 
+         BundleContext bundleContext = getSystemBundleContext(context);
+         ServiceReference sref = bundleContext.getServiceReference(PackageAdmin.class.getName());
+         PackageAdmin pa = (PackageAdmin)bundleContext.getService(sref);
+         testbundle = pa.getBundle(testClass);
+      }
+      return testbundle;
+   }
+
+   /**
+    * Get the BundleContext associated with the arquillian-bundle
+    */
+   private BundleContext getBundleContextFromHolder()
+   {
+      try
+      {
+         MBeanServer mbeanServer = findOrCreateMBeanServer();
+         ObjectName oname = new ObjectName(BundleContextHolder.OBJECT_NAME);
+         BundleContextHolder holder = MBeanServerInvocationHandler.newProxyInstance(mbeanServer, oname, BundleContextHolder.class, false);
+         return holder.getBundleContext();
+      }
+      catch (JMException ex)
+      {
+         throw new IllegalStateException("Cannot obtain arquillian-bundle context", ex);
+      }
+   }
+   
+   /**
+    * Find or create the MBeanServer
+    */
+   public static MBeanServer findOrCreateMBeanServer()
+   {
+      MBeanServer mbeanServer = null;
+
+      ArrayList<MBeanServer> serverArr = MBeanServerFactory.findMBeanServer(null);
+      if (serverArr.size() > 1)
+         log.warn("Multiple MBeanServer instances: " + serverArr);
+
+      if (serverArr.size() > 0)
+      {
+         mbeanServer = serverArr.get(0);
+         log.debug("Found MBeanServer: " + mbeanServer.getDefaultDomain());
+      }
+
+      if (mbeanServer == null)
+      {
+         log.debug("No MBeanServer, create one ...");
+         mbeanServer = MBeanServerFactory.createMBeanServer();
+      }
+
+      return mbeanServer;
+   }
+   
 }

Added: arquillian/trunk/testenrichers/osgi/src/main/resources/META-INF/services/org.jboss.arquillian.spi.TestEnricher
===================================================================
--- arquillian/trunk/testenrichers/osgi/src/main/resources/META-INF/services/org.jboss.arquillian.spi.TestEnricher	                        (rev 0)
+++ arquillian/trunk/testenrichers/osgi/src/main/resources/META-INF/services/org.jboss.arquillian.spi.TestEnricher	2010-06-24 08:56:59 UTC (rev 4551)
@@ -0,0 +1 @@
+org.jboss.arquillian.testenricher.osgi.OSGiTestEnricher

Modified: arquillian/trunk/testenrichers/pom.xml
===================================================================
--- arquillian/trunk/testenrichers/pom.xml	2010-06-24 08:55:49 UTC (rev 4550)
+++ arquillian/trunk/testenrichers/pom.xml	2010-06-24 08:56:59 UTC (rev 4551)
@@ -28,6 +28,7 @@
     
     <module>cdi</module>
     <module>ejb</module>
+    <module>osgi</module>
     <module>resource</module>
 
   </modules>



More information about the jboss-svn-commits mailing list