[jboss-cvs] JBossAS SVN: r97024 - in projects/kernel/trunk/dependency/src: main/java/org/jboss/dependency/spi/tracker and 2 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Thu Nov 26 09:23:16 EST 2009


Author: alesj
Date: 2009-11-26 09:23:15 -0500 (Thu, 26 Nov 2009)
New Revision: 97024

Added:
   projects/kernel/trunk/dependency/src/test/java/org/jboss/test/dependency/controller/support/TrackerAction.java
   projects/kernel/trunk/dependency/src/test/java/org/jboss/test/dependency/controller/test/AbstractContextQueryAndRegistryTest.java
   projects/kernel/trunk/dependency/src/test/java/org/jboss/test/dependency/controller/test/ContextRegistryTestCase.java
   projects/kernel/trunk/dependency/src/test/java/org/jboss/test/dependency/controller/test/ContextTrackerTestCase.java
   projects/kernel/trunk/dependency/src/test/java/org/jboss/test/dependency/controller/test/ControllerAsRnQTestCase.java
Modified:
   projects/kernel/trunk/dependency/src/main/java/org/jboss/dependency/plugins/AbstractControllerContext.java
   projects/kernel/trunk/dependency/src/main/java/org/jboss/dependency/spi/tracker/ContextQueries.java
   projects/kernel/trunk/dependency/src/test/java/org/jboss/test/dependency/controller/support/TestControllerContext.java
   projects/kernel/trunk/dependency/src/test/java/org/jboss/test/dependency/controller/support/TestDelegate.java
   projects/kernel/trunk/dependency/src/test/java/org/jboss/test/dependency/controller/test/AbstractDependencyTest.java
   projects/kernel/trunk/dependency/src/test/java/org/jboss/test/dependency/controller/test/ControllerTestSuite.java
Log:
[JBKERNEL-61] tracker, queries, registry tests.

Modified: projects/kernel/trunk/dependency/src/main/java/org/jboss/dependency/plugins/AbstractControllerContext.java
===================================================================
--- projects/kernel/trunk/dependency/src/main/java/org/jboss/dependency/plugins/AbstractControllerContext.java	2009-11-26 14:13:37 UTC (rev 97023)
+++ projects/kernel/trunk/dependency/src/main/java/org/jboss/dependency/plugins/AbstractControllerContext.java	2009-11-26 14:23:15 UTC (rev 97024)
@@ -28,6 +28,7 @@
 import java.util.List;
 import java.util.Set;
 
+import static org.jboss.dependency.plugins.tracker.TrackerConstants.NOOP;
 import org.jboss.dependency.spi.Controller;
 import org.jboss.dependency.spi.ControllerContext;
 import org.jboss.dependency.spi.ControllerContextActions;
@@ -38,10 +39,10 @@
 import org.jboss.dependency.spi.ScopeInfo;
 import org.jboss.dependency.spi.tracker.ContextTracker;
 import org.jboss.dependency.spi.tracker.ContextTracking;
-import static org.jboss.dependency.plugins.tracker.TrackerConstants.NOOP;
 import org.jboss.metadata.spi.MetaData;
 import org.jboss.metadata.spi.scope.CommonLevels;
 import org.jboss.metadata.spi.scope.CommonLevelsUtil;
+import org.jboss.metadata.spi.scope.ScopeKey;
 import org.jboss.metadata.spi.scope.ScopeLevel;
 import org.jboss.util.JBossObject;
 import org.jboss.util.JBossStringBuilder;
