[seam-commits] Seam SVN: r10897 - in modules/trunk/bridge-api/src: main/java/org/jboss/seam/bridge and 17 other directories.

seam-commits at lists.jboss.org seam-commits at lists.jboss.org
Tue May 12 03:00:44 EDT 2009


Author: dan.j.allen
Date: 2009-05-12 03:00:44 -0400 (Tue, 12 May 2009)
New Revision: 10897

Added:
   modules/trunk/bridge-api/src/test/
   modules/trunk/bridge-api/src/test/java/
   modules/trunk/bridge-api/src/test/java/org/
   modules/trunk/bridge-api/src/test/java/org/jboss/
   modules/trunk/bridge-api/src/test/java/org/jboss/seam/
   modules/trunk/bridge-api/src/test/java/org/jboss/seam/bridge/
   modules/trunk/bridge-api/src/test/java/org/jboss/seam/bridge/ManagerBridgeOverrideTest.java
   modules/trunk/bridge-api/src/test/java/org/jboss/seam/bridge/ManagerBridgeServiceLoaderTest.java
   modules/trunk/bridge-api/src/test/java/org/jboss/seam/bridge/ManagerBridgeTest.java
   modules/trunk/bridge-api/src/test/java/org/jboss/seam/bridge/stubs/
   modules/trunk/bridge-api/src/test/java/org/jboss/seam/bridge/stubs/IncorrectManagerProviderResolverStub.java
   modules/trunk/bridge-api/src/test/java/org/jboss/seam/bridge/stubs/IncorrectManagerProviderStub.java
   modules/trunk/bridge-api/src/test/java/org/jboss/seam/bridge/stubs/ManagerProviderResolverStub.java
   modules/trunk/bridge-api/src/test/java/org/jboss/seam/bridge/stubs/ManagerProviderStub.java
   modules/trunk/bridge-api/src/test/resources/
   modules/trunk/bridge-api/src/test/resources/META-INF/
   modules/trunk/bridge-api/src/test/resources/META-INF/services/
   modules/trunk/bridge-api/src/test/resources/META-INF/services/org.jboss.seam.bridge.spi.ManagerProvider
   modules/trunk/bridge-api/src/test/resources/log4j.xml
   modules/trunk/bridge-api/src/test/resources/org/
   modules/trunk/bridge-api/src/test/resources/org/jboss/
   modules/trunk/bridge-api/src/test/resources/org/jboss/testharness/
   modules/trunk/bridge-api/src/test/resources/org/jboss/testharness/impl/
   modules/trunk/bridge-api/src/test/resources/org/jboss/testharness/impl/packaging/
   modules/trunk/bridge-api/src/test/resources/org/jboss/testharness/impl/packaging/jsr299/
   modules/trunk/bridge-api/src/test/resources/org/jboss/testharness/impl/packaging/jsr299/default/
   modules/trunk/bridge-api/src/test/resources/org/jboss/testharness/impl/packaging/jsr299/default/beans.xml
   modules/trunk/bridge-api/src/test/resources/test-suite.xml
Modified:
   modules/trunk/bridge-api/src/main/java/org/jboss/seam/bridge/ManagerBridge.java
   modules/trunk/bridge-api/src/main/java/org/jboss/seam/bridge/ServiceLoaderManagerProviderResolver.java
Log:
add tests for ManagerBridge and friends


Modified: modules/trunk/bridge-api/src/main/java/org/jboss/seam/bridge/ManagerBridge.java
===================================================================
--- modules/trunk/bridge-api/src/main/java/org/jboss/seam/bridge/ManagerBridge.java	2009-05-12 07:00:27 UTC (rev 10896)
+++ modules/trunk/bridge-api/src/main/java/org/jboss/seam/bridge/ManagerBridge.java	2009-05-12 07:00:44 UTC (rev 10897)
@@ -5,17 +5,21 @@
 import javax.inject.manager.Manager;
 
 import org.jboss.seam.bridge.spi.ManagerProvider;
