Author: jmarino
Date: Sat May  6 02:43:37 2006
New Revision: 400276

URL: http://svn.apache.org/viewcvs?rev=400276&view=rev
Log:
start of testcase framework

Added:
    
incubator/tuscany/sandbox/jboynes/sca/core2/src/main/java/org/apache/tuscany/core/injection/SingletonObjectFactory.java
    
incubator/tuscany/sandbox/jboynes/sca/core2/src/test/java/org/apache/tuscany/core/mock/MockFactory.java
Removed:
    
incubator/tuscany/sandbox/jboynes/sca/core2/src/main/java/org/apache/tuscany/core/context/event/InstanceCreated.java
Modified:
    
incubator/tuscany/sandbox/jboynes/sca/core2/src/main/java/org/apache/tuscany/core/injection/PojoObjectFactory.java
    
incubator/tuscany/sandbox/jboynes/sca/core2/src/test/java/org/apache/tuscany/core/context/scope/DependencyLifecycleTestCase.java

Modified: 
incubator/tuscany/sandbox/jboynes/sca/core2/src/main/java/org/apache/tuscany/core/injection/PojoObjectFactory.java
URL: 
http://svn.apache.org/viewcvs/incubator/tuscany/sandbox/jboynes/sca/core2/src/main/java/org/apache/tuscany/core/injection/PojoObjectFactory.java?rev=400276&r1=400275&r2=400276&view=diff
==============================================================================
--- 
incubator/tuscany/sandbox/jboynes/sca/core2/src/main/java/org/apache/tuscany/core/injection/PojoObjectFactory.java
 (original)
+++ 
incubator/tuscany/sandbox/jboynes/sca/core2/src/main/java/org/apache/tuscany/core/injection/PojoObjectFactory.java
 Sat May  6 02:43:37 2006
@@ -39,6 +39,10 @@
 
     private final List<Injector> setters;
 
