diff options
Diffstat (limited to 'sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope')
29 files changed, 3166 insertions, 0 deletions
diff --git a/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/AbstractScopeContainerTestCase.java b/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/AbstractScopeContainerTestCase.java new file mode 100644 index 0000000000..f8f8dd5f60 --- /dev/null +++ b/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/AbstractScopeContainerTestCase.java @@ -0,0 +1,126 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.core.component.scope; + +import org.apache.tuscany.spi.component.AtomicComponent; +import org.apache.tuscany.spi.component.WorkContext; +import org.apache.tuscany.spi.event.Event; +import org.apache.tuscany.spi.event.EventFilter; +import org.apache.tuscany.spi.event.RuntimeEventListener; +import org.apache.tuscany.spi.event.TrueFilter; +import org.apache.tuscany.spi.model.Scope; + +import junit.framework.TestCase; +import org.easymock.EasyMock; + +/** + * @version $Rev$ $Date$ + */ +public class AbstractScopeContainerTestCase extends TestCase { + + public void testFireListener() { + TestContainer container = new TestContainer("foo"); + RuntimeEventListener listener = EasyMock.createMock(RuntimeEventListener.class); + Event event = new TestEvent(); + listener.onEvent(EasyMock.eq(event)); + EasyMock.replay(listener); + container.addListener(listener); + container.publish(event); + EasyMock.verify(listener); + } + + public void testRemoveListener() { + TestContainer container = new TestContainer("foo"); + RuntimeEventListener listener = EasyMock.createMock(RuntimeEventListener.class); + EasyMock.replay(listener); + Event event = new TestEvent(); + container.addListener(listener); + container.removeListener(listener); + container.publish(event); + EasyMock.verify(listener); + } + + public void testFalseFilterListener() { + TestContainer container = new TestContainer("foo"); + RuntimeEventListener listener = EasyMock.createMock(RuntimeEventListener.class); + Event event = new TestEvent(); + EasyMock.replay(listener); + container.addListener(new FalseFilter(), listener); + container.publish(event); + EasyMock.verify(listener); + } + + public void testTrueFilterListener() { + TestContainer container = new TestContainer("foo"); + RuntimeEventListener listener = EasyMock.createMock(RuntimeEventListener.class); + Event event = new TestEvent(); + listener.onEvent(EasyMock.eq(event)); + EasyMock.replay(listener); + container.addListener(new TrueFilter(), listener); + container.publish(event); + EasyMock.verify(listener); + } + + public void testToString() { + TestContainer container = new TestContainer("foo"); + assertNotNull(container.toString()); + } + + private class TestContainer extends AbstractScopeContainer { + + public TestContainer(String name) { + super(null, null); + } + + protected InstanceWrapper getInstanceWrapper(AtomicComponent component, boolean create) { + return null; + } + + public Scope getScope() { + return null; + } + + public void register(AtomicComponent component) { + + } + + public void onEvent(Event event) { + + } + + public WorkContext getWorkContext() { + return super.getWorkContext(); + } + } + + private class TestEvent implements Event { + public Object getSource() { + return null; + } + } + + private class FalseFilter implements EventFilter { + + public boolean match(Event event) { + return false; + } + } + + +} diff --git a/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/BasicCompositeScopeTestCase.java b/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/BasicCompositeScopeTestCase.java new file mode 100644 index 0000000000..cd68b46541 --- /dev/null +++ b/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/BasicCompositeScopeTestCase.java @@ -0,0 +1,127 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.core.component.scope; + +import org.apache.tuscany.spi.component.AtomicComponent; +import org.apache.tuscany.spi.component.ScopeContainer; +import org.apache.tuscany.spi.component.TargetNotFoundException; + +import junit.framework.TestCase; +import org.apache.tuscany.core.component.event.CompositeStop; +import org.apache.tuscany.core.implementation.PojoConfiguration; +import org.apache.tuscany.core.implementation.system.component.SystemAtomicComponentImpl; +import org.apache.tuscany.core.injection.EventInvoker; +import org.apache.tuscany.core.injection.MethodEventInvoker; +import org.apache.tuscany.core.injection.PojoObjectFactory; +import org.apache.tuscany.core.mock.component.CompositeScopeInitDestroyComponent; + +/** + * @version $$Rev$$ $$Date$$ + */ +public class BasicCompositeScopeTestCase extends TestCase { + + private EventInvoker<Object> initInvoker; + private EventInvoker<Object> destroyInvoker; + private PojoObjectFactory<?> factory; + + public void testLifecycleManagement() throws Exception { + CompositeScopeContainer scopeContext = new CompositeScopeContainer(null); + scopeContext.start(); + AtomicComponent component = createComponent(scopeContext); + // start the request + CompositeScopeInitDestroyComponent o1 = + (CompositeScopeInitDestroyComponent) scopeContext.getInstance(component); + assertTrue(o1.isInitialized()); + assertFalse(o1.isDestroyed()); + CompositeScopeInitDestroyComponent o2 = + (CompositeScopeInitDestroyComponent) scopeContext.getInstance(component); + assertEquals(o1, o2); + scopeContext.onEvent(new CompositeStop(this, null)); + assertTrue(o1.isDestroyed()); + scopeContext.stop(); + } + + + public void testGetAssociatedInstance() throws Exception { + CompositeScopeContainer scopeContext = new CompositeScopeContainer(null); + scopeContext.start(); + AtomicComponent component = createComponent(scopeContext); + // start the request + scopeContext.getInstance(component); + scopeContext.getAssociatedInstance(component); + } + + public void testGetAssociatedInstanceNonExistent() throws Exception { + CompositeScopeContainer scopeContext = new CompositeScopeContainer(null); + scopeContext.start(); + AtomicComponent component = createComponent(scopeContext); + // start the request + try { + scopeContext.getAssociatedInstance(component); + fail(); + } catch (TargetNotFoundException e) { + // expected + } + } + + public void testCompositeIsolation() throws Exception { + CompositeScopeContainer scopeContext = new CompositeScopeContainer(null); + scopeContext.start(); + + AtomicComponent component = createComponent(scopeContext); + + CompositeScopeInitDestroyComponent o1 = + (CompositeScopeInitDestroyComponent) scopeContext.getInstance(component); + assertTrue(o1.isInitialized()); + assertFalse(o1.isDestroyed()); + + CompositeScopeInitDestroyComponent o2 = + (CompositeScopeInitDestroyComponent) scopeContext.getInstance(component); + assertSame(o1, o2); + scopeContext.onEvent(new CompositeStop(this, null)); + assertTrue(o1.isDestroyed()); + scopeContext.stop(); + } + + protected void setUp() throws Exception { + super.setUp(); + factory = new PojoObjectFactory<CompositeScopeInitDestroyComponent>( + CompositeScopeInitDestroyComponent.class.getConstructor((Class[]) null)); + initInvoker = new MethodEventInvoker<Object>(CompositeScopeInitDestroyComponent.class.getMethod( + "init", (Class[]) null)); + destroyInvoker = new MethodEventInvoker<Object>(CompositeScopeInitDestroyComponent.class.getMethod( + "destroy", (Class[]) null)); + } + + protected void tearDown() throws Exception { + super.tearDown(); + } + + private AtomicComponent createComponent(ScopeContainer scopeContainer) { + PojoConfiguration configuration = new PojoConfiguration(); + configuration.setInstanceFactory(factory); + configuration.setInitInvoker(initInvoker); + configuration.setDestroyInvoker(destroyInvoker); + configuration.setName("foo"); + SystemAtomicComponentImpl component = new SystemAtomicComponentImpl(configuration); + component.setScopeContainer(scopeContainer); + component.start(); + return component; + } +} diff --git a/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/BasicConversationalScopeTestCase.java b/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/BasicConversationalScopeTestCase.java new file mode 100644 index 0000000000..c97f3cb96f --- /dev/null +++ b/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/BasicConversationalScopeTestCase.java @@ -0,0 +1,129 @@ +/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.tuscany.core.component.scope;
+
+import org.apache.tuscany.spi.component.AtomicComponent;
+import org.apache.tuscany.spi.component.ScopeContainer;
+import org.apache.tuscany.spi.component.WorkContext;
+import org.apache.tuscany.spi.model.Scope;
+import org.apache.tuscany.spi.services.store.StoreMonitor;
+
+import junit.framework.TestCase;
+import org.apache.tuscany.core.component.WorkContextImpl;
+import org.apache.tuscany.core.component.event.ConversationEnd;
+import org.apache.tuscany.core.implementation.PojoConfiguration;
+import org.apache.tuscany.core.implementation.system.component.SystemAtomicComponentImpl;
+import org.apache.tuscany.core.injection.EventInvoker;
+import org.apache.tuscany.core.injection.MethodEventInvoker;
+import org.apache.tuscany.core.injection.PojoObjectFactory;
+import org.apache.tuscany.core.mock.component.ConversationalScopeInitDestroyComponent;
+import org.apache.tuscany.core.services.store.memory.MemoryStore;
+import org.easymock.EasyMock;
+
+/**
+ * @version $$Rev: 471111 $$ $$Date: 2006-11-03 23:06:48 -0500 (Fri, 03 Nov 2006) $$
+ */
+public class BasicConversationalScopeTestCase extends TestCase {
+
+ private EventInvoker<Object> initInvoker;
+ private EventInvoker<Object> destroyInvoker;
+ private PojoObjectFactory<?> factory;
+
+ public void testLifecycleManagement() throws Exception {
+ StoreMonitor monitor = EasyMock.createMock(StoreMonitor.class);
+ monitor.start(EasyMock.isA(String.class));
+ monitor.stop(EasyMock.isA(String.class));
+ MemoryStore store = new MemoryStore(monitor);
+ WorkContext workContext = new WorkContextImpl();
+ ConversationalScopeContainer scopeContext = new ConversationalScopeContainer(store, workContext, null);
+ scopeContext.start();
+ AtomicComponent atomicContext = createContext(scopeContext);
+ // start the request
+ String conversation = "conv";
+ workContext.setIdentifier(Scope.CONVERSATION, conversation);
+ ConversationalScopeInitDestroyComponent o1 =
+ (ConversationalScopeInitDestroyComponent) scopeContext.getInstance(atomicContext);
+ //assertTrue(o1.isInitialized());
+ assertFalse(o1.isDestroyed());
+ ConversationalScopeInitDestroyComponent o2 =
+ (ConversationalScopeInitDestroyComponent) scopeContext.getInstance(atomicContext);
+ assertSame(o1, o2);
+ scopeContext.onEvent(new ConversationEnd(this, conversation));
+ //assertTrue(o1.isDestroyed());
+ scopeContext.stop();
+ }
+
+ public void testCompositeIsolation() throws Exception {
+ StoreMonitor monitor = EasyMock.createMock(StoreMonitor.class);
+ monitor.start(EasyMock.isA(String.class));
+ monitor.stop(EasyMock.isA(String.class));
+ MemoryStore store = new MemoryStore(monitor);
+ WorkContext workContext = new WorkContextImpl();
+ ConversationalScopeContainer scopeContext = new ConversationalScopeContainer(store, workContext, null);
+ scopeContext.start();
+
+ AtomicComponent atomicContext = createContext(scopeContext);
+
+ String conversation1 = "conv";
+ workContext.setIdentifier(Scope.CONVERSATION, conversation1);
+ ConversationalScopeInitDestroyComponent o1 =
+ (ConversationalScopeInitDestroyComponent) scopeContext.getInstance(atomicContext);
+ //assertTrue(o1.isInitialized());
+ assertFalse(o1.isDestroyed());
+
+ String conversation2 = "conv2";
+ workContext.setIdentifier(Scope.CONVERSATION, conversation2);
+ ConversationalScopeInitDestroyComponent o2 =
+ (ConversationalScopeInitDestroyComponent) scopeContext.getInstance(atomicContext);
+ assertNotSame(o1, o2);
+
+ scopeContext.onEvent(new ConversationEnd(this, conversation1));
+ //assertTrue(o1.isDestroyed());
+ assertFalse(o2.isDestroyed());
+ scopeContext.onEvent(new ConversationEnd(this, conversation2));
+ //assertTrue(o2.isDestroyed());
+ scopeContext.stop();
+ }
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ factory = new PojoObjectFactory<ConversationalScopeInitDestroyComponent>(
+ ConversationalScopeInitDestroyComponent.class.getConstructor((Class[]) null));
+ initInvoker = new MethodEventInvoker<Object>(
+ ConversationalScopeInitDestroyComponent.class.getMethod("init", (Class[]) null));
+ destroyInvoker = new MethodEventInvoker<Object>(
+ ConversationalScopeInitDestroyComponent.class.getMethod("destroy", (Class[]) null));
+ }
+
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ }
+
+ private AtomicComponent createContext(ScopeContainer scopeContainer) {
+ PojoConfiguration configuration = new PojoConfiguration();
+ configuration.setInstanceFactory(factory);
+ configuration.setInitInvoker(initInvoker);
+ configuration.setDestroyInvoker(destroyInvoker);
+ configuration.setName("foo");
+ SystemAtomicComponentImpl component = new SystemAtomicComponentImpl(configuration);
+ component.setScopeContainer(scopeContainer);
+ component.start();
+ return component;
+ }
+}
diff --git a/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/BasicHttpSessionScopeTestCase.java b/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/BasicHttpSessionScopeTestCase.java new file mode 100644 index 0000000000..44e9bbc1d6 --- /dev/null +++ b/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/BasicHttpSessionScopeTestCase.java @@ -0,0 +1,145 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.core.component.scope; + +import org.apache.tuscany.spi.component.AtomicComponent; +import org.apache.tuscany.spi.component.ScopeContainer; +import org.apache.tuscany.spi.component.TargetNotFoundException; +import org.apache.tuscany.spi.component.WorkContext; +import org.apache.tuscany.spi.model.Scope; + +import junit.framework.TestCase; +import org.apache.tuscany.core.component.WorkContextImpl; +import org.apache.tuscany.core.component.event.HttpSessionEnd; +import org.apache.tuscany.core.implementation.PojoConfiguration; +import org.apache.tuscany.core.implementation.system.component.SystemAtomicComponentImpl; +import org.apache.tuscany.core.injection.EventInvoker; +import org.apache.tuscany.core.injection.MethodEventInvoker; +import org.apache.tuscany.core.injection.PojoObjectFactory; +import org.apache.tuscany.core.mock.component.SessionScopeInitDestroyComponent; + +/** + * @version $$Rev$$ $$Date$$ + */ +public class BasicHttpSessionScopeTestCase extends TestCase { + private EventInvoker<Object> initInvoker; + private EventInvoker<Object> destroyInvoker; + private PojoObjectFactory<?> factory; + + public void testLifecycleManagement() throws Exception { + WorkContext workContext = new WorkContextImpl(); + HttpSessionScopeContainer scopeContext = new HttpSessionScopeContainer(workContext, null); + scopeContext.start(); + AtomicComponent component = createComponent(scopeContext); + // start the request + Object session = new Object(); + workContext.setIdentifier(Scope.SESSION, session); + SessionScopeInitDestroyComponent o1 = + (SessionScopeInitDestroyComponent) scopeContext.getInstance(component); + assertTrue(o1.isInitialized()); + assertFalse(o1.isDestroyed()); + SessionScopeInitDestroyComponent o2 = + (SessionScopeInitDestroyComponent) scopeContext.getInstance(component); + assertSame(o1, o2); + scopeContext.onEvent(new HttpSessionEnd(this, session)); + assertTrue(o1.isDestroyed()); + scopeContext.stop(); + } + + public void testGetAssociatedInstance() throws Exception { + WorkContext workContext = new WorkContextImpl(); + HttpSessionScopeContainer scopeContext = new HttpSessionScopeContainer(workContext, null); + scopeContext.start(); + AtomicComponent component = createComponent(scopeContext); + // start the request + Object session = new Object(); + workContext.setIdentifier(Scope.SESSION, session); + scopeContext.getInstance(component); + scopeContext.getAssociatedInstance(component); + } + + public void testGetAssociatedInstanceNonExistent() throws Exception { + WorkContext workContext = new WorkContextImpl(); + HttpSessionScopeContainer scopeContext = new HttpSessionScopeContainer(workContext, null); + scopeContext.start(); + AtomicComponent component = createComponent(scopeContext); + // start the request + Object session = new Object(); + workContext.setIdentifier(Scope.SESSION, session); + try { + scopeContext.getAssociatedInstance(component); + fail(); + } catch (TargetNotFoundException e) { + // expected + } + } + + public void testSessionIsolation() throws Exception { + WorkContext workContext = new WorkContextImpl(); + HttpSessionScopeContainer scopeContext = new HttpSessionScopeContainer(workContext, null); + scopeContext.start(); + + AtomicComponent component = createComponent(scopeContext); + + Object session1 = new Object(); + workContext.setIdentifier(Scope.SESSION, session1); + SessionScopeInitDestroyComponent o1 = + (SessionScopeInitDestroyComponent) scopeContext.getInstance(component); + assertTrue(o1.isInitialized()); + + Object session2 = new Object(); + workContext.setIdentifier(Scope.SESSION, session2); + SessionScopeInitDestroyComponent o2 = + (SessionScopeInitDestroyComponent) scopeContext.getInstance(component); + assertNotSame(o1, o2); + + scopeContext.onEvent(new HttpSessionEnd(this, session1)); + assertTrue(o1.isDestroyed()); + assertFalse(o2.isDestroyed()); + scopeContext.onEvent(new HttpSessionEnd(this, session2)); + assertTrue(o2.isDestroyed()); + scopeContext.stop(); + } + + protected void setUp() throws Exception { + super.setUp(); + factory = new PojoObjectFactory<SessionScopeInitDestroyComponent>( + SessionScopeInitDestroyComponent.class.getConstructor((Class[]) null)); + initInvoker = new MethodEventInvoker<Object>( + SessionScopeInitDestroyComponent.class.getMethod("init", (Class[]) null)); + destroyInvoker = new MethodEventInvoker<Object>( + SessionScopeInitDestroyComponent.class.getMethod("destroy", (Class[]) null)); + } + + protected void tearDown() throws Exception { + super.tearDown(); + } + + private AtomicComponent createComponent(ScopeContainer scopeContainer) { + PojoConfiguration configuration = new PojoConfiguration(); + configuration.setInstanceFactory(factory); + configuration.setInitInvoker(initInvoker); + configuration.setDestroyInvoker(destroyInvoker); + configuration.setName("foo"); + SystemAtomicComponentImpl component = new SystemAtomicComponentImpl(configuration); + component.setScopeContainer(scopeContainer); + component.start(); + return component; + } +} diff --git a/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/BasicRequestScopeTestCase.java b/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/BasicRequestScopeTestCase.java new file mode 100644 index 0000000000..435e7d29b7 --- /dev/null +++ b/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/BasicRequestScopeTestCase.java @@ -0,0 +1,128 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.core.component.scope; + +import org.apache.tuscany.spi.component.AtomicComponent; +import org.apache.tuscany.spi.component.ScopeContainer; +import org.apache.tuscany.spi.component.TargetNotFoundException; + +import junit.framework.TestCase; +import org.apache.tuscany.core.component.event.RequestEnd; +import org.apache.tuscany.core.implementation.PojoConfiguration; +import org.apache.tuscany.core.implementation.system.component.SystemAtomicComponentImpl; +import org.apache.tuscany.core.injection.EventInvoker; +import org.apache.tuscany.core.injection.MethodEventInvoker; +import org.apache.tuscany.core.injection.PojoObjectFactory; +import org.apache.tuscany.core.mock.component.RequestScopeInitDestroyComponent; + +/** + * @version $$Rev$$ $$Date$$ + */ +public class BasicRequestScopeTestCase extends TestCase { + private EventInvoker<Object> initInvoker; + private EventInvoker<Object> destroyInvoker; + private PojoObjectFactory<?> factory; + + public void testLifecycleManagement() throws Exception { + RequestScopeContainer scopeContainer = new RequestScopeContainer(null, null); + scopeContainer.start(); + AtomicComponent component = createComponent(scopeContainer); + // start the request + RequestScopeInitDestroyComponent o1 = + (RequestScopeInitDestroyComponent) scopeContainer.getInstance(component); + assertTrue(o1.isInitialized()); + assertFalse(o1.isDestroyed()); + RequestScopeInitDestroyComponent o2 = + (RequestScopeInitDestroyComponent) scopeContainer.getInstance(component); + assertSame(o1, o2); + scopeContainer.onEvent(new RequestEnd(this)); + assertTrue(o1.isDestroyed()); + scopeContainer.stop(); + } + + public void testGetAssociatedInstance() throws Exception { + RequestScopeContainer scopeContainer = new RequestScopeContainer(null, null); + scopeContainer.start(); + AtomicComponent component = createComponent(scopeContainer); + // start the request + scopeContainer.getInstance(component); + scopeContainer.getAssociatedInstance(component); + scopeContainer.stop(); + } + + public void testGetAssociatedInstanceNonExistent() throws Exception { + RequestScopeContainer scopeContainer = new RequestScopeContainer(null, null); + scopeContainer.start(); + AtomicComponent component = createComponent(scopeContainer); + // start the request + try { + scopeContainer.getAssociatedInstance(component); + fail(); + } catch (TargetNotFoundException e) { + // expected + } + scopeContainer.stop(); + } + + public void testRequestIsolation() throws Exception { + RequestScopeContainer scopeContainer = new RequestScopeContainer(null, null); + scopeContainer.start(); + + AtomicComponent component = createComponent(scopeContainer); + + RequestScopeInitDestroyComponent o1 = + (RequestScopeInitDestroyComponent) scopeContainer.getInstance(component); + assertTrue(o1.isInitialized()); + scopeContainer.onEvent(new RequestEnd(this)); + assertTrue(o1.isDestroyed()); + + RequestScopeInitDestroyComponent o2 = + (RequestScopeInitDestroyComponent) scopeContainer.getInstance(component); + assertNotSame(o1, o2); + scopeContainer.onEvent(new RequestEnd(this)); + assertTrue(o2.isDestroyed()); + scopeContainer.stop(); + } + + protected void setUp() throws Exception { + super.setUp(); + factory = new PojoObjectFactory<RequestScopeInitDestroyComponent>( + RequestScopeInitDestroyComponent.class.getConstructor((Class[]) null)); + initInvoker = new MethodEventInvoker<Object>( + RequestScopeInitDestroyComponent.class.getMethod("init", (Class[]) null)); + destroyInvoker = new MethodEventInvoker<Object>( + RequestScopeInitDestroyComponent.class.getMethod("destroy", (Class[]) null)); + } + + protected void tearDown() throws Exception { + super.tearDown(); + } + + private AtomicComponent createComponent(ScopeContainer scopeContainer) { + PojoConfiguration configuration = new PojoConfiguration(); + configuration.setInstanceFactory(factory); + configuration.setInitInvoker(initInvoker); + configuration.setDestroyInvoker(destroyInvoker); + configuration.setName("foo"); + SystemAtomicComponentImpl component = new SystemAtomicComponentImpl(configuration); + component.setScopeContainer(scopeContainer); + component.start(); + return component; + } +} diff --git a/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/BasicStatelessScopeTestCase.java b/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/BasicStatelessScopeTestCase.java new file mode 100644 index 0000000000..9ca9230e5b --- /dev/null +++ b/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/BasicStatelessScopeTestCase.java @@ -0,0 +1,120 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.core.component.scope; + +import org.apache.tuscany.spi.component.AtomicComponent; +import org.apache.tuscany.spi.component.ScopeContainer; +import org.apache.tuscany.spi.component.TargetNotFoundException; +import org.apache.tuscany.spi.component.WorkContext; + +import junit.framework.Assert; +import junit.framework.TestCase; +import org.apache.tuscany.core.component.WorkContextImpl; +import org.apache.tuscany.core.implementation.PojoConfiguration; +import org.apache.tuscany.core.implementation.system.component.SystemAtomicComponentImpl; +import org.apache.tuscany.core.injection.PojoObjectFactory; +import org.apache.tuscany.core.mock.component.StatelessComponent; +import org.apache.tuscany.core.mock.component.StatelessComponentImpl; + +/** + * Unit tests for the composite scope container + * + * @version $Rev$ $Date$ + */ +public class BasicStatelessScopeTestCase extends TestCase { + private PojoObjectFactory<StatelessComponentImpl> factory; + + /** + * Verfies instance identity is properly maintained + */ + public void testInstanceManagement() throws Exception { + WorkContext ctx = new WorkContextImpl(); + StatelessScopeContainer scope = new StatelessScopeContainer(ctx, null); + scope.start(); + AtomicComponent component1 = createComponent(scope); + scope.register(component1); + AtomicComponent component2 = createComponent(scope); + scope.register(component2); + StatelessComponentImpl comp1 = (StatelessComponentImpl) scope.getInstance(component1); + Assert.assertNotNull(comp1); + StatelessComponentImpl comp2 = (StatelessComponentImpl) scope.getInstance(component2); + Assert.assertNotNull(comp2); + Assert.assertNotSame(comp1, comp2); + scope.stop(); + } + + public void testGetAssociatedInstance() throws Exception { + WorkContext ctx = new WorkContextImpl(); + StatelessScopeContainer scope = new StatelessScopeContainer(ctx, null); + scope.start(); + AtomicComponent component1 = createComponent(scope); + scope.register(component1); + try { + // always throws an exception, which is the semantic for stateless implementations + scope.getAssociatedInstance(component1); + fail(); + } catch (TargetNotFoundException e) { + // expected + } + scope.stop(); + } + + public void testRegisterContextAfterRequest() throws Exception { + WorkContext ctx = new WorkContextImpl(); + StatelessScopeContainer scope = new StatelessScopeContainer(ctx, null); + + scope.start(); + AtomicComponent component1 = createComponent(scope); + scope.register(component1); + StatelessComponent comp1 = (StatelessComponentImpl) scope.getInstance(component1); + Assert.assertNotNull(comp1); + AtomicComponent component2 = createComponent(scope); + scope.register(component2); + StatelessComponentImpl comp2 = (StatelessComponentImpl) scope.getInstance(component2); + Assert.assertNotNull(comp2); + scope.stop(); + } + + + /** + * Tests setting no components in the scope + */ + public void testSetNullComponents() throws Exception { + WorkContext ctx = new WorkContextImpl(); + StatelessScopeContainer scope = new StatelessScopeContainer(ctx, null); + scope.start(); + scope.stop(); + } + + protected void setUp() throws Exception { + super.setUp(); + factory = + new PojoObjectFactory<StatelessComponentImpl>(StatelessComponentImpl.class.getConstructor((Class[]) null)); + + } + + private AtomicComponent createComponent(ScopeContainer scopeContainer) { + PojoConfiguration configuration = new PojoConfiguration(); + configuration.setInstanceFactory(factory); + configuration.setName("foo"); + AtomicComponent component = new SystemAtomicComponentImpl(configuration); + component.setScopeContainer(scopeContainer); + return component; + } +} diff --git a/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/CompositeScopeInitDestroyErrorTestCase.java b/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/CompositeScopeInitDestroyErrorTestCase.java new file mode 100644 index 0000000000..8ac4ce46a4 --- /dev/null +++ b/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/CompositeScopeInitDestroyErrorTestCase.java @@ -0,0 +1,78 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.core.component.scope; + +import org.apache.tuscany.spi.ObjectCreationException; +import org.apache.tuscany.spi.component.AtomicComponent; +import org.apache.tuscany.spi.component.ScopeContainerMonitor; +import org.apache.tuscany.spi.component.TargetDestructionException; + +import junit.framework.TestCase; +import org.apache.tuscany.core.component.event.CompositeStart; +import org.apache.tuscany.core.component.event.CompositeStop; +import org.easymock.EasyMock; + +/** + * @version $Rev$ $Date$ + */ +public class CompositeScopeInitDestroyErrorTestCase extends TestCase { + + public void testInitializeErrorMonitor() throws Exception { + ScopeContainerMonitor monitor; + monitor = EasyMock.createMock(ScopeContainerMonitor.class); + monitor.eagerInitializationError(EasyMock.isA(ObjectCreationException.class)); + EasyMock.replay(monitor); + CompositeScopeContainer scope = new CompositeScopeContainer(monitor); + scope.start(); + AtomicComponent component = EasyMock.createMock(AtomicComponent.class); + EasyMock.expect(component.getName()).andReturn("foo").atLeastOnce(); + EasyMock.expect(component.createInstance()).andThrow(new ObjectCreationException("")); + EasyMock.expect(component.getInitLevel()).andReturn(1); + EasyMock.replay(component); + scope.register(component); + scope.onEvent(new CompositeStart(this, null)); + EasyMock.verify(monitor); + } + + public void testDestroyErrorMonitor() throws Exception { + ScopeContainerMonitor monitor; + monitor = EasyMock.createMock(ScopeContainerMonitor.class); + monitor.destructionError(EasyMock.isA(TargetDestructionException.class)); + EasyMock.replay(monitor); + CompositeScopeContainer scope = new CompositeScopeContainer(monitor); + scope.start(); + AtomicComponent component = EasyMock.createMock(AtomicComponent.class); + EasyMock.expect(component.getName()).andReturn("foo").atLeastOnce(); + EasyMock.expect(component.createInstance()).andReturn(new Object()); + EasyMock.expect(component.getInitLevel()).andReturn(1); + component.init(EasyMock.isA(Object.class)); + component.destroy(EasyMock.isA(Object.class)); + EasyMock.expectLastCall().andThrow(new TargetDestructionException("", "")); + EasyMock.replay(component); + scope.register(component); + scope.onEvent(new CompositeStart(this, null)); + scope.onEvent(new CompositeStop(this, null)); + EasyMock.verify(monitor); + } + + + protected void setUp() throws Exception { + super.setUp(); + } +} diff --git a/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/CompositeScopeInstanceLifecycleTestCase.java b/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/CompositeScopeInstanceLifecycleTestCase.java new file mode 100644 index 0000000000..0d70bcf252 --- /dev/null +++ b/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/CompositeScopeInstanceLifecycleTestCase.java @@ -0,0 +1,186 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.core.component.scope; + + +import org.apache.tuscany.spi.component.AtomicComponent; +import org.apache.tuscany.spi.component.TargetException; + +import junit.framework.TestCase; +import org.apache.tuscany.core.component.event.CompositeStart; +import org.apache.tuscany.core.component.event.CompositeStop; +import org.apache.tuscany.core.mock.component.OrderedInitPojo; +import org.apache.tuscany.core.mock.component.OrderedInitPojoImpl; +import org.easymock.EasyMock; +import org.easymock.IAnswer; + +/** + * Lifecycle unit tests for the composite scope container + * + * @version $Rev$ $Date$ + */ +public class CompositeScopeInstanceLifecycleTestCase extends TestCase { + + /** + * Verify init and stop by scope container on an atomic component + * + * @throws Exception + */ + public void testInitDestroy() throws Exception { + CompositeScopeContainer scope = new CompositeScopeContainer(null); + scope.start(); + Foo comp = new Foo(); + AtomicComponent component = EasyMock.createMock(AtomicComponent.class); + EasyMock.expect(component.createInstance()).andReturn(comp); + EasyMock.expect(component.getInitLevel()).andReturn(1).atLeastOnce(); + component.init(EasyMock.eq(comp)); + component.destroy(EasyMock.eq(comp)); + EasyMock.replay(component); + scope.register(component); + scope.onEvent(new CompositeStart(this, null)); + assertNotNull(scope.getInstance(component)); + // expire composite + scope.onEvent(new CompositeStop(this, null)); + scope.stop(); + EasyMock.verify(component); + } + + /** + * Verify init and stop by scope container on an atomic component when set to eager initialize + * + * @throws Exception + */ + public void testEagerInitDestroy() throws Exception { + CompositeScopeContainer scope = new CompositeScopeContainer(null); + scope.start(); + Foo comp = new Foo(); + AtomicComponent initDestroyComponent = EasyMock.createMock(AtomicComponent.class); + EasyMock.expect(initDestroyComponent.createInstance()).andReturn(comp); + EasyMock.expect(initDestroyComponent.getInitLevel()).andReturn(1).atLeastOnce(); + initDestroyComponent.init(EasyMock.eq(comp)); + initDestroyComponent.destroy(EasyMock.eq(comp)); + EasyMock.replay(initDestroyComponent); + scope.register(initDestroyComponent); + scope.onEvent(new CompositeStart(this, null)); + // expire composite + scope.onEvent(new CompositeStop(this, null)); + scope.stop(); + EasyMock.verify(initDestroyComponent); + } + + + public void testDestroyOrder() throws Exception { + CompositeScopeContainer scope = new CompositeScopeContainer(null); + scope.start(); + + AtomicComponent oneComponent = createComponent(0); + scope.register(oneComponent); + AtomicComponent twoComponent = createComponent(0); + scope.register(twoComponent); + AtomicComponent threeComponent = createComponent(0); + scope.register(threeComponent); + + scope.onEvent(new CompositeStart(this, null)); + OrderedInitPojo one = (OrderedInitPojo) scope.getInstance(oneComponent); + assertNotNull(one); + assertEquals(1, one.getNumberInstantiated()); + assertEquals(1, one.getInitOrder()); + + OrderedInitPojo two = (OrderedInitPojo) scope.getInstance(twoComponent); + assertNotNull(two); + assertEquals(2, two.getNumberInstantiated()); + assertEquals(2, two.getInitOrder()); + + OrderedInitPojo three = (OrderedInitPojo) scope.getInstance(threeComponent); + assertNotNull(three); + assertEquals(3, three.getNumberInstantiated()); + assertEquals(3, three.getInitOrder()); + + // expire composite + scope.onEvent(new CompositeStop(this, null)); + assertEquals(0, one.getNumberInstantiated()); + scope.stop(); + EasyMock.verify(oneComponent); + EasyMock.verify(twoComponent); + EasyMock.verify(threeComponent); + } + + public void testEagerInitDestroyOrder() throws Exception { + CompositeScopeContainer scope = new CompositeScopeContainer(null); + scope.start(); + + AtomicComponent oneComponent = createComponent(1); + scope.register(oneComponent); + AtomicComponent twoComponent = createComponent(1); + scope.register(twoComponent); + AtomicComponent threeComponent = createComponent(1); + scope.register(threeComponent); + + scope.onEvent(new CompositeStart(this, null)); + OrderedInitPojo one = (OrderedInitPojo) scope.getInstance(oneComponent); + assertNotNull(one); + + OrderedInitPojo two = (OrderedInitPojo) scope.getInstance(twoComponent); + assertNotNull(two); + + OrderedInitPojo three = (OrderedInitPojo) scope.getInstance(threeComponent); + assertNotNull(three); + + // expire composite + scope.onEvent(new CompositeStop(this, null)); + assertEquals(0, one.getNumberInstantiated()); + scope.stop(); + EasyMock.verify(oneComponent); + EasyMock.verify(twoComponent); + EasyMock.verify(threeComponent); + } + + @SuppressWarnings("unchecked") + private AtomicComponent createComponent(int init) throws TargetException { + AtomicComponent component = EasyMock.createMock(AtomicComponent.class); + EasyMock.expect(component.createInstance()).andStubAnswer(new IAnswer() { + public Object answer() throws Throwable { + return new OrderedInitPojoImpl(); + } + }); + EasyMock.expect(component.getInitLevel()).andReturn(init).atLeastOnce(); + component.init(EasyMock.isA(OrderedInitPojoImpl.class)); + EasyMock.expectLastCall().andAnswer(new IAnswer() { + public Object answer() throws Throwable { + OrderedInitPojoImpl pojo = (OrderedInitPojoImpl) EasyMock.getCurrentArguments()[0]; + pojo.init(); + return null; + } + }); + component.destroy(EasyMock.isA(OrderedInitPojoImpl.class)); + EasyMock.expectLastCall().andAnswer(new IAnswer() { + public Object answer() throws Throwable { + OrderedInitPojoImpl pojo = (OrderedInitPojoImpl) EasyMock.getCurrentArguments()[0]; + pojo.destroy(); + return null; + } + }); + EasyMock.replay(component); + return component; + } + + private class Foo { + + } +} diff --git a/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/CompositeScopeObjectFactoryTestCase.java b/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/CompositeScopeObjectFactoryTestCase.java new file mode 100644 index 0000000000..611fbfc13f --- /dev/null +++ b/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/CompositeScopeObjectFactoryTestCase.java @@ -0,0 +1,38 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.core.component.scope; + +import org.apache.tuscany.spi.component.ScopeRegistry; +import org.apache.tuscany.spi.model.Scope; + +import junit.framework.TestCase; +import org.easymock.EasyMock; + +/** + * @version $Rev$ $Date$ + */ +public class CompositeScopeObjectFactoryTestCase extends TestCase { + + public void testCreation() { + ScopeRegistry registry = EasyMock.createMock(ScopeRegistry.class); + registry.registerFactory(EasyMock.isA(Scope.class), EasyMock.isA(CompositeScopeObjectFactory.class)); + + assertNotNull(new CompositeScopeObjectFactory(registry, null).getInstance()); + } +} diff --git a/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/CompositeScopeRestartTestCase.java b/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/CompositeScopeRestartTestCase.java new file mode 100644 index 0000000000..1fdaeed48c --- /dev/null +++ b/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/CompositeScopeRestartTestCase.java @@ -0,0 +1,99 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.core.component.scope; + +import java.lang.reflect.Constructor; + +import org.apache.tuscany.spi.component.AtomicComponent; + +import junit.framework.TestCase; +import org.apache.tuscany.core.component.event.CompositeStart; +import org.apache.tuscany.core.component.event.CompositeStop; +import org.apache.tuscany.core.implementation.PojoConfiguration; +import org.apache.tuscany.core.implementation.system.component.SystemAtomicComponentImpl; +import org.apache.tuscany.core.injection.MethodEventInvoker; +import org.apache.tuscany.core.injection.PojoObjectFactory; + +/** + * Verifies the scope container properly disposes resources and canbe restarted + * + * @version $$Rev$$ $$Date$$ + */ +public class CompositeScopeRestartTestCase extends TestCase { + + public void testRestart() throws Exception { + CompositeScopeContainer scope = new CompositeScopeContainer(null); + scope.start(); + MethodEventInvoker<Object> initInvoker = + new MethodEventInvoker<Object>(InitDestroyOnce.class.getMethod("init")); + MethodEventInvoker<Object> destroyInvoker = + new MethodEventInvoker<Object>(InitDestroyOnce.class.getMethod("destroy")); + PojoConfiguration configuration = new PojoConfiguration(); + configuration.setInitInvoker(initInvoker); + configuration.setDestroyInvoker(destroyInvoker); + Constructor<InitDestroyOnce> ctr = InitDestroyOnce.class.getConstructor((Class<?>[]) null); + configuration.setInstanceFactory(new PojoObjectFactory<InitDestroyOnce>(ctr)); + configuration.setName("InitDestroy"); + AtomicComponent component = new SystemAtomicComponentImpl(configuration); + component.setScopeContainer(scope); + component.start(); + + scope.onEvent(new CompositeStart(this, null)); + Object instance = component.getTargetInstance(); + assertSame(instance, component.getTargetInstance()); + + scope.onEvent(new CompositeStop(this, null)); + scope.stop(); + component.stop(); + + scope.start(); + scope.onEvent(new CompositeStart(this, null)); + component.start(); + assertNotSame(instance, component.getTargetInstance()); + scope.onEvent(new CompositeStop(this, null)); + scope.stop(); + component.stop(); + } + + public static class InitDestroyOnce { + + private boolean initialized; + private boolean destroyed; + + public InitDestroyOnce() { + } + + public void init() { + if (!initialized) { + initialized = true; + } else { + fail("Scope did not clean up properly - Init called more than once"); + } + } + + public void destroy() { + if (!destroyed) { + destroyed = true; + } else { + fail("Scope did not clean up properly - Destroyed called more than once"); + } + } + + } +} diff --git a/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/ConversationalScopeContainerDestroyOnExpirationTestCase.java b/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/ConversationalScopeContainerDestroyOnExpirationTestCase.java new file mode 100644 index 0000000000..a1a3616d0a --- /dev/null +++ b/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/ConversationalScopeContainerDestroyOnExpirationTestCase.java @@ -0,0 +1,121 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.core.component.scope; + +import org.apache.tuscany.spi.component.AtomicComponent; +import org.apache.tuscany.spi.component.SCAObject; +import org.apache.tuscany.spi.component.ScopeContainer; +import org.apache.tuscany.spi.component.WorkContext; +import org.apache.tuscany.spi.event.Event; +import org.apache.tuscany.spi.event.EventFilter; +import org.apache.tuscany.spi.event.RuntimeEventListener; +import org.apache.tuscany.spi.services.store.RecoveryListener; +import org.apache.tuscany.spi.services.store.Store; +import org.apache.tuscany.spi.services.store.StoreExpirationEvent; +import org.apache.tuscany.spi.services.store.StoreReadException; +import org.apache.tuscany.spi.services.store.StoreWriteException; + +import junit.framework.TestCase; +import org.apache.tuscany.core.component.WorkContextImpl; +import org.easymock.EasyMock; + +/** + * @version $Rev$ $Date$ + */ +public class ConversationalScopeContainerDestroyOnExpirationTestCase extends TestCase { + private ScopeContainer container; + private TestStore store; + private AtomicComponent component; + + /** + * Verifies the scope container registers a callback listener for component instance destroy events when a + * conversational instance expires + */ + public void testDestroyNotification() throws Exception { + store.getListener().onEvent(new StoreExpirationEvent(this, component, new Object())); + EasyMock.verify(component); + } + + protected void setUp() throws Exception { + super.setUp(); + component = EasyMock.createMock(AtomicComponent.class); + component.destroy(EasyMock.isA(Object.class)); + EasyMock.replay(component); + store = new TestStore(); + WorkContext context = new WorkContextImpl(); + container = new ConversationalScopeContainer(store, context, null); + container.start(); + } + + protected void tearDown() throws Exception { + super.tearDown(); + container.stop(); + } + + private class TestStore implements Store { + private RuntimeEventListener listener; + + public RuntimeEventListener getListener() { + return listener; + } + + public void insertRecord(SCAObject owner, String id, Object object, long expiration) + throws StoreWriteException { + + } + + public void updateRecord(SCAObject owner, String id, Object object, long expiration) + throws StoreWriteException { + + } + + public Object readRecord(SCAObject owner, String id) throws StoreReadException { + return null; + } + + public void removeRecord(SCAObject owner, String id) throws StoreWriteException { + + } + + public void removeRecords() throws StoreWriteException { + + } + + public void recover(RecoveryListener listener) throws StoreReadException { + + } + + public void publish(Event object) { + + } + + public void addListener(RuntimeEventListener listener) { + this.listener = listener; + } + + public void addListener(EventFilter filter, RuntimeEventListener listener) { + + } + + public void removeListener(RuntimeEventListener listener) { + + } + } + +} diff --git a/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/ConversationalScopeContainerMaxAgeTestCase.java b/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/ConversationalScopeContainerMaxAgeTestCase.java new file mode 100644 index 0000000000..79ea057652 --- /dev/null +++ b/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/ConversationalScopeContainerMaxAgeTestCase.java @@ -0,0 +1,76 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.core.component.scope; + +import org.apache.tuscany.spi.component.AtomicComponent; +import org.apache.tuscany.spi.component.SCAObject; +import org.apache.tuscany.spi.component.ScopeContainer; +import org.apache.tuscany.spi.component.WorkContext; +import org.apache.tuscany.spi.event.RuntimeEventListener; +import org.apache.tuscany.spi.model.Scope; +import org.apache.tuscany.spi.services.store.Store; + +import junit.framework.TestCase; +import org.apache.tuscany.core.component.WorkContextImpl; +import org.easymock.EasyMock; + +/** + * @version $Rev$ $Date$ + */ +public class ConversationalScopeContainerMaxAgeTestCase extends TestCase { + + private ScopeContainer container; + private WorkContext context; + private Store store; + private AtomicComponent component; + + public void testMaxAgeUpdate() throws Exception { + context.setIdentifier(Scope.CONVERSATION, "12345"); + container.getInstance(component); + EasyMock.verify(store); + } + + protected void setUp() throws Exception { + super.setUp(); + ConversationalScopeContainerMaxAgeTestCase.Foo foo = new ConversationalScopeContainerMaxAgeTestCase.Foo(); + context = new WorkContextImpl(); + component = EasyMock.createMock(AtomicComponent.class); + EasyMock.expect(component.getName()).andReturn("foo").atLeastOnce(); + EasyMock.expect(component.getMaxIdleTime()).andReturn(-1L).atLeastOnce(); + EasyMock.expect(component.getMaxAge()).andReturn(600000L).atLeastOnce(); + EasyMock.replay(component); + store = EasyMock.createMock(Store.class); + EasyMock.expect(store.readRecord(EasyMock.isA(SCAObject.class), EasyMock.isA(String.class))).andReturn(foo); + store.addListener(EasyMock.isA(RuntimeEventListener.class)); + EasyMock.replay(store); + container = new ConversationalScopeContainer(store, context, null); + container.start(); + } + + protected void tearDown() throws Exception { + super.tearDown(); + context.clearIdentifier(Scope.CONVERSATION); + container.stop(); + } + + private class Foo { + + } + +} diff --git a/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/ConversationalScopeContainerMaxIdleTimeTestCase.java b/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/ConversationalScopeContainerMaxIdleTimeTestCase.java new file mode 100644 index 0000000000..2ba152d223 --- /dev/null +++ b/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/ConversationalScopeContainerMaxIdleTimeTestCase.java @@ -0,0 +1,79 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.core.component.scope; + +import org.apache.tuscany.spi.component.AtomicComponent; +import org.apache.tuscany.spi.component.SCAObject; +import org.apache.tuscany.spi.component.ScopeContainer; +import org.apache.tuscany.spi.component.WorkContext; +import org.apache.tuscany.spi.event.RuntimeEventListener; +import org.apache.tuscany.spi.model.Scope; +import org.apache.tuscany.spi.services.store.Store; + +import junit.framework.TestCase; +import org.apache.tuscany.core.component.WorkContextImpl; +import org.easymock.EasyMock; + +/** + * @version $Rev$ $Date$ + */ +public class ConversationalScopeContainerMaxIdleTimeTestCase extends TestCase { + + private ScopeContainer container; + private WorkContext context; + private Store store; + private AtomicComponent component; + + public void testMaxIdleTimeUpdate() throws Exception { + context.setIdentifier(Scope.CONVERSATION, "12345"); + container.getInstance(component); + EasyMock.verify(store); + } + + protected void setUp() throws Exception { + super.setUp(); + Foo foo = new Foo(); + context = new WorkContextImpl(); + component = EasyMock.createMock(AtomicComponent.class); + EasyMock.expect(component.getName()).andReturn("foo").atLeastOnce(); + EasyMock.expect(component.getMaxIdleTime()).andReturn(600000L).atLeastOnce(); + EasyMock.replay(component); + store = EasyMock.createMock(Store.class); + EasyMock.expect(store.readRecord(EasyMock.isA(SCAObject.class), EasyMock.isA(String.class))).andReturn(foo); + store.addListener(EasyMock.isA(RuntimeEventListener.class)); + store.updateRecord(EasyMock.isA(SCAObject.class), + EasyMock.isA(String.class), + EasyMock.eq(foo), + EasyMock.anyLong()); + EasyMock.replay(store); + container = new ConversationalScopeContainer(store, context, null); + container.start(); + } + + protected void tearDown() throws Exception { + super.tearDown(); + context.clearIdentifier(Scope.CONVERSATION); + container.stop(); + } + + private class Foo { + + } + +} diff --git a/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/ConversationalScopeContainerPersistenceTestCase.java b/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/ConversationalScopeContainerPersistenceTestCase.java new file mode 100644 index 0000000000..ae7e91a59a --- /dev/null +++ b/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/ConversationalScopeContainerPersistenceTestCase.java @@ -0,0 +1,181 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.core.component.scope; + +import java.util.UUID; + +import org.apache.tuscany.spi.component.AtomicComponent; +import org.apache.tuscany.spi.component.ScopeContainer; +import org.apache.tuscany.spi.component.TargetNotFoundException; +import org.apache.tuscany.spi.component.WorkContext; +import org.apache.tuscany.spi.model.Scope; +import org.apache.tuscany.spi.services.store.Store; +import org.apache.tuscany.spi.services.store.StoreMonitor; + +import junit.framework.TestCase; +import org.apache.tuscany.core.component.WorkContextImpl; +import org.apache.tuscany.core.services.store.memory.MemoryStore; +import org.easymock.EasyMock; + +/** + * @version $Rev$ $Date$ + */ +public class ConversationalScopeContainerPersistenceTestCase extends TestCase { + private ScopeContainer container; + private WorkContext context; + + /** + * Verifies the scope container properly creates an instance + */ + public void testNotYetPersistedInMemory() throws Exception { + String id = UUID.randomUUID().toString(); + context.setIdentifier(Scope.CONVERSATION, id); + AtomicComponent component = EasyMock.createMock(AtomicComponent.class); + component.addListener(EasyMock.eq(container)); + EasyMock.expect(component.createInstance()).andReturn(new Foo()); + EasyMock.expect(component.getMaxAge()).andReturn(600000L).atLeastOnce(); + component.init(EasyMock.isA(Object.class)); + EasyMock.replay(component); + container.register(component); + assertTrue(container.getInstance(component) instanceof Foo); + EasyMock.verify(component); + } + + public void testPersistNewInMemory() throws Exception { + String id = UUID.randomUUID().toString(); + String id2 = UUID.randomUUID().toString(); + context.setIdentifier(Scope.CONVERSATION, id); + AtomicComponent component = EasyMock.createMock(AtomicComponent.class); + component.addListener(EasyMock.eq(container)); + EasyMock.expect(component.getMaxIdleTime()).andReturn(-1L).atLeastOnce(); + EasyMock.replay(component); + container.register(component); + Foo foo = new Foo(); + Foo foo2 = new Foo(); + container.persistNew(component, id, foo, System.currentTimeMillis() + 100000); + assertEquals(foo, container.getInstance(component)); + container.persistNew(component, id2, foo2, System.currentTimeMillis() + 100000); + context.setIdentifier(Scope.CONVERSATION, id2); + assertEquals(foo2, container.getInstance(component)); + EasyMock.verify(component); + } + + public void testPersistInMemory() throws Exception { + String id = UUID.randomUUID().toString(); + context.setIdentifier(Scope.CONVERSATION, id); + AtomicComponent component = EasyMock.createMock(AtomicComponent.class); + component.addListener(EasyMock.eq(container)); + EasyMock.expect(component.getMaxIdleTime()).andReturn(-1L).atLeastOnce(); + EasyMock.replay(component); + container.register(component); + Foo foo = new Foo(); + container.persistNew(component, id, foo, System.currentTimeMillis() + 100000); + assertEquals(foo, container.getInstance(component)); + container.persist(component, id, foo, System.currentTimeMillis() + 100000); + assertEquals(foo, container.getInstance(component)); + EasyMock.verify(component); + } + + public void testRemoveInMemory() throws Exception { + String id = UUID.randomUUID().toString(); + context.setIdentifier(Scope.CONVERSATION, id); + AtomicComponent component = EasyMock.createMock(AtomicComponent.class); + component.addListener(EasyMock.eq(container)); + EasyMock.expect(component.getMaxIdleTime()).andReturn(-1L).atLeastOnce(); + EasyMock.expect(component.getName()).andReturn("foo").atLeastOnce(); + EasyMock.replay(component); + container.register(component); + Foo foo = new Foo(); + container.persistNew(component, id, foo, System.currentTimeMillis() + 100000); + assertEquals(foo, container.getInstance(component)); + container.remove(component); + try { + container.getAssociatedInstance(component); + fail(); + } catch (TargetNotFoundException e) { + //expected + } + EasyMock.verify(component); + } + + public void testRecreateAfterRemoveInMemory() throws Exception { + String id = UUID.randomUUID().toString(); + context.setIdentifier(Scope.CONVERSATION, id); + AtomicComponent component = EasyMock.createMock(AtomicComponent.class); + component.addListener(EasyMock.eq(container)); + EasyMock.expect(component.getMaxAge()).andReturn(600000L).atLeastOnce(); + component.init(EasyMock.isA(Object.class)); + EasyMock.expect(component.getMaxIdleTime()).andReturn(-1L).atLeastOnce(); + EasyMock.expect(component.createInstance()).andReturn(new Foo()); + EasyMock.replay(component); + container.register(component); + Foo foo = new Foo(); + container.persistNew(component, id, foo, System.currentTimeMillis() + 100000); + assertEquals(foo, container.getInstance(component)); + container.remove(component); + Foo foo2 = (Foo) container.getInstance(component); + assertNotNull(foo2); + assertNotSame(foo, foo2); + EasyMock.verify(component); + } + + public void testGetPersistedInstance() throws Exception { + String id = UUID.randomUUID().toString(); + String id2 = UUID.randomUUID().toString(); + context.setIdentifier(Scope.CONVERSATION, id); + AtomicComponent component = EasyMock.createMock(AtomicComponent.class); + EasyMock.expect(component.getName()).andReturn("foo").atLeastOnce(); + EasyMock.expect(component.getMaxIdleTime()).andReturn(-1L).atLeastOnce(); + component.addListener(EasyMock.eq(container)); + EasyMock.replay(component); + container.register(component); + Foo foo = new Foo(); + container.persistNew(component, id, foo, System.currentTimeMillis() + 100000); + assertEquals(foo, container.getAssociatedInstance(component)); + assertEquals(foo, container.getAssociatedInstance(component)); + context.setIdentifier(Scope.CONVERSATION, id2); + try { + container.getAssociatedInstance(component); + fail(); + } catch (TargetNotFoundException e) { + //expected + } + EasyMock.verify(component); + } + + protected void setUp() throws Exception { + super.setUp(); + context = new WorkContextImpl(); + StoreMonitor mock = EasyMock.createNiceMock(StoreMonitor.class); + EasyMock.replay(mock); + Store store = new MemoryStore(mock); + container = new ConversationalScopeContainer(store, context, null); + container.start(); + } + + protected void tearDown() throws Exception { + super.tearDown(); + context.clearIdentifier(Scope.CONVERSATION); + container.stop(); + } + + private class Foo { + + } +} diff --git a/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/ConversationalScopeInstanceLifecycleTestCase.java b/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/ConversationalScopeInstanceLifecycleTestCase.java new file mode 100644 index 0000000000..782b175b47 --- /dev/null +++ b/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/ConversationalScopeInstanceLifecycleTestCase.java @@ -0,0 +1,70 @@ +/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.tuscany.core.component.scope;
+
+import org.apache.tuscany.spi.component.AtomicComponent;
+import org.apache.tuscany.spi.component.WorkContext;
+import org.apache.tuscany.spi.event.RuntimeEventListener;
+import org.apache.tuscany.spi.model.Scope;
+import org.apache.tuscany.spi.services.store.StoreMonitor;
+
+import junit.framework.TestCase;
+import org.apache.tuscany.core.component.WorkContextImpl;
+import org.apache.tuscany.core.services.store.memory.MemoryStore;
+import org.easymock.EasyMock;
+
+/**
+ * Lifecycle unit tests for the conversational scope container
+ *
+ * @version $Rev: 451895 $ $Date: 2006-10-02 02:58:18 -0400 (Mon, 02 Oct 2006) $
+ */
+public class ConversationalScopeInstanceLifecycleTestCase extends TestCase {
+
+ public void testInitRemove() throws Exception {
+ StoreMonitor monitor = EasyMock.createMock(StoreMonitor.class);
+ monitor.start(EasyMock.isA(String.class));
+ monitor.stop(EasyMock.isA(String.class));
+ MemoryStore store = new MemoryStore(monitor);
+ WorkContext ctx = new WorkContextImpl();
+ ConversationalScopeContainer scope = new ConversationalScopeContainer(store, ctx, null);
+ scope.start();
+
+ Foo comp = new Foo();
+ AtomicComponent component = EasyMock.createMock(AtomicComponent.class);
+ EasyMock.expect(component.createInstance()).andReturn(comp);
+ component.init(EasyMock.eq(comp));
+ EasyMock.expect(component.getMaxAge()).andReturn(1L).anyTimes();
+ component.addListener(EasyMock.isA(RuntimeEventListener.class));
+ EasyMock.replay(component);
+ scope.register(component);
+ String convID = "ConvID";
+ ctx.setIdentifier(Scope.CONVERSATION, convID);
+ assertNotNull(scope.getInstance(component));
+ // expire
+ scope.remove(component);
+ scope.stop();
+ EasyMock.verify(component);
+ scope.stop();
+ }
+
+ private class Foo {
+
+ }
+
+}
diff --git a/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/ConversationalScopeRestartTestCase.java b/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/ConversationalScopeRestartTestCase.java new file mode 100644 index 0000000000..bc5ddc7c2c --- /dev/null +++ b/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/ConversationalScopeRestartTestCase.java @@ -0,0 +1,112 @@ +/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.tuscany.core.component.scope;
+
+import java.lang.reflect.Constructor;
+
+import org.apache.tuscany.spi.component.AtomicComponent;
+import org.apache.tuscany.spi.component.WorkContext;
+import org.apache.tuscany.spi.model.Scope;
+import org.apache.tuscany.spi.services.store.StoreMonitor;
+
+import junit.framework.TestCase;
+import org.apache.tuscany.core.component.WorkContextImpl;
+import org.apache.tuscany.core.component.event.ConversationEnd;
+import org.apache.tuscany.core.component.event.ConversationStart;
+import org.apache.tuscany.core.implementation.PojoConfiguration;
+import org.apache.tuscany.core.implementation.system.component.SystemAtomicComponentImpl;
+import org.apache.tuscany.core.injection.MethodEventInvoker;
+import org.apache.tuscany.core.injection.PojoObjectFactory;
+import org.apache.tuscany.core.services.store.memory.MemoryStore;
+import org.easymock.EasyMock;
+
+/**
+ * Verifies the scope container properly disposes resources and can be restarted
+ *
+ * @version $$Rev: 471111 $$ $$Date: 2006-11-03 23:06:48 -0500 (Fri, 03 Nov 2006) $$
+ */
+public class ConversationalScopeRestartTestCase extends TestCase {
+
+ public void testRestart() throws Exception {
+ StoreMonitor monitor = EasyMock.createMock(StoreMonitor.class);
+ monitor.start(EasyMock.isA(String.class));
+ monitor.stop(EasyMock.isA(String.class));
+ MemoryStore store = new MemoryStore(monitor);
+ WorkContext ctx = new WorkContextImpl();
+ ConversationalScopeContainer scope = new ConversationalScopeContainer(store, ctx, null);
+ scope.start();
+ MethodEventInvoker<Object> initInvoker = new MethodEventInvoker<Object>(
+ ConversationalScopeRestartTestCase.InitDestroyOnce.class.getMethod("init"));
+ MethodEventInvoker<Object> destroyInvoker =
+ new MethodEventInvoker<Object>(InitDestroyOnce.class.getMethod("destroy"));
+ PojoConfiguration configuration = new PojoConfiguration();
+ configuration.setInitInvoker(initInvoker);
+ configuration.setDestroyInvoker(destroyInvoker);
+ Constructor<InitDestroyOnce> ctr = InitDestroyOnce.class.getConstructor((Class<?>[]) null);
+ configuration.setInstanceFactory(new PojoObjectFactory<InitDestroyOnce>(ctr));
+ configuration.setName("InitDestroy");
+ AtomicComponent component = new SystemAtomicComponentImpl(configuration);
+ component.setScopeContainer(scope);
+ component.start();
+
+ String conversation = "conv";
+ ctx.setIdentifier(Scope.CONVERSATION, conversation);
+ scope.onEvent(new ConversationStart(this, conversation));
+ Object instance = component.getTargetInstance();
+ assertSame(instance, component.getTargetInstance());
+
+ scope.onEvent(new ConversationEnd(this, conversation));
+ scope.stop();
+ component.stop();
+
+ scope.start();
+ scope.onEvent(new ConversationStart(this, conversation));
+ component.start();
+ //assertNotSame(instance, context.getServiceInstance());
+ scope.onEvent(new ConversationEnd(this, conversation));
+ scope.stop();
+ component.stop();
+ }
+
+ public static class InitDestroyOnce {
+
+ private boolean initialized;
+ private boolean destroyed;
+
+ public InitDestroyOnce() {
+ }
+
+ public void init() {
+ if (!initialized) {
+ initialized = true;
+ } else {
+ fail("Scope did not clean up properly - Init called more than once");
+ }
+ }
+
+ public void destroy() {
+ if (!destroyed) {
+ destroyed = true;
+ } else {
+ fail("Scope did not clean up properly - Destroyed called more than once");
+ }
+ }
+
+ }
+}
diff --git a/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/DependencyLifecycleTestCase.java b/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/DependencyLifecycleTestCase.java new file mode 100644 index 0000000000..4a2497a725 --- /dev/null +++ b/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/DependencyLifecycleTestCase.java @@ -0,0 +1,170 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.core.component.scope; + +import java.util.Map; + +import org.apache.tuscany.spi.component.AtomicComponent; +import org.apache.tuscany.spi.component.WorkContext; +import org.apache.tuscany.spi.model.Scope; + +import junit.framework.TestCase; +import org.apache.tuscany.core.component.WorkContextImpl; +import org.apache.tuscany.core.component.event.CompositeStart; +import org.apache.tuscany.core.component.event.CompositeStop; +import org.apache.tuscany.core.component.event.HttpSessionEnd; +import org.apache.tuscany.core.component.event.RequestEnd; +import org.apache.tuscany.core.component.event.RequestStart; +import org.apache.tuscany.core.mock.component.OrderedDependentPojo; +import org.apache.tuscany.core.mock.component.OrderedDependentPojoImpl; +import org.apache.tuscany.core.mock.component.OrderedInitPojo; +import org.apache.tuscany.core.mock.component.OrderedInitPojoImpl; + +/** + * Tests dependencies are initalized and destroyed in the proper order (i.e. LIFO) + * + * @version $Rev$ $Date$ + */ +public class DependencyLifecycleTestCase extends TestCase { + + public void testInitDestroyOrderCompositeScope() throws Exception { + CompositeScopeContainer scopeCtx = new CompositeScopeContainer(null); + scopeCtx.start(); + Map<String, AtomicComponent> contexts = MockFactory.createWiredComponents("source", + OrderedDependentPojoImpl.class, + scopeCtx, + "target", + OrderedInitPojoImpl.class, + scopeCtx); + for (AtomicComponent component : contexts.values()) { + scopeCtx.register(component); + } + AtomicComponent sourceComponent = contexts.get("source"); + AtomicComponent targetComponent = contexts.get("target"); + scopeCtx.register(sourceComponent); + scopeCtx.register(targetComponent); + scopeCtx.onEvent(new CompositeStart(this, null)); + OrderedDependentPojo source = (OrderedDependentPojo) scopeCtx.getInstance(sourceComponent); + OrderedInitPojo target = (OrderedInitPojo) scopeCtx.getInstance(targetComponent); + assertNotNull(source.getPojo()); + assertNotNull(target); + assertEquals(2, source.getNumberInstantiated()); + scopeCtx.onEvent(new CompositeStop(this, null)); + assertEquals(0, source.getNumberInstantiated()); + scopeCtx.stop(); + } + + public void testInitDestroyOrderAfterStartCompositeScope() throws Exception { + CompositeScopeContainer scopeCtx = new CompositeScopeContainer(null); + scopeCtx.start(); + Map<String, AtomicComponent> contexts = MockFactory.createWiredComponents("source", + OrderedDependentPojoImpl.class, + scopeCtx, + "target", + OrderedInitPojoImpl.class, + scopeCtx); + AtomicComponent sourceComponent = contexts.get("source"); + AtomicComponent targetComponent = contexts.get("target"); + scopeCtx.onEvent(new CompositeStart(this, null)); + scopeCtx.register(sourceComponent); + scopeCtx.register(targetComponent); + OrderedDependentPojo source = (OrderedDependentPojo) scopeCtx.getInstance(sourceComponent); + OrderedInitPojo target = (OrderedInitPojo) scopeCtx.getInstance(targetComponent); + assertNotNull(source.getPojo()); + assertNotNull(target); + assertEquals(2, source.getNumberInstantiated()); + scopeCtx.onEvent(new CompositeStop(this, null)); + assertEquals(0, source.getNumberInstantiated()); + scopeCtx.stop(); + } + + + public void testInitDestroyOrderSessionScope() throws Exception { + WorkContext ctx = new WorkContextImpl(); + HttpSessionScopeContainer scopeCtx = new HttpSessionScopeContainer(ctx, null); + scopeCtx.start(); + Object session = new Object(); + Map<String, AtomicComponent> contexts = MockFactory.createWiredComponents("source", + OrderedDependentPojoImpl.class, + scopeCtx, + "target", + OrderedInitPojoImpl.class, + scopeCtx); + AtomicComponent sourceComponent = contexts.get("source"); + AtomicComponent targetComponent = contexts.get("target"); + scopeCtx.register(sourceComponent); + scopeCtx.register(targetComponent); + ctx.setIdentifier(Scope.SESSION, session); + OrderedDependentPojo source = (OrderedDependentPojo) scopeCtx.getInstance(sourceComponent); + assertNotNull(source.getPojo()); + assertEquals(2, source.getNumberInstantiated()); + scopeCtx.onEvent(new HttpSessionEnd(this, session)); + assertEquals(0, source.getNumberInstantiated()); + scopeCtx.stop(); + } + + + public void testInitDestroyOrderAfterStartSessionScope() throws Exception { + WorkContext ctx = new WorkContextImpl(); + HttpSessionScopeContainer scopeCtx = new HttpSessionScopeContainer(ctx, null); + scopeCtx.start(); + Object session = new Object(); + Map<String, AtomicComponent> contexts = MockFactory.createWiredComponents("source", + OrderedDependentPojoImpl.class, + scopeCtx, + "target", + OrderedInitPojoImpl.class, + scopeCtx); + AtomicComponent sourceComponent = contexts.get("source"); + AtomicComponent targetComponent = contexts.get("target"); + ctx.setIdentifier(Scope.SESSION, session); + scopeCtx.register(sourceComponent); + scopeCtx.register(targetComponent); + OrderedDependentPojo source = (OrderedDependentPojo) scopeCtx.getInstance(sourceComponent); + assertNotNull(source.getPojo()); + assertEquals(2, source.getNumberInstantiated()); + scopeCtx.onEvent(new HttpSessionEnd(this, session)); + assertEquals(0, source.getNumberInstantiated()); + scopeCtx.stop(); + } + + public void testInitDestroyOrderRequestScope() throws Exception { + WorkContext ctx = new WorkContextImpl(); + RequestScopeContainer scopeCtx = new RequestScopeContainer(ctx, null); + scopeCtx.start(); + scopeCtx.onEvent(new RequestStart(this)); + Map<String, AtomicComponent> contexts = MockFactory.createWiredComponents("source", + OrderedDependentPojoImpl.class, + scopeCtx, + "target", + OrderedInitPojoImpl.class, + scopeCtx); + AtomicComponent sourceComponent = contexts.get("source"); + AtomicComponent targetComponent = contexts.get("target"); + scopeCtx.register(sourceComponent); + scopeCtx.register(targetComponent); + OrderedDependentPojo source = (OrderedDependentPojo) scopeCtx.getInstance(sourceComponent); + assertNotNull(source.getPojo()); + assertEquals(2, source.getNumberInstantiated()); + scopeCtx.onEvent(new RequestEnd(this)); + assertEquals(0, source.getNumberInstantiated()); + scopeCtx.stop(); + } + +} diff --git a/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/HttpSessionScopeInitDestroyErrorTestCase.java b/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/HttpSessionScopeInitDestroyErrorTestCase.java new file mode 100644 index 0000000000..7aa2f9a120 --- /dev/null +++ b/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/HttpSessionScopeInitDestroyErrorTestCase.java @@ -0,0 +1,69 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.core.component.scope; + +import org.apache.tuscany.spi.component.AtomicComponent; +import org.apache.tuscany.spi.component.ScopeContainerMonitor; +import org.apache.tuscany.spi.component.TargetDestructionException; +import org.apache.tuscany.spi.component.WorkContext; +import org.apache.tuscany.spi.event.RuntimeEventListener; +import org.apache.tuscany.spi.model.Scope; + +import junit.framework.TestCase; +import org.apache.tuscany.core.component.WorkContextImpl; +import org.apache.tuscany.core.component.event.HttpSessionEnd; +import org.apache.tuscany.core.component.event.HttpSessionStart; +import org.easymock.EasyMock; + +/** + * @version $Rev$ $Date$ + */ +public class HttpSessionScopeInitDestroyErrorTestCase extends TestCase { + + public void testDestroyErrorMonitor() throws Exception { + ScopeContainerMonitor monitor; + monitor = EasyMock.createMock(ScopeContainerMonitor.class); + monitor.destructionError(EasyMock.isA(TargetDestructionException.class)); + EasyMock.replay(monitor); + WorkContext workContext = new WorkContextImpl(); + HttpSessionScopeContainer scope = new HttpSessionScopeContainer(workContext, monitor); + scope.start(); + AtomicComponent component = EasyMock.createMock(AtomicComponent.class); + component.addListener(EasyMock.isA(RuntimeEventListener.class)); + EasyMock.expect(component.getName()).andReturn("foo").atLeastOnce(); + EasyMock.expect(component.createInstance()).andReturn(new Object()); + EasyMock.expect(component.isEagerInit()).andReturn(true); + component.init(EasyMock.isA(Object.class)); + component.destroy(EasyMock.isA(Object.class)); + EasyMock.expectLastCall().andThrow(new TargetDestructionException("", "")); + EasyMock.replay(component); + scope.register(component); + Object id = new Object(); + scope.onEvent(new HttpSessionStart(this, id)); + workContext.setIdentifier(Scope.SESSION, id); + scope.getInstance(component); + scope.onEvent(new HttpSessionEnd(this, id)); + EasyMock.verify(monitor); + } + + + protected void setUp() throws Exception { + super.setUp(); + } +} diff --git a/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/HttpSessionScopeInstanceLifecycleTestCase.java b/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/HttpSessionScopeInstanceLifecycleTestCase.java new file mode 100644 index 0000000000..29d6d54450 --- /dev/null +++ b/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/HttpSessionScopeInstanceLifecycleTestCase.java @@ -0,0 +1,136 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.core.component.scope; + +import org.apache.tuscany.spi.component.AtomicComponent; +import org.apache.tuscany.spi.component.TargetException; +import org.apache.tuscany.spi.component.WorkContext; +import org.apache.tuscany.spi.event.RuntimeEventListener; +import org.apache.tuscany.spi.model.Scope; + +import junit.framework.TestCase; +import org.apache.tuscany.core.component.WorkContextImpl; +import org.apache.tuscany.core.component.event.HttpSessionEnd; +import org.apache.tuscany.core.component.event.HttpSessionStart; +import org.apache.tuscany.core.mock.component.OrderedInitPojo; +import org.apache.tuscany.core.mock.component.OrderedInitPojoImpl; +import org.easymock.EasyMock; +import org.easymock.IAnswer; + +/** + * Lifecycle unit tests for the composite scope container + * + * @version $Rev$ $Date$ + */ +public class HttpSessionScopeInstanceLifecycleTestCase extends TestCase { + + public void testInitDestroy() throws Exception { + WorkContext ctx = new WorkContextImpl(); + HttpSessionScopeContainer scope = new HttpSessionScopeContainer(ctx, null); + scope.start(); + Foo comp = new Foo(); + AtomicComponent component = EasyMock.createMock(AtomicComponent.class); + EasyMock.expect(component.createInstance()).andReturn(comp); + component.addListener(EasyMock.isA(RuntimeEventListener.class)); + component.init(EasyMock.eq(comp)); + component.destroy(EasyMock.eq(comp)); + EasyMock.replay(component); + scope.register(component); + Object session = new Object(); + ctx.setIdentifier(Scope.SESSION, session); + scope.onEvent(new HttpSessionStart(this, session)); + assertNotNull(scope.getInstance(component)); + // expire + scope.onEvent(new HttpSessionEnd(this, session)); + scope.stop(); + EasyMock.verify(component); + scope.stop(); + } + + public void testDestroyOrder() throws Exception { + WorkContext ctx = new WorkContextImpl(); + HttpSessionScopeContainer scope = new HttpSessionScopeContainer(ctx, null); + scope.start(); + + AtomicComponent oneComponent = createComponent(); + scope.register(oneComponent); + AtomicComponent twoComponent = createComponent(); + scope.register(twoComponent); + AtomicComponent threeComponent = createComponent(); + scope.register(threeComponent); + + Object session = new Object(); + ctx.setIdentifier(Scope.SESSION, session); + scope.onEvent(new HttpSessionStart(this, session)); + OrderedInitPojo one = (OrderedInitPojo) scope.getInstance(oneComponent); + assertNotNull(one); + assertEquals(1, one.getNumberInstantiated()); + assertEquals(1, one.getInitOrder()); + + OrderedInitPojo two = (OrderedInitPojo) scope.getInstance(twoComponent); + assertNotNull(two); + assertEquals(2, two.getNumberInstantiated()); + assertEquals(2, two.getInitOrder()); + + OrderedInitPojo three = (OrderedInitPojo) scope.getInstance(threeComponent); + assertNotNull(three); + assertEquals(3, three.getNumberInstantiated()); + assertEquals(3, three.getInitOrder()); + + scope.onEvent(new HttpSessionEnd(this, session)); + assertEquals(0, one.getNumberInstantiated()); + scope.stop(); + EasyMock.verify(oneComponent); + EasyMock.verify(twoComponent); + EasyMock.verify(threeComponent); + } + + @SuppressWarnings("unchecked") + private AtomicComponent createComponent() throws TargetException { + AtomicComponent component = EasyMock.createMock(AtomicComponent.class); + EasyMock.expect(component.createInstance()).andStubAnswer(new IAnswer() { + public Object answer() throws Throwable { + return new OrderedInitPojoImpl(); + } + }); + component.addListener(EasyMock.isA(RuntimeEventListener.class)); + component.init(EasyMock.isA(OrderedInitPojoImpl.class)); + EasyMock.expectLastCall().andAnswer(new IAnswer() { + public Object answer() throws Throwable { + OrderedInitPojoImpl pojo = (OrderedInitPojoImpl) EasyMock.getCurrentArguments()[0]; + pojo.init(); + return null; + } + }); + component.destroy(EasyMock.isA(OrderedInitPojoImpl.class)); + EasyMock.expectLastCall().andAnswer(new IAnswer() { + public Object answer() throws Throwable { + OrderedInitPojoImpl pojo = (OrderedInitPojoImpl) EasyMock.getCurrentArguments()[0]; + pojo.destroy(); + return null; + } + }); + EasyMock.replay(component); + return component; + } + + private class Foo { + + } +} diff --git a/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/HttpSessionScopeRestartTestCase.java b/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/HttpSessionScopeRestartTestCase.java new file mode 100644 index 0000000000..eff0eb6d3a --- /dev/null +++ b/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/HttpSessionScopeRestartTestCase.java @@ -0,0 +1,105 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.core.component.scope; + +import java.lang.reflect.Constructor; + +import org.apache.tuscany.spi.component.AtomicComponent; +import org.apache.tuscany.spi.component.WorkContext; +import org.apache.tuscany.spi.model.Scope; + +import junit.framework.TestCase; +import org.apache.tuscany.core.component.WorkContextImpl; +import org.apache.tuscany.core.component.event.HttpSessionEnd; +import org.apache.tuscany.core.component.event.HttpSessionStart; +import org.apache.tuscany.core.implementation.PojoConfiguration; +import org.apache.tuscany.core.implementation.system.component.SystemAtomicComponentImpl; +import org.apache.tuscany.core.injection.MethodEventInvoker; +import org.apache.tuscany.core.injection.PojoObjectFactory; + +/** + * Verifies the scope container properly disposes resources and canbe restarted + * + * @version $$Rev$$ $$Date$$ + */ +public class HttpSessionScopeRestartTestCase extends TestCase { + + public void testRestart() throws Exception { + WorkContext ctx = new WorkContextImpl(); + HttpSessionScopeContainer scope = new HttpSessionScopeContainer(ctx, null); + scope.start(); + MethodEventInvoker<Object> initInvoker = new MethodEventInvoker<Object>( + HttpSessionScopeRestartTestCase.InitDestroyOnce.class.getMethod("init")); + MethodEventInvoker<Object> destroyInvoker = + new MethodEventInvoker<Object>(InitDestroyOnce.class.getMethod("destroy")); + PojoConfiguration configuration = new PojoConfiguration(); + configuration.setInitInvoker(initInvoker); + configuration.setDestroyInvoker(destroyInvoker); + Constructor<InitDestroyOnce> ctr = InitDestroyOnce.class.getConstructor((Class<?>[]) null); + configuration.setInstanceFactory(new PojoObjectFactory<InitDestroyOnce>(ctr)); + configuration.setName("InitDestroy"); + AtomicComponent component = new SystemAtomicComponentImpl(configuration); + component.setScopeContainer(scope); + component.start(); + + Object session = new Object(); + ctx.setIdentifier(Scope.SESSION, session); + scope.onEvent(new HttpSessionStart(this, session)); + Object instance = component.getTargetInstance(); + assertSame(instance, component.getTargetInstance()); + + scope.onEvent(new HttpSessionEnd(this, session)); + scope.stop(); + component.stop(); + + scope.start(); + scope.onEvent(new HttpSessionStart(this, session)); + component.start(); + assertNotSame(instance, component.getTargetInstance()); + scope.onEvent(new HttpSessionEnd(this, session)); + scope.stop(); + component.stop(); + } + + public static class InitDestroyOnce { + + private boolean initialized; + private boolean destroyed; + + public InitDestroyOnce() { + } + + public void init() { + if (!initialized) { + initialized = true; + } else { + fail("Scope did not clean up properly - Init called more than once"); + } + } + + public void destroy() { + if (!destroyed) { + destroyed = true; + } else { + fail("Scope did not clean up properly - Destroyed called more than once"); + } + } + + } +} diff --git a/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/InstanceWrapperTestCase.java b/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/InstanceWrapperTestCase.java new file mode 100644 index 0000000000..5600987d8b --- /dev/null +++ b/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/InstanceWrapperTestCase.java @@ -0,0 +1,66 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.core.component.scope; + +import org.apache.tuscany.spi.component.AtomicComponent; + +import junit.framework.TestCase; +import org.easymock.EasyMock; + +/** + * @version $Rev$ $Date$ + */ +public class InstanceWrapperTestCase extends TestCase { + + public void testExceptionInit() throws Exception { + AtomicComponent component = getComponent(); + InstanceWrapper wrapper = new InstanceWrapperImpl(component, new Object()); + try { + wrapper.start(); + fail(); + } catch (SomeException e) { + // expected + } + assertFalse(wrapper.isStarted()); + EasyMock.verify(component); + } + + public void testNonStart() throws Exception { + AtomicComponent comp = EasyMock.createNiceMock(AtomicComponent.class); // class-level one has an expects + InstanceWrapper wrapper = new InstanceWrapperImpl(comp, new Object()); + try { + wrapper.getInstance(); + fail(); + } catch (IllegalStateException e) { + // expected + } + } + + private AtomicComponent getComponent() throws Exception { + // do not use setUp() since we do not need this in all testcases + AtomicComponent comp = EasyMock.createMock(AtomicComponent.class); + comp.init(EasyMock.isA(Object.class)); + EasyMock.expectLastCall().andThrow(new SomeException()); + EasyMock.replay(comp); + return comp; + } + + private class SomeException extends RuntimeException { + } +} diff --git a/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/MockFactory.java b/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/MockFactory.java new file mode 100644 index 0000000000..30c12292ba --- /dev/null +++ b/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/MockFactory.java @@ -0,0 +1,137 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.core.component.scope; + +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + +import org.osoa.sca.annotations.Destroy; +import org.osoa.sca.annotations.EagerInit; +import org.osoa.sca.annotations.Init; + +import org.apache.tuscany.spi.QualifiedName; +import org.apache.tuscany.spi.component.AtomicComponent; +import org.apache.tuscany.spi.component.ScopeContainer; +import org.apache.tuscany.spi.idl.java.JavaServiceContract; +import org.apache.tuscany.spi.wire.InboundWire; +import org.apache.tuscany.spi.wire.OutboundWire; + +import org.apache.tuscany.core.implementation.PojoConfiguration; +import org.apache.tuscany.core.implementation.system.component.SystemAtomicComponentImpl; +import org.apache.tuscany.core.injection.MethodEventInvoker; +import org.apache.tuscany.core.injection.PojoObjectFactory; +import org.apache.tuscany.core.wire.InboundWireImpl; +import org.apache.tuscany.core.wire.OutboundWireImpl; + +/** + * @version $$Rev$$ $$Date$$ + */ +public final class MockFactory { + + private MockFactory() { + } + + @SuppressWarnings("unchecked") + public static Map<String, AtomicComponent> createWiredComponents(String source, + Class<?> sourceClass, + ScopeContainer sourceScopeContainer, + String target, + Class<?> targetClass, + ScopeContainer targetScopeContainer) + throws NoSuchMethodException { + + Map<String, AtomicComponent> components = new HashMap<String, AtomicComponent>(); + AtomicComponent targetComponent = createAtomicComponent(target, targetScopeContainer, targetClass); + PojoConfiguration sourceConfig = new PojoConfiguration(); + sourceConfig.setInstanceFactory(new PojoObjectFactory(sourceClass.getConstructor())); + + //create target wire + Method[] sourceMethods = sourceClass.getMethods(); + Class[] interfaces = targetClass.getInterfaces(); + EagerInit eager = targetClass.getAnnotation(EagerInit.class); + if (eager != null) { + sourceConfig.setInitLevel(eager.value()); + } + + Method setter = null; + for (Class interfaze : interfaces) { + + for (Method method : sourceMethods) { + if (method.getParameterTypes().length == 1) { + if (interfaze.isAssignableFrom(method.getParameterTypes()[0])) { + setter = method; + } + } + if (method.getAnnotation(Init.class) != null) { + sourceConfig.setInitInvoker(new MethodEventInvoker<Object>(method)); + + } else if (method.getAnnotation(Destroy.class) != null) { + sourceConfig.setDestroyInvoker(new MethodEventInvoker<Object>(method)); + } + } + + } + if (setter == null) { + throw new IllegalArgumentException("No setter found on source for target"); + } + + sourceConfig.addReferenceSite(setter.getName(), setter); + sourceConfig.setName(source); + AtomicComponent sourceComponent = new SystemAtomicComponentImpl(sourceConfig); + sourceComponent.setScopeContainer(sourceScopeContainer); + QualifiedName targetName = new QualifiedName(target); + OutboundWire wire = new OutboundWireImpl(); + wire.setReferenceName(setter.getName()); + wire.setServiceContract(new JavaServiceContract(targetClass)); + InboundWire inboundWire = new InboundWireImpl(); + inboundWire.setContainer(targetComponent); + inboundWire.setServiceContract(new JavaServiceContract(targetClass)); + inboundWire.setServiceName(targetName.getPortName()); + wire.setTargetWire(inboundWire); + sourceComponent.addOutboundWire(wire); + components.put(source, sourceComponent); + components.put(target, targetComponent); + return components; + } + + @SuppressWarnings("unchecked") + public static AtomicComponent createAtomicComponent(String name, ScopeContainer container, Class<?> clazz) + throws NoSuchMethodException { + PojoConfiguration configuration = new PojoConfiguration(); + configuration.setInstanceFactory(new PojoObjectFactory(clazz.getConstructor())); + EagerInit eager = clazz.getAnnotation(EagerInit.class); + if (eager != null) { + configuration.setInitLevel(eager.value()); + } + Method[] methods = clazz.getMethods(); + for (Method method : methods) { + if (method.getAnnotation(Init.class) != null) { + configuration.setInitInvoker(new MethodEventInvoker<Object>(method)); + } else if (method.getAnnotation(Destroy.class) != null) { + configuration.setDestroyInvoker(new MethodEventInvoker<Object>(method)); + } + } + configuration.setName(name); + AtomicComponent component = new SystemAtomicComponentImpl(configuration); + component.setScopeContainer(container); + return component; + } + +} diff --git a/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/RequestScopeInitDestroyErrorTestCase.java b/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/RequestScopeInitDestroyErrorTestCase.java new file mode 100644 index 0000000000..3f562c9b61 --- /dev/null +++ b/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/RequestScopeInitDestroyErrorTestCase.java @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.core.component.scope; + +import org.apache.tuscany.spi.component.AtomicComponent; +import org.apache.tuscany.spi.component.ScopeContainerMonitor; +import org.apache.tuscany.spi.component.TargetDestructionException; +import org.apache.tuscany.spi.event.RuntimeEventListener; + +import junit.framework.TestCase; +import org.apache.tuscany.core.component.WorkContextImpl; +import org.apache.tuscany.core.component.event.RequestEnd; +import org.apache.tuscany.core.component.event.RequestStart; +import org.easymock.EasyMock; + +/** + * @version $Rev$ $Date$ + */ +public class RequestScopeInitDestroyErrorTestCase extends TestCase { + + public void testDestroyErrorMonitor() throws Exception { + ScopeContainerMonitor monitor; + monitor = EasyMock.createMock(ScopeContainerMonitor.class); + monitor.destructionError(EasyMock.isA(TargetDestructionException.class)); + EasyMock.replay(monitor); + RequestScopeContainer scope = new RequestScopeContainer(new WorkContextImpl(), monitor); + scope.start(); + AtomicComponent component = EasyMock.createMock(AtomicComponent.class); + component.addListener(EasyMock.isA(RuntimeEventListener.class)); + EasyMock.expect(component.getName()).andReturn("foo").atLeastOnce(); + EasyMock.expect(component.createInstance()).andReturn(new Object()); + component.init(EasyMock.isA(Object.class)); + component.destroy(EasyMock.isA(Object.class)); + EasyMock.expectLastCall().andThrow(new TargetDestructionException("", "")); + EasyMock.replay(component); + scope.register(component); + scope.onEvent(new RequestStart(this)); + scope.getInstance(component); + scope.onEvent(new RequestEnd(this)); + EasyMock.verify(monitor); + } + + + protected void setUp() throws Exception { + super.setUp(); + } +} diff --git a/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/RequestScopeInstanceLifecycleTestCase.java b/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/RequestScopeInstanceLifecycleTestCase.java new file mode 100644 index 0000000000..6601640c3b --- /dev/null +++ b/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/RequestScopeInstanceLifecycleTestCase.java @@ -0,0 +1,129 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.core.component.scope; + +import org.apache.tuscany.spi.component.AtomicComponent; +import org.apache.tuscany.spi.component.TargetException; +import org.apache.tuscany.spi.component.WorkContext; + +import junit.framework.TestCase; +import org.apache.tuscany.core.component.WorkContextImpl; +import org.apache.tuscany.core.component.event.RequestEnd; +import org.apache.tuscany.core.component.event.RequestStart; +import org.apache.tuscany.core.mock.component.OrderedInitPojo; +import org.apache.tuscany.core.mock.component.OrderedInitPojoImpl; +import org.easymock.EasyMock; +import org.easymock.IAnswer; + +/** + * Lifecycle unit tests for the request scope container + * + * @version $Rev$ $Date$ + */ +public class RequestScopeInstanceLifecycleTestCase extends TestCase { + + public void testInitDestroy() throws Exception { + WorkContext ctx = new WorkContextImpl(); + RequestScopeContainer scope = new RequestScopeContainer(ctx, null); + scope.start(); + Foo comp = new Foo(); + AtomicComponent component = EasyMock.createMock(AtomicComponent.class); + EasyMock.expect(component.createInstance()).andReturn(comp); + component.init(EasyMock.eq(comp)); + component.destroy(EasyMock.eq(comp)); + EasyMock.replay(component); + scope.register(component); + scope.onEvent(new RequestStart(this)); + assertNotNull(scope.getInstance(component)); + // expire + scope.onEvent(new RequestEnd(this)); + scope.stop(); + EasyMock.verify(component); + scope.stop(); + } + + public void testDestroyOrder() throws Exception { + WorkContext ctx = new WorkContextImpl(); + RequestScopeContainer scope = new RequestScopeContainer(ctx, null); + scope.start(); + + AtomicComponent oneComponent = createComponent(); + scope.register(oneComponent); + AtomicComponent twoComponent = createComponent(); + scope.register(twoComponent); + AtomicComponent threeComponent = createComponent(); + scope.register(threeComponent); + + scope.onEvent(new RequestStart(this)); + OrderedInitPojo one = (OrderedInitPojo) scope.getInstance(oneComponent); + assertNotNull(one); + assertEquals(1, one.getNumberInstantiated()); + assertEquals(1, one.getInitOrder()); + + OrderedInitPojo two = (OrderedInitPojo) scope.getInstance(twoComponent); + assertNotNull(two); + assertEquals(2, two.getNumberInstantiated()); + assertEquals(2, two.getInitOrder()); + + OrderedInitPojo three = (OrderedInitPojo) scope.getInstance(threeComponent); + assertNotNull(three); + assertEquals(3, three.getNumberInstantiated()); + assertEquals(3, three.getInitOrder()); + + scope.onEvent(new RequestEnd(this)); + assertEquals(0, one.getNumberInstantiated()); + scope.stop(); + EasyMock.verify(oneComponent); + EasyMock.verify(twoComponent); + EasyMock.verify(threeComponent); + } + + @SuppressWarnings("unchecked") + private AtomicComponent createComponent() throws TargetException { + AtomicComponent component = EasyMock.createMock(AtomicComponent.class); + EasyMock.expect(component.createInstance()).andStubAnswer(new IAnswer() { + public Object answer() throws Throwable { + return new OrderedInitPojoImpl(); + } + }); + component.init(EasyMock.isA(OrderedInitPojoImpl.class)); + EasyMock.expectLastCall().andAnswer(new IAnswer() { + public Object answer() throws Throwable { + OrderedInitPojoImpl pojo = (OrderedInitPojoImpl) EasyMock.getCurrentArguments()[0]; + pojo.init(); + return null; + } + }); + component.destroy(EasyMock.isA(OrderedInitPojoImpl.class)); + EasyMock.expectLastCall().andAnswer(new IAnswer() { + public Object answer() throws Throwable { + OrderedInitPojoImpl pojo = (OrderedInitPojoImpl) EasyMock.getCurrentArguments()[0]; + pojo.destroy(); + return null; + } + }); + EasyMock.replay(component); + return component; + } + + private class Foo { + + } + +} diff --git a/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/RequestScopeRestartTestCase.java b/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/RequestScopeRestartTestCase.java new file mode 100644 index 0000000000..335d8c621d --- /dev/null +++ b/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/RequestScopeRestartTestCase.java @@ -0,0 +1,99 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.core.component.scope; + +import java.lang.reflect.Constructor; + +import org.apache.tuscany.spi.component.AtomicComponent; +import org.apache.tuscany.spi.component.WorkContext; + +import junit.framework.TestCase; +import org.apache.tuscany.core.component.WorkContextImpl; +import org.apache.tuscany.core.component.event.RequestEnd; +import org.apache.tuscany.core.implementation.PojoConfiguration; +import org.apache.tuscany.core.implementation.system.component.SystemAtomicComponentImpl; +import org.apache.tuscany.core.injection.MethodEventInvoker; +import org.apache.tuscany.core.injection.PojoObjectFactory; + +/** + * Verifies the scope container properly disposes resources and canbe restarted + * + * @version $$Rev$$ $$Date$$ + */ +public class RequestScopeRestartTestCase extends TestCase { + + public void testRestart() throws Exception { + WorkContext ctx = new WorkContextImpl(); + RequestScopeContainer scope = new RequestScopeContainer(ctx, null); + scope.start(); + MethodEventInvoker<Object> initInvoker = + new MethodEventInvoker<Object>(InitDestroyOnce.class.getMethod("init")); + MethodEventInvoker<Object> destroyInvoker = + new MethodEventInvoker<Object>(InitDestroyOnce.class.getMethod("destroy")); + PojoConfiguration configuration = new PojoConfiguration(); + configuration.setInitInvoker(initInvoker); + configuration.setDestroyInvoker(destroyInvoker); + Constructor<InitDestroyOnce> ctr = InitDestroyOnce.class.getConstructor((Class<?>[]) null); + configuration.setInstanceFactory(new PojoObjectFactory<InitDestroyOnce>(ctr)); + configuration.setName("InitDestroy"); + AtomicComponent component = new SystemAtomicComponentImpl(configuration); + component.setScopeContainer(scope); + component.start(); + + Object instance = component.getTargetInstance(); + assertSame(instance, component.getTargetInstance()); + + scope.onEvent(new RequestEnd(this)); + scope.stop(); + component.stop(); + + scope.start(); + component.start(); + assertNotSame(instance, component.getTargetInstance()); + scope.onEvent(new RequestEnd(this)); + scope.stop(); + component.stop(); + } + + public static class InitDestroyOnce { + + private boolean initialized; + private boolean destroyed; + + public InitDestroyOnce() { + } + + public void init() { + if (!initialized) { + initialized = true; + } else { + fail("Scope did not clean up properly - Init called more than once"); + } + } + + public void destroy() { + if (!destroyed) { + destroyed = true; + } else { + fail("Scope did not clean up properly - Destroyed called more than once"); + } + } + + } +} diff --git a/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/ScopeRegistryTestCase.java b/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/ScopeRegistryTestCase.java new file mode 100644 index 0000000000..e1380eb4c0 --- /dev/null +++ b/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/ScopeRegistryTestCase.java @@ -0,0 +1,80 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.core.component.scope; + +import org.apache.tuscany.spi.component.ScopeContainer; +import org.apache.tuscany.spi.component.ScopeRegistry; +import org.apache.tuscany.spi.component.WorkContext; +import org.apache.tuscany.spi.model.Scope; + +import junit.framework.TestCase; +import org.apache.tuscany.core.component.WorkContextImpl; + +/** + * Verifies retrieval of standard scope contexts from the default scope registry + * + * @version $$Rev$$ $$Date$$ + */ +public class ScopeRegistryTestCase extends TestCase { + public void testScopeContextCreation() throws Exception { + WorkContext context = new WorkContextImpl(); + ScopeRegistry scopeRegistry = new ScopeRegistryImpl(); + scopeRegistry.registerFactory(Scope.REQUEST, new RequestScopeObjectFactory(context, null)); + HttpSessionScopeObjectFactory sessionFactory = new HttpSessionScopeObjectFactory(scopeRegistry, context, null); + scopeRegistry.registerFactory(Scope.SESSION, sessionFactory); + scopeRegistry.registerFactory(Scope.CONVERSATION, + new ConversationalScopeObjectFactory(scopeRegistry, context, null, null)); + ScopeContainer request = scopeRegistry.getScopeContainer(Scope.REQUEST); + assertTrue(request instanceof RequestScopeContainer); + assertSame(request, scopeRegistry.getScopeContainer(Scope.REQUEST)); + ScopeContainer session = scopeRegistry.getScopeContainer(Scope.SESSION); + assertTrue(session instanceof HttpSessionScopeContainer); + assertSame(session, scopeRegistry.getScopeContainer(Scope.SESSION)); + assertNotSame(request, session); + ScopeContainer conversation = scopeRegistry.getScopeContainer(Scope.CONVERSATION); + assertTrue(conversation instanceof ConversationalScopeContainer); + assertSame(conversation, scopeRegistry.getScopeContainer(Scope.CONVERSATION)); + assertNotSame(session, conversation); + } + + public void testDeregisterFactory() throws Exception { + WorkContext context = new WorkContextImpl(); + ScopeRegistry scopeRegistry = new ScopeRegistryImpl(); + RequestScopeObjectFactory factory = new RequestScopeObjectFactory(context, null); + scopeRegistry.registerFactory(Scope.REQUEST, factory); + scopeRegistry.deregisterFactory(Scope.REQUEST); + assertNull(scopeRegistry.getScopeContainer(Scope.REQUEST)); + ConversationalScopeObjectFactory convFactory = + new ConversationalScopeObjectFactory(scopeRegistry, context, null, null); + scopeRegistry.registerFactory(Scope.CONVERSATION, convFactory); + scopeRegistry.deregisterFactory(Scope.CONVERSATION); + assertNull(scopeRegistry.getScopeContainer(Scope.CONVERSATION)); + } + + public void testScopeNotRegistered() throws Exception { + WorkContext workContext = new WorkContextImpl(); + ScopeRegistry scopeRegistry = new ScopeRegistryImpl(); + assertNull(scopeRegistry.getScopeContainer(Scope.REQUEST)); + assertNull(scopeRegistry.getScopeContainer(Scope.SESSION)); + assertNull(scopeRegistry.getScopeContainer(Scope.CONVERSATION)); + assertNull(scopeRegistry.getScopeContainer(Scope.STATELESS)); + } + + +} diff --git a/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/StatelessScopeContainerTestCase.java b/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/StatelessScopeContainerTestCase.java new file mode 100644 index 0000000000..89cd9b51d7 --- /dev/null +++ b/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/StatelessScopeContainerTestCase.java @@ -0,0 +1,48 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.core.component.scope; + +import junit.framework.TestCase; + +/** + * @version $Rev$ $Date$ + */ +public class StatelessScopeContainerTestCase extends TestCase { + + public void testBadStopWithoutStart() throws Exception { + StatelessScopeContainer container = new StatelessScopeContainer(null, null); + try { + container.stop(); + fail(); + } catch (IllegalStateException e) { + //expected + } + } + + public void testBadDoubleStart() throws Exception { + StatelessScopeContainer container = new StatelessScopeContainer(null, null); + try { + container.start(); + container.start(); + fail(); + } catch (IllegalStateException e) { + //expected + } + } +} diff --git a/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/StatelessScopeObjectFactoryTestCase.java b/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/StatelessScopeObjectFactoryTestCase.java new file mode 100644 index 0000000000..0902b9d78c --- /dev/null +++ b/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/StatelessScopeObjectFactoryTestCase.java @@ -0,0 +1,37 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.core.component.scope; + +import org.apache.tuscany.spi.component.ScopeRegistry; +import org.apache.tuscany.spi.model.Scope; + +import junit.framework.TestCase; +import org.easymock.EasyMock; + +/** + * @version $Rev$ $Date$ + */ +public class StatelessScopeObjectFactoryTestCase extends TestCase { + + public void testCreation() { + ScopeRegistry registry = EasyMock.createMock(ScopeRegistry.class); + registry.registerFactory(EasyMock.isA(Scope.class), EasyMock.isA(StatelessScopeObjectFactory.class)); + assertNotNull(new StatelessScopeObjectFactory(registry, null, null).getInstance()); + } +} diff --git a/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/WorkContextTestCase.java b/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/WorkContextTestCase.java new file mode 100644 index 0000000000..8a9fdf4ac4 --- /dev/null +++ b/sca-java-1.x/branches/pre-spec-changes/kernel/core/src/test/java/org/apache/tuscany/core/component/scope/WorkContextTestCase.java @@ -0,0 +1,212 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tuscany.core.component.scope; + +import org.apache.tuscany.spi.component.AtomicComponent; +import org.apache.tuscany.spi.component.CompositeComponent; +import org.apache.tuscany.spi.component.WorkContext; + +import junit.framework.TestCase; +import org.apache.tuscany.core.component.WorkContextImpl; +import org.easymock.EasyMock; + +/** + * @version $Rev$ $Date$ + */ +public class WorkContextTestCase extends TestCase { + + public void testRemoteComponent() throws Exception { + WorkContext ctx = new WorkContextImpl(); + CompositeComponent component = EasyMock.createNiceMock(CompositeComponent.class); + CompositeComponent component2 = EasyMock.createNiceMock(CompositeComponent.class); + ctx.setRemoteComponent(component); + assertEquals(component, ctx.getRemoteComponent()); + ctx.setRemoteComponent(component2); + assertEquals(component2, ctx.getRemoteComponent()); + } + + public void testNonSetRemoteComponent() throws Exception { + WorkContext ctx = new WorkContextImpl(); + assertNull(ctx.getRemoteComponent()); + } + + public void testSetCurrentAtomicComponent() throws Exception { + WorkContext ctx = new WorkContextImpl(); + AtomicComponent component = EasyMock.createNiceMock(AtomicComponent.class); + AtomicComponent component2 = EasyMock.createNiceMock(AtomicComponent.class); + ctx.setCurrentAtomicComponent(component); + assertEquals(component, ctx.getCurrentAtomicComponent()); + ctx.setCurrentAtomicComponent(component2); + assertEquals(component2, ctx.getCurrentAtomicComponent()); + } + + public void testNonSetCurrentAtomicComponent() throws Exception { + WorkContext ctx = new WorkContextImpl(); + assertNull(ctx.getCurrentAtomicComponent()); + } + + public void testIndentifier() throws Exception { + WorkContext ctx = new WorkContextImpl(); + Object id = new Object(); + ctx.setIdentifier(this, id); + assertEquals(id, ctx.getIdentifier(this)); + } + + public void testClearIndentifier() throws Exception { + WorkContext ctx = new WorkContextImpl(); + Object id = new Object(); + ctx.setIdentifier(this, id); + ctx.clearIdentifier(this); + assertNull(ctx.getIdentifier(this)); + } + + public void testClearIndentifiers() throws Exception { + WorkContext ctx = new WorkContextImpl(); + Object id = new Object(); + Object id2 = new Object(); + ctx.setIdentifier(id, id); + ctx.setIdentifier(id2, id2); + ctx.clearIdentifiers(); + assertNull(ctx.getIdentifier(id)); + assertNull(ctx.getIdentifier(id2)); + } + + public void testClearNonExistentIndentifier() throws Exception { + WorkContext ctx = new WorkContextImpl(); + ctx.clearIdentifier(this); + } + + public void testNullIndentifier() throws Exception { + WorkContext ctx = new WorkContextImpl(); + Object id = new Object(); + ctx.setIdentifier(this, id); + ctx.clearIdentifier(null); + assertEquals(id, ctx.getIdentifier(this)); + } + + public void testNoIndentifier() throws Exception { + WorkContext ctx = new WorkContextImpl(); + assertNull(ctx.getIdentifier(this)); + } + + public void testSetGetCorrelationId() { + WorkContext context = new WorkContextImpl(); + context.setCurrentCorrelationId("msg-005"); + assertEquals(context.getCurrentCorrelationId(), "msg-005"); + context.setCurrentCorrelationId(null); + assertNull(context.getCurrentCorrelationId()); + } + + public void testSetGetCorrelationIdInNewThread() throws InterruptedException { + WorkContext context = new WorkContextImpl(); + context.setCurrentCorrelationId("msg-005"); + assertEquals(context.getCurrentCorrelationId(), "msg-005"); + context.setIdentifier("TX", "002"); + ChildThread t = new ChildThread(context); + t.start(); + t.join(); + assertTrue(t.passed); + context.setCurrentCorrelationId(null); + assertNull(context.getCurrentCorrelationId()); + } + + public void testCurrentAtomicComponentDoesNotPropagateToChildThread() throws InterruptedException { + // NOTE should behaviour be to propagate? + WorkContext context = new WorkContextImpl(); + context.setCurrentAtomicComponent(EasyMock.createNiceMock(AtomicComponent.class)); + TestCurrentAtomicComponentChildThread t = new TestCurrentAtomicComponentChildThread(context); + t.start(); + t.join(); + assertTrue(t.passed); + context.setCurrentAtomicComponent(null); + assertNull(context.getCurrentAtomicComponent()); + } + + public void testCurrentRemoteComponentDoesNotPropagateToChildThread() throws InterruptedException { + // NOTE should behaviour be to propagate? + WorkContext context = new WorkContextImpl(); + context.setRemoteComponent(EasyMock.createNiceMock(CompositeComponent.class)); + TestCurrentRemoteComponentChildThread t = new TestCurrentRemoteComponentChildThread(context); + t.start(); + t.join(); + assertTrue(t.passed); + context.setRemoteComponent(null); + assertNull(context.getRemoteComponent()); + } + + private static final class ChildThread extends Thread { + private WorkContext context; + private boolean passed = true; + + private ChildThread(WorkContext context) { + this.context = context; + } + + @Override + public void run() { + try { + assertNull(context.getCurrentCorrelationId()); + assertEquals("002", context.getIdentifier("TX")); + } catch (AssertionError e) { + passed = false; + } + } + + } + + private static final class TestCurrentAtomicComponentChildThread extends Thread { + private WorkContext context; + private boolean passed = true; + + private TestCurrentAtomicComponentChildThread(WorkContext context) { + this.context = context; + } + + @Override + public void run() { + try { + assertNull(context.getCurrentAtomicComponent()); + } catch (AssertionError e) { + passed = false; + } + } + + } + + private static final class TestCurrentRemoteComponentChildThread extends Thread { + private WorkContext context; + private boolean passed = true; + + private TestCurrentRemoteComponentChildThread(WorkContext context) { + this.context = context; + } + + @Override + public void run() { + try { + assertNull(context.getRemoteComponent()); + } catch (AssertionError e) { + passed = false; + } + } + + } + + +} |