[jboss-cvs] JBossAS SVN: r96499 - in projects/jboss-cl/trunk/classloading/src: test/java/org/jboss/test/classloading and 9 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Nov 18 12:09:49 EST 2009


Author: adrian at jboss.org
Date: 2009-11-18 12:09:48 -0500 (Wed, 18 Nov 2009)
New Revision: 96499

Added:
   projects/jboss-cl/trunk/classloading/src/main/java/org/jboss/classloading/spi/dependency/LifeCycle.java
   projects/jboss-cl/trunk/classloading/src/main/java/org/jboss/classloading/spi/dependency/ResolutionContext.java
   projects/jboss-cl/trunk/classloading/src/main/java/org/jboss/classloading/spi/dependency/Resolver.java
   projects/jboss-cl/trunk/classloading/src/test/java/org/jboss/test/classloading/lifecycle/
   projects/jboss-cl/trunk/classloading/src/test/java/org/jboss/test/classloading/lifecycle/LifeCycleTestSuite.java
   projects/jboss-cl/trunk/classloading/src/test/java/org/jboss/test/classloading/lifecycle/support/
   projects/jboss-cl/trunk/classloading/src/test/java/org/jboss/test/classloading/lifecycle/support/a/
   projects/jboss-cl/trunk/classloading/src/test/java/org/jboss/test/classloading/lifecycle/support/a/A.java
   projects/jboss-cl/trunk/classloading/src/test/java/org/jboss/test/classloading/lifecycle/support/a/MockLifeCycle.java
   projects/jboss-cl/trunk/classloading/src/test/java/org/jboss/test/classloading/lifecycle/support/a/MockLifeCycleClassLoaderPolicyModule.java
   projects/jboss-cl/trunk/classloading/src/test/java/org/jboss/test/classloading/lifecycle/test/
   projects/jboss-cl/trunk/classloading/src/test/java/org/jboss/test/classloading/lifecycle/test/AbstractMockLifeCycleUnitTest.java
   projects/jboss-cl/trunk/classloading/src/test/java/org/jboss/test/classloading/lifecycle/test/LifeCycleUnitTestCase.java
   projects/jboss-cl/trunk/classloading/src/test/java/org/jboss/test/classloading/resolver/
   projects/jboss-cl/trunk/classloading/src/test/java/org/jboss/test/classloading/resolver/ResolverTestSuite.java
   projects/jboss-cl/trunk/classloading/src/test/java/org/jboss/test/classloading/resolver/support/
   projects/jboss-cl/trunk/classloading/src/test/java/org/jboss/test/classloading/resolver/support/a/
   projects/jboss-cl/trunk/classloading/src/test/java/org/jboss/test/classloading/resolver/support/a/A.java
   projects/jboss-cl/trunk/classloading/src/test/java/org/jboss/test/classloading/resolver/test/
   projects/jboss-cl/trunk/classloading/src/test/java/org/jboss/test/classloading/resolver/test/ResolverUnitTestCase.java
Modified:
   projects/jboss-cl/trunk/classloading/src/main/java/org/jboss/classloading/spi/dependency/ClassLoading.java
   projects/jboss-cl/trunk/classloading/src/main/java/org/jboss/classloading/spi/dependency/Domain.java
   projects/jboss-cl/trunk/classloading/src/main/java/org/jboss/classloading/spi/dependency/Module.java
   projects/jboss-cl/trunk/classloading/src/test/java/org/jboss/test/classloading/ClassLoadingAllTestSuite.java
   projects/jboss-cl/trunk/classloading/src/test/java/org/jboss/test/classloading/dependency/test/AbstractMockClassLoaderUnitTest.java
Log:
[JBCL-130] [JBCL-131] [JBCL-132] - Initial prototypes of lazy resolution/start and repository resolution

Modified: projects/jboss-cl/trunk/classloading/src/main/java/org/jboss/classloading/spi/dependency/ClassLoading.java
===================================================================
--- projects/jboss-cl/trunk/classloading/src/main/java/org/jboss/classloading/spi/dependency/ClassLoading.java	2009-11-18 17:05:43 UTC (rev 96498)
+++ projects/jboss-cl/trunk/classloading/src/main/java/org/jboss/classloading/spi/dependency/ClassLoading.java	2009-11-18 17:09:48 UTC (rev 96499)
@@ -26,6 +26,7 @@
 import java.util.Map;
 import java.util.Set;
 import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.CopyOnWriteArrayList;
 
 import org.jboss.classloader.spi.ClassLoaderSystem;
 import org.jboss.classloading.spi.metadata.Capability;
@@ -39,7 +40,7 @@
  * @author <a href="ales.justin at jboss.org">Ales Justin</a>
  * @version $Revision: 1.1 $
  */