@@ -386,7 +387,8 @@
                ct = metaData.getMetaData(ContextTracker.class);
                if (ct == null)
                {
-                  List<ScopeLevel> levels = CommonLevelsUtil.getSubLevels(CommonLevels.SERVER);
+                  // go all the way up to default scope, get min level -- default should have single scope?
+                  List<ScopeLevel> levels = CommonLevelsUtil.getSubLevels(ScopeKey.DEFAULT_SCOPE.getMaxScopeLevel());
                   int instanceIndex = levels.indexOf(CommonLevels.INSTANCE);
                   for (int i = instanceIndex; i >= 0 && ct == null; i--)
                   {

Modified: projects/kernel/trunk/dependency/src/main/java/org/jboss/dependency/spi/tracker/ContextQueries.java
===================================================================
--- projects/kernel/trunk/dependency/src/main/java/org/jboss/dependency/spi/tracker/ContextQueries.java	2009-11-26 14:13:37 UTC (rev 97023)
+++ projects/kernel/trunk/dependency/src/main/java/org/jboss/dependency/spi/tracker/ContextQueries.java	2009-11-26 14:23:15 UTC (rev 97024)
@@ -74,11 +74,13 @@
    Set<ControllerContext> getInstantiatedContexts(Class<?> clazz);
 
    /**
-    * Get all contexts of a type which are in the given state or above
+    * Get all contexts of a type which are in the given state or above.
+    * Note: state must not be null
     *
     * @param clazz the type
     * @param state the required state
     * @return the contexts
+    * @throws IllegalArgumentException for null state
     */
    Set<ControllerContext> getContexts(Class<?> clazz, ControllerState state);
 

Modified: projects/kernel/trunk/dependency/src/test/java/org/jboss/test/dependency/controller/support/TestControllerContext.java
===================================================================
--- projects/kernel/trunk/dependency/src/test/java/org/jboss/test/dependency/controller/support/TestControllerContext.java	2009-11-26 14:13:37 UTC (rev 97023)
+++ projects/kernel/trunk/dependency/src/test/java/org/jboss/test/dependency/controller/support/TestControllerContext.java	2009-11-26 14:23:15 UTC (rev 97024)
@@ -26,6 +26,7 @@
 import org.jboss.dependency.plugins.AbstractControllerContext;
 import org.jboss.dependency.plugins.AbstractControllerContextActions;
 import org.jboss.dependency.plugins.action.ControllerContextAction;
+import org.jboss.dependency.plugins.action.SimpleControllerContextAction;
 import org.jboss.dependency.spi.ControllerContext;
 import org.jboss.dependency.spi.ControllerState;
 
@@ -33,6 +34,7 @@
  * A TestControllerContext.
  * 
  * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @author <a href="ales.justin at jboss.org">Ales Justin</a>
  * @version $Revision$
  */
 public class TestControllerContext extends AbstractControllerContext
@@ -60,7 +62,7 @@
    
    public TestControllerContext(TestDelegate delegate, AbstractControllerContextActions actions)
    {
-      super(delegate.getName(), actions, delegate.dependencies);
+      super(delegate.getName(), actions, delegate.dependencies, delegate.target);
       setMode(delegate.mode);
       setAliases(delegate.getAliases());
       this.delegate = delegate;
@@ -71,81 +73,94 @@
       return delegate;
    }
    
-   public static class DescribeAction implements ControllerContextAction
+   public abstract static class TestControllerContextAction extends SimpleControllerContextAction<TestControllerContext>
    {
-      public void install(ControllerContext context) throws Throwable
+      protected TestControllerContext contextCast(ControllerContext context)
       {
-         ((TestControllerContext) context).delegate.describeInstall();
+         return TestControllerContext.class.cast(context);
       }
 
-      public void uninstall(ControllerContext context)
+      protected boolean validateContext(ControllerContext context)
       {
-         ((TestControllerContext) context).delegate.describeUninstall();
+         return (context instanceof TestControllerContext);
       }
    }
    
-   public static class InstantiateAction implements ControllerContextAction
+   public static class DescribeAction extends TestControllerContextAction
    {
-      public void install(ControllerContext context) throws Throwable
+      public void installAction(TestControllerContext context) throws Throwable
       {
-         ((TestControllerContext) context).delegate.instantiateInstall();
+         context.delegate.describeInstall();
       }
 
-      public void uninstall(ControllerContext context)
+      public void uninstallAction(TestControllerContext context)
       {
-         ((TestControllerContext) context).delegate.instantiateUninstall();
+         context.delegate.describeUninstall();
       }
    }
    
-   public static class ConfigureAction implements ControllerContextAction
+   public static class InstantiateAction extends TestControllerContextAction
    {
-      public void install(ControllerContext context) throws Throwable
+      public void installAction(TestControllerContext context) throws Throwable
       {
-         ((TestControllerContext) context).delegate.configureInstall();
+         context.delegate.instantiateInstall();
       }
 
-      public void uninstall(ControllerContext context)
+      public void uninstallAction(TestControllerContext context)
       {
-         ((TestControllerContext) context).delegate.configureUninstall();
+         context.delegate.instantiateUninstall();
       }
    }
    
-   public static class CreateAction implements ControllerContextAction
+   public static class ConfigureAction extends TestControllerContextAction
    {
-      public void install(ControllerContext context) throws Throwable
+      public void installAction(TestControllerContext context) throws Throwable
       {
-         ((TestControllerContext) context).delegate.createInstall();
+         context.delegate.configureInstall();
       }
 
-      public void uninstall(ControllerContext context)
+      public void uninstallAction(TestControllerContext context)
       {
-         ((TestControllerContext) context).delegate.createUninstall();
+         context.delegate.configureUninstall();
       }
    }
    
-   public static class StartAction implements ControllerContextAction
+   public static class CreateAction extends TestControllerContextAction
    {
-      public void install(ControllerContext context) throws Throwable
+      public void installAction(TestControllerContext context) throws Throwable
       {
-         ((TestControllerContext) context).delegate.startInstall();
+         context.delegate.createInstall();
       }
 
-      public void uninstall(ControllerContext context)
+      public void uninstallAction(TestControllerContext context)
       {
-         ((TestControllerContext) context).delegate.startUninstall();
+         context.delegate.createUninstall();
       }
    }
    
-   public static class InstallAction implements ControllerContextAction
+   public static class StartAction extends TestControllerContextAction
    {
-      public void install(ControllerContext context) throws Throwable
+      public void installAction(TestControllerContext context) throws Throwable
       {
-         ((TestControllerContext) context).delegate.installInstall();
+         context.delegate.startInstall();
       }
 
-      public void uninstall(ControllerContext context)
+      public void uninstallAction(TestControllerContext context)
       {
-         ((TestControllerContext) context).delegate.installUninstall();
+         context.delegate.startUninstall();
       }
    }
+   
+   public static class InstallAction extends TestControllerContextAction
+   {
+      public void installAction(TestControllerContext context) throws Throwable
+      {
+         context.delegate.installInstall();
+      }
+
+      public void uninstallAction(TestControllerContext context)
+      {
+         context.delegate.installUninstall();
+      }
+   }
 }

Modified: projects/kernel/trunk/dependency/src/test/java/org/jboss/test/dependency/controller/support/TestDelegate.java
===================================================================
--- projects/kernel/trunk/dependency/src/test/java/org/jboss/test/dependency/controller/support/TestDelegate.java	2009-11-26 14:13:37 UTC (rev 97023)
+++ projects/kernel/trunk/dependency/src/test/java/org/jboss/test/dependency/controller/support/TestDelegate.java	2009-11-26 14:23:15 UTC (rev 97024)
@@ -23,6 +23,7 @@
 
 import java.util.HashSet;
 import java.util.Set;
+import java.util.Arrays;
 
 import org.jboss.dependency.plugins.AbstractDependencyInfo;
 import org.jboss.dependency.spi.ControllerMode;
@@ -32,6 +33,7 @@
  * A TestDelegate.
  * 
  * @author <a href="adrian at jboss.com">Adrian Brock</a>
+ * @author <a href="ales.justin at jboss.org">Ales Justin</a>
  * @version $Revision$
  */
 public class TestDelegate extends Ordering
@@ -42,8 +44,12 @@
    
    public AbstractDependencyInfo dependencies = new AbstractDependencyInfo();
 
+   public Object target;
+
    public ControllerMode mode = ControllerMode.AUTOMATIC;
-   
+
+   public Class<?>[] classes;
+
    public int describeInstallOrder = -1;
    public int describeUninstallOrder = -1;
    public int instantiateInstallOrder = -1;
@@ -68,8 +74,7 @@
       if (aliases != null)
       {
          this.aliases = new HashSet<Object>();
-         for (int i = 0; i < aliases.length; ++i)
-            this.aliases.add(aliases[i]);
+         this.aliases.addAll(Arrays.asList(aliases));
       }
    }
    