+import org.jboss.webbeans.log.LogProvider;
+import org.jboss.webbeans.log.Logging;
 
 /**
- * A bridge to the JCDI Manager implementation. This bean observers the event
+ * A bridge to the JCDI Manager implementation. This bean observes the event
  * that is fired when the Manager is deployed and attempts to match a provider
  * that produces that same Manager. That provider is then used to access the
- * deployed Manager by a non-managed resource (such as a JSF phase listener)
+ * deployed Manager by a non-managed resource (such as a JSF phase listener).
  * 
  * @author Dan Allen
  */
 public class ManagerBridge
 {
+   private LogProvider log = Logging.getLogProvider(ManagerBridge.class);
+   
    private static ManagerProvider provider;
    
    private static boolean discover = true;
@@ -23,6 +27,8 @@
    // QUESTION: should this be a String, Class, or instance?
    private Class<? extends ManagerProviderResolver> resolverClass = ServiceLoaderManagerProviderResolver.class;
    
+   private static Class<? extends Manager> managerImplClass;
+   
    public Class<? extends ManagerProviderResolver> getResolverClass()
    {
       return resolverClass;
@@ -35,22 +41,46 @@
 
    public static ManagerProvider getProvider()
    {
+      if (provider == null)
+      {
+         if (managerImplClass == null)
+         {
+            throw new IllegalStateException("The ManagerBridge was not properly initialized.");
+         }
+         else
+         {
+            throw new UnsupportedOperationException("Cannot resolve a ManagerProvider for the deployed Manager implementation " + managerImplClass.getName() + ". " +
+               "Please specify the correct ManagerProvider implementation in the META-INF/services/" + ManagerProvider.class.getName() + " file.");
+         }
+      }
       return provider;
    }
    
+   /**
+    * Manually configure the provider. When this value is
+    * set, discovery of a provider is disabled, which is
+    * useful in a mock environment. Calling this method
+    * also resets the manager implementation class that
+    * was detected during initialization in anticipation
+    * of a reinitialization.
+    */
    public static void setProvider(ManagerProvider override)
    {
       provider = override;
+      discover = false;
+      managerImplClass = null;
    }
    
    /**
-    * Configure whether discovery of the provider when the manager
-    * is deployed. The default is to use discovery. This can
-    * be disabled in a mock environment.
+    * Reenable discovery if it was disabled by setting a
+    * provider manually. This also resets the provider
+    * and the manager implementation class that was detected.
     */
-   public static void setDiscover(boolean flag)
+   public static void enableDiscovery()
    {
-      discover = flag;
+      discover = true;
+      provider = null;
+      managerImplClass = null;
    }
    
    /**
@@ -66,7 +96,7 @@
       {
          return;
       }
-      
+
       ManagerProviderResolver resolver;
       try
       {
@@ -74,22 +104,27 @@
       }
       catch (InstantiationException e)
       {
-         throw new IllegalStateException("Could not instantiate ManagerProviderResolver class " + resolverClass, e);
+         throw new IllegalStateException("Could not instantiate ManagerProviderResolver class " + resolverClass.getName(), e);
       }
       catch (IllegalAccessException e)
       {
-         throw new IllegalStateException("Access was denied while attempting to instantiate ManagerProviderResolver class " + resolverClass, e);
+         throw new IllegalStateException("Access was denied while attempting to instantiate ManagerProviderResolver class " + resolverClass.getName(), e);
       }
       
+      managerImplClass = manager.getClass();
       for (ManagerProvider candidate : resolver.getManagerProviders())
       {
          if (manager.equals(candidate.getRootManager()))
          {
             provider = candidate;
+            if (log.isDebugEnabled())
+            {
+               log.debug("Using ManagerProvider implementation " + provider.getClass().getName());
+            }
             return;
          }
       }
 
-      throw new IllegalStateException("Cannot resolve a ManagerProvider corresponding to the deployed Manager implementation " + manager.getClass());
+      //throw new IllegalStateException("Cannot resolve a ManagerProvider corresponding to the deployed Manager implementation " + manager.getClass());
    }
 }

Modified: modules/trunk/bridge-api/src/main/java/org/jboss/seam/bridge/ServiceLoaderManagerProviderResolver.java
===================================================================
--- modules/trunk/bridge-api/src/main/java/org/jboss/seam/bridge/ServiceLoaderManagerProviderResolver.java	2009-05-12 07:00:27 UTC (rev 10896)
+++ modules/trunk/bridge-api/src/main/java/org/jboss/seam/bridge/ServiceLoaderManagerProviderResolver.java	2009-05-12 07:00:44 UTC (rev 10897)
@@ -6,6 +6,8 @@
 import java.util.ServiceLoader;
 
 import org.jboss.seam.bridge.spi.ManagerProvider;
+import org.jboss.webbeans.log.LogProvider;
+import org.jboss.webbeans.log.Logging;
 
 /**
  * An implementation of the ManagerProviderResolver interface that utilizes the
@@ -15,6 +17,8 @@
  */
 public class ServiceLoaderManagerProviderResolver implements ManagerProviderResolver
 {
+   LogProvider log = Logging.getLogProvider(ServiceLoaderManagerProviderResolver.class);
+   
    public List<ManagerProvider> getManagerProviders()
    {
       List<ManagerProvider> providers = new ArrayList<ManagerProvider>();
@@ -25,6 +29,11 @@
          providers.add(iterator.next());
       }
       
+      if (log.isDebugEnabled())
+      {
+         log.debug("Located " + providers.size() + " ManagerProvider instance(s): " + providers);
+      }
+      
       return providers;
    }
 }

Added: modules/trunk/bridge-api/src/test/java/org/jboss/seam/bridge/ManagerBridgeOverrideTest.java
===================================================================
--- modules/trunk/bridge-api/src/test/java/org/jboss/seam/bridge/ManagerBridgeOverrideTest.java	                        (rev 0)
+++ modules/trunk/bridge-api/src/test/java/org/jboss/seam/bridge/ManagerBridgeOverrideTest.java	2009-05-12 07:00:44 UTC (rev 10897)
@@ -0,0 +1,53 @@
+package org.jboss.seam.bridge;
+
+import static org.testng.Assert.assertSame;
+
+import javax.el.ELResolver;
+import javax.inject.manager.Manager;
+
+import org.jboss.seam.bridge.spi.ManagerProvider;
+import org.testng.annotations.Test;
+
+/**
+ * A set of basic tests to verify that the ManagerBridge can be used
+ * outside of a managed environment and that scanning does not take
+ * place in this case.
+ * 
+ * @author Dan Allen
+ */
+public class ManagerBridgeOverrideTest
+{ 
+   @Test
+   public void testProviderOverride()
+   {
+      ManagerProvider provider = new MockManagerProvider();
+      
+      ManagerBridge.setProvider(provider);
+      
+      ManagerBridge bridge = new ManagerBridge();
+      // no scanning should occur; ensure by forcing NullPointerException if it does
+      bridge.setResolverClass(null);
+      bridge.onManagerDeployed(null);
+      
+      assertSame(ManagerBridge.getProvider(), provider);
+   }
+   
+   class MockManagerProvider implements ManagerProvider
+   {
+      public Manager getRootManager()
+      {
+         return null;
+      }
+
+      public Manager getCurrentManager()
+      {
+         return null;
+      }
+
+      public ELResolver getELResolver()
+      {
+         return null;
+      }
+      
+   }
+}

Added: modules/trunk/bridge-api/src/test/java/org/jboss/seam/bridge/ManagerBridgeServiceLoaderTest.java
===================================================================
--- modules/trunk/bridge-api/src/test/java/org/jboss/seam/bridge/ManagerBridgeServiceLoaderTest.java	                        (rev 0)
+++ modules/trunk/bridge-api/src/test/java/org/jboss/seam/bridge/ManagerBridgeServiceLoaderTest.java	2009-05-12 07:00:44 UTC (rev 10897)
@@ -0,0 +1,54 @@
+package org.jboss.seam.bridge;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertSame;
+import static org.testng.Assert.assertTrue;
+
+import javax.context.RequestScoped;
+import javax.inject.manager.Manager;
+
+import org.jboss.seam.bridge.stubs.ManagerProviderStub;
+import org.jboss.testharness.impl.packaging.Artifact;
+import org.jboss.webbeans.test.AbstractWebBeansTest;
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.Test;
+
+/**
+ * This test method bootstraps the Manager with the ManagerBridge registered to
+ * verify that the ServiceLoader properly detects the correct ManagerProvider
+ * from META-INF/services/org.jboss.seam.bridge.spi.ManagerProvider. There
+ * should only be one method in this test class.
+ * 
+ * @author Dan Allen
+ */
+public
+ at Artifact
+class ManagerBridgeServiceLoaderTest extends AbstractWebBeansTest
+{
+   @Override
+   @BeforeClass
+   public void beforeClass() throws Throwable
+   {
+      // reset the statics from previous tests
+      ManagerBridge.enableDiscovery();
+      super.beforeClass();
+   }
+
+   /**
+    * Check that when Web Beans booted, the ManagerBridge was instantiated
+    * and that the ServiceLoader detected and instantiated the ManagerProvider
+    * instance.
+    */
+   @Test
+   public void testLoadValidManagerProvider()
+   {
+      assertNotNull(ManagerBridge.getProvider());
+      assertTrue(ManagerBridge.getProvider() instanceof ManagerProviderStub);
+      assertSame(ManagerBridge.getProvider().getRootManager(), getCurrentManager());
+      assertSame(ManagerBridge.getProvider().getCurrentManager(), getCurrentManager());
+      Manager currentManager = getCurrentManager().setCurrent(RequestScoped.class);
+      assertSame(ManagerBridge.getProvider().getCurrentManager(), currentManager);
+   }
+   
+}

Added: modules/trunk/bridge-api/src/test/java/org/jboss/seam/bridge/ManagerBridgeTest.java
===================================================================
--- modules/trunk/bridge-api/src/test/java/org/jboss/seam/bridge/ManagerBridgeTest.java	                        (rev 0)
+++ modules/trunk/bridge-api/src/test/java/org/jboss/seam/bridge/ManagerBridgeTest.java	2009-05-12 07:00:44 UTC (rev 10897)
@@ -0,0 +1,79 @@
+package org.jboss.seam.bridge;
+
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertSame;
+import static org.testng.Assert.assertTrue;
+
+import javax.context.RequestScoped;
+import javax.inject.manager.Manager;
+
+import org.jboss.seam.bridge.stubs.ManagerProviderStub;
+import org.jboss.seam.bridge.stubs.ManagerProviderResolverStub;
+import org.jboss.seam.bridge.stubs.IncorrectManagerProviderResolverStub;
+import org.jboss.testharness.impl.packaging.Artifact;
+import org.jboss.webbeans.test.AbstractWebBeansTest;
+import org.testng.annotations.BeforeMethod;
+import org.testng.annotations.Test;
+
+/**
+ * This set of tests bootstraps the Manager without the
+ * ManagerBridge present. Then the Manager deployed
+ * observer method on ManagerBridge is called manually
+ * with different provider implementations to verify
+ * it behaves appropriately.
+ * 
+ * @author Dan Allen
+ */
+public
+ at Artifact(addCurrentPackage = false)
+class ManagerBridgeTest extends AbstractWebBeansTest
+{
+   @Override
+   @BeforeMethod
+   public void before() throws Exception
+   {
+      super.before();
+      ManagerBridge.enableDiscovery();
+   }
+
+   /**
+    * Verify that the provider produced by a proper resolver produces
+    * the same Manager as the one that is deployed.
+    */
+   @Test
+   public void testResolveValidManagerProvider()
+   {
+      ManagerBridge bridge = new ManagerBridge();
+      bridge.setResolverClass(ManagerProviderResolverStub.class);
+      bridge.onManagerDeployed(getCurrentManager());
+      assertTrue(ManagerBridge.getProvider() instanceof ManagerProviderStub);
+      assertSame(ManagerBridge.getProvider().getRootManager(), getCurrentManager());
+      assertSame(ManagerBridge.getProvider().getCurrentManager(), getCurrentManager());
+      Manager currentManager = getCurrentManager().setCurrent(RequestScoped.class);
+      assertSame(ManagerBridge.getProvider().getCurrentManager(), currentManager);
+   }
+   
+   /**
+    * Attempt to get the provider when the one discovered didn't match
+    * the Manager that is deployed.
+    */
+   @Test(expectedExceptions = UnsupportedOperationException.class)
+   public void testResolveInvalidManagerProvider()
+   {
+      ManagerBridge bridge = new ManagerBridge();
+      bridge.setResolverClass(IncorrectManagerProviderResolverStub.class);
+      bridge.onManagerDeployed(getCurrentManager());
+      ManagerBridge.getProvider();
+   }
+   
+   /**
+    * Attempt to get the provider when hasn't been set or discovered.
+    */
+   @Test(expectedExceptions = IllegalStateException.class)
+   public void testUninitializedManagerBridge()
+   {
+      ManagerBridge.getProvider();
+   }
+   
+}

Added: modules/trunk/bridge-api/src/test/java/org/jboss/seam/bridge/stubs/IncorrectManagerProviderResolverStub.java
===================================================================
--- modules/trunk/bridge-api/src/test/java/org/jboss/seam/bridge/stubs/IncorrectManagerProviderResolverStub.java	                        (rev 0)
+++ modules/trunk/bridge-api/src/test/java/org/jboss/seam/bridge/stubs/IncorrectManagerProviderResolverStub.java	2009-05-12 07:00:44 UTC (rev 10897)
@@ -0,0 +1,19 @@
+/**
+ * 
+ */
+package org.jboss.seam.bridge.stubs;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import org.jboss.seam.bridge.ManagerProviderResolver;
+import org.jboss.seam.bridge.spi.ManagerProvider;
+
+public class IncorrectManagerProviderResolverStub implements ManagerProviderResolver
+{
+   public List<ManagerProvider> getManagerProviders()
+   {
+      return new ArrayList<ManagerProvider>(Arrays.asList(new IncorrectManagerProviderStub()));
+   }
+}
\ No newline at end of file

Added: modules/trunk/bridge-api/src/test/java/org/jboss/seam/bridge/stubs/IncorrectManagerProviderStub.java
===================================================================
--- modules/trunk/bridge-api/src/test/java/org/jboss/seam/bridge/stubs/IncorrectManagerProviderStub.java	                        (rev 0)
+++ modules/trunk/bridge-api/src/test/java/org/jboss/seam/bridge/stubs/IncorrectManagerProviderStub.java	2009-05-12 07:00:44 UTC (rev 10897)
@@ -0,0 +1,23 @@
+/**
+ * 
+ */
+package org.jboss.seam.bridge.stubs;
+
+import javax.inject.manager.Manager;
+
+import org.jboss.webbeans.CurrentManager;
+import org.jboss.webbeans.ManagerImpl;
+
+public class IncorrectManagerProviderStub extends ManagerProviderStub
+{
+   public Manager getRootManager()
+   {
+      return ManagerImpl.newChildManager(CurrentManager.rootManager());
+   }
+   
+   public Manager getCurrentManager()
+   {
+      return ManagerImpl.newChildManager(CurrentManager.rootManager());
+   }
+   
+}
\ No newline at end of file

Added: modules/trunk/bridge-api/src/test/java/org/jboss/seam/bridge/stubs/ManagerProviderResolverStub.java
===================================================================
--- modules/trunk/bridge-api/src/test/java/org/jboss/seam/bridge/stubs/ManagerProviderResolverStub.java	                        (rev 0)
+++ modules/trunk/bridge-api/src/test/java/org/jboss/seam/bridge/stubs/ManagerProviderResolverStub.java	2009-05-12 07:00:44 UTC (rev 10897)
@@ -0,0 +1,17 @@
+package org.jboss.seam.bridge.stubs;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+
+import org.jboss.seam.bridge.ManagerProviderResolver;
+import org.jboss.seam.bridge.spi.ManagerProvider;
+
+public class ManagerProviderResolverStub implements ManagerProviderResolver
+{
+   public List<ManagerProvider> getManagerProviders()
+   {
+      return new ArrayList<ManagerProvider>(Arrays.asList(new ManagerProviderStub()));
+   }
+}
\ No newline at end of file

Added: modules/trunk/bridge-api/src/test/java/org/jboss/seam/bridge/stubs/ManagerProviderStub.java
===================================================================
--- modules/trunk/bridge-api/src/test/java/org/jboss/seam/bridge/stubs/ManagerProviderStub.java	                        (rev 0)
+++ modules/trunk/bridge-api/src/test/java/org/jboss/seam/bridge/stubs/ManagerProviderStub.java	2009-05-12 07:00:44 UTC (rev 10897)
@@ -0,0 +1,29 @@
+/**
+ * 
+ */
+package org.jboss.seam.bridge.stubs;
+
+import javax.el.ELResolver;
+import javax.inject.manager.Manager;
+
+import org.jboss.seam.bridge.spi.ManagerProvider;
+import org.jboss.webbeans.CurrentManager;
+import org.jboss.webbeans.el.WebBeansELResolver;
+
+public class ManagerProviderStub implements ManagerProvider
+{
+   public Manager getRootManager()
+   {
+      return CurrentManager.rootManager();
+   }
+   
+   public Manager getCurrentManager()
+   {
+      return CurrentManager.rootManager().getCurrent();
+   }
+   
+   public ELResolver getELResolver()
+   {
+      return new WebBeansELResolver();
+   }
+}
\ No newline at end of file

Added: modules/trunk/bridge-api/src/test/resources/META-INF/services/org.jboss.seam.bridge.spi.ManagerProvider
===================================================================
--- modules/trunk/bridge-api/src/test/resources/META-INF/services/org.jboss.seam.bridge.spi.ManagerProvider	                        (rev 0)
+++ modules/trunk/bridge-api/src/test/resources/META-INF/services/org.jboss.seam.bridge.spi.ManagerProvider	2009-05-12 07:00:44 UTC (rev 10897)
@@ -0,0 +1,2 @@
+org.jboss.seam.bridge.stubs.IncorrectManagerProviderStub
+org.jboss.seam.bridge.stubs.ManagerProviderStub

Added: modules/trunk/bridge-api/src/test/resources/log4j.xml
===================================================================
--- modules/trunk/bridge-api/src/test/resources/log4j.xml	                        (rev 0)
+++ modules/trunk/bridge-api/src/test/resources/log4j.xml	2009-05-12 07:00:44 UTC (rev 10897)
@@ -0,0 +1,23 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
+<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
+
+   <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
+      <param name="Target" value="System.out"/>
+      <layout class="org.apache.log4j.PatternLayout">
+         <param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%c{2}] %m%n"/>
+      </layout>
+   </appender>
+
+   <!--
+   <category name="org.jboss.seam.bridge">
+      <priority value="DEBUG"/>
+   </category>
+   -->
+   
+   <root>
+      <priority value="INFO"/>
+      <appender-ref ref="CONSOLE"/>
+   </root>
+
+</log4j:configuration>

Added: modules/trunk/bridge-api/src/test/resources/org/jboss/testharness/impl/packaging/jsr299/default/beans.xml
===================================================================

Added: modules/trunk/bridge-api/src/test/resources/test-suite.xml
===================================================================
--- modules/trunk/bridge-api/src/test/resources/test-suite.xml	                        (rev 0)
+++ modules/trunk/bridge-api/src/test/resources/test-suite.xml	2009-05-12 07:00:44 UTC (rev 10897)
@@ -0,0 +1,8 @@
+<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
+<suite name="Seam Bridge Module Test Suite" verbose="1">
+   <test name="Seam Bridge Module - Unit Tests">
+      <packages>
+         <package name="org.jboss.seam.bridge"/>
+      </packages>
+   </test>
+</suite>




More information about the seam-commits mailing list