-public class ClassLoading
+public class ClassLoading implements Resolver
 {
    /** The log */
    private static final Logger log = Logger.getLogger(ClassLoading.class);
@@ -56,6 +57,9 @@
    /** The module registries */
    private final Set<ModuleRegistry> moduleRegistries = new ConcurrentSet<ModuleRegistry>();
 
+   /** The resolvers */
+   private List<Resolver> resolvers = null;
+
    /**
     * Add a module
     *
@@ -151,7 +155,59 @@
       
       globalCapabilitiesProviders.remove(provider);
    }
+
+   /**
+    * Add a resolver
+    * 
+    * @param resolver the resolver
+    */
+   public void addResolver(Resolver resolver)
+   {
+      if (resolver == null)
+         throw new IllegalArgumentException("Null resolver");
+      
+      if (resolvers == null)
+         resolvers = new CopyOnWriteArrayList<Resolver>();
+      
+      resolvers.add(resolver);
+   }
+
+   /**
+    * Remove a resolver
+    * 
+    * @param resolver the resolver
+    */
+   public void removeResolver(Resolver resolver)
+   {
+      if (resolver == null)
+         throw new IllegalArgumentException("Null resolver");
+      
+      if (resolvers == null)
+         return;
+      
+      resolvers.remove(resolver);
+   }
    
+   public boolean resolve(ResolutionContext context)
+   {
+      if (resolvers != null && resolvers.isEmpty() == false)
+      {
+         for (Resolver resolver : resolvers)
+         {
+            try
+            {
+               if (resolver.resolve(context))
+                  return true;
+            }
+            catch (Throwable t)
+            {
+               log.warn("Error in resolver: " + resolver + " context=" + context, t);
+            }
+         }
+      }
+      return false;
+   }
+   
    /**
     * Get or create the domain
     * 

Modified: projects/jboss-cl/trunk/classloading/src/main/java/org/jboss/classloading/spi/dependency/Domain.java
===================================================================
--- projects/jboss-cl/trunk/classloading/src/main/java/org/jboss/classloading/spi/dependency/Domain.java	2009-11-18 17:05:43 UTC (rev 96498)
+++ projects/jboss-cl/trunk/classloading/src/main/java/org/jboss/classloading/spi/dependency/Domain.java	2009-11-18 17:09:48 UTC (rev 96499)
@@ -60,7 +60,7 @@
    
    /** The registered modules by name */
    private Map<String, Module> modulesByName = new ConcurrentHashMap<String, Module>();
-
+   
    /**
     * Create a new Domain.
     * 
@@ -218,7 +218,7 @@
    {
       return classLoading.mergeGlobalCapabilities(capabilities);
    }
-   
+
    /**
     * Resolve a requirement to a module
     * 
@@ -228,6 +228,35 @@
     */
    protected Module resolveModule(Module module, Requirement requirement)
    {
+      // Try to resolve the module
+      Module result = doResolveModule(module, requirement);
+      if (result == null)
+      {
+         // If we have resolvers, try again if they find it
+         if (classLoading.resolve(new ResolutionContext(this, module, requirement)))
+            result = doResolveModule(module, requirement);
+      }
+      
+      // If there is a result, check to see whether we need to resolve it
+      if (result != null)
+      {
+         LifeCycle lifeCycle = result.getLifeCycle();
+         if (lifeCycle != null && lifeCycle.isLazyResolve() && lifeCycle.isResolved() == false)
+            lifeCycle.doResolve();
+      }
+      
+      return result;
+   }   
+   
+   /**
+    * Resolve a requirement to a module
+    * 
+    * @param module the module
+    * @param requirement the requirement
+    * @return the resolved name or null if not resolved
+    */
+   protected Module doResolveModule(Module module, Requirement requirement)
+   {
       // First check the parent domain has been setup
       Domain parentDomain = null;
       if (parentDomainName != null)

Added: projects/jboss-cl/trunk/classloading/src/main/java/org/jboss/classloading/spi/dependency/LifeCycle.java
===================================================================
--- projects/jboss-cl/trunk/classloading/src/main/java/org/jboss/classloading/spi/dependency/LifeCycle.java	                        (rev 0)
+++ projects/jboss-cl/trunk/classloading/src/main/java/org/jboss/classloading/spi/dependency/LifeCycle.java	2009-11-18 17:09:48 UTC (rev 96499)
@@ -0,0 +1,305 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, Red Hat Middleware LLC, and individual contributors
+* as indicated by the @author tags. See the copyright.txt file 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.classloading.spi.dependency;
+
+import org.jboss.classloader.spi.ClassFoundEvent;
+import org.jboss.classloader.spi.ClassFoundHandler;
+import org.jboss.classloader.spi.ClassLoaderPolicy;
+import org.jboss.classloading.spi.dependency.policy.ClassLoaderPolicyModule;
+import org.jboss.dependency.spi.ControllerContext;
+import org.jboss.dependency.spi.ControllerState;
+import org.jboss.logging.Logger;
+
+/**
+ * Lifecycle.
+ * 
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @version $Revision: 1.1 $
+ */
+public class LifeCycle
+{
+   /** The log */
+   private static final Logger log = Logger.getLogger(LifeCycle.class);
+   
+   /** The module associated with this lifecycle */
+   private Module module;
+   
+   /** Any lazy start handler */
+   private LazyStartHandler lazyStart;
+   
+   /** Whether we are already in the lifecycle */
+   // TODO FIX THIS IN THE MC?
+   private boolean lifeCycle = false;
+   
+   /**
+    * Create a new LifeCycle.
+    * 
+    * @param module the module associated with the lifecycle
+    */
+   public LifeCycle(Module module)
+   {
+      if (module == null)
+         throw new IllegalArgumentException("Null module");
+      this.module = module;
+   }
+   
+   /**
+    * Get the module.
+    * 
+    * @return the module.
+    */
+   public Module getModule()
+   {
+      return module;
+   }
+
+   /**
+    * Whether the module is resolved
+    * 
+    * @return true when resolved
+    */
+   public boolean isResolved()
+   {
+      return module.getClassLoader() != null;
+   }
+   
+   /**
+    * Whether the context associated with the classloader is lazy resolve,
+    * i.e. the resolve method will be invoked the context is needed
+    * 
+    * @return true if it is lazy resolve
+    */
+   public boolean isLazyResolve()
+   {
+      return false;
+   }
+   
+   /**
+    * Resolve the classloader
+    */
+   void doResolve()
+   {
+      if (lifeCycle == false)
+      {
+         lifeCycle = true;
+         try
+         {
+            resolve();
+         }
+         catch (Throwable t)
+         {
+            log.warn("Error in resolve: " + this, t);
+         }
+         finally
+         {
+            lifeCycle = false;
+         }
+      }
+   }
+   
+   /**
+    * Resolve the classloader
+    */
+   public void resolve()
+   {
+   }
+   
+   /**
+    * Unresolve the classloader
+    */
+   public void unresolve()
+   {
+   }
+   
+   /**
+    * Fired when the classloader is resolved
+    */
+   public void resolved()
+   {
+   }
+   
+   /**
+    * Fired when the classloader is unresolved
+    */
+   public void unresolved()
+   {
+   }
+   
+   /**
+    * Whether the module is started
+    * 
+    * @return true when started
+    */
+   public boolean isStarted()
+   {
+      ControllerContext context = module.getControllerContext();
+      if (context == null)
+         return false;
+      return ControllerState.INSTALLED.equals(context.getState());
+   }
+   
+   /**
+    * Start the context associated with the classloader
+    */
+   void doStart()
+   {
+      if (lifeCycle == false)
+      {
+         lifeCycle = true;
+         try
+         {
+            start();
+         }
+         catch (Throwable t)
+         {
+            log.warn("Error in start: " + this, t);
+         }
+         finally
+         {
+            lifeCycle = false;
+         }
+      }
+   }
+   
+   /**
+    * Start the context associated with the classloader
+    */
+   public void start()
+   {
+   }
+   
+   /**
+    * Stop the context associated with the classloader
+    */
+   void doStop()
+   {
+      if (lifeCycle == false)
+      {
+         lifeCycle = true;
+         try
+         {
+            start();
+         }
+         catch (Throwable t)
+         {
+            log.warn("Error in stop: " + this, t);
+         }
+         finally
+         {
+            lifeCycle = false;
+         }
+      }
+   }
+   
+   /**
+    * Stop the context associated with the classloader
+    */
+   public void stop()
+   {
+   }
+   
+   /**
+    * Whether the context associated with the classloader is lazy start,
+    * i.e. the start method will be invoked on first class load
+    * 
+    * @return true if it is lazy start
+    */
+   public boolean isLazyStart()
+   {
+      return false;
+   }
+
+   /**
+    * Setup lazy start
+    */
+   protected void setUpLazyStart()
+   {
+      if (isStarted())
+         return;
+      if (module instanceof ClassLoaderPolicyModule)
+      {
+         ClassLoaderPolicy policy = ((ClassLoaderPolicyModule) module).getPolicy();
+         lazyStart = new LazyStartHandler(policy);
+      }
+      else
+      {
+         throw new IllegalStateException("Cannot do lazy start for " + module);
+      }
+   }
+
+   /**
+    * Remove lazy start
+    */
+   protected void removeLazyStart()
+   {
+      if (lazyStart == null)
+         return;
+
+      lazyStart.cleanup();
+      lazyStart = null;
+   }
+   
+   void fireResolved()
+   {
+      if (isLazyStart())
+         setUpLazyStart();
+      resolved();
+   }
+   
+   void fireUnresolved()
+   {
+      removeLazyStart();
+      unresolved();
+   }
+
+   @Override
+   public String toString()
+   {
+      return getClass().getName() + "{" + getModule() + "}";
+   }
+   
+   /**
+    * LazyStartHandler.
+    */
+   private class LazyStartHandler implements ClassFoundHandler
+   {
+      ClassLoaderPolicy policy;
+      
+      public LazyStartHandler(ClassLoaderPolicy policy)
+      {
+         this.policy = policy;
+         policy.addClassFoundHandler(this);
+      }
+      
+      public void classFound(ClassFoundEvent event)
+      {
+         removeLazyStart();
+         if (isStarted() == false)
+            start();
+      }
+      
+      public void cleanup()
+      {
+         policy.removeClassFoundHandler(this);
+      }
+   }
+}

Modified: projects/jboss-cl/trunk/classloading/src/main/java/org/jboss/classloading/spi/dependency/Module.java
===================================================================
--- projects/jboss-cl/trunk/classloading/src/main/java/org/jboss/classloading/spi/dependency/Module.java	2009-11-18 17:05:43 UTC (rev 96498)
+++ projects/jboss-cl/trunk/classloading/src/main/java/org/jboss/classloading/spi/dependency/Module.java	2009-11-18 17:09:48 UTC (rev 96499)
@@ -67,7 +67,7 @@
  */
 public abstract class Module extends NameAndVersionSupport
 {
-   /** The serialVersionUID - not really serializable */
+   /** The serialVersionUID */
    private static final long serialVersionUID = 1L;
 
    /** The modules by classloader */
@@ -93,6 +93,9 @@
    
    /** The requirements */
    private List<RequirementDependencyItem> requirementDependencies;
+
+   /** Any lifecycle associated with the classloader */
+   private LifeCycle lifeCycle;
    
    /**
     * Register a classloader for a module
@@ -109,6 +112,10 @@
          throw new IllegalArgumentException("Null classloader");
 
       modulesByClassLoader.put(classLoader, module);
+
+      LifeCycle lifeCycle = module.getLifeCycle();
+      if (lifeCycle != null)
+         lifeCycle.fireResolved();
    }
 
    /**
@@ -126,6 +133,10 @@
          throw new IllegalArgumentException("Null classloader");
 
       modulesByClassLoader.remove(classLoader);
+
+      LifeCycle lifeCycle = module.getLifeCycle();
+      if (lifeCycle != null)
+         lifeCycle.fireUnresolved();
    }
    
    /**
@@ -368,6 +379,28 @@
    }
    
    /**
+    * Get the lifecycle.
+    * 
+    * @return the lifecycle.
+    */
+   public LifeCycle getLifeCycle()
+   {
+      return lifeCycle;
+   }
+
+   /**
+    * Set the lifeCycle.
+    * 
+    * @param lifeCycle the lifeCycle.
+    */
+   public void setLifeCycle(LifeCycle lifeCycle)
+   {
+      if (lifeCycle != null && lifeCycle.getModule() != this)
+         throw new IllegalArgumentException("Cannot setLifeCycle on module " + this + " it is associated with a different module: " + lifeCycle.getModule());
+      this.lifeCycle = lifeCycle;
+   }
+
+   /**
     * Find the module for a classloader
     * 
     * @param cl the classloader

Added: projects/jboss-cl/trunk/classloading/src/main/java/org/jboss/classloading/spi/dependency/ResolutionContext.java
===================================================================
--- projects/jboss-cl/trunk/classloading/src/main/java/org/jboss/classloading/spi/dependency/ResolutionContext.java	                        (rev 0)
+++ projects/jboss-cl/trunk/classloading/src/main/java/org/jboss/classloading/spi/dependency/ResolutionContext.java	2009-11-18 17:09:48 UTC (rev 96499)
@@ -0,0 +1,98 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, Red Hat Middleware LLC, and individual contributors
+* as indicated by the @author tags. See the copyright.txt file 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.classloading.spi.dependency;
+
+import org.jboss.classloading.spi.metadata.Requirement;
+
+/**
+ * ResolutionContext.
+ * 
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @version $Revision: 1.1 $
+ */
+public class ResolutionContext
+{
+   /** The domain */
+   private Domain domain;
+   
+   /** The module */
+   private Module module;
+   
+   /** The requirement */
+   private Requirement requirement;
+
+   /**
+    * Create a new ResolutionContext.
+    * 
+    * @param domain the domain
+    * @param module the module
+    * @param requirement the requirement
+    */
+   public ResolutionContext(Domain domain, Module module, Requirement requirement)
+   {
+      this.domain = domain;
+      this.module = module;
+      this.requirement = requirement;
+   }
+
+   /**
+    * Get the domain.
+    * 
+    * @return the domain.
+    */
+   public Domain getDomain()
+   {
+      return domain;
+   }
+
+   /**
+    * Get the module.
+    * 
+    * @return the module.
+    */
+   public Module getModule()
+   {
+      return module;
+   }
+
+   /**
+    * Get the requirement.
+    * 
+    * @return the requirement.
+    */
+   public Requirement getRequirement()
+   {
+      return requirement;
+   }
+
+   @Override
+   public String toString()
+   {
+      StringBuilder builder = new StringBuilder();
+      builder.append(getClass().getSimpleName());
+      builder.append("{domain=").append(domain);
+      builder.append(" module=").append(module);
+      builder.append(" requirement=").append(requirement);
+      builder.append('}');
+      return builder.toString();
+   }
+}

Added: projects/jboss-cl/trunk/classloading/src/main/java/org/jboss/classloading/spi/dependency/Resolver.java
===================================================================
--- projects/jboss-cl/trunk/classloading/src/main/java/org/jboss/classloading/spi/dependency/Resolver.java	                        (rev 0)
+++ projects/jboss-cl/trunk/classloading/src/main/java/org/jboss/classloading/spi/dependency/Resolver.java	2009-11-18 17:09:48 UTC (rev 96499)
@@ -0,0 +1,39 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, Red Hat Middleware LLC, and individual contributors
+* as indicated by the @author tags. See the copyright.txt file 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.classloading.spi.dependency;
+
+/**
+ * Resolver.
+ * 
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @version $Revision: 1.1 $
+ */
+public interface Resolver
+{
+   /**
+    * Resolve a requirement
+    * 
+    * @param context the resolution context
+    * @return true when resolved
+    */
+   boolean resolve(ResolutionContext context);
+}

Modified: projects/jboss-cl/trunk/classloading/src/test/java/org/jboss/test/classloading/ClassLoadingAllTestSuite.java
===================================================================
--- projects/jboss-cl/trunk/classloading/src/test/java/org/jboss/test/classloading/ClassLoadingAllTestSuite.java	2009-11-18 17:05:43 UTC (rev 96498)
+++ projects/jboss-cl/trunk/classloading/src/test/java/org/jboss/test/classloading/ClassLoadingAllTestSuite.java	2009-11-18 17:09:48 UTC (rev 96499)
@@ -26,7 +26,9 @@
 import junit.textui.TestRunner;
 
 import org.jboss.test.classloading.dependency.DependencyTestSuite;
+import org.jboss.test.classloading.lifecycle.LifeCycleTestSuite;
 import org.jboss.test.classloading.metadata.MetaDataTestSuite;
+import org.jboss.test.classloading.resolver.ResolverTestSuite;
 import org.jboss.test.classloading.version.VersionTestSuite;
 
 /**
@@ -59,6 +61,8 @@
       suite.addTest(VersionTestSuite.suite());
       suite.addTest(MetaDataTestSuite.suite());
       suite.addTest(DependencyTestSuite.suite());
+      suite.addTest(LifeCycleTestSuite.suite());
+      suite.addTest(ResolverTestSuite.suite());
       
       return suite;
    }

Modified: projects/jboss-cl/trunk/classloading/src/test/java/org/jboss/test/classloading/dependency/test/AbstractMockClassLoaderUnitTest.java
===================================================================
--- projects/jboss-cl/trunk/classloading/src/test/java/org/jboss/test/classloading/dependency/test/AbstractMockClassLoaderUnitTest.java	2009-11-18 17:05:43 UTC (rev 96498)
+++ projects/jboss-cl/trunk/classloading/src/test/java/org/jboss/test/classloading/dependency/test/AbstractMockClassLoaderUnitTest.java	2009-11-18 17:09:48 UTC (rev 96499)
@@ -31,6 +31,7 @@
 import org.jboss.classloading.spi.dependency.ClassLoading;
 import org.jboss.classloading.spi.dependency.policy.mock.MockClassLoaderPolicyModule;
 import org.jboss.classloading.spi.dependency.policy.mock.MockClassLoadingMetaData;
+import org.jboss.dependency.spi.ControllerContext;
 import org.jboss.dependency.spi.ControllerState;
 import org.jboss.dependency.spi.ControllerStateModel;
 import org.jboss.kernel.Kernel;
@@ -61,6 +62,13 @@
    {
       super(name);
    }
+
+   protected <T> T getBean(Object name, Class<T> expected)
+   {
+      ControllerContext context = controller.getInstalledContext(name);
+      assertNotNull("Context should be found: " + name, context);
+      return assertInstanceOf(context.getTarget(), expected);
+   }
    
    protected ClassLoader assertClassLoader(KernelControllerContext context) throws Exception
    {

Added: projects/jboss-cl/trunk/classloading/src/test/java/org/jboss/test/classloading/lifecycle/LifeCycleTestSuite.java
===================================================================
--- projects/jboss-cl/trunk/classloading/src/test/java/org/jboss/test/classloading/lifecycle/LifeCycleTestSuite.java	                        (rev 0)
+++ projects/jboss-cl/trunk/classloading/src/test/java/org/jboss/test/classloading/lifecycle/LifeCycleTestSuite.java	2009-11-18 17:09:48 UTC (rev 96499)
@@ -0,0 +1,61 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file 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.classloading.lifecycle;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+import junit.textui.TestRunner;
+
+import org.jboss.test.classloading.lifecycle.test.LifeCycleUnitTestCase;
+
+/**
+ * LifeCycle Test Suite.
+ * 
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @version $Revision: 37459 $
+ */
+public class LifeCycleTestSuite extends TestSuite
+{
+   /**
+    * For running the testsuite from the command line
+    * 
+    * @param args the command line args
+    */
+   public static void main(String[] args)
+   {
+      TestRunner.run(suite());
+   }
+
+   /**
+    * Create the testsuite
+    * 
+    * @return the testsuite
+    */
+   public static Test suite()
+   {
+      TestSuite suite = new TestSuite("LifeCycle Tests");
+
+      suite.addTest(LifeCycleUnitTestCase.suite());
+
+      return suite;
+   }
+}

Added: projects/jboss-cl/trunk/classloading/src/test/java/org/jboss/test/classloading/lifecycle/support/a/A.java
===================================================================
--- projects/jboss-cl/trunk/classloading/src/test/java/org/jboss/test/classloading/lifecycle/support/a/A.java	                        (rev 0)
+++ projects/jboss-cl/trunk/classloading/src/test/java/org/jboss/test/classloading/lifecycle/support/a/A.java	2009-11-18 17:09:48 UTC (rev 96499)
@@ -0,0 +1,32 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, Red Hat Middleware LLC, and individual contributors
+* as indicated by the @author tags. See the copyright.txt file 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.classloading.lifecycle.support.a;
+
+/**
+ * A.
+ * 
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @version $Revision: 1.1 $
+ */
+public class A
+{
+}

Added: projects/jboss-cl/trunk/classloading/src/test/java/org/jboss/test/classloading/lifecycle/support/a/MockLifeCycle.java
===================================================================
--- projects/jboss-cl/trunk/classloading/src/test/java/org/jboss/test/classloading/lifecycle/support/a/MockLifeCycle.java	                        (rev 0)
+++ projects/jboss-cl/trunk/classloading/src/test/java/org/jboss/test/classloading/lifecycle/support/a/MockLifeCycle.java	2009-11-18 17:09:48 UTC (rev 96499)
@@ -0,0 +1,113 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, Red Hat Middleware LLC, and individual contributors
+* as indicated by the @author tags. See the copyright.txt file 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.classloading.lifecycle.support.a;
+
+import org.jboss.classloading.spi.dependency.LifeCycle;
+import org.jboss.classloading.spi.dependency.Module;
+
+/**
+ * MockLifeCycle.
+ * 
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @version $Revision: 1.1 $
+ */
+public class MockLifeCycle extends LifeCycle
+{
+   public boolean gotResolved = false;
+   public boolean gotUnresolved = false;
+   public boolean gotResolve = false;
+   public boolean gotUnresolve = false;
+   public boolean gotStart= false;
+   public boolean gotStop = false;
+
+   public boolean lazyResolve = false;
+   public boolean lazyStart = false;
+   
+   public MockLifeCycle(Module module)
+   {
+      super(module);
+   }
+
+   public MockLifeCycleClassLoaderPolicyModule getModule()
+   {
+      return (MockLifeCycleClassLoaderPolicyModule) super.getModule();
+   }
+
+   public void resetFlags()
+   {
+      gotResolved = false;
+      gotUnresolved = false;
+      gotResolve = false;
+      gotUnresolve = false;
+      gotStart = false;
+      gotStop = false;
+   }
+   
+   @Override
+   public void resolve()
+   {
+      gotResolve = true;
+      getModule().resolveIt();
+   }
+
+   @Override
+   public void resolved()
+   {
+      gotResolved = true;
+   }
+
+   @Override
+   public void start()
+   {
+      gotStart = true;
+   }
+
+   @Override
+   public void stop()
+   {
+      gotStop = true;
+   }
+
+   @Override
+   public void unresolve()
+   {
+      gotUnresolve = true;
+   }
+
+   @Override
+   public void unresolved()
+   {
+      gotUnresolved = true;
+   }
+
+   @Override
+   public boolean isLazyResolve()
+   {
+      return lazyResolve;
+   }
+
+   @Override
+   public boolean isLazyStart()
+   {
+      return lazyStart;
+   }
+}

Added: projects/jboss-cl/trunk/classloading/src/test/java/org/jboss/test/classloading/lifecycle/support/a/MockLifeCycleClassLoaderPolicyModule.java
===================================================================
--- projects/jboss-cl/trunk/classloading/src/test/java/org/jboss/test/classloading/lifecycle/support/a/MockLifeCycleClassLoaderPolicyModule.java	                        (rev 0)
+++ projects/jboss-cl/trunk/classloading/src/test/java/org/jboss/test/classloading/lifecycle/support/a/MockLifeCycleClassLoaderPolicyModule.java	2009-11-18 17:09:48 UTC (rev 96499)
@@ -0,0 +1,73 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, Red Hat Middleware LLC, and individual contributors
+* as indicated by the @author tags. See the copyright.txt file 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.classloading.lifecycle.support.a;
+
+import org.jboss.classloading.spi.dependency.policy.mock.MockClassLoaderPolicyModule;
+import org.jboss.classloading.spi.dependency.policy.mock.MockClassLoadingMetaData;
+import org.jboss.dependency.spi.Controller;
+import org.jboss.dependency.spi.ControllerContext;
+import org.jboss.dependency.spi.ControllerState;
+
+/**
+ * MockLifeCycleClassLoaderPolicyModule.
+ * 
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @version $Revision: 1.1 $
+ */
+public class MockLifeCycleClassLoaderPolicyModule extends MockClassLoaderPolicyModule
+{
+   /** The serialVersionUID */
+   private static final long serialVersionUID = 1L;
+
+   public MockLifeCycleClassLoaderPolicyModule(MockClassLoadingMetaData classLoadingMetaData, String contextName)
+   {
+      super(classLoadingMetaData, contextName);
+      MockLifeCycle lifeCycle = new MockLifeCycle(this);
+      setLifeCycle(lifeCycle);
+   }
+   
+   @Override
+   public ClassLoader getClassLoader()
+   {
+      return super.getClassLoader();
+   }
+   
+   @Override
+   public ControllerState getClassLoaderState()
+   {
+      return ControllerState.CREATE;
+   }
+   
+   void resolveIt()
+   {
+      ControllerContext context = getControllerContext();
+      Controller controller = context.getController();
+      try
+      {
+         controller.change(context, getClassLoaderState());
+      }
+      catch (Throwable t)
+      {
+         throw new Error("Error", t);
+      }
+   }
+}

Added: projects/jboss-cl/trunk/classloading/src/test/java/org/jboss/test/classloading/lifecycle/test/AbstractMockLifeCycleUnitTest.java
===================================================================
--- projects/jboss-cl/trunk/classloading/src/test/java/org/jboss/test/classloading/lifecycle/test/AbstractMockLifeCycleUnitTest.java	                        (rev 0)
+++ projects/jboss-cl/trunk/classloading/src/test/java/org/jboss/test/classloading/lifecycle/test/AbstractMockLifeCycleUnitTest.java	2009-11-18 17:09:48 UTC (rev 96499)
@@ -0,0 +1,238 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file 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.classloading.lifecycle.test;
+
+import org.jboss.beans.metadata.spi.BeanMetaData;
+import org.jboss.beans.metadata.spi.builder.BeanMetaDataBuilder;
+import org.jboss.classloader.plugins.system.DefaultClassLoaderSystem;
+import org.jboss.classloader.spi.ClassLoaderDomain;
+import org.jboss.classloader.spi.ClassLoaderSystem;
+import org.jboss.classloader.spi.ParentPolicy;
+import org.jboss.classloading.spi.dependency.ClassLoading;
+import org.jboss.classloading.spi.dependency.policy.mock.MockClassLoadingMetaData;
+import org.jboss.dependency.spi.ControllerMode;
+import org.jboss.dependency.spi.ControllerState;
+import org.jboss.kernel.Kernel;
+import org.jboss.kernel.plugins.bootstrap.AbstractBootstrap;
+import org.jboss.kernel.plugins.bootstrap.basic.BasicBootstrap;
+import org.jboss.kernel.spi.dependency.KernelController;
+import org.jboss.kernel.spi.dependency.KernelControllerContext;
+import org.jboss.test.classloading.AbstractClassLoadingTest;
+import org.jboss.test.classloading.lifecycle.support.a.MockLifeCycle;
+import org.jboss.test.classloading.lifecycle.support.a.MockLifeCycleClassLoaderPolicyModule;
+
+/**
+ * AbstractMockLifeCycleUnitTest.
+ * 
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @version $Revision: 1.1 $
+ */
+public abstract class AbstractMockLifeCycleUnitTest extends AbstractClassLoadingTest
+{
+   private KernelController controller;
+
+   protected ClassLoaderSystem system;
+
+   public AbstractMockLifeCycleUnitTest(String name)
+   {
+      super(name);
+   }
+
+   protected void assertNoClassLoader(KernelControllerContext context) throws Exception
+   {
+      MockLifeCycleClassLoaderPolicyModule module = assertMockClassPolicyModule(context);
+      ClassLoader cl = module.getClassLoader();
+      assertNull("" + cl, cl);
+   }
+
+   protected ClassLoader assertClassLoader(KernelControllerContext context) throws Exception
+   {
+      MockLifeCycleClassLoaderPolicyModule module = assertMockClassPolicyModule(context);
+      ClassLoader cl = module.getClassLoader();
+      assertNotNull("Should be a classloader for " + module, cl);
+      return cl;
+   }
+   
+   protected MockLifeCycle assertNotResolved(KernelControllerContext context) throws Exception
+   {
+      assertNoClassLoader(context);
+      MockLifeCycle lifeCycle = assertLifeCycle(context);
+      assertFalse(context.getName() + " should be unresolved", lifeCycle.isResolved());
+      assertFalse(context.getName() + " should be no resolved notification", lifeCycle.gotResolved);
+      assertFalse(context.getName() + " should not be started", lifeCycle.isStarted());
+      return lifeCycle;
+   }
+   
+   protected MockLifeCycle assertUnresolved(KernelControllerContext context) throws Exception
+   {
+      assertNoClassLoader(context);
+      MockLifeCycle lifeCycle = assertLifeCycle(context);
+      assertFalse(context.getName() + " should be unresolved", lifeCycle.isResolved());
+      assertTrue(context.getName() + " should be an unresolved notification", lifeCycle.gotUnresolved);
+      assertFalse(context.getName() + " should not be started", lifeCycle.isStarted());
+      return lifeCycle;
+   }
+   
+   protected ClassLoader assertResolved(KernelControllerContext context) throws Exception
+   {
+      MockLifeCycle lifeCycle = assertLifeCycle(context);
+      assertTrue(context.getName() + " should be resolved: " + context.getDependencyInfo().getUnresolvedDependencies(null), lifeCycle.isResolved());
+      assertTrue(context.getName() + " should be a resolved notification", lifeCycle.gotResolved);
+      assertFalse(context.getName() + " should not be started", lifeCycle.isStarted());
+      return assertClassLoader(context);
+   }
+   
+   protected ClassLoader assertStarted(KernelControllerContext context) throws Exception
+   {
+      MockLifeCycle lifeCycle = assertLifeCycle(context);
+      assertTrue(context.getName() + " should be resolved: " + context.getDependencyInfo().getUnresolvedDependencies(null), lifeCycle.isResolved());
+      assertTrue(context.getName() + " should be started", lifeCycle.isStarted());
+      assertEquals(ControllerState.INSTALLED, context.getState());
+      return assertClassLoader(context);
+   }
+
+   protected MockLifeCycle assertLifeCycle(KernelControllerContext context) throws Exception
+   {
+      MockLifeCycleClassLoaderPolicyModule module = assertMockClassPolicyModule(context);
+      MockLifeCycle result = (MockLifeCycle) module.getLifeCycle();
+      assertNotNull(result);
+      return result;
+   }
+
+   protected MockLifeCycleClassLoaderPolicyModule assertMockClassPolicyModule(KernelControllerContext context) throws Exception
+   {
+      MockLifeCycleClassLoaderPolicyModule module = (MockLifeCycleClassLoaderPolicyModule) context.getTarget();
+      assertNotNull("Should be a module for " + context, module);
+      return module;
+   }
+
+   protected KernelControllerContext install(MockClassLoadingMetaData metaData) throws Exception
+   {
+      // Determine some properties
+      String contextName = metaData.getName() + ":" + metaData.getVersion().toString(); 
+      
+      // Create the module
+      MockLifeCycleClassLoaderPolicyModule mockModule = new MockLifeCycleClassLoaderPolicyModule(metaData, contextName);
+      BeanMetaDataBuilder builder = BeanMetaDataBuilder.createBuilder(contextName, mockModule.getClass().getName());
+      builder.setConstructorValue(mockModule);
+      builder.setMode(ControllerMode.MANUAL);
+      builder.addConstructorParameter(MockClassLoadingMetaData.class.getName(), metaData);
+      builder.addConstructorParameter(String.class.getName(), contextName);
+      builder.setNoClassLoader();
+      builder.setCreate("registerClassLoaderPolicy");
+      builder.addCreateParameter(ClassLoaderSystem.class.getName(), builder.createValue(system));
+      builder.setDestroy("removeClassLoader");
+      BeanMetaData module = builder.getBeanMetaData();
+      KernelControllerContext result = install(module);
+      change(result, ControllerState.CONFIGURED);
+      return result;
+   }
+   
+   protected KernelControllerContext install(BeanMetaData beanMetaData) throws Exception
+   {
+      try
+      {
+         return controller.install(beanMetaData);
+      }
+      catch (Exception e)
+      {
+         throw e;
+      }
+      catch (Throwable t)
+      {
+         throw new RuntimeException("Error during install: " + beanMetaData, t);
+      }
+   }
+   
+   protected void change(KernelControllerContext controllerContext, ControllerState state) throws Exception
+   {
+      try
+      {
+         controller.change(controllerContext, state);
+      }
+      catch (Exception e)
+      {
+         throw e;
+      }
+      catch (Throwable t)
+      {
+         throw new RuntimeException("Error during change: " + controllerContext, t);
+      }
+   }
+   
+   protected void install(KernelControllerContext controllerContext) throws Exception
+   {
+      change(controllerContext, ControllerState.CONFIGURED);
+   }
+   
+   protected void resolve(KernelControllerContext controllerContext) throws Exception
+   {
+      change(controllerContext, ControllerState.CREATE);
+   }
+   
+   protected void unresolve(KernelControllerContext controllerContext) throws Exception
+   {
+      change(controllerContext, ControllerState.CONFIGURED);
+   }
+   
+   protected void start(KernelControllerContext controllerContext) throws Exception
+   {
+      change(controllerContext, ControllerState.INSTALLED);
+   }
+   
+   protected void stop(KernelControllerContext controllerContext) throws Exception
+   {
+      change(controllerContext, ControllerState.CREATE);
+   }
+
+   protected void uninstall(KernelControllerContext context)
+   {
+      controller.uninstall(context.getName());
+   }
+   
+   protected void setUp() throws Exception
+   {
+      super.setUp();
+      
+      // Bootstrap the kernel
+      AbstractBootstrap bootstrap = new BasicBootstrap();
+      bootstrap.run();
+      Kernel kernel = bootstrap.getKernel();
+      controller = kernel.getController();
+
+      system = new DefaultClassLoaderSystem();
+      ClassLoaderDomain defaultDomain = system.getDefaultDomain();
+      defaultDomain.setParentPolicy(ParentPolicy.BEFORE_BUT_JAVA_ONLY);
+
+      BeanMetaDataBuilder builder = BeanMetaDataBuilder.createBuilder("ClassLoading", ClassLoading.class.getName());
+      builder.addMethodInstallCallback("addModule", null, null, ControllerState.CONFIGURED, null);
+      builder.addMethodUninstallCallback("removeModule", null, null, ControllerState.CONFIGURED, null);
+
+      install(builder.getBeanMetaData());
+   }
+
+   protected void tearDown() throws Exception
+   {
+      controller.shutdown();
+      super.tearDown();
+   }
+}

Added: projects/jboss-cl/trunk/classloading/src/test/java/org/jboss/test/classloading/lifecycle/test/LifeCycleUnitTestCase.java
===================================================================
--- projects/jboss-cl/trunk/classloading/src/test/java/org/jboss/test/classloading/lifecycle/test/LifeCycleUnitTestCase.java	                        (rev 0)
+++ projects/jboss-cl/trunk/classloading/src/test/java/org/jboss/test/classloading/lifecycle/test/LifeCycleUnitTestCase.java	2009-11-18 17:09:48 UTC (rev 96499)
@@ -0,0 +1,236 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file 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.classloading.lifecycle.test;
+
+import junit.framework.Test;
+
+import org.jboss.classloader.plugins.ClassLoaderUtils;
+import org.jboss.classloading.plugins.metadata.PackageRequirement;
+import org.jboss.classloading.spi.dependency.policy.mock.MockClassLoadingMetaData;
+import org.jboss.classloading.spi.metadata.ClassLoadingMetaDataFactory;
+import org.jboss.kernel.spi.dependency.KernelControllerContext;
+import org.jboss.test.classloading.lifecycle.support.a.A;
+import org.jboss.test.classloading.lifecycle.support.a.MockLifeCycle;
+
+/**
+ * LifeCycleUnitTestCase.
+ * 
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @version $Revision: 1.1 $
+ */
+public class LifeCycleUnitTestCase extends AbstractMockLifeCycleUnitTest
+{
+   public static Test suite()
+   {
+      return suite(LifeCycleUnitTestCase.class);
+   }
+
+   public LifeCycleUnitTestCase(String name)
+   {
+      super(name);
+   }
+
+   static String packageA = ClassLoaderUtils.getClassPackageName(A.class.getName());
+   
+   public void testInstall() throws Exception
+   {
+      MockClassLoadingMetaData metaData = new MockClassLoadingMetaData("test");
+      KernelControllerContext context = install(metaData);
+      try
+      {
+         assertNotResolved(context);
+      }
+      finally
+      {
+         uninstall(context);
+      }
+   }
+   
+   public void testResolve() throws Exception
+   {
+      MockClassLoadingMetaData metaData = new MockClassLoadingMetaData("test");
+      KernelControllerContext context = install(metaData);
+      try
+      {
+         assertNotResolved(context);
+         
+         resolve(context);
+         assertResolved(context);
+      }
+      finally
+      {
+         uninstall(context);
+      }
+   }
+   
+   public void testUnresolve() throws Exception
+   {
+      MockClassLoadingMetaData metaData = new MockClassLoadingMetaData("test");
+      KernelControllerContext context = install(metaData);
+      try
+      {
+         assertNotResolved(context);
+         
+         resolve(context);
+         assertResolved(context);
+         
+         unresolve(context);
+         assertUnresolved(context);
+      }
+      finally
+      {
+         uninstall(context);
+      }
+   }
+   
+   public void testStart() throws Exception
+   {
+      MockClassLoadingMetaData metaData = new MockClassLoadingMetaData("test");
+      KernelControllerContext context = install(metaData);
+      try
+      {
+         assertNotResolved(context);
+         
+         start(context);
+         assertStarted(context);
+      }
+      finally
+      {
+         uninstall(context);
+      }
+   }
+   
+   public void testStop() throws Exception
+   {
+      MockClassLoadingMetaData metaData = new MockClassLoadingMetaData("test");
+      KernelControllerContext context = install(metaData);
+      try
+      {
+         assertNotResolved(context);
+         
+         start(context);
+         assertStarted(context);
+         
+         stop(context);
+         assertResolved(context);
+      }
+      finally
+      {
+         uninstall(context);
+      }
+   }
+   
+   public void testNoLazyResolve() throws Exception
+   {
+      MockClassLoadingMetaData metaDataA = new MockClassLoadingMetaData("a");
+      metaDataA.setPathsAndPackageNames(A.class);
+      KernelControllerContext contextA = install(metaDataA);
+
+      ClassLoadingMetaDataFactory factory = ClassLoadingMetaDataFactory.getInstance();
+      MockClassLoadingMetaData metaData = new MockClassLoadingMetaData("test");
+      metaData.getRequirements().addRequirement(factory.createRequirePackage(packageA));
+      KernelControllerContext context = install(metaData);
+      try
+      {
+         assertNotResolved(context);
+         assertNotResolved(contextA);
+         
+         resolve(context);
+         assertNotResolved(context);
+         assertNotResolved(contextA);
+      }
+      finally
+      {
+         uninstall(context);
+      }
+   }
+   
+   public void testLazyResolve() throws Exception
+   {
+      MockClassLoadingMetaData metaDataA = new MockClassLoadingMetaData("a");
+      metaDataA.setPathsAndPackageNames(A.class);
+      KernelControllerContext contextA = install(metaDataA);
+
+      MockClassLoadingMetaData metaData = new MockClassLoadingMetaData("test");
+      metaData.getRequirements().addRequirement(new PackageRequirement(ClassLoaderUtils.getClassPackageName(A.class.getName())));
+      KernelControllerContext context = install(metaData);
+      try
+      {
+         assertNotResolved(context);
+         MockLifeCycle lifeCycleA = assertNotResolved(contextA);
+         lifeCycleA.lazyResolve = true;
+         
+         resolve(context);
+         ClassLoader cl = assertResolved(context);
+
+         ClassLoader clA = assertResolved(contextA);
+         
+         assertLoadClass(A.class, cl, clA);
+         assertTrue("Should get resolve invocation", lifeCycleA.gotResolve);
+      }
+      finally
+      {
+         uninstall(context);
+      }
+   }
+   
+   public void testNotLazyStart() throws Exception
+   {
+      MockClassLoadingMetaData metaData = new MockClassLoadingMetaData("test");
+      metaData.setPathsAndPackageNames(A.class);
+      KernelControllerContext context = install(metaData);
+      try
+      {
+         assertNotResolved(context);
+         
+         resolve(context);
+         ClassLoader cl = assertResolved(context);
+         assertLoadClass(A.class, cl);
+         assertResolved(context);
+      }
+      finally
+      {
+         uninstall(context);
+      }
+   }
+   
+   public void testLazyStart() throws Exception
+   {
+      MockClassLoadingMetaData metaData = new MockClassLoadingMetaData("test");
+      metaData.setPathsAndPackageNames(A.class);
+      KernelControllerContext context = install(metaData);
+      try
+      {
+         MockLifeCycle lifeCycle = assertNotResolved(context);
+         lifeCycle.lazyStart = true;
+         
+         resolve(context);
+         ClassLoader cl = assertResolved(context);
+         assertLoadClass(A.class, cl);
+         assertTrue("Should get start invocation", lifeCycle.gotStart);
+      }
+      finally
+      {
+         uninstall(context);
+      }
+   }
+}

Added: projects/jboss-cl/trunk/classloading/src/test/java/org/jboss/test/classloading/resolver/ResolverTestSuite.java
===================================================================
--- projects/jboss-cl/trunk/classloading/src/test/java/org/jboss/test/classloading/resolver/ResolverTestSuite.java	                        (rev 0)
+++ projects/jboss-cl/trunk/classloading/src/test/java/org/jboss/test/classloading/resolver/ResolverTestSuite.java	2009-11-18 17:09:48 UTC (rev 96499)
@@ -0,0 +1,61 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file 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.classloading.resolver;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+import junit.textui.TestRunner;
+
+import org.jboss.test.classloading.lifecycle.test.LifeCycleUnitTestCase;
+
+/**
+ * LifeCycle Test Suite.
+ * 
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @version $Revision: 37459 $
+ */
+public class ResolverTestSuite extends TestSuite
+{
+   /**
+    * For running the testsuite from the command line
+    * 
+    * @param args the command line args
+    */
+   public static void main(String[] args)
+   {
+      TestRunner.run(suite());
+   }
+
+   /**
+    * Create the testsuite
+    * 
+    * @return the testsuite
+    */
+   public static Test suite()
+   {
+      TestSuite suite = new TestSuite("LifeCycle Tests");
+
+      suite.addTest(LifeCycleUnitTestCase.suite());
+
+      return suite;
+   }
+}

Added: projects/jboss-cl/trunk/classloading/src/test/java/org/jboss/test/classloading/resolver/support/a/A.java
===================================================================
--- projects/jboss-cl/trunk/classloading/src/test/java/org/jboss/test/classloading/resolver/support/a/A.java	                        (rev 0)
+++ projects/jboss-cl/trunk/classloading/src/test/java/org/jboss/test/classloading/resolver/support/a/A.java	2009-11-18 17:09:48 UTC (rev 96499)
@@ -0,0 +1,32 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2009, Red Hat Middleware LLC, and individual contributors
+* as indicated by the @author tags. See the copyright.txt file 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.classloading.resolver.support.a;
+
+/**
+ * A.
+ * 
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @version $Revision: 1.1 $
+ */
+public class A
+{
+}

Added: projects/jboss-cl/trunk/classloading/src/test/java/org/jboss/test/classloading/resolver/test/ResolverUnitTestCase.java
===================================================================
--- projects/jboss-cl/trunk/classloading/src/test/java/org/jboss/test/classloading/resolver/test/ResolverUnitTestCase.java	                        (rev 0)
+++ projects/jboss-cl/trunk/classloading/src/test/java/org/jboss/test/classloading/resolver/test/ResolverUnitTestCase.java	2009-11-18 17:09:48 UTC (rev 96499)
@@ -0,0 +1,144 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file 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.classloading.resolver.test;
+
+import junit.framework.Test;
+
+import org.jboss.classloader.plugins.ClassLoaderUtils;
+import org.jboss.classloading.plugins.metadata.PackageRequirement;
+import org.jboss.classloading.spi.dependency.ClassLoading;
+import org.jboss.classloading.spi.dependency.ResolutionContext;
+import org.jboss.classloading.spi.dependency.Resolver;
+import org.jboss.classloading.spi.dependency.policy.mock.MockClassLoadingMetaData;
+import org.jboss.classloading.spi.metadata.ClassLoadingMetaDataFactory;
+import org.jboss.classloading.spi.metadata.Requirement;
+import org.jboss.kernel.spi.dependency.KernelControllerContext;
+import org.jboss.test.classloading.dependency.test.AbstractMockClassLoaderUnitTest;
+import org.jboss.test.classloading.resolver.support.a.A;
+
+/**
+ * ResolverUnitTestCase.
+ * 
+ * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @version $Revision: 1.1 $
+ */
+public class ResolverUnitTestCase extends AbstractMockClassLoaderUnitTest implements Resolver
+{
+   public static Test suite()
+   {
+      return suite(ResolverUnitTestCase.class);
+   }
+
+   static String packageA = ClassLoaderUtils.getClassPackageName(A.class.getName());
+   
+   boolean doResolve = true;
+   KernelControllerContext contextA = null;
+   ClassLoader other = null;
+   
+   public ResolverUnitTestCase(String name)
+   {
+      super(name);
+   }
+   
+   public boolean resolve(ResolutionContext context)
+   {
+      if (doResolve == false)
+         return false;
+      
+      Requirement requirement = context.getRequirement();
+      if (requirement instanceof PackageRequirement == false)
+         return false;
+      
+      PackageRequirement packageRequirement = (PackageRequirement) requirement;
+      if (packageA.equals(packageRequirement.getName()) == false)
+         return false;
+      
+      if (other != null)
+         return true;
+
+      MockClassLoadingMetaData a = new MockClassLoadingMetaData("a");
+      a.setPathsAndPackageNames(A.class);
+      try
+      {
+         contextA = install(a);
+         other = assertClassLoader(contextA);
+         return true;
+      }
+      catch (Exception e)
+      {
+         getLog().warn("Error:", e);
+      }
+      return false;
+   }
+
+   public void testResolve() throws Exception
+   {
+      doResolve = true;
+
+      MockClassLoadingMetaData metaData = new MockClassLoadingMetaData("test");
+      metaData.getRequirements().addRequirement(new PackageRequirement(ClassLoaderUtils.getClassPackageName(A.class.getName())));
+      KernelControllerContext context = install(metaData);
+      try
+      {
+         ClassLoader cl = assertClassLoader(context);
+         assertNotNull("Should have resolved the other classloader", other);
+         assertLoadClass(A.class, cl, other);
+      }
+      finally
+      {
+         uninstall(context);
+      }
+   }
+
+   public void testNotResolved() throws Exception
+   {
+      doResolve = false;
+      
+      ClassLoadingMetaDataFactory factory = ClassLoadingMetaDataFactory.getInstance();
+      MockClassLoadingMetaData metaData = new MockClassLoadingMetaData("test");
+      metaData.getRequirements().addRequirement(factory.createRequirePackage(packageA));
+      KernelControllerContext context = install(metaData);
+      try
+      {
+         assertNoClassLoader(context);
+         assertNull("Should not have resolved the other classloader", other);
+      }
+      finally
+      {
+         uninstall(context);
+      }
+   }
+
+   protected void setUp() throws Exception
+   {
+      super.setUp();
+      ClassLoading classLoading = getBean("ClassLoading", ClassLoading.class);
+      classLoading.addResolver(this);
+   }
+
+   protected void tearDown() throws Exception
+   {
+      if (contextA != null)
+         uninstall(contextA);
+      super.tearDown();
+   }
+}




More information about the jboss-cvs-commits mailing list