@@ -82,7 +87,7 @@
    {
       return aliases;
    }
-   
+
    public void describeInstall()
    {
       describeInstallOrder = order.incrementAndGet();

Added: projects/kernel/trunk/dependency/src/test/java/org/jboss/test/dependency/controller/support/TrackerAction.java
===================================================================
--- projects/kernel/trunk/dependency/src/test/java/org/jboss/test/dependency/controller/support/TrackerAction.java	                        (rev 0)
+++ projects/kernel/trunk/dependency/src/test/java/org/jboss/test/dependency/controller/support/TrackerAction.java	2009-11-26 14:23:15 UTC (rev 97024)
@@ -0,0 +1,35 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, 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.dependency.controller.support;
+
+import org.jboss.dependency.spi.tracker.ContextTracker;
+import org.jboss.dependency.spi.ControllerContext;
+
+/**
+ * @author <a href="mailto:ales.justin at jboss.org">Ales Justin</a>
+ */
+public interface TrackerAction
+{
+   void get(ContextTracker tracker, ControllerContext context, Object user);
+
+   void unget(ContextTracker tracker, ControllerContext context, Object user);
+}

Added: projects/kernel/trunk/dependency/src/test/java/org/jboss/test/dependency/controller/test/AbstractContextQueryAndRegistryTest.java
===================================================================
--- projects/kernel/trunk/dependency/src/test/java/org/jboss/test/dependency/controller/test/AbstractContextQueryAndRegistryTest.java	                        (rev 0)
+++ projects/kernel/trunk/dependency/src/test/java/org/jboss/test/dependency/controller/test/AbstractContextQueryAndRegistryTest.java	2009-11-26 14:23:15 UTC (rev 97024)
@@ -0,0 +1,128 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, 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.dependency.controller.test;
+
+import java.util.Collections;
+import java.util.Set;
+
+import org.jboss.dependency.plugins.AbstractControllerContext;
+import org.jboss.dependency.spi.ControllerContext;
+import org.jboss.dependency.spi.ControllerState;
+import org.jboss.dependency.spi.tracker.ContextQueries;
+import org.jboss.dependency.spi.tracker.ContextTracking;
+import org.jboss.test.dependency.controller.support.TestControllerContext;
+import org.jboss.test.dependency.controller.support.TestDelegate;
+import org.jboss.util.JBossObject;
+
+/**
+ * Context queries and registry tests.
+ *
+ * @author <a href="mailto:ales.justin at jboss.org">Ales Justin</a>
+ */
+public abstract class AbstractContextQueryAndRegistryTest extends AbstractDependencyTest
+{
+   protected AbstractContextQueryAndRegistryTest(String name)
+   {
+      super(name);
+   }
+
+   protected abstract ContextQueries getContextQueries();
+
+   protected abstract TestControllerContext installContext(TestDelegate delegate) throws Throwable;
+
+   protected abstract void uninstallContext(TestControllerContext context) throws Throwable;
+
+   public void testAllClasses() throws Throwable
+   {
+      TestDelegate delegate = new TestDelegate("test");
+      delegate.target = new TestControllerContext(delegate);
+      TestControllerContext context = installContext(delegate);
+      try
+      {
+         assertMatchByClass(context, TestControllerContext.class);
+         assertMatchByClass(context, AbstractControllerContext.class);
+         assertMatchByClass(context, JBossObject.class);
+         assertMatchByClass(context, ControllerContext.class);
+         assertMatchByClass(context, ContextTracking.class);
+      }
+      finally
+      {
+         uninstallContext(context);
+
+         assertNotFoundByClass(TestControllerContext.class);
+         assertNotFoundByClass(AbstractControllerContext.class);
+         assertNotFoundByClass(JBossObject.class);
+         assertNotFoundByClass(ControllerContext.class);
+         assertNotFoundByClass(ContextTracking.class);
+      }
+   }
+
+   public void testExplicitClasses() throws Throwable
+   {
+      TestDelegate delegate = new TestDelegate("test");
+      delegate.target = new TestControllerContext(delegate);
+      delegate.classes = new Class<?>[]{AbstractControllerContext.class, ContextTracking.class};
+      TestControllerContext context = installContext(delegate);
+      try
+      {
+         assertNotFoundByClass(TestControllerContext.class);
+         assertMatchByClass(context, AbstractControllerContext.class);
+         assertNotFoundByClass(JBossObject.class);
+         assertNotFoundByClass(ControllerContext.class);
+         assertMatchByClass(context, ContextTracking.class);
+      }
+      finally
+      {
+         uninstallContext(context);
+
+         assertNotFoundByClass(TestControllerContext.class);
+         assertNotFoundByClass(AbstractControllerContext.class);
+         assertNotFoundByClass(JBossObject.class);
+         assertNotFoundByClass(ControllerContext.class);
+         assertNotFoundByClass(ContextTracking.class);
+      }
+   }
+
+   protected void assertNotFoundByClass(Class<?> clazz) throws Throwable
+   {
+      assertMatchByClass(null, clazz);  
+   }
+   
+   protected void assertMatchByClass(ControllerContext context, Class<?> clazz) throws Throwable
+   {
+      ContextQueries query = getContextQueries();
+      Set<ControllerContext> singleton = null;
+      if (context != null)
+         singleton = Collections.singleton(context);
+
+      ControllerContext found = query.getContextByClass(clazz);
+      assertSame(context, found);
+      Set<ControllerContext> founds = query.getContexts(clazz, ControllerState.CREATE);
+      assertEquals(singleton, founds);
+      founds = query.getContexts(clazz, ControllerState.INSTALLED);
+      assertEquals(singleton, founds);
+      founds = query.getInstantiatedContexts(clazz);
+      assertEquals(singleton, founds);
+      founds = query.filter(founds, null);
+      assertEquals(singleton, founds);
+   }
+}
\ No newline at end of file

Modified: projects/kernel/trunk/dependency/src/test/java/org/jboss/test/dependency/controller/test/AbstractDependencyTest.java
===================================================================
--- projects/kernel/trunk/dependency/src/test/java/org/jboss/test/dependency/controller/test/AbstractDependencyTest.java	2009-11-26 14:13:37 UTC (rev 97023)
+++ projects/kernel/trunk/dependency/src/test/java/org/jboss/test/dependency/controller/test/AbstractDependencyTest.java	2009-11-26 14:23:15 UTC (rev 97024)
@@ -163,7 +163,12 @@
    
    protected ControllerContext createControllerContext(Object name, Object... aliases)
    {
-      AbstractControllerContext result = new AbstractControllerContext(name, new MockControllerContextActions());
+      return createControllerContextWithTarget(name, null, aliases);  
+   }
+
+   protected ControllerContext createControllerContextWithTarget(Object name, Object target, Object... aliases)
+   {
+      AbstractControllerContext result = new AbstractControllerContext(name, new MockControllerContextActions(), null, target);
       result.setAliases(createAliases(aliases));
       return result;
    }

Added: projects/kernel/trunk/dependency/src/test/java/org/jboss/test/dependency/controller/test/ContextRegistryTestCase.java
===================================================================
--- projects/kernel/trunk/dependency/src/test/java/org/jboss/test/dependency/controller/test/ContextRegistryTestCase.java	                        (rev 0)
+++ projects/kernel/trunk/dependency/src/test/java/org/jboss/test/dependency/controller/test/ContextRegistryTestCase.java	2009-11-26 14:23:15 UTC (rev 97024)
@@ -0,0 +1,100 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, 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.dependency.controller.test;
+
+import junit.framework.Test;
+
+import org.jboss.dependency.plugins.tracker.AbstractContextRegistry;
+import org.jboss.dependency.spi.tracker.ContextQueries;
+import org.jboss.dependency.spi.ControllerState;
+import org.jboss.test.dependency.controller.support.TestControllerContext;
+import org.jboss.test.dependency.controller.support.TestDelegate;
+
+/**
+ * Context queries and registry tests via explicit registry.
+ *
+ * @author <a href="mailto:ales.justin at jboss.org">Ales Justin</a>
+ */
+public class ContextRegistryTestCase extends AbstractContextQueryAndRegistryTest
+{
+   private AbstractContextRegistry registry;
+
+   public ContextRegistryTestCase(String name)
+   {
+      super(name);
+   }
+
+   public static Test suite()
+   {
+      return suite(ContextRegistryTestCase.class);
+   }
+
+   @Override
+   protected void setUp() throws Exception
+   {
+      super.setUp();
+      registry = new AbstractContextRegistry(controller);
+   }
+
+   @Override
+   protected void tearDown() throws Exception
+   {
+      registry = null;
+      super.tearDown();
+   }
+
+   protected ContextQueries getContextQueries()
+   {
+      return registry;
+   }
+
+   protected TestControllerContext installContext(TestDelegate delegate)
+   {
+      TestControllerContext context = new TestControllerContext(delegate);
+      if (delegate.classes != null)
+      {
+         for (Class<?> clazz : delegate.classes)
+            registry.registerInstantiatedContext(context, clazz);
+      }
+      else
+      {
+         registry.addInstantiatedContext(context);
+      }
+      context.setState(ControllerState.INSTALLED);
+      return context;
+   }
+
+   protected void uninstallContext(TestControllerContext context)
+   {
+      TestDelegate delegate = context.getDelegate();
+      if (delegate.classes != null)
+      {
+         for (Class<?> clazz : delegate.classes)
+            registry.unregisterInstantiatedContext(context, clazz);
+      }
+      else
+      {
+         registry.removeInstantiatedContext(context);
+      }
+      context.setState(ControllerState.ERROR);
+   }
+}
\ No newline at end of file

Added: projects/kernel/trunk/dependency/src/test/java/org/jboss/test/dependency/controller/test/ContextTrackerTestCase.java
===================================================================
--- projects/kernel/trunk/dependency/src/test/java/org/jboss/test/dependency/controller/test/ContextTrackerTestCase.java	                        (rev 0)
+++ projects/kernel/trunk/dependency/src/test/java/org/jboss/test/dependency/controller/test/ContextTrackerTestCase.java	2009-11-26 14:23:15 UTC (rev 97024)
@@ -0,0 +1,243 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, 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.dependency.controller.test;
+
+import java.util.Collections;
+
+import junit.framework.Test;
+
+import org.jboss.dependency.plugins.tracker.AbstractContextTracker;
+import org.jboss.dependency.spi.ControllerContext;
+import org.jboss.dependency.spi.ScopeInfo;
+import org.jboss.dependency.spi.tracker.ContextTracker;
+import org.jboss.dependency.spi.tracker.ContextTracking;
+import org.jboss.metadata.plugins.loader.memory.MemoryMetaDataLoader;
+import org.jboss.metadata.plugins.repository.basic.BasicMetaDataRepository;
+import org.jboss.metadata.spi.repository.MutableMetaDataRepository;
+import org.jboss.metadata.spi.retrieval.MetaDataRetrieval;
+import org.jboss.metadata.spi.scope.ScopeKey;
+import org.jboss.metadata.spi.loader.MutableMetaDataLoader;
+import org.jboss.test.dependency.controller.support.TrackerAction;
+
+/**
+ * Context tracker tests.
+ * 
+ * @author <a href="mailto:ales.justin at jboss.org">Ales Justin</a>
+ */
+public class ContextTrackerTestCase extends AbstractDependencyTest
+{
+   private MutableMetaDataRepository repository;
+   private MetaDataRetrieval retrieval;
+   private ContextTracker tracker;
+
+   public ContextTrackerTestCase(String name)
+   {
+      super(name);
+   }
+
+   public static Test suite()
+   {
+      return suite(ContextTrackerTestCase.class);
+   }
+
+   @Override
+   protected void setUp() throws Exception
+   {
+      super.setUp();
+
+      tracker = new AbstractContextTracker();
+      MemoryMetaDataLoader mem = new MemoryMetaDataLoader(ScopeKey.DEFAULT_SCOPE);
+      mem.addMetaData(tracker, ContextTracker.class);
+      retrieval = mem;
+      repository = new BasicMetaDataRepository();
+      repository.addMetaDataRetrieval(retrieval);
+   }
+
+   @Override
+   protected void tearDown() throws Exception
+   {
+      repository.removeMetaDataRetrieval(retrieval.getScope());
+      repository = null;
+      retrieval = null;
+
+      super.tearDown();
+   }
+
+   public void testTargetOverContext() throws Throwable
+   {
+      ControllerContext a = assertInstallContext("a");
+      ControllerContext b = assertInstallContext("b");
+      testContexts(a, b, 2); // same tracker, hence duplicate
+   }
+
+   public void testTargetOverTracker() throws Throwable
+   {
+      ControllerContext a = assertInstallContext("a");
+      ContextTracker b = createContextTracker();
+
+      ContextTracking ctA = toTracking(a);
+      ctA.getTarget(b);
+      ContextTracker trackerA = ctA.getContextTracker();
+      assertNotNull(trackerA);
+      assertEquals(Collections.<Object>singleton(b), trackerA.getUsers(a));
+      assertEquals(Collections.singleton(a), trackerA.getUsedContexts(null));
+      assertEquals(Collections.<Object>singleton(b), b.getUsers(a));
+      assertEquals(Collections.singleton(a), b.getUsedContexts(null));
+      assertEquals(1, trackerA.getUsedByCount(a, b));
+      assertEquals(1, b.getUsedByCount(a, b));
+      ctA.ungetTarget(b);
+      assertEmpty(trackerA.getUsers(a));
+      assertEmpty(trackerA.getUsedContexts(null));
+      assertEmpty(b.getUsers(a));
+      assertEmpty(b.getUsedContexts(null));
+
+      assertUninstallContext(a);
+   }
+
+   public void testUsedByRemoved() throws Throwable
+   {
+      testTrackerViaObject(0, new TrackerAction()
+      {
+         public void get(ContextTracker tracker, ControllerContext context, Object user)
+         {
+            tracker.addUsedBy(context, user);
+         }
+
+         public void unget(ContextTracker tracker, ControllerContext context, Object user)
+         {
+            tracker.removeUsedBy(context, user);
+         }
+      });
+   }
+
+   public void testIncrementDecement() throws Throwable
+   {
+      testTrackerViaObject(1, new TrackerAction()
+      {
+         public void get(ContextTracker tracker, ControllerContext context, Object user)
+         {
+            tracker.incrementUsedBy(context, user);
+         }
+
+         public void unget(ContextTracker tracker, ControllerContext context, Object user)
+         {
+            tracker.decrementUsedBy(context, user);
+         }
+      });
+   }
+
+   public void testDifferentTrackers() throws Throwable
+   {
+      ControllerContext a = assertInstallContext("a");
+      MetaDataRetrieval data = repository.getMetaDataRetrieval(a.getScopeInfo().getMutableScope());
+      ((MutableMetaDataLoader)data).addMetaData(new AbstractContextTracker(), ContextTracker.class);
+      ControllerContext b = assertInstallContext("b");
+      testContexts(a, b, 1);
+   }
+
+   // --- helpers
+
+   protected void testContexts(ControllerContext a, ControllerContext b, int count) throws Throwable
+   {
+      try
+      {
+         ContextTracking ctA = toTracking(a);
+         ctA.getTarget(b);
+
+         ContextTracker trackerA = ctA.getContextTracker();
+         assertNotNull(trackerA);
+         ContextTracking ctB = toTracking(b);
+         ContextTracker trackerB = ctB.getContextTracker();
+         assertNotNull(trackerB);
+
+         // A
+         assertEquals(Collections.<Object>singleton(b), trackerA.getUsers(a));
+         assertEquals(Collections.singleton(a), trackerA.getUsedContexts(null));
+         assertEquals(count, trackerA.getUsedByCount(a, b));
+         // B
+         assertEquals(Collections.<Object>singleton(b), trackerB.getUsers(a));
+         assertEquals(Collections.singleton(a), trackerB.getUsedContexts(null));
+         assertEquals(count, trackerB.getUsedByCount(a, b));
+
+         ctA.ungetTarget(b);
+
+         // A
+         assertEmpty(trackerA.getUsers(a));
+         assertEmpty(trackerA.getUsedContexts(null));
+         assertEquals(0, trackerA.getUsedByCount(a, b));
+         // B
+         assertEmpty(trackerB.getUsers(a));
+         assertEmpty(trackerB.getUsedContexts(null));
+         assertEquals(0, trackerB.getUsedByCount(a, b));
+      }
+      finally
+      {
+         assertUninstallContext(a);
+         assertUninstallContext(b);
+      }
+   }
+
+   protected void testTrackerViaObject(int count, TrackerAction action) throws Throwable
+   {
+      ControllerContext a = assertInstallContext("a");
+      Object b = new Object();
+
+      ContextTracking ctA = toTracking(a);
+      ContextTracker trackerA = ctA.getContextTracker();
+      assertNotNull(trackerA);
+      action.get(trackerA, a, b);
+      assertEquals(Collections.<Object>singleton(b), trackerA.getUsers(a));
+      assertEquals(Collections.singleton(a), trackerA.getUsedContexts(null));
+      assertEquals(count, trackerA.getUsedByCount(a, b));
+      action.unget(trackerA, a, b);
+      assertEmpty(trackerA.getUsers(a));
+      assertEmpty(trackerA.getUsedContexts(null));
+
+      assertUninstallContext(a);
+   }
+
+   protected ControllerContext assertInstallContext(Object name) throws Throwable
+   {
+      ControllerContext context = createControllerContextWithTarget(name, new Object());
+      ScopeInfo scopeInfo = context.getScopeInfo();
+      scopeInfo.addMetaData(repository, context);
+      scopeInfo.initMetaDataRetrieval(repository, context); // TODO - remove this?
+      install(context);
+      return context;
+   }
+
+   protected void assertUninstallContext(ControllerContext context) throws Throwable
+   {
+      context.getScopeInfo().removeMetaData(repository, context);
+      uninstall(context);
+   }
+
+   protected ContextTracking toTracking(ControllerContext context)
+   {
+      return assertInstanceOf(context, ContextTracking.class, false);
+   }
+
+   protected ContextTracker createContextTracker()
+   {
+      return new AbstractContextTracker();
+   }
+}

Added: projects/kernel/trunk/dependency/src/test/java/org/jboss/test/dependency/controller/test/ControllerAsRnQTestCase.java
===================================================================
--- projects/kernel/trunk/dependency/src/test/java/org/jboss/test/dependency/controller/test/ControllerAsRnQTestCase.java	                        (rev 0)
+++ projects/kernel/trunk/dependency/src/test/java/org/jboss/test/dependency/controller/test/ControllerAsRnQTestCase.java	2009-11-26 14:23:15 UTC (rev 97024)
@@ -0,0 +1,106 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2008, 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.dependency.controller.test;
+
+import java.util.Collections;
+import java.util.Map;
+
+import junit.framework.Test;
+
+import org.jboss.dependency.plugins.AbstractControllerContextActions;
+import org.jboss.dependency.plugins.action.ControllerContextAction;
+import org.jboss.dependency.spi.ControllerState;
+import org.jboss.dependency.spi.tracker.ContextQueries;
+import org.jboss.dependency.spi.tracker.ContextRegistry;
+import org.jboss.test.dependency.controller.support.TestControllerContext;
+import org.jboss.test.dependency.controller.support.TestDelegate;
+
+/**
+ * Context queries and registry tests via Controller.
+ *
+ * @author <a href="mailto:ales.justin at jboss.org">Ales Justin</a>
+ */
+public class ControllerAsRnQTestCase extends AbstractContextQueryAndRegistryTest
+{
+   public ControllerAsRnQTestCase(String name)
+   {
+      super(name);
+   }
+
+   public static Test suite()
+   {
+      return suite(ControllerAsRnQTestCase.class);
+   }
+
+   protected ContextQueries getContextQueries()
+   {
+      return assertInstanceOf(controller, ContextQueries.class, false);
+   }
+
+   protected ContextRegistry getContextRegistry()
+   {
+      return assertInstanceOf(controller, ContextRegistry.class, false);
+   }
+
+   protected TestControllerContext installContext(TestDelegate delegate) throws Throwable
+   {
+      ControllerContextAction action = new TestControllerContext.TestControllerContextAction()
+      {
+         public void installAction(TestControllerContext context) throws Throwable
+         {
+            TestDelegate delegate = context.getDelegate();
+            if (delegate.classes != null)
+            {
+               for (Class<?> clazz : delegate.classes)
+                  getContextRegistry().registerInstantiatedContext(context, clazz);
+            }
+            else
+            {
+               getContextRegistry().addInstantiatedContext(context);
+            }
+         }
+
+         public void uninstallAction(TestControllerContext context)
+         {
+            TestDelegate delegate = context.getDelegate();
+            if (delegate.classes != null)
+            {
+               for (Class<?> clazz : delegate.classes)
+                  getContextRegistry().unregisterInstantiatedContext(context, clazz);
+            }
+            else
+            {
+               getContextRegistry().removeInstantiatedContext(context);
+            }
+         }
+      };
+      Map<ControllerState, ControllerContextAction> map = Collections.singletonMap(ControllerState.INSTANTIATED, action);
+      TestControllerContext context = new TestControllerContext(delegate, new AbstractControllerContextActions(map));
+      controller.install(context);
+      return context;
+   }
+
+   protected void uninstallContext(TestControllerContext context) throws Throwable
+   {
+      uninstall(context);
+   }
+}
\ No newline at end of file

Modified: projects/kernel/trunk/dependency/src/test/java/org/jboss/test/dependency/controller/test/ControllerTestSuite.java
===================================================================
--- projects/kernel/trunk/dependency/src/test/java/org/jboss/test/dependency/controller/test/ControllerTestSuite.java	2009-11-26 14:13:37 UTC (rev 97023)
+++ projects/kernel/trunk/dependency/src/test/java/org/jboss/test/dependency/controller/test/ControllerTestSuite.java	2009-11-26 14:23:15 UTC (rev 97024)
@@ -70,6 +70,9 @@
       suite.addTest(ControllerStateModelTestCase.suite());
       suite.addTest(InitialStateTestCase.suite());
       suite.addTest(RecursiveResolutionTestCase.suite());
+      suite.addTest(ContextTrackerTestCase.suite());
+      suite.addTest(ContextRegistryTestCase.suite());
+      suite.addTest(ControllerAsRnQTestCase.suite());
 
       return suite;
    }




More information about the jboss-cvs-commits mailing list