You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by jm...@apache.org on 2006/05/14 04:03:00 UTC
svn commit: r406188 - in
/incubator/tuscany/sandbox/jboynes/sca/containers/container.java/src/test/java/org/apache/tuscany/container/java:
integration/context/ScopeReferenceTestCase.java mock/MockFactory.java
Author: jmarino
Date: Sat May 13 19:02:59 2006
New Revision: 406188
URL: http://svn.apache.org/viewcvs?rev=406188&view=rev
Log:
testcases for scope references
Modified:
incubator/tuscany/sandbox/jboynes/sca/containers/container.java/src/test/java/org/apache/tuscany/container/java/integration/context/ScopeReferenceTestCase.java
incubator/tuscany/sandbox/jboynes/sca/containers/container.java/src/test/java/org/apache/tuscany/container/java/mock/MockFactory.java
Modified: incubator/tuscany/sandbox/jboynes/sca/containers/container.java/src/test/java/org/apache/tuscany/container/java/integration/context/ScopeReferenceTestCase.java
URL: http://svn.apache.org/viewcvs/incubator/tuscany/sandbox/jboynes/sca/containers/container.java/src/test/java/org/apache/tuscany/container/java/integration/context/ScopeReferenceTestCase.java?rev=406188&r1=406187&r2=406188&view=diff
==============================================================================
--- incubator/tuscany/sandbox/jboynes/sca/containers/container.java/src/test/java/org/apache/tuscany/container/java/integration/context/ScopeReferenceTestCase.java (original)
+++ incubator/tuscany/sandbox/jboynes/sca/containers/container.java/src/test/java/org/apache/tuscany/container/java/integration/context/ScopeReferenceTestCase.java Sat May 13 19:02:59 2006
@@ -33,9 +33,12 @@
import org.apache.tuscany.core.context.event.HttpSessionStart;
import org.apache.tuscany.core.context.event.ModuleStart;
import org.apache.tuscany.core.context.event.ModuleStop;
+import org.apache.tuscany.core.context.event.RequestEnd;
+import org.apache.tuscany.core.context.event.RequestStart;
import org.apache.tuscany.core.context.scope.HttpSessionScopeContext;
import org.apache.tuscany.core.context.scope.ModuleScopeContext;
import org.apache.tuscany.core.context.scope.RequestScopeContext;
+import org.apache.tuscany.core.context.scope.StatelessScopeContext;
import org.apache.tuscany.core.util.JavaIntrospectionHelper;
import org.apache.tuscany.spi.context.AtomicContext;
import org.apache.tuscany.spi.context.ScopeContext;
@@ -50,7 +53,6 @@
private Map<String, Member> members;
-
/**
* Tests a module-to-module scoped wire
*/
@@ -110,9 +112,9 @@
sessionScope.onEvent(new HttpSessionStart(this, session2));
Target target2 = targetContext.getService();
- assertTrue(!"foo".equals(target2.getString()));
+ assertFalse("foo".equals(target2.getString()));
- assertTrue(!"foo".equals(source.getTarget().getString()));
+ assertFalse("foo".equals(source.getTarget().getString()));
source.getTarget().setString("bar");
assertEquals("bar", target2.getString());
assertEquals("bar", source.getTarget().getString());
@@ -132,12 +134,13 @@
WorkContext ctx = new WorkContextImpl();
ScopeContext<AtomicContext> moduleScope = new ModuleScopeContext(ctx);
moduleScope.start();
- ScopeContext<AtomicContext> requestScope = new RequestScopeContext(ctx);
+ final ScopeContext<AtomicContext> requestScope = new RequestScopeContext(ctx);
requestScope.start();
Map<String, AtomicContext> contexts = MockFactory.createWiredContexts("source", SourceImpl.class,
moduleScope, members, "target", Target.class, TargetImpl.class, requestScope);
moduleScope.onEvent(new ModuleStart(this, null));
+ requestScope.onEvent(new RequestStart(this));
AtomicContext<Source> sourceContext = (AtomicContext<Source>) contexts.get("source");
final AtomicContext<Target> targetContext = (AtomicContext<Target>) contexts.get("target");
@@ -153,619 +156,588 @@
Executor executor = Executors.newSingleThreadExecutor();
FutureTask<Void> future = new FutureTask<Void>(new Runnable() {
public void run() {
+ requestScope.onEvent(new RequestStart(this));
Target target2 = targetContext.getService();
- assertTrue(!"foo".equals(target2.getString()));
- assertTrue(!"foo".equals(source.getTarget().getString()));
+ assertFalse("foo".equals(target2.getString()));
+ assertFalse("foo".equals(source.getTarget().getString()));
source.getTarget().setString("bar");
assertEquals("bar", target2.getString());
assertEquals("bar", source.getTarget().getString());
+ requestScope.onEvent(new RequestEnd(this));
}
}, null);
executor.execute(future);
future.get();
+ assertEquals("foo", source.getTarget().getString());
+ requestScope.onEvent(new RequestEnd(this));
+ moduleScope.onEvent(new ModuleStop(this,null));
+ requestScope.stop();
+ moduleScope.stop();
}
/**
* Tests a module-to-stateless scoped wire is setup properly by the runtime
*/
+ @SuppressWarnings("unchecked")
public void testModuleToStateless() throws Exception {
-// RuntimeContext runtime = MockFactory.createJavaRuntime();
-// Context ctx = runtime.getSystemContext().getContext("tuscany.system.child");
-// Assert.assertNotNull(ctx);
-// runtime.getRootContext().registerModelObject(MockFactory.createCompositeComponent("test"));
-// CompositeContext testCtx = (CompositeContext) runtime.getRootContext().getContext("test");
-// Assert.assertNotNull(testCtx);
-// testCtx.registerModelObject(MockFactory.createModule(Scope.MODULE,Scope.INSTANCE));
-// testCtx.publish(new ModuleStart(this));
-//
-// // first request
-// Object id = new Object();
-// testCtx.publish(new RequestStart(this,id));
-// GenericComponent source = (GenericComponent)testCtx.getContext("source").getInstance(null);
-// Assert.assertNotNull(source);
-// GenericComponent target = (GenericComponent)testCtx.getContext("target").getInstance(null);
-// Assert.assertNotNull(target);
-// source.getGenericComponent().setString("foo");
-// Assert.assertTrue(!"foo".equals(target.getString()));
-// testCtx.publish(new RequestEnd(this,id));
-//
-// //second request
-// Object id2 = new Object();
-// testCtx.publish(new RequestStart(this,id2));
-// GenericComponent target2 = (GenericComponent)testCtx.getContext("target").getInstance(null);
-// Assert.assertNotNull(target2);
-// Assert.assertTrue(!"foo".equals(target2.getString()));
-//
-// Assert.assertTrue(!"foo".equals(source.getGenericComponent().getString()));
-// source.getGenericComponent().setString("bar");
-// Assert.assertTrue(!"bar".equals(target2.getString()));
+ WorkContext ctx = new WorkContextImpl();
+ ScopeContext<AtomicContext> moduleScope = new ModuleScopeContext(ctx);
+ moduleScope.start();
+ ScopeContext<AtomicContext> statelessScope = new StatelessScopeContext(ctx);
+ statelessScope.start();
+
+ Map<String, AtomicContext> contexts = MockFactory.createWiredContexts("source", SourceImpl.class,
+ moduleScope, members, "target", Target.class, TargetImpl.class, statelessScope);
+ moduleScope.onEvent(new ModuleStart(this, null));
+
+ AtomicContext<Source> sourceContext = (AtomicContext<Source>) contexts.get("source");
+ AtomicContext<Target> targetContext = (AtomicContext<Target>) contexts.get("target");
+ Source source = sourceContext.getService();
+ Target target = targetContext.getService();
+ assertTrue(Proxy.isProxyClass(source.getTarget().getClass()));
+ assertNull(source.getTarget().getString());
+ assertNull(target.getString());
+ target.setString("foo");
+ assertFalse("foo".equals(source.getTarget().getString()));
+ Target target2 = targetContext.getService();
+ assertFalse("foo".equals(target2.getString()));
+ source.getTarget().setString("bar");
+ assertFalse("bar".equals(source.getTarget().getString()));
+ moduleScope.onEvent(new ModuleStop(this, null));
+ moduleScope.stop();
+ statelessScope.stop();
}
+
/**
- * Tests a session-to-session scoped wire is setup properly by the runtime
+ * Tests a session-to-session scoped wire
*/
+ @SuppressWarnings("unchecked")
public void testSessionToSession() throws Exception {
-// RuntimeContext runtime = MockFactory.createJavaRuntime();
-// Context ctx = runtime.getSystemContext().getContext("tuscany.system.child");
-// Assert.assertNotNull(ctx);
-// runtime.getRootContext().registerModelObject(MockFactory.createCompositeComponent("test"));
-// CompositeContext testCtx = (CompositeContext) runtime.getRootContext().getContext("test");
-// Assert.assertNotNull(testCtx);
-// testCtx.registerModelObject(MockFactory.createModule(Scope.SESSION,Scope.SESSION));
-// testCtx.publish(new ModuleStart(this));
-//
-// // first session
-// Object session = new Object();
-// Object id = new Object();
-// testCtx.publish(new RequestStart(this,id));
-// testCtx.publish(new HttpSessionBound(this,session));
-// GenericComponent source = (GenericComponent)testCtx.getContext("source").getInstance(null);
-// Assert.assertNotNull(source);
-// GenericComponent target = (GenericComponent)testCtx.getContext("target").getInstance(null);
-// Assert.assertNotNull(target);
-// source.getGenericComponent().setString("foo");
-// source.getGenericComponent().setString("foo");
-// Assert.assertEquals("foo",target.getString());
-// testCtx.publish(new RequestEnd(this,id));
-//
-// //second session
-// Object session2 = new Object();
-// Object id2 = new Object();
-// testCtx.publish(new RequestStart(this,id2));
-// testCtx.publish(new HttpSessionBound(this,session2));
-// GenericComponent source2 = (GenericComponent)testCtx.getContext("source").getInstance(null);
-// Assert.assertNotNull(source2);
-// GenericComponent target2 = (GenericComponent)testCtx.getContext("target").getInstance(null);
-//
-// Assert.assertNotNull(target2);
-// Assert.assertEquals(null,target2.getString());
-// Assert.assertEquals(null,source2.getGenericComponent().getString());
-// source2.getGenericComponent().setString("baz");
-// Assert.assertEquals("baz",source2.getGenericComponent().getString());
-// Assert.assertEquals("baz",target2.getString());
-//
-// testCtx.publish(new RequestEnd(this,id2));
-//
+ WorkContext ctx = new WorkContextImpl();
+ ScopeContext<AtomicContext> sessionScope = new HttpSessionScopeContext(ctx);
+ sessionScope.start();
+
+ Map<String, AtomicContext> contexts = MockFactory.createWiredContexts("source", SourceImpl.class,
+ sessionScope, members, "target", Target.class, TargetImpl.class, sessionScope);
+
+ Object session1 = new Object();
+ ctx.setIdentifier(HttpSessionScopeContext.HTTP_IDENTIFIER, session1);
+ sessionScope.onEvent(new HttpSessionStart(this, session1));
+ AtomicContext<Source> sourceContext = (AtomicContext<Source>) contexts.get("source");
+ AtomicContext<Target> targetContext = (AtomicContext<Target>) contexts.get("target");
+ Source source = sourceContext.getService();
+ Target target = targetContext.getService();
+ source.getTarget().setString("foo");
+ source.getTarget().setString("foo");
+ assertEquals("foo", target.getString());
+
+ ctx.clearIdentifier(HttpSessionScopeContext.HTTP_IDENTIFIER);
+ sessionScope.onEvent(new HttpSessionEnd(this, session1));
+
+ //second session
+ Object session2 = new Object();
+ ctx.setIdentifier(HttpSessionScopeContext.HTTP_IDENTIFIER, session2);
+ sessionScope.onEvent(new HttpSessionStart(this, session2));
+
+ Source source2 = sourceContext.getService();
+ assertNotNull(source2);
+ Target target2 = targetContext.getService();
+
+ assertNotNull(target2);
+ assertNull(target2.getString());
+ assertEquals(null, source2.getTarget().getString());
+ source2.getTarget().setString("baz");
+ assertEquals("baz", source2.getTarget().getString());
+ assertEquals("baz", target2.getString());
+ ctx.clearIdentifier(HttpSessionScopeContext.HTTP_IDENTIFIER);
+ sessionScope.onEvent(new HttpSessionEnd(this, session2));
+ sessionScope.stop();
}
/**
- * Tests a session-to-module scoped wire is setup properly by the runtime
+ * Tests a session-to-module scoped wire
*/
+ @SuppressWarnings("unchecked")
public void testSessionToModule() throws Exception {
-// RuntimeContext runtime = MockFactory.createJavaRuntime();
-// Context ctx = runtime.getSystemContext().getContext("tuscany.system.child");
-// Assert.assertNotNull(ctx);
-// runtime.getRootContext().registerModelObject(MockFactory.createCompositeComponent("test"));
-// CompositeContext testCtx = (CompositeContext) runtime.getRootContext().getContext("test");
-// Assert.assertNotNull(testCtx);
-// testCtx.registerModelObject(MockFactory.createModule(Scope.SESSION,Scope.MODULE));
-// testCtx.publish(new ModuleStart(this));
-//
-// // first session
-// Object session = new Object();
-// Object id = new Object();
-// testCtx.publish(new RequestStart(this,id));
-// testCtx.publish(new HttpSessionBound(this,session));
-// GenericComponent source = (GenericComponent)testCtx.getContext("source").getInstance(null);
-// Assert.assertNotNull(source);
-// GenericComponent target = (GenericComponent)testCtx.getContext("target").getInstance(null);
-// Assert.assertNotNull(target);
-// source.getGenericComponent().setString("foo");
-// source.getGenericComponent().setString("foo");
-// Assert.assertEquals("foo",target.getString());
-// testCtx.publish(new RequestEnd(this,id));
-//
-// //second session
-// Object session2 = new Object();
-// Object id2 = new Object();
-// testCtx.publish(new RequestStart(this,id2));
-// testCtx.publish(new HttpSessionBound(this,session2));
-// GenericComponent source2 = (GenericComponent)testCtx.getContext("source").getInstance(null);
-// Assert.assertNotNull(source2);
-// GenericComponent target2 = (GenericComponent)testCtx.getContext("target").getInstance(null);
-//
-// Assert.assertNotNull(target2);
-// Assert.assertEquals("foo",target2.getString());
-// Assert.assertEquals("foo",source2.getGenericComponent().getString());
-// source2.getGenericComponent().setString("baz");
-// Assert.assertEquals("baz",source2.getGenericComponent().getString());
-// Assert.assertEquals("baz",target2.getString());
-// Assert.assertEquals("baz",target.getString());
-//
-// testCtx.publish(new RequestEnd(this,session2));
+ WorkContext ctx = new WorkContextImpl();
+ ScopeContext<AtomicContext> moduleScope = new ModuleScopeContext(ctx);
+ moduleScope.start();
+ ScopeContext<AtomicContext> sessionScope = new HttpSessionScopeContext(ctx);
+ sessionScope.start();
+
+ Map<String, AtomicContext> contexts = MockFactory.createWiredContexts("source", SourceImpl.class,
+ sessionScope, members, "target", Target.class, TargetImpl.class, moduleScope);
+ moduleScope.onEvent(new ModuleStart(this, null));
+ Object session1 = new Object();
+ ctx.setIdentifier(HttpSessionScopeContext.HTTP_IDENTIFIER, session1);
+ sessionScope.onEvent(new HttpSessionStart(this, session1));
+ AtomicContext<Source> sourceContext = (AtomicContext<Source>) contexts.get("source");
+ AtomicContext<Target> targetContext = (AtomicContext<Target>) contexts.get("target");
+ Source source = sourceContext.getService();
+ Target target = targetContext.getService();
+ assertNull(source.getTarget().getString());
+ assertNull(target.getString());
+ target.setString("foo");
+ assertTrue(Proxy.isProxyClass(source.getTarget().getClass()));
+ assertEquals("foo", source.getTarget().getString());
+ ctx.clearIdentifier(HttpSessionScopeContext.HTTP_IDENTIFIER);
+ sessionScope.onEvent(new HttpSessionEnd(this, session1));
+
+ //second session
+ Object session2 = new Object();
+ ctx.setIdentifier(HttpSessionScopeContext.HTTP_IDENTIFIER, session2);
+ sessionScope.onEvent(new HttpSessionStart(this, session2));
+ Target target2 = targetContext.getService();
+ Source source2 = sourceContext.getService();
+ assertEquals("foo", target2.getString());
+ assertEquals("foo", source2.getTarget().getString());
+ source2.getTarget().setString("baz");
+ assertEquals("baz", source2.getTarget().getString());
+ assertEquals("baz", target2.getString());
+ assertEquals("baz", target.getString());
+ ctx.clearIdentifier(HttpSessionScopeContext.HTTP_IDENTIFIER);
+ sessionScope.onEvent(new HttpSessionEnd(this, session2));
+ moduleScope.stop();
+ sessionScope.stop();
}
/**
* Tests a session-to-request scoped wire is setup properly by the runtime
*/
+ @SuppressWarnings("unchecked")
public void testSessionToRequest() throws Exception {
-// RuntimeContext runtime = MockFactory.createJavaRuntime();
-// Context ctx = runtime.getSystemContext().getContext("tuscany.system.child");
-// Assert.assertNotNull(ctx);
-// runtime.getRootContext().registerModelObject(MockFactory.createCompositeComponent("test"));
-// CompositeContext testCtx = (CompositeContext) runtime.getRootContext().getContext("test");
-// Assert.assertNotNull(testCtx);
-// testCtx.registerModelObject(MockFactory.createModule(Scope.SESSION,Scope.REQUEST));
-// testCtx.publish(new ModuleStart(this));
-//
-// // first session
-// Object session = new Object();
-// Object id = new Object();
-// testCtx.publish(new RequestStart(this,id));
-// testCtx.publish(new HttpSessionBound(this,session));
-// GenericComponent source = (GenericComponent)testCtx.getContext("source").getInstance(null);
-// Assert.assertNotNull(source);
-// GenericComponent target = (GenericComponent)testCtx.getContext("target").getInstance(null);
-// Assert.assertNotNull(target);
-// source.getGenericComponent().setString("foo");
-// Assert.assertEquals("foo",target.getString());
-// testCtx.publish(new RequestEnd(this,session));
-//
-// //second session
-// Object session2 = new Object();
-// Object id2 = new Object();
-// testCtx.publish(new RequestStart(this,id2));
-// testCtx.publish(new HttpSessionBound(this,session2));
-// GenericComponent source2 = (GenericComponent)testCtx.getContext("source").getInstance(null);
-// Assert.assertNotNull(source);
-// GenericComponent target2 = (GenericComponent)testCtx.getContext("target").getInstance(null);
-//
-// Assert.assertNotNull(target2);
-// Assert.assertEquals(null,target2.getString());
-// source2.getGenericComponent().setString("baz");
-// Assert.assertEquals("baz",target2.getString());
-// Assert.assertEquals("baz",source2.getGenericComponent().getString());
-//
-// Assert.assertEquals("foo",target.getString());
-// testCtx.publish(new RequestEnd(this,session));
+ WorkContext ctx = new WorkContextImpl();
+ final ScopeContext<AtomicContext> requestScope = new RequestScopeContext(ctx);
+ requestScope.start();
+ ScopeContext<AtomicContext> sessionScope = new HttpSessionScopeContext(ctx);
+ sessionScope.start();
+ Map<String, AtomicContext> contexts = MockFactory.createWiredContexts("source", SourceImpl.class,
+ sessionScope, members, "target", Target.class, TargetImpl.class, requestScope);
+ Object session1 = new Object();
+ ctx.setIdentifier(HttpSessionScopeContext.HTTP_IDENTIFIER, session1);
+ sessionScope.onEvent(new HttpSessionStart(this, session1));
+ requestScope.onEvent(new RequestStart(this));
+ AtomicContext<Source> sourceContext = (AtomicContext<Source>) contexts.get("source");
+ final AtomicContext<Target> targetContext = (AtomicContext<Target>) contexts.get("target");
+ final Source source = sourceContext.getService();
+ Target target = targetContext.getService();
+ assertNull(source.getTarget().getString());
+ assertNull(target.getString());
+ target.setString("foo");
+ assertTrue(Proxy.isProxyClass(source.getTarget().getClass()));
+ assertEquals("foo", source.getTarget().getString());
+
+ // spin off another request
+ Executor executor = Executors.newSingleThreadExecutor();
+ FutureTask<Void> future = new FutureTask<Void>(new Runnable() {
+ public void run() {
+ requestScope.onEvent(new RequestStart(this));
+ Target target2 = targetContext.getService();
+ assertFalse("foo".equals(target2.getString()));
+ assertFalse("foo".equals(source.getTarget().getString()));
+ source.getTarget().setString("bar");
+ assertEquals("bar", target2.getString());
+ assertEquals("bar", source.getTarget().getString());
+ requestScope.onEvent(new RequestEnd(this));
+ }
+ }, null);
+ executor.execute(future);
+ future.get();
+ assertEquals("foo", source.getTarget().getString());
+ requestScope.onEvent(new RequestEnd(this));
+ ctx.clearIdentifier(HttpSessionScopeContext.HTTP_IDENTIFIER);
+ sessionScope.onEvent(new HttpSessionEnd(this, session1));
+ requestScope.stop();
+ sessionScope.stop();
}
/**
* Tests a session-to-stateless scoped wire is setup properly by the runtime
*/
+ @SuppressWarnings("unchecked")
public void testSessionToStateless() throws Exception {
-// RuntimeContext runtime = MockFactory.createJavaRuntime();
-// Context ctx = runtime.getSystemContext().getContext("tuscany.system.child");
-// Assert.assertNotNull(ctx);
-// runtime.getRootContext().registerModelObject(MockFactory.createCompositeComponent("test"));
-// CompositeContext testCtx = (CompositeContext) runtime.getRootContext().getContext("test");
-// Assert.assertNotNull(testCtx);
-// testCtx.registerModelObject(MockFactory.createModule(Scope.SESSION,Scope.INSTANCE));
-// testCtx.publish(new ModuleStart(this));
-//
-// // first session
-// Object session = new Object();
-// Object id = new Object();
-// testCtx.publish(new RequestStart(this,id));
-// testCtx.publish(new HttpSessionBound(this,session));
-// GenericComponent source = (GenericComponent)testCtx.getContext("source").getInstance(null);
-// Assert.assertNotNull(source);
-// GenericComponent target = (GenericComponent)testCtx.getContext("target").getInstance(null);
-// Assert.assertNotNull(target);
-// source.getGenericComponent().setString("foo");
-// Assert.assertEquals(null,target.getString());
-// testCtx.publish(new RequestEnd(this,session));
-//
-// //second session
-// Object session2 = new Object();
-// Object id2 = new Object();
-// testCtx.publish(new RequestStart(this,id2));
-// testCtx.publish(new HttpSessionBound(this,session2));
-// GenericComponent source2 = (GenericComponent)testCtx.getContext("source").getInstance(null);
-// Assert.assertNotNull(source);
-// GenericComponent target2 = (GenericComponent)testCtx.getContext("target").getInstance(null);
-//
-// Assert.assertNotNull(target2);
-// Assert.assertEquals(null,target2.getString());
-// source2.getGenericComponent().setString("baz");
-// Assert.assertEquals(null,target2.getString()); //Note assumes no pooling
-// Assert.assertEquals(null,source2.getGenericComponent().getString());
-//
-// Assert.assertEquals(null,target.getString()); //Note assumes no pooling
-// testCtx.publish(new RequestEnd(this,session));
-//
+ WorkContext ctx = new WorkContextImpl();
+ ScopeContext<AtomicContext> sessionScope = new HttpSessionScopeContext(ctx);
+ sessionScope.start();
+ ScopeContext<AtomicContext> statelessScope = new StatelessScopeContext(ctx);
+ statelessScope.start();
+
+ Map<String, AtomicContext> contexts = MockFactory.createWiredContexts("source", SourceImpl.class,
+ sessionScope, members, "target", Target.class, TargetImpl.class, statelessScope);
+
+ Object session1 = new Object();
+ ctx.setIdentifier(HttpSessionScopeContext.HTTP_IDENTIFIER, session1);
+ sessionScope.onEvent(new HttpSessionStart(this, session1));
+
+ AtomicContext<Source> sourceContext = (AtomicContext<Source>) contexts.get("source");
+ AtomicContext<Target> targetContext = (AtomicContext<Target>) contexts.get("target");
+ Source source = sourceContext.getService();
+ Target target = targetContext.getService();
+ assertTrue(Proxy.isProxyClass(source.getTarget().getClass()));
+ assertNull(source.getTarget().getString());
+ assertNull(target.getString());
+ target.setString("foo");
+ assertFalse("foo".equals(source.getTarget().getString()));
+ Target target2 = targetContext.getService();
+ assertFalse("foo".equals(target2.getString()));
+ source.getTarget().setString("bar");
+ assertFalse("bar".equals(source.getTarget().getString()));
+
+ ctx.clearIdentifier(HttpSessionScopeContext.HTTP_IDENTIFIER);
+ sessionScope.onEvent(new HttpSessionEnd(this, session1));
+ sessionScope.stop();
+ statelessScope.stop();
}
/**
* Tests a request-to-request scoped wire is setup properly by the runtime
*/
+ @SuppressWarnings("unchecked")
public void testRequestToRequest() throws Exception {
-// RuntimeContext runtime = MockFactory.createJavaRuntime();
-// Context ctx = runtime.getSystemContext().getContext("tuscany.system.child");
-// Assert.assertNotNull(ctx);
-// runtime.getRootContext().registerModelObject(MockFactory.createCompositeComponent("test"));
-// CompositeContext testCtx = (CompositeContext) runtime.getRootContext().getContext("test");
-// Assert.assertNotNull(testCtx);
-// testCtx.registerModelObject(MockFactory.createModule(Scope.REQUEST,Scope.REQUEST));
-// testCtx.publish(new ModuleStart(this));
-//
-// // first request
-// Object id = new Object();
-// testCtx.publish(new RequestStart(this,id));
-// GenericComponent source = (GenericComponent)testCtx.getContext("source").getInstance(null);
-// Assert.assertNotNull(source);
-// GenericComponent target = (GenericComponent)testCtx.getContext("target").getInstance(null);
-// Assert.assertNotNull(target);
-// source.getGenericComponent().setString("foo");
-// Assert.assertEquals("foo",target.getString());
-// testCtx.publish(new RequestEnd(this,new Object()));
-//
-// //second request
-// Object id2 = new Object();
-// testCtx.publish(new RequestStart(this,id2));
-// GenericComponent source2 = (GenericComponent)testCtx.getContext("source").getInstance(null);
-// Assert.assertNotNull(source2);
-// GenericComponent target2 = (GenericComponent)testCtx.getContext("target").getInstance(null);
-//
-// Assert.assertNotNull(target2);
-// Assert.assertEquals(null,target2.getString());
-// Assert.assertEquals(null,source2.getGenericComponent().getString());
-// source2.getGenericComponent().setString("baz");
-// Assert.assertEquals("baz",source2.getGenericComponent().getString());
-// Assert.assertEquals("baz",target2.getString());
-//
-// testCtx.publish(new RequestEnd(this,new Object()));
+ WorkContext ctx = new WorkContextImpl();
+ final ScopeContext<AtomicContext> requestScope = new RequestScopeContext(ctx);
+ requestScope.start();
+
+ Map<String, AtomicContext> contexts = MockFactory.createWiredContexts("source", SourceImpl.class,
+ requestScope, members, "target", Target.class, TargetImpl.class, requestScope);
+ requestScope.onEvent(new RequestStart(this));
+
+ final AtomicContext<Source> sourceContext = (AtomicContext<Source>) contexts.get("source");
+ final AtomicContext<Target> targetContext = (AtomicContext<Target>) contexts.get("target");
+ Source source = sourceContext.getService();
+ Target target = targetContext.getService();
+ assertNull(source.getTarget().getString());
+ assertNull(target.getString());
+ target.setString("foo");
+ assertTrue(Proxy.isProxyClass(source.getTarget().getClass()));
+ assertEquals("foo", source.getTarget().getString());
+
+ // spin off another request
+ Executor executor = Executors.newSingleThreadExecutor();
+ FutureTask<Void> future = new FutureTask<Void>(new Runnable() {
+ public void run() {
+ requestScope.onEvent(new RequestStart(this));
+ Source source2 = sourceContext.getService();
+ Target target2 = targetContext.getService();
+ assertFalse("foo".equals(target2.getString()));
+ assertFalse("foo".equals(source2.getTarget().getString()));
+ source2.getTarget().setString("bar");
+ assertEquals("bar", target2.getString());
+ assertEquals("bar", source2.getTarget().getString());
+ requestScope.onEvent(new RequestEnd(this));
+ }
+ }, null);
+ executor.execute(future);
+ future.get();
+ requestScope.onEvent(new RequestEnd(this));
+ requestScope.stop();
}
/**
- * Tests a request-to-module scoped wire is setup properly by the runtime
+ * Tests a request-to-module scoped wire
*/
+ @SuppressWarnings("unchecked")
public void testRequestToModule() throws Exception {
-// RuntimeContext runtime = MockFactory.createJavaRuntime();
-// Context ctx = runtime.getSystemContext().getContext("tuscany.system.child");
-// Assert.assertNotNull(ctx);
-// runtime.getRootContext().registerModelObject(MockFactory.createCompositeComponent("test"));
-// CompositeContext testCtx = (CompositeContext) runtime.getRootContext().getContext("test");
-// Assert.assertNotNull(testCtx);
-// testCtx.registerModelObject(MockFactory.createModule(Scope.REQUEST,Scope.MODULE));
-// testCtx.publish(new ModuleStart(this));
-//
-// // first request
-// Object id = new Object();
-// testCtx.publish(new RequestStart(this,id));
-// GenericComponent source = (GenericComponent)testCtx.getContext("source").getInstance(null);
-// Assert.assertNotNull(source);
-// GenericComponent target = (GenericComponent)testCtx.getContext("target").getInstance(null);
-// Assert.assertNotNull(target);
-// source.getGenericComponent().setString("foo");
-// Assert.assertEquals("foo",target.getString());
-// testCtx.publish(new RequestEnd(this,new Object()));
-//
-// //second request
-// Object id2 = new Object();
-// testCtx.publish(new RequestStart(this,id2));
-// GenericComponent source2 = (GenericComponent)testCtx.getContext("source").getInstance(null);
-// Assert.assertNotNull(source2);
-// GenericComponent target2 = (GenericComponent)testCtx.getContext("target").getInstance(null);
-//
-// Assert.assertNotNull(target2);
-// Assert.assertEquals("foo",target2.getString());
-// Assert.assertEquals("foo",source2.getGenericComponent().getString());
-// source2.getGenericComponent().setString("baz");
-// Assert.assertEquals("baz",source2.getGenericComponent().getString());
-// Assert.assertEquals("baz",target2.getString());
-// Assert.assertEquals("baz",target.getString());
-//
-// testCtx.publish(new RequestEnd(this,new Object()));
+ WorkContext ctx = new WorkContextImpl();
+ final ScopeContext<AtomicContext> requestScope = new RequestScopeContext(ctx);
+ final ScopeContext<AtomicContext> moduleScope = new ModuleScopeContext(ctx);
+ requestScope.start();
+ moduleScope.start();
+ moduleScope.onEvent(new ModuleStart(this, null));
+
+ Map<String, AtomicContext> contexts = MockFactory.createWiredContexts("source", SourceImpl.class,
+ requestScope, members, "target", Target.class, TargetImpl.class, moduleScope);
+ requestScope.onEvent(new RequestStart(this));
+
+ final AtomicContext<Source> sourceContext = (AtomicContext<Source>) contexts.get("source");
+ final AtomicContext<Target> targetContext = (AtomicContext<Target>) contexts.get("target");
+ Source source = sourceContext.getService();
+ Target target = targetContext.getService();
+ assertNull(source.getTarget().getString());
+ assertNull(target.getString());
+ target.setString("foo");
+ assertTrue(Proxy.isProxyClass(source.getTarget().getClass()));
+ assertEquals("foo", source.getTarget().getString());
+
+ // spin off another request
+ Executor executor = Executors.newSingleThreadExecutor();
+ FutureTask<Void> future = new FutureTask<Void>(new Runnable() {
+ public void run() {
+ requestScope.onEvent(new RequestStart(this));
+ Source source2 = sourceContext.getService();
+ Target target2 = targetContext.getService();
+ assertTrue("foo".equals(target2.getString()));
+ assertTrue("foo".equals(source2.getTarget().getString()));
+ source2.getTarget().setString("bar");
+ assertEquals("bar", target2.getString());
+ assertEquals("bar", source2.getTarget().getString());
+ requestScope.onEvent(new RequestEnd(this));
+ }
+ }, null);
+ executor.execute(future);
+ future.get();
+ assertEquals("bar", target.getString());
+
+ requestScope.onEvent(new RequestEnd(this));
+ requestScope.stop();
+ moduleScope.onEvent(new ModuleStop(this, null));
+ moduleScope.stop();
}
/**
* Tests a request-to-session scoped wire is setup properly by the runtime
*/
+ @SuppressWarnings("unchecked")
public void testRequestToSession() throws Exception {
-// RuntimeContext runtime = MockFactory.createJavaRuntime();
-// Context ctx = runtime.getSystemContext().getContext("tuscany.system.child");
-// Assert.assertNotNull(ctx);
-// runtime.getRootContext().registerModelObject(MockFactory.createCompositeComponent("test"));
-// CompositeContext testCtx = (CompositeContext) runtime.getRootContext().getContext("test");
-// Assert.assertNotNull(testCtx);
-// testCtx.registerModelObject(MockFactory.createModule(Scope.REQUEST,Scope.SESSION));
-// testCtx.publish(new ModuleStart(this));
-//
-// // first session
-// Object session = new Object();
-// Object id = new Object();
-// testCtx.publish(new RequestStart(this,id));
-// testCtx.publish(new HttpSessionBound(this,session));
-// GenericComponent source = (GenericComponent)testCtx.getContext("source").getInstance(null);
-// Assert.assertNotNull(source);
-// GenericComponent target = (GenericComponent)testCtx.getContext("target").getInstance(null);
-// Assert.assertNotNull(target);
-// source.getGenericComponent().setString("foo");
-// Assert.assertEquals("foo",target.getString());
-// testCtx.publish(new RequestEnd(this,new Object()));
-//
-// //second request for session
-// Object id2 = new Object();
-// testCtx.publish(new RequestStart(this,id2));
-// testCtx.publish(new HttpSessionBound(this,session));
-// GenericComponent targetR2 = (GenericComponent)testCtx.getContext("target").getInstance(null);
-// Assert.assertEquals("foo",targetR2.getString());
-// GenericComponent sourceR2 = (GenericComponent)testCtx.getContext("source").getInstance(null);
-// Assert.assertNotNull(sourceR2);
-// Assert.assertEquals("foo",sourceR2.getGenericComponent().getString());
-//
-// testCtx.publish(new RequestEnd(this,new Object()));
-//
-// //second session
-// Object session2 = new Object();
-// Object id3 = new Object();
-// testCtx.publish(new RequestStart(this,id3));
-// testCtx.publish(new HttpSessionBound(this,session2));
-// GenericComponent source2 = (GenericComponent)testCtx.getContext("source").getInstance(null);
-// Assert.assertNotNull(source2);
-// GenericComponent target2 = (GenericComponent)testCtx.getContext("target").getInstance(null);
-//
-// Assert.assertNotNull(target2);
-// Assert.assertEquals(null,target2.getString());
-// Assert.assertEquals(null,source2.getGenericComponent().getString());
-// source2.getGenericComponent().setString("baz");
-// Assert.assertEquals("baz",source2.getGenericComponent().getString());
-// Assert.assertEquals("baz",target2.getString());
-//
-// testCtx.publish(new RequestEnd(this,session2));
-// Object id4 = new Object();
-// testCtx.publish(new RequestStart(this,id4));
-// testCtx.publish(new HttpSessionBound(this,session));
-// testCtx.publish(new RequestEnd(this,session));
+ WorkContext ctx = new WorkContextImpl();
+ final ScopeContext<AtomicContext> requestScope = new RequestScopeContext(ctx);
+ final ScopeContext<AtomicContext> sessionScope = new HttpSessionScopeContext(ctx);
+ requestScope.start();
+ sessionScope.start();
+
+ Object session1 = new Object();
+ ctx.setIdentifier(HttpSessionScopeContext.HTTP_IDENTIFIER, session1);
+ sessionScope.onEvent(new HttpSessionStart(this, session1));
+ Map<String, AtomicContext> contexts = MockFactory.createWiredContexts("source", SourceImpl.class,
+ requestScope, members, "target", Target.class, TargetImpl.class, sessionScope);
+
+ final AtomicContext<Source> sourceContext = (AtomicContext<Source>) contexts.get("source");
+ final AtomicContext<Target> targetContext = (AtomicContext<Target>) contexts.get("target");
+ requestScope.onEvent(new RequestStart(this));
+ Source source = sourceContext.getService();
+ Target target = targetContext.getService();
+ assertNull(source.getTarget().getString());
+ assertNull(target.getString());
+ target.setString("foo");
+ assertTrue(Proxy.isProxyClass(source.getTarget().getClass()));
+ assertEquals("foo", source.getTarget().getString());
+ // spin off another request
+ Executor executor = Executors.newSingleThreadExecutor();
+ FutureTask<Void> future = new FutureTask<Void>(new Runnable() {
+ public void run() {
+ requestScope.onEvent(new RequestStart(this));
+ Source source2 = sourceContext.getService();
+ Target target2 = targetContext.getService();
+ assertTrue("foo".equals(target2.getString()));
+ assertTrue("foo".equals(source2.getTarget().getString()));
+ source2.getTarget().setString("bar");
+ assertEquals("bar", target2.getString());
+ assertEquals("bar", source2.getTarget().getString());
+ requestScope.onEvent(new RequestEnd(this));
+ }
+ }, null);
+ executor.execute(future);
+ future.get();
+ assertEquals("bar", target.getString());
+
+ requestScope.onEvent(new RequestEnd(this));
+ requestScope.stop();
+ ctx.clearIdentifier(HttpSessionScopeContext.HTTP_IDENTIFIER);
+ sessionScope.onEvent(new HttpSessionEnd(this, session1));
+ sessionScope.stop();
}
/**
* Tests a request-to-stateless scoped wire is setup properly by the runtime
*/
+ @SuppressWarnings("unchecked")
public void testRequestToStateless() throws Exception {
-// RuntimeContext runtime = MockFactory.createJavaRuntime();
-// Context ctx = runtime.getSystemContext().getContext("tuscany.system.child");
-// Assert.assertNotNull(ctx);
-// runtime.getRootContext().registerModelObject(MockFactory.createCompositeComponent("test"));
-// CompositeContext testCtx = (CompositeContext) runtime.getRootContext().getContext("test");
-// Assert.assertNotNull(testCtx);
-// testCtx.registerModelObject(MockFactory.createModule(Scope.REQUEST,Scope.INSTANCE));
-// testCtx.publish(new ModuleStart(this));
-//
-// // first request
-// Object id = new Object();
-// testCtx.publish(new RequestStart(this,id));
-// GenericComponent source = (GenericComponent)testCtx.getContext("source").getInstance(null);
-// Assert.assertNotNull(source);
-// GenericComponent target = (GenericComponent)testCtx.getContext("target").getInstance(null);
-// Assert.assertNotNull(target);
-// source.getGenericComponent().setString("foo");
-// Assert.assertEquals(null,target.getString());
-// testCtx.publish(new RequestEnd(this,new Object()));
-//
-// //second request
-// Object id2 = new Object();
-// testCtx.publish(new RequestStart(this,id2));
-// GenericComponent source2 = (GenericComponent)testCtx.getContext("source").getInstance(null);
-// Assert.assertNotNull(source2);
-// GenericComponent target2 = (GenericComponent)testCtx.getContext("target").getInstance(null);
-//
-// Assert.assertNotNull(target2);
-// Assert.assertEquals(null,target2.getString());
-// Assert.assertEquals(null,source2.getGenericComponent().getString());
-// source2.getGenericComponent().setString("baz");
-// Assert.assertEquals(null,source2.getGenericComponent().getString());
-// Assert.assertEquals(null,target2.getString());
-//
-// testCtx.publish(new RequestEnd(this,new Object()));
+ WorkContext ctx = new WorkContextImpl();
+ ScopeContext<AtomicContext> requestScope = new RequestScopeContext(ctx);
+ requestScope.start();
+ ScopeContext<AtomicContext> statelessScope = new StatelessScopeContext(ctx);
+ statelessScope.start();
+
+ Map<String, AtomicContext> contexts = MockFactory.createWiredContexts("source", SourceImpl.class,
+ requestScope, members, "target", Target.class, TargetImpl.class, statelessScope);
+
+ AtomicContext<Source> sourceContext = (AtomicContext<Source>) contexts.get("source");
+ AtomicContext<Target> targetContext = (AtomicContext<Target>) contexts.get("target");
+ requestScope.onEvent(new RequestStart(this));
+ Source source = sourceContext.getService();
+ Target target = targetContext.getService();
+ assertTrue(Proxy.isProxyClass(source.getTarget().getClass()));
+ assertNull(source.getTarget().getString());
+ assertNull(target.getString());
+ target.setString("foo");
+ assertFalse("foo".equals(source.getTarget().getString()));
+ Target target2 = targetContext.getService();
+ assertFalse("foo".equals(target2.getString()));
+ source.getTarget().setString("bar");
+ assertFalse("bar".equals(source.getTarget().getString()));
+ requestScope.onEvent(new RequestEnd(this));
+ requestScope.stop();
+ statelessScope.stop();
}
/**
* Tests a stateless-to-stateless scoped wire is setup properly by the runtime
*/
+ @SuppressWarnings("unchecked")
public void testStatelessToStateless() throws Exception {
-// RuntimeContext runtime = MockFactory.createJavaRuntime();
-// Context ctx = runtime.getSystemContext().getContext("tuscany.system.child");
-// Assert.assertNotNull(ctx);
-// runtime.getRootContext().registerModelObject(MockFactory.createCompositeComponent("test"));
-// CompositeContext testCtx = (CompositeContext) runtime.getRootContext().getContext("test");
-// Assert.assertNotNull(testCtx);
-// testCtx.registerModelObject(MockFactory.createModule(Scope.INSTANCE,Scope.INSTANCE));
-// testCtx.publish(new ModuleStart(this));
-//
-// // first request
-// Object id = new Object();
-// testCtx.publish(new RequestStart(this,id));
-// GenericComponent source = (GenericComponent)testCtx.getContext("source").getInstance(null);
-// Assert.assertNotNull(source);
-// GenericComponent target = (GenericComponent)testCtx.getContext("target").getInstance(null);
-// Assert.assertNotNull(target);
-// source.getGenericComponent().setString("foo");
-// Assert.assertEquals(null,target.getString());
-// testCtx.publish(new RequestEnd(this,new Object()));
-//
-// //second request
-// Object id2 = new Object();
-// testCtx.publish(new RequestStart(this,id2));
-// GenericComponent source2 = (GenericComponent)testCtx.getContext("source").getInstance(null);
-// Assert.assertNotNull(source2);
-// GenericComponent target2 = (GenericComponent)testCtx.getContext("target").getInstance(null);
-//
-// Assert.assertNotNull(target2);
-// Assert.assertEquals(null,target2.getString());
-// Assert.assertEquals(null,source2.getGenericComponent().getString());
-// source2.getGenericComponent().setString("baz");
-// Assert.assertEquals(null,source2.getGenericComponent().getString());
-// Assert.assertEquals(null,target2.getString());
-//
-// testCtx.publish(new RequestEnd(this,new Object()));
+ WorkContext ctx = new WorkContextImpl();
+ ScopeContext<AtomicContext> statelessScope = new StatelessScopeContext(ctx);
+ statelessScope.start();
+
+ Map<String, AtomicContext> contexts = MockFactory.createWiredContexts("source", SourceImpl.class,
+ statelessScope, members, "target", Target.class, TargetImpl.class, statelessScope);
+
+ AtomicContext<Source> sourceContext = (AtomicContext<Source>) contexts.get("source");
+ AtomicContext<Target> targetContext = (AtomicContext<Target>) contexts.get("target");
+ Source source = sourceContext.getService();
+ Target target = targetContext.getService();
+ assertTrue(Proxy.isProxyClass(source.getTarget().getClass()));
+ assertNull(source.getTarget().getString());
+ assertNull(target.getString());
+ target.setString("foo");
+ assertFalse("foo".equals(source.getTarget().getString()));
+ Target target2 = targetContext.getService();
+ assertFalse("foo".equals(target2.getString()));
+ source.getTarget().setString("bar");
+ assertFalse("bar".equals(source.getTarget().getString()));
+ statelessScope.stop();
}
/**
* Tests a stateless-to-request scoped wire is setup properly by the runtime
*/
+ @SuppressWarnings("unchecked")
public void testStatelessToRequest() throws Exception {
-// RuntimeContext runtime = MockFactory.createJavaRuntime();
-// Context ctx = runtime.getSystemContext().getContext("tuscany.system.child");
-// Assert.assertNotNull(ctx);
-// runtime.getRootContext().registerModelObject(MockFactory.createCompositeComponent("test"));
-// CompositeContext testCtx = (CompositeContext) runtime.getRootContext().getContext("test");
-// Assert.assertNotNull(testCtx);
-// testCtx.registerModelObject(MockFactory.createModule(Scope.INSTANCE,Scope.REQUEST));
-// testCtx.publish(new ModuleStart(this));
-//
-// // first request
-// Object id = new Object();
-// testCtx.publish(new RequestStart(this,id));
-// GenericComponent source = (GenericComponent)testCtx.getContext("source").getInstance(null);
-// Assert.assertNotNull(source);
-// GenericComponent target = (GenericComponent)testCtx.getContext("target").getInstance(null);
-// Assert.assertNotNull(target);
-// source.getGenericComponent().setString("foo");
-// Assert.assertEquals("foo",target.getString());
-// testCtx.publish(new RequestEnd(this,new Object()));
-//
-// GenericComponent targetR1 = (GenericComponent)testCtx.getContext("target").getInstance(null);
-// Assert.assertNotNull(targetR1);
-// Assert.assertEquals("foo",target.getString());
-//
-// //second request
-// Object id2 = new Object();
-// testCtx.publish(new RequestStart(this,id2));
-// GenericComponent source2 = (GenericComponent)testCtx.getContext("source").getInstance(null);
-// Assert.assertNotNull(source2);
-// GenericComponent target2 = (GenericComponent)testCtx.getContext("target").getInstance(null);
-//
-// Assert.assertNotNull(target2);
-// Assert.assertEquals(null,target2.getString());
-// Assert.assertEquals(null,source2.getGenericComponent().getString());
-// source2.getGenericComponent().setString("baz");
-// Assert.assertEquals("baz",source2.getGenericComponent().getString());
-// Assert.assertEquals("baz",target2.getString());
-//
-// testCtx.publish(new RequestEnd(this,new Object()));
+ WorkContext ctx = new WorkContextImpl();
+ final ScopeContext<AtomicContext> requestScope = new RequestScopeContext(ctx);
+ requestScope.start();
+ ScopeContext<AtomicContext> statelessScope = new StatelessScopeContext(ctx);
+ statelessScope.start();
+
+ Map<String, AtomicContext> contexts = MockFactory.createWiredContexts("source", SourceImpl.class,
+ statelessScope, members, "target", Target.class, TargetImpl.class, requestScope);
+ requestScope.onEvent(new RequestStart(this));
+ AtomicContext<Source> sourceContext = (AtomicContext<Source>) contexts.get("source");
+ final AtomicContext<Target> targetContext = (AtomicContext<Target>) contexts.get("target");
+ final Source source = sourceContext.getService();
+ Target target = targetContext.getService();
+ assertNull(source.getTarget().getString());
+ assertNull(target.getString());
+ target.setString("foo");
+ assertTrue(Proxy.isProxyClass(source.getTarget().getClass()));
+ assertEquals("foo", source.getTarget().getString());
+
+ // spin off another request
+ Executor executor = Executors.newSingleThreadExecutor();
+ FutureTask<Void> future = new FutureTask<Void>(new Runnable() {
+ public void run() {
+ requestScope.onEvent(new RequestStart(this));
+ Target target2 = targetContext.getService();
+ assertFalse("foo".equals(target2.getString()));
+ assertFalse("foo".equals(source.getTarget().getString()));
+ source.getTarget().setString("bar");
+ assertEquals("bar", target2.getString());
+ assertEquals("bar", source.getTarget().getString());
+ requestScope.onEvent(new RequestEnd(this));
+ }
+ }, null);
+ executor.execute(future);
+ future.get();
+ requestScope.stop();
+ statelessScope.stop();
}
/**
* Tests a stateless-to-session scoped wire is setup properly by the runtime
*/
+ @SuppressWarnings("unchecked")
public void testStatelessToSession() throws Exception {
-// RuntimeContext runtime = MockFactory.createJavaRuntime();
-// Context ctx = runtime.getSystemContext().getContext("tuscany.system.child");
-// Assert.assertNotNull(ctx);
-// runtime.getRootContext().registerModelObject(MockFactory.createCompositeComponent("test"));
-// CompositeContext testCtx = (CompositeContext) runtime.getRootContext().getContext("test");
-// Assert.assertNotNull(testCtx);
-// testCtx.registerModelObject(MockFactory.createModule(Scope.INSTANCE,Scope.SESSION));
-// testCtx.publish(new ModuleStart(this));
-//
-// // first session
-// Object session = new Object();
-// Object id = new Object();
-// testCtx.publish(new RequestStart(this,id));
-// testCtx.publish(new HttpSessionBound(this,session));
-// GenericComponent source = (GenericComponent)testCtx.getContext("source").getInstance(null);
-// Assert.assertNotNull(source);
-// GenericComponent target = (GenericComponent)testCtx.getContext("target").getInstance(null);
-// Assert.assertNotNull(target);
-// source.getGenericComponent().setString("foo");
-// Assert.assertEquals("foo",target.getString());
-// testCtx.publish(new RequestEnd(this,new Object()));
-//
-// //second request for session
-// Object id2 = new Object();
-// testCtx.publish(new RequestStart(this,id2));
-// testCtx.publish(new HttpSessionBound(this,session));
-// GenericComponent targetR2 = (GenericComponent)testCtx.getContext("target").getInstance(null);
-// Assert.assertEquals("foo",targetR2.getString());
-// GenericComponent sourceR2 = (GenericComponent)testCtx.getContext("source").getInstance(null);
-// Assert.assertNotNull(sourceR2);
-// Assert.assertEquals("foo",sourceR2.getGenericComponent().getString());
-//
-// testCtx.publish(new RequestEnd(this,new Object()));
-//
-// //second session
-// Object session2 = new Object();
-// Object id3 = new Object();
-// testCtx.publish(new RequestStart(this,id3));
-// testCtx.publish(new HttpSessionBound(this,session2));
-// GenericComponent source2 = (GenericComponent)testCtx.getContext("source").getInstance(null);
-// Assert.assertNotNull(source2);
-// GenericComponent target2 = (GenericComponent)testCtx.getContext("target").getInstance(null);
-//
-// Assert.assertNotNull(target2);
-// Assert.assertEquals(null,target2.getString());
-// Assert.assertEquals(null,source2.getGenericComponent().getString());
-// source2.getGenericComponent().setString("baz");
-// Assert.assertEquals("baz",source2.getGenericComponent().getString());
-// Assert.assertEquals("baz",target2.getString());
-//
-// testCtx.publish(new RequestEnd(this,session2));
-// Object id4 = new Object();
-// testCtx.publish(new RequestStart(this,id4));
-// testCtx.publish(new HttpSessionBound(this,session));
-// testCtx.publish(new RequestEnd(this,session));
-//
+ WorkContext ctx = new WorkContextImpl();
+ ScopeContext<AtomicContext> statelessScope = new StatelessScopeContext(ctx);
+ statelessScope.start();
+ ScopeContext<AtomicContext> sessionScope = new HttpSessionScopeContext(ctx);
+ sessionScope.start();
+
+ Map<String, AtomicContext> contexts = MockFactory.createWiredContexts("source", SourceImpl.class,
+ statelessScope, members, "target", Target.class, TargetImpl.class, sessionScope);
+ Object session1 = new Object();
+ ctx.setIdentifier(HttpSessionScopeContext.HTTP_IDENTIFIER, session1);
+ sessionScope.onEvent(new HttpSessionStart(this, session1));
+ AtomicContext<Source> sourceContext = (AtomicContext<Source>) contexts.get("source");
+ AtomicContext<Target> targetContext = (AtomicContext<Target>) contexts.get("target");
+ Source source = sourceContext.getService();
+ Target target = targetContext.getService();
+ assertNull(source.getTarget().getString());
+ assertNull(target.getString());
+ target.setString("foo");
+ assertTrue(Proxy.isProxyClass(source.getTarget().getClass()));
+ assertEquals("foo", source.getTarget().getString());
+ ctx.clearIdentifier(HttpSessionScopeContext.HTTP_IDENTIFIER);
+ sessionScope.onEvent(new HttpSessionEnd(this, session1));
+
+ //second session
+ Object session2 = new Object();
+ ctx.setIdentifier(HttpSessionScopeContext.HTTP_IDENTIFIER, session2);
+ sessionScope.onEvent(new HttpSessionStart(this, session2));
+
+ Target target2 = targetContext.getService();
+ assertFalse("foo".equals(target2.getString()));
+
+ assertFalse("foo".equals(source.getTarget().getString()));
+ source.getTarget().setString("bar");
+ assertEquals("bar", target2.getString());
+ assertEquals("bar", source.getTarget().getString());
+ sessionScope.onEvent(new HttpSessionEnd(this, session2));
+
+ ctx.clearIdentifier(HttpSessionScopeContext.HTTP_IDENTIFIER);
+ sessionScope.stop();
+ statelessScope.stop();
}
/**
* Tests a stateless-to-module scoped wire is setup properly by the runtime
*/
+ @SuppressWarnings("unchecked")
public void testStatelessToModule() throws Exception {
-// RuntimeContext runtime = MockFactory.createJavaRuntime();
-// Context ctx = runtime.getSystemContext().getContext("tuscany.system.child");
-// Assert.assertNotNull(ctx);
-// runtime.getRootContext().registerModelObject(MockFactory.createCompositeComponent("test"));
-// CompositeContext testCtx = (CompositeContext) runtime.getRootContext().getContext("test");
-// Assert.assertNotNull(testCtx);
-// testCtx.registerModelObject(MockFactory.createModule(Scope.INSTANCE,Scope.MODULE));
-// testCtx.publish(new ModuleStart(this));
-//
-// Object id = new Object();
-// testCtx.publish(new RequestStart(this,id));
-// GenericComponent source = (GenericComponent)testCtx.getContext("source").getInstance(null);
-// Assert.assertNotNull(source);
-// GenericComponent target = (GenericComponent)testCtx.getContext("target").getInstance(null);
-// Assert.assertNotNull(target);
-// source.getGenericComponent().setString("foo");
-// Assert.assertEquals("foo",target.getString());
-// testCtx.publish(new RequestEnd(this,new Object()));
-//
-// //second session
-// Object id2 = new Object();
-// testCtx.publish(new RequestStart(this,id2));
-// GenericComponent source2 = (GenericComponent)testCtx.getContext("source").getInstance(null);
-// Assert.assertNotNull(source2);
-// GenericComponent target2 = (GenericComponent)testCtx.getContext("target").getInstance(null);
-//
-// Assert.assertNotNull(target2);
-// Assert.assertEquals("foo",target2.getString());
-// Assert.assertEquals("foo",source2.getGenericComponent().getString());
-// source2.getGenericComponent().setString("baz");
-// Assert.assertEquals("baz",source2.getGenericComponent().getString());
-// Assert.assertEquals("baz",target2.getString());
-//
-// testCtx.publish(new RequestEnd(this,new Object()));
-//
+ WorkContext ctx = new WorkContextImpl();
+ ScopeContext<AtomicContext> statelessScope = new StatelessScopeContext(ctx);
+ statelessScope.start();
+ ScopeContext<AtomicContext> moduleScope = new ModuleScopeContext(ctx);
+ moduleScope.start();
+
+ Map<String, AtomicContext> contexts = MockFactory.createWiredContexts("source", SourceImpl.class,
+ statelessScope, members, "target", Target.class, TargetImpl.class, moduleScope);
+ moduleScope.onEvent(new ModuleStart(this, null));
+ AtomicContext<Source> sourceContext = (AtomicContext<Source>) contexts.get("source");
+ AtomicContext<Target> targetContext = (AtomicContext<Target>) contexts.get("target");
+ Source source = sourceContext.getService();
+ Target target = targetContext.getService();
+ assertNull(source.getTarget().getString());
+ assertNull(target.getString());
+ target.setString("foo");
+ assertTrue(Proxy.isProxyClass(source.getTarget().getClass()));
+ assertEquals("foo", source.getTarget().getString());
+
+ //second session
+ Object session2 = new Object();
+ ctx.setIdentifier(HttpSessionScopeContext.HTTP_IDENTIFIER, session2);
+ moduleScope.onEvent(new HttpSessionStart(this, session2));
+
+ Target target2 = targetContext.getService();
+ assertTrue("foo".equals(target2.getString()));
+
+ assertTrue("foo".equals(source.getTarget().getString()));
+ source.getTarget().setString("bar");
+ assertEquals("bar", target2.getString());
+ assertEquals("bar", source.getTarget().getString());
+
+ moduleScope.onEvent(new ModuleStop(this, null));
+ moduleScope.stop();
+ statelessScope.stop();
}
protected void setUp() throws Exception {
Modified: incubator/tuscany/sandbox/jboynes/sca/containers/container.java/src/test/java/org/apache/tuscany/container/java/mock/MockFactory.java
URL: http://svn.apache.org/viewcvs/incubator/tuscany/sandbox/jboynes/sca/containers/container.java/src/test/java/org/apache/tuscany/container/java/mock/MockFactory.java?rev=406188&r1=406187&r2=406188&view=diff
==============================================================================
--- incubator/tuscany/sandbox/jboynes/sca/containers/container.java/src/test/java/org/apache/tuscany/container/java/mock/MockFactory.java (original)
+++ incubator/tuscany/sandbox/jboynes/sca/containers/container.java/src/test/java/org/apache/tuscany/container/java/mock/MockFactory.java Sat May 13 19:02:59 2006
@@ -92,8 +92,8 @@
Map<String, Member> members, String targetName,
Class<?> targetService, Class<?> targetClass,
ScopeContext<AtomicContext> targetScope) throws Exception {
- return createWiredContexts(sourceName,sourceClass,sourceReferenceClass,sourceScope,null,null,null,members,targetName,targetService,
- targetClass,targetScope,null,null,null);
+ return createWiredContexts(sourceName, sourceClass, sourceReferenceClass, sourceScope, null, null, null, members, targetName, targetService,
+ targetClass, targetScope, null, null, null);
}
public static Map<String, AtomicContext> createWiredContexts(String sourceName, Class<?> sourceClass, Class<?> sourceReferenceClass,
@@ -109,12 +109,12 @@
MessageHandler targetResponseHeadHandler) throws Exception {
JavaAtomicContext targetContext = createJavaAtomicContext(targetName, targetClass);
TargetWire targetWire = createTargetWire(targetService.getName().substring(
- targetService.getName().lastIndexOf('.') + 1), targetService,targetHeadInterceptor,targetRequestHeadHandler,targetResponseHeadHandler);
+ targetService.getName().lastIndexOf('.') + 1), targetService, targetHeadInterceptor, targetRequestHeadHandler, targetResponseHeadHandler);
targetContext.addTargetWire(targetWire);
JavaAtomicContext sourceContext = createJavaAtomicContext(sourceName, sourceClass, false, null, null, null, members);
- SourceWire sourceWire = createSourceWire(targetName, sourceReferenceClass,sourceHeadInterceptor,
- sourceHeadRequestHandler,sourceHeadResponseHandler);
+ SourceWire sourceWire = createSourceWire(targetName, sourceReferenceClass, sourceHeadInterceptor,
+ sourceHeadRequestHandler, sourceHeadResponseHandler);
sourceContext.addSourceWire(sourceWire);
targetScope.register(targetContext);
sourceContext.setScopeContext(sourceScope);
@@ -148,11 +148,11 @@
Map<String, Member> members, ScopeContext<AtomicContext> targetScope) throws Exception {
JavaAtomicContext targetContext = createJavaAtomicContext(targetName, targetClass);
TargetWire targetWire = createTargetWire(targetService.getName().substring(
- targetService.getName().lastIndexOf('.') + 1), targetService,null,null,null);
+ targetService.getName().lastIndexOf('.') + 1), targetService, null, null, null);
targetContext.addTargetWire(targetWire);
JavaAtomicContext sourceContext = createJavaAtomicContext(sourceName, sourceClass, false, null, null, null, members);
- SourceWire sourceWire = createSourceWire(targetName, sourceReferenceClass,null,null,null);
+ SourceWire sourceWire = createSourceWire(targetName, sourceReferenceClass, null, null, null);
List<SourceWire> factories = new ArrayList<SourceWire>();
factories.add(sourceWire);
sourceContext.addSourceWires(sourceReferenceClass, factories);
@@ -174,7 +174,7 @@
TargetWire<T> wire = new JDKTargetWire<T>();
wire.setBusinessInterface(interfaze);
wire.setServiceName(serviceName);
- wire.setInvocationChains(createTargetInvocationChains(interfaze,headInterceptor,headRequestHandler,headResponseHandler));
+ wire.setInvocationChains(createTargetInvocationChains(interfaze, headInterceptor, headRequestHandler, headResponseHandler));
wire.initialize();
return wire;
}
@@ -186,7 +186,7 @@
SourceWire<T> wire = new JDKSourceWire<T>();
wire.setReferenceName(refName);
- wire.setInvocationChains(createSourceInvocationChains(interfaze,headInterceptor,headRequestHandler,headResponseHandler));
+ wire.setInvocationChains(createSourceInvocationChains(interfaze, headInterceptor, headRequestHandler, headResponseHandler));
wire.setBusinessInterface(interfaze);
wire.initialize();
return wire;
@@ -277,33 +277,32 @@
public static Map<Method, TargetInvocationChain> createTargetInvocationChains(Class<?> interfaze,
Interceptor headInterceptor, MessageHandler headRequestHandler,
MessageHandler headResponseHandler) {
- Map<Method, TargetInvocationChain> invocations = new MethodHashMap<TargetInvocationChain>();
- Method[] methods = interfaze.getMethods();
- for (Method method : methods) {
- TargetInvocationChain chain = new TargetInvocationChainImpl(method);
- if (headInterceptor != null) {
- chain.addInterceptor(headInterceptor);
- }
- if (headRequestHandler != null) {
- chain.addRequestHandler(headRequestHandler);
- }
- if (headResponseHandler != null) {
- chain.addRequestHandler(headResponseHandler);
- }
- // add tail interceptor
- chain.addInterceptor(new InvokerInterceptor());
- invocations.put(method, chain);
+ Map<Method, TargetInvocationChain> invocations = new MethodHashMap<TargetInvocationChain>();
+ Method[] methods = interfaze.getMethods();
+ for (Method method : methods) {
+ TargetInvocationChain chain = new TargetInvocationChainImpl(method);
+ if (headInterceptor != null) {
+ chain.addInterceptor(headInterceptor);
+ }
+ if (headRequestHandler != null) {
+ chain.addRequestHandler(headRequestHandler);
}
- return invocations;
+ if (headResponseHandler != null) {
+ chain.addRequestHandler(headResponseHandler);
+ }
+ // add tail interceptor
+ chain.addInterceptor(new InvokerInterceptor());
+ invocations.put(method, chain);
}
+ return invocations;
+ }
- 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 <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);
+ }
- }
+}