+    public PojoObjectFactory(Constructor<T> ctr) {
+        this(ctr,null,null);
+    }
+
     public PojoObjectFactory(Constructor<T> ctr, List<ObjectFactory> 
initParams, List<Injector> setters) {
         this.ctr = ctr;
         if (initParams != null && initParams.size() > 0) {

Added: 
incubator/tuscany/sandbox/jboynes/sca/core2/src/main/java/org/apache/tuscany/core/injection/SingletonObjectFactory.java
URL: 
http://svn.apache.org/viewcvs/incubator/tuscany/sandbox/jboynes/sca/core2/src/main/java/org/apache/tuscany/core/injection/SingletonObjectFactory.java?rev=400276&view=auto
==============================================================================
--- 
incubator/tuscany/sandbox/jboynes/sca/core2/src/main/java/org/apache/tuscany/core/injection/SingletonObjectFactory.java
 (added)
+++ 
incubator/tuscany/sandbox/jboynes/sca/core2/src/main/java/org/apache/tuscany/core/injection/SingletonObjectFactory.java
 Sat May  6 02:43:37 2006
@@ -0,0 +1,21 @@
+package org.apache.tuscany.core.injection;
+
+import org.apache.tuscany.common.ObjectFactory;
+
+/**
+ * Implementation of ObjectFactory that returns a single instance, typically 
an immutable type.
+ *
+ * @version $Rev: 399488 $ $Date: 2006-05-03 16:20:27 -0700 (Wed, 03 May 2006) 
$
+ */
+public class SingletonObjectFactory<T> implements ObjectFactory<T> {
+    private final T instance;
+
+    public SingletonObjectFactory(T instance) {
+        this.instance = instance;
+    }
+
+    public T getInstance() {
+        return instance;
+    }
+
+}

Modified: 
incubator/tuscany/sandbox/jboynes/sca/core2/src/test/java/org/apache/tuscany/core/context/scope/DependencyLifecycleTestCase.java
URL: 
http://svn.apache.org/viewcvs/incubator/tuscany/sandbox/jboynes/sca/core2/src/test/java/org/apache/tuscany/core/context/scope/DependencyLifecycleTestCase.java?rev=400276&r1=400275&r2=400276&view=diff
==============================================================================
--- 
incubator/tuscany/sandbox/jboynes/sca/core2/src/test/java/org/apache/tuscany/core/context/scope/DependencyLifecycleTestCase.java
 (original)
+++ 
incubator/tuscany/sandbox/jboynes/sca/core2/src/test/java/org/apache/tuscany/core/context/scope/DependencyLifecycleTestCase.java
 Sat May  6 02:43:37 2006
@@ -1,5 +1,7 @@
 package org.apache.tuscany.core.context.scope;
 
+import java.util.List;
+
 import junit.framework.TestCase;
 import junit.framework.Assert;
 import org.apache.tuscany.core.context.event.ModuleStart;
@@ -8,6 +10,14 @@
 import org.apache.tuscany.core.context.event.HttpSessionEnd;
 import org.apache.tuscany.core.context.event.RequestStart;
 import org.apache.tuscany.core.context.event.RequestEnd;
+import org.apache.tuscany.core.context.WorkContextImpl;
+import org.apache.tuscany.core.mock.MockFactory;
+import org.apache.tuscany.core.mock.component.SourceImpl;
+import org.apache.tuscany.core.mock.component.TargetImpl;
+import org.apache.tuscany.core.mock.component.Source;
+import org.apache.tuscany.spi.context.WorkContext;
+import org.apache.tuscany.spi.context.AtomicContext;
+import org.apache.tuscany.model.Scope;
 
 /**
  * Tests that dependencies are initalized and destroyed in the proper order 
(i.e. LIFO)
@@ -16,12 +26,18 @@
  */
 public class DependencyLifecycleTestCase extends TestCase {
 
+    public void testInitDestroyOrderModuleScope() throws Exception {
+        WorkContext ctx = new WorkContextImpl();
+        ModuleScopeContext scopeCtx = new ModuleScopeContext(ctx);
+        scopeCtx.start();
+        //scopeCtx.onEvent(new ModuleStart(this, null));
+        List<AtomicContext> contexts = 
MockFactory.createWiredContexts("source", SourceImpl.class,"target", 
TargetImpl.class,scopeCtx,scopeCtx);
+        Source source = (Source)contexts.get(0).getInstance(null);
+        assertNotNull(source.getTarget());
+        //scopeCtx.onEvent(new ModuleStop(this, null));
 
 
-    public void testInitDestroyOrderModuleScope() throws Exception {
-//        WorkContext ctx = new WorkContextImpl();
-//        ModuleScopeContext scope = new ModuleScopeContext(ctx);
-//        
scope.registerFactories(MockContextFactory.createWiredContexts(Scope.MODULE,scope));
+        
//scope.registerFactories(MockContextFactory.createWiredContexts(Scope.MODULE,scope));
 //        scope.start();
 //        scope.onEvent(new ModuleStart(this));
 //        OrderedDependentPojo source = (OrderedDependentPojo) 
scope.getContext("source").getInstance(null);

Added: 
incubator/tuscany/sandbox/jboynes/sca/core2/src/test/java/org/apache/tuscany/core/mock/MockFactory.java
URL: 
http://svn.apache.org/viewcvs/incubator/tuscany/sandbox/jboynes/sca/core2/src/test/java/org/apache/tuscany/core/mock/MockFactory.java?rev=400276&view=auto
==============================================================================
--- 
incubator/tuscany/sandbox/jboynes/sca/core2/src/test/java/org/apache/tuscany/core/mock/MockFactory.java
 (added)
+++ 
incubator/tuscany/sandbox/jboynes/sca/core2/src/test/java/org/apache/tuscany/core/mock/MockFactory.java
 Sat May  6 02:43:37 2006
@@ -0,0 +1,104 @@
+package org.apache.tuscany.core.mock;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.tuscany.common.ObjectCreationException;
+import org.apache.tuscany.common.ObjectFactory;
+import org.apache.tuscany.core.injection.EventInvoker;
+import org.apache.tuscany.core.injection.Injector;
+import org.apache.tuscany.core.injection.MethodInjector;
+import org.apache.tuscany.core.injection.PojoObjectFactory;
+import org.apache.tuscany.core.system.context.SystemAtomicContext;
+import org.apache.tuscany.spi.context.AtomicContext;
+import org.apache.tuscany.spi.context.ScopeContext;
+
+/**
+ * @version $$Rev$$ $$Date$$
+ */
+public class MockFactory {
+
+    /**
+     * Creates source and target [EMAIL PROTECTED] AtomicContext}s whose 
instances are wired together. The wiring
+     * algorithm searches for the first method on the source with a single 
parameter type matching an
+     * interface implemented by the target.
+     *
+     * @param source
+     * @param sourceClass
+     * @param target
+     * @param targetClass
+     * @param sourceScope
+     * @param targetScope
+     * @return
+     * @throws NoSuchMethodException
+     */
+    public static List<AtomicContext> createWiredContexts(String source, 
Class<?> sourceClass, String target,
+                                                          Class<?> targetClass,
+                                                          
ScopeContext<AtomicContext> sourceScope,
+                                                          
ScopeContext<AtomicContext> targetScope) throws NoSuchMethodException {
+
+        List<AtomicContext> contexts = new ArrayList<AtomicContext>();
+        SystemAtomicContext targetCtx = createSystemAtomicContext(target, 
targetClass, false, null, null, null);
+        targetCtx.setScopeContext(targetScope);
+
+        //wire the contexts
+        Class[] interfaces = targetClass.getInterfaces();
+        Method setter = null;
+        for (Class interfaze : interfaces) {
+            Method[] methods = sourceClass.getMethods();
+            for (Method method : methods) {
+                if (method.getParameterTypes().length == 1) {
+                    if 
(interfaze.isAssignableFrom(method.getParameterTypes()[0])) {
+                        setter = method;
+                        break;
+                    }
+                }
+            }
+            if (setter != null) {
+                break;
+            }
+        }
+        if (setter == null) {
+            throw new IllegalArgumentException("No setter found on source for 
target");
+        }
+        MethodInjector injector = new MethodInjector(setter, new 
AtomicContextInstanceFactory(targetCtx));
+        List<Injector> injectors = new ArrayList<Injector>();
+        injectors.add(injector);
+        SystemAtomicContext sourceCtx = createSystemAtomicContext(source, 
sourceClass, false, null, null, injectors);
+        sourceCtx.setScopeContext(sourceScope);
+
+        contexts.add(sourceCtx);
+        contexts.add(targetCtx);
+        return contexts;
+    }
+
+    public static SystemAtomicContext createSystemAtomicContext(String name, 
Class<?> clazz, boolean eagerInit, EventInvoker<Object> initInvoker,
+                                                                
EventInvoker<Object> destroyInvoker, List<Injector> injectors) throws 
NoSuchMethodException {
+        return new SystemAtomicContext(name, createObjectFactory(clazz, 
injectors), eagerInit, initInvoker, destroyInvoker);
+    }
+
+    private static <T> ObjectFactory<T> createObjectFactory(Class<T> clazz, 
List<Injector> injectors) throws NoSuchMethodException {
+        Constructor<T> ctr = clazz.getConstructor((Class<T>[]) null);
+        return new PojoObjectFactory<T>(ctr, null, injectors);
+    }
+
+
+    private static class AtomicContextInstanceFactory implements ObjectFactory 
{
+
+        private AtomicContext ctx;
+
+        public AtomicContextInstanceFactory(AtomicContext ctx) {
+            this.ctx = ctx;
+        }
+
+        public Object getInstance() throws ObjectCreationException {
+            return ctx.getTargetInstance();
+        }
+    }
+
+}
+
+//ObjectFactory<?> objectFactory, boolean eagerInit, EventInvoker<Object> 
initInvoker,
+//                               EventInvoker<Object> destroyInvoker
\ No newline at end of file


Reply via email to