diff options
Diffstat (limited to 'sandbox/rfeng/minicore/src/test/java/org/apache/tuscany/core/component')
17 files changed, 1831 insertions, 0 deletions
diff --git a/sandbox/rfeng/minicore/src/test/java/org/apache/tuscany/core/component/ComponentManagerImplTestCase.java b/sandbox/rfeng/minicore/src/test/java/org/apache/tuscany/core/component/ComponentManagerImplTestCase.java new file mode 100644 index 0000000000..c1710e9b8e --- /dev/null +++ b/sandbox/rfeng/minicore/src/test/java/org/apache/tuscany/core/component/ComponentManagerImplTestCase.java @@ -0,0 +1,90 @@ +/* + * 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; + +import java.net.URI; + +import junit.framework.TestCase; +import org.easymock.EasyMock; + +import org.apache.tuscany.spi.component.Component; +import org.apache.tuscany.spi.component.DuplicateNameException; + +/** + * @version $Rev$ $Date$ + */ +public class ComponentManagerImplTestCase extends TestCase { + private static final URI DOMAIN = URI.create("sca://localhost/"); + private static final URI ROOT1 = DOMAIN.resolve("root1"); + private static final URI GRANDCHILD = DOMAIN.resolve("parent/child2/grandchild"); + + private ComponentManagerImpl manager; + + public void testRegister() throws Exception { + Component root = EasyMock.createMock(Component.class); + EasyMock.expect(root.getUri()).andReturn(ROOT1); + EasyMock.replay(root); + manager.register(root); + assertEquals(root, manager.getComponent(ROOT1)); + EasyMock.verify(root); + + EasyMock.reset(root); + EasyMock.expect(root.getUri()).andReturn(ROOT1); + EasyMock.replay(root); + manager.unregister(root); + EasyMock.verify(root); + assertEquals(null, manager.getComponent(ROOT1)); + } + + public void testRegisterGrandchild() throws Exception { + Component root = EasyMock.createMock(Component.class); + EasyMock.expect(root.getUri()).andReturn(GRANDCHILD); + EasyMock.replay(root); + manager.register(root); + assertEquals(root, manager.getComponent(GRANDCHILD)); + EasyMock.verify(root); + } + + public void testRegisterDuplicate() throws Exception { + Component root = EasyMock.createMock(Component.class); + EasyMock.expect(root.getUri()).andReturn(ROOT1); + EasyMock.replay(root); + + Component duplicate = EasyMock.createMock(Component.class); + EasyMock.expect(duplicate.getUri()).andReturn(ROOT1); + EasyMock.replay(duplicate); + + manager.register(root); + assertEquals(root, manager.getComponent(ROOT1)); + try { + manager.register(duplicate); + fail(); + } catch (DuplicateNameException e) { + // expected + } + assertEquals(root, manager.getComponent(ROOT1)); + EasyMock.verify(root); + EasyMock.verify(duplicate); + } + + protected void setUp() throws Exception { + super.setUp(); + manager = new ComponentManagerImpl(); + } +} diff --git a/sandbox/rfeng/minicore/src/test/java/org/apache/tuscany/core/component/JavaObjectRegistrationTestCase.java b/sandbox/rfeng/minicore/src/test/java/org/apache/tuscany/core/component/JavaObjectRegistrationTestCase.java new file mode 100644 index 0000000000..2d00a13716 --- /dev/null +++ b/sandbox/rfeng/minicore/src/test/java/org/apache/tuscany/core/component/JavaObjectRegistrationTestCase.java @@ -0,0 +1,77 @@ +/* + * 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; + +import java.net.URI; + +import org.apache.tuscany.spi.component.AtomicComponent; +import org.apache.tuscany.spi.component.Component; +import org.apache.tuscany.spi.component.ComponentManager; +import org.apache.tuscany.spi.component.DuplicateNameException; +import org.apache.tuscany.spi.idl.java.JavaServiceContract; + +import junit.framework.TestCase; + +/** + * @version $Rev$ $Date$ + */ +public class JavaObjectRegistrationTestCase extends TestCase { + private ComponentManager componentManager; + + public void testRegistration() throws Exception { + MockComponent instance = new MockComponent(); + URI uri = URI.create("foo"); + JavaServiceContract<MockComponent> contract = new JavaServiceContract<MockComponent>(MockComponent.class) { + }; + componentManager.registerJavaObject(uri, contract, instance); + Component component = componentManager.getComponent(URI.create("foo")); + assertTrue(component instanceof AtomicComponent); + MockComponent resolvedInstance = (MockComponent) ((AtomicComponent) component).getTargetInstance(); + assertSame(instance, resolvedInstance); + } + + public void testDuplicateRegistration() throws Exception { + MockComponent instance = new MockComponent(); + URI uri = URI.create("foo"); + JavaServiceContract<MockComponent> contract = new JavaServiceContract<MockComponent>(MockComponent.class) { + }; + componentManager.registerJavaObject(uri, contract, instance); + try { + componentManager.registerJavaObject(uri, contract, instance); + fail(); + } catch (DuplicateNameException e) { + // ok + } + } + + protected void setUp() throws Exception { + super.setUp(); + componentManager = new ComponentManagerImpl(); + } + + protected void tearDown() throws Exception { + super.tearDown(); + } + + private static class MockComponent { + public String hello(String message) { + return message; + } + } +} diff --git a/sandbox/rfeng/minicore/src/test/java/org/apache/tuscany/core/component/SystemSingletonAtomicComponentTestCase.java b/sandbox/rfeng/minicore/src/test/java/org/apache/tuscany/core/component/SystemSingletonAtomicComponentTestCase.java new file mode 100644 index 0000000000..fac45ebfb5 --- /dev/null +++ b/sandbox/rfeng/minicore/src/test/java/org/apache/tuscany/core/component/SystemSingletonAtomicComponentTestCase.java @@ -0,0 +1,82 @@ +/* + * 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; + +import java.net.URI; +import java.util.ArrayList; +import java.util.List; + +import org.apache.tuscany.spi.component.TargetException; +import org.apache.tuscany.spi.idl.java.JavaServiceContract; + +import junit.framework.TestCase; + +/** + * @version $Rev$ $Date$ + */ +public class SystemSingletonAtomicComponentTestCase extends TestCase { + + public void testGetInstance() throws TargetException { + JavaServiceContract<Foo> contract = new JavaServiceContract<Foo>(Foo.class) { + + }; + FooImpl foo = new FooImpl(); + SystemSingletonAtomicComponent<Foo, FooImpl> component = + new SystemSingletonAtomicComponent<Foo, FooImpl>(URI.create("foo"), contract, foo); + assertEquals(foo, component.getTargetInstance()); + } + + public void testGetInstanceMultipleServices() throws TargetException { + FooImpl foo = new FooImpl(); + List<JavaServiceContract<?>> services = new ArrayList<JavaServiceContract<?>>(); + services.add(new JavaServiceContract<Foo>(Foo.class) { + }); + services.add(new JavaServiceContract<Bar>(Bar.class) { + }); + SystemSingletonAtomicComponent<Foo, FooImpl> component = + new SystemSingletonAtomicComponent<Foo, FooImpl>(URI.create("foo"), services, foo); + assertEquals(foo, component.getTargetInstance()); + } + + public void testOptimizable() { + JavaServiceContract<Foo> contract = new JavaServiceContract<Foo>(Foo.class) { + }; + FooImpl foo = new FooImpl(); + SystemSingletonAtomicComponent<Foo, FooImpl> component = + new SystemSingletonAtomicComponent<Foo, FooImpl>(URI.create("foo"), contract, foo); + assertTrue(component.isOptimizable()); + } + + + protected void setUp() throws Exception { + super.setUp(); + } + + private interface Foo { + + } + + private interface Bar { + + } + + private class FooImpl implements Foo, Bar { + + } +} diff --git a/sandbox/rfeng/minicore/src/test/java/org/apache/tuscany/core/component/event/EventTestCase.java b/sandbox/rfeng/minicore/src/test/java/org/apache/tuscany/core/component/event/EventTestCase.java new file mode 100644 index 0000000000..93cb50ddcb --- /dev/null +++ b/sandbox/rfeng/minicore/src/test/java/org/apache/tuscany/core/component/event/EventTestCase.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.event; + +import java.net.URI; + +import junit.framework.TestCase; + +/** + * @version $Rev$ $Date$ + */ +public class EventTestCase extends TestCase { + private URI uri = URI.create("foo"); + + public void testCompositeStart() { + ComponentStart event = new ComponentStart(this, uri); + assertEquals(uri, event.getComponentUri()); + } + + public void testCompositeStop() { + ComponentStop event = new ComponentStop(this, uri); + assertEquals(uri, event.getComponentUri()); + } + + public void testHttpSessionStart() { + Object id = new Object(); + HttpSessionEvent event = new HttpSessionStart(this, id); + assertEquals(this, event.getSource()); + assertEquals(id, event.getId()); + } + + public void testHttpSessionEnd() { + Object id = new Object(); + HttpSessionEvent event = new HttpSessionEnd(this, id); + assertEquals(this, event.getSource()); + assertEquals(id, event.getId()); + } + + public void testRequestStart() { + RequestStart event = new RequestStart(this); + assertEquals(this, event.getSource()); + } + + public void testReequestEnd() { + RequestEnd event = new RequestEnd(this); + assertEquals(this, event.getSource()); + } + + + protected void setUp() throws Exception { + super.setUp(); + } +} diff --git a/sandbox/rfeng/minicore/src/test/java/org/apache/tuscany/core/component/scope/AbstractScopeContainerTestCase.java b/sandbox/rfeng/minicore/src/test/java/org/apache/tuscany/core/component/scope/AbstractScopeContainerTestCase.java new file mode 100644 index 0000000000..a6e4de3c48 --- /dev/null +++ b/sandbox/rfeng/minicore/src/test/java/org/apache/tuscany/core/component/scope/AbstractScopeContainerTestCase.java @@ -0,0 +1,61 @@ +/* + * 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.net.URI; + +import junit.framework.TestCase; + +import org.apache.tuscany.spi.component.ScopeContainer; +import org.apache.tuscany.spi.component.AtomicComponent; +import org.apache.tuscany.spi.component.InstanceWrapper; + +import org.easymock.EasyMock; +import org.easymock.IMocksControl; + +/** + * @version $Rev$ $Date$ + */ +public abstract class AbstractScopeContainerTestCase<T, KEY> extends TestCase { + protected IMocksControl control; + protected ScopeContainer<KEY> scopeContainer; + protected URI groupId; + protected KEY contextId; + protected AtomicComponent<T> component; + protected InstanceWrapper<T> wrapper; + + @SuppressWarnings("unchecked") + protected void setUp() throws Exception { + super.setUp(); + control = EasyMock.createStrictControl(); + component = control.createMock(AtomicComponent.class); + wrapper = control.createMock(InstanceWrapper.class); + } + + protected void preRegisterComponent() throws Exception { + scopeContainer.start(); + scopeContainer.register(component, groupId); + EasyMock.expect(component.isEagerInit()).andStubReturn(false); + } + + protected void expectCreateWrapper() throws Exception { + EasyMock.expect(component.createInstanceWrapper()).andReturn(wrapper); + wrapper.start(); + } +} diff --git a/sandbox/rfeng/minicore/src/test/java/org/apache/tuscany/core/component/scope/BasicCompositeScopeTestCase.java b/sandbox/rfeng/minicore/src/test/java/org/apache/tuscany/core/component/scope/BasicCompositeScopeTestCase.java new file mode 100644 index 0000000000..cb66af27b0 --- /dev/null +++ b/sandbox/rfeng/minicore/src/test/java/org/apache/tuscany/core/component/scope/BasicCompositeScopeTestCase.java @@ -0,0 +1,143 @@ +/* + * 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.net.URI; + +import junit.framework.TestCase; +import org.easymock.EasyMock; +import org.easymock.IMocksControl; + +import org.apache.tuscany.spi.component.AtomicComponent; +import org.apache.tuscany.spi.component.InstanceWrapper; +import org.apache.tuscany.spi.component.ScopeContainer; +import org.apache.tuscany.spi.component.TargetResolutionException; +import org.apache.tuscany.spi.model.Scope; + +/** + * @version $$Rev$$ $$Date$$ + */ +public class BasicCompositeScopeTestCase<T> extends TestCase { + protected IMocksControl control; + protected ScopeContainer<URI> scopeContainer; + protected URI groupId; + protected URI contextId; + protected AtomicComponent<T> component; + protected InstanceWrapper<T> wrapper; + + public void testCorrectScope() { + assertEquals(Scope.COMPOSITE, scopeContainer.getScope()); + } + + public void testWrapperCreation() throws Exception { + EasyMock.expect(component.createInstanceWrapper()).andReturn(wrapper); + wrapper.start(); + control.replay(); + scopeContainer.register(component, groupId); + assertSame(wrapper, scopeContainer.getWrapper(component, contextId)); + assertSame(wrapper, scopeContainer.getWrapper(component, contextId)); + assertSame(wrapper, scopeContainer.getAssociatedWrapper(component, contextId)); + control.verify(); + } + + public void testGetAssociatedInstanceNonExistent() throws Exception { + URI uri = URI.create("oops"); + EasyMock.expect(component.getUri()).andReturn(uri); + control.replay(); + scopeContainer.register(component, groupId); + try { + scopeContainer.getAssociatedWrapper(component, contextId); + fail(); + } catch (TargetResolutionException e) { + assertEquals(uri.toString(), e.getMessage()); + } + control.verify(); + } + + /* + public void testLifecycleWithNoEagerInit() throws Exception { + EasyMock.expect(component.getInitLevel()).andReturn(0); + control.replay(); + scopeContainer.startContext(contextId, groupId); + scopeContainer.stopContext(contextId); + control.verify(); + } + + public void testLifecycleWithEagerInit() throws Exception { + EasyMock.expect(component.getInitLevel()).andReturn(1); + EasyMock.expect(component.createInstanceWrapper()).andReturn(wrapper); + wrapper.start(); + wrapper.stop(); + EasyMock.replay(component, wrapper); + scopeContainer.onEvent(new ComponentStart(this, null)); + scopeContainer.onEvent(new ComponentStop(this, null)); + EasyMock.verify(component, wrapper); + } + + public void testDestroyOrder() throws Exception { + scopeContainer = new CompositeScopeContainer(null); + scopeContainer.start(); + IMocksControl control = EasyMock.createStrictControl(); + InstanceWrapper wrapper1 = control.createMock(InstanceWrapper.class); + InstanceWrapper wrapper2 = control.createMock(InstanceWrapper.class); + InstanceWrapper wrapper3 = control.createMock(InstanceWrapper.class); + AtomicComponent component1 = control.createMock(AtomicComponent.class); + AtomicComponent component2 = control.createMock(AtomicComponent.class); + AtomicComponent component3 = control.createMock(AtomicComponent.class); + + EasyMock.expect(component1.getInitLevel()).andStubReturn(-1); + EasyMock.expect(component2.getInitLevel()).andStubReturn(1); + EasyMock.expect(component3.getInitLevel()).andStubReturn(-1); + + EasyMock.expect(component2.createInstanceWrapper()).andReturn(wrapper2); + wrapper2.start(); + EasyMock.expect(component1.createInstanceWrapper()).andReturn(wrapper1); + wrapper1.start(); + EasyMock.expect(component3.createInstanceWrapper()).andReturn(wrapper3); + wrapper3.start(); + wrapper3.stop(); + wrapper1.stop(); + wrapper2.stop(); + control.replay(); + + scopeContainer.register(component1, contextId); + scopeContainer.register(component2, contextId); + scopeContainer.register(component3, contextId); + scopeContainer.onEvent(new ComponentStart(this, null)); + assertSame(wrapper1, scopeContainer.getWrapper(component1)); + assertSame(wrapper2, scopeContainer.getWrapper(component2)); + assertSame(wrapper3, scopeContainer.getWrapper(component3)); + scopeContainer.onEvent(new ComponentStop(this, null)); + control.verify(); + } + */ + @SuppressWarnings("unchecked") + protected void setUp() throws Exception { + super.setUp(); + groupId = contextId = URI.create("compositeId"); + control = EasyMock.createStrictControl(); + component = control.createMock(AtomicComponent.class); + EasyMock.expect(component.isEagerInit()).andStubReturn(false); + wrapper = control.createMock(InstanceWrapper.class); + + scopeContainer = new CompositeScopeContainer<URI>(null); + scopeContainer.start(); + scopeContainer.startContext(contextId, groupId); + } +} diff --git a/sandbox/rfeng/minicore/src/test/java/org/apache/tuscany/core/component/scope/BasicConversationalScopeTestCase.java b/sandbox/rfeng/minicore/src/test/java/org/apache/tuscany/core/component/scope/BasicConversationalScopeTestCase.java new file mode 100644 index 0000000000..02f5540dea --- /dev/null +++ b/sandbox/rfeng/minicore/src/test/java/org/apache/tuscany/core/component/scope/BasicConversationalScopeTestCase.java @@ -0,0 +1,95 @@ +/*
+ * 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;
+
+import org.apache.tuscany.core.component.SimpleWorkContext;
+import org.apache.tuscany.core.services.store.memory.MemoryStore;
+import org.apache.tuscany.spi.component.AtomicComponent;
+import org.apache.tuscany.spi.component.InstanceWrapper;
+import org.apache.tuscany.spi.component.ScopeContainer;
+import org.apache.tuscany.spi.component.WorkContext;
+import org.apache.tuscany.spi.services.store.StoreMonitor;
+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 AtomicComponent component;
+ private InstanceWrapper wrapper;
+ private ScopeContainer scopeContainer;
+ private WorkContext workContext;
+
+/*
+ public void testLifecycleManagement() throws Exception {
+ // start the request
+ String conversation = "conv";
+ workContext.setIdentifier(Scope.CONVERSATION, conversation);
+
+ EasyMock.expect(component.createInstanceWrapper()).andReturn(wrapper);
+ wrapper.start();
+ // FIXME shouldn't stop be called when the component is removed?
+// wrapper.stop();
+ EasyMock.replay(component, wrapper);
+ assertSame(wrapper, scopeContainer.getWrapper(component));
+ assertSame(wrapper, scopeContainer.getWrapper(component));
+ scopeContainer.remove(component);
+ EasyMock.verify(component, wrapper);
+ }
+*/
+
+/*
+ public void testCoversationIsolation() throws Exception {
+ String conversation1 = "conv";
+ String conversation2 = "conv2";
+
+ InstanceWrapper wrapper2 = EasyMock.createStrictMock(InstanceWrapper.class);
+ EasyMock.expect(component.createInstanceWrapper()).andReturn(wrapper);
+ wrapper.start();
+ EasyMock.expect(component.createInstanceWrapper()).andReturn(wrapper2);
+ wrapper2.start();
+ EasyMock.replay(component, wrapper, wrapper2);
+ workContext.setIdentifier(Scope.CONVERSATION, conversation1);
+ assertSame(wrapper, scopeContainer.getWrapper(component));
+ workContext.setIdentifier(Scope.CONVERSATION, conversation2);
+ assertSame(wrapper2, scopeContainer.getWrapper(component));
+ EasyMock.verify(component, wrapper);
+ }
+*/
+ public void testX() {
+}
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ component = EasyMock.createStrictMock(AtomicComponent.class);
+ EasyMock.expect(component.getMaxAge()).andStubReturn(-1);
+ EasyMock.expect(component.getMaxIdleTime()).andStubReturn(-1);
+ wrapper = EasyMock.createStrictMock(InstanceWrapper.class);
+
+ StoreMonitor monitor = EasyMock.createMock(StoreMonitor.class);
+ monitor.start(EasyMock.isA(String.class));
+ monitor.stop(EasyMock.isA(String.class));
+ MemoryStore store = new MemoryStore(monitor);
+ workContext = new SimpleWorkContext();
+ scopeContainer = new ConversationalScopeContainer(store, workContext, null);
+ scopeContainer.start();
+ }
+}
diff --git a/sandbox/rfeng/minicore/src/test/java/org/apache/tuscany/core/component/scope/BasicHttpSessionScopeTestCase.java b/sandbox/rfeng/minicore/src/test/java/org/apache/tuscany/core/component/scope/BasicHttpSessionScopeTestCase.java new file mode 100644 index 0000000000..1f29c92c92 --- /dev/null +++ b/sandbox/rfeng/minicore/src/test/java/org/apache/tuscany/core/component/scope/BasicHttpSessionScopeTestCase.java @@ -0,0 +1,220 @@ +/* + * 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; + +import org.apache.tuscany.core.component.SimpleWorkContext; +import org.apache.tuscany.spi.component.AtomicComponent; +import org.apache.tuscany.spi.component.InstanceWrapper; +import org.apache.tuscany.spi.component.ScopeContainer; +import org.apache.tuscany.spi.component.ScopeContainerMonitor; +import org.apache.tuscany.spi.component.WorkContext; +import org.easymock.EasyMock; + +/** + * @version $$Rev$$ $$Date$$ + */ +public abstract class BasicHttpSessionScopeTestCase extends TestCase { + private ScopeContainerMonitor monitor; + private ScopeContainer scopeContainer; + private AtomicComponent component; + private InstanceWrapper wrapper; + private WorkContext workContext; + + public void testLifecycleManagement() throws Exception { +/* + // start the request + Object session = new Object(); + workContext.setIdentifier(Scope.SESSION, session); + + EasyMock.expect(component.createInstanceWrapper()).andReturn(wrapper); + wrapper.start(); + wrapper.stop(); + EasyMock.replay(component, wrapper); + assertSame(wrapper, scopeContainer.getWrapper(component)); + assertSame(wrapper, scopeContainer.getWrapper(component)); + scopeContainer.onEvent(new HttpSessionEnd(this, session)); + EasyMock.verify(component, wrapper); +*/ + } + + public void testGetAssociatedInstance() throws Exception { +/* + // start the request + Object session = new Object(); + workContext.setIdentifier(Scope.SESSION, session); + + EasyMock.expect(component.createInstanceWrapper()).andReturn(wrapper); + wrapper.start(); + EasyMock.replay(component, wrapper); + assertSame(wrapper, scopeContainer.getWrapper(component)); + assertSame(wrapper, scopeContainer.getAssociatedWrapper(component)); + EasyMock.verify(component, wrapper); +*/ + } + + public void testGetAssociatedInstanceNonExistent() throws Exception { +/* + URI id = URI.create("oops"); + EasyMock.expect(component.getUri()).andReturn(id); + EasyMock.replay(component); + + // start the request + Object session = new Object(); + workContext.setIdentifier(Scope.SESSION, session); + try { + scopeContainer.getAssociatedWrapper(component); + fail(); + } catch (TargetNotFoundException e) { + assertEquals(id.toString(), e.getMessage()); + EasyMock.verify(component); + } +*/ + } + + public void testSessionIsolation() throws Exception { +/* + // start the request + Object session1 = new Object(); + Object session2 = new Object(); + + InstanceWrapper wrapper2 = EasyMock.createNiceMock(InstanceWrapper.class); + EasyMock.expect(component.createInstanceWrapper()).andReturn(wrapper); + wrapper.start(); + EasyMock.expect(component.createInstanceWrapper()).andReturn(wrapper2); + wrapper2.start(); + EasyMock.replay(component, wrapper); + workContext.setIdentifier(Scope.SESSION, session1); + assertSame(wrapper, scopeContainer.getWrapper(component)); + assertSame(wrapper, scopeContainer.getAssociatedWrapper(component)); + workContext.setIdentifier(Scope.SESSION, session2); + assertSame(wrapper2, scopeContainer.getWrapper(component)); + assertSame(wrapper2, scopeContainer.getAssociatedWrapper(component)); + workContext.setIdentifier(Scope.SESSION, session1); + assertSame(wrapper, scopeContainer.getWrapper(component)); + EasyMock.verify(component, wrapper); +*/ + } + + public void testDestroyErrorMonitor() throws Exception { +/* + TargetDestructionException ex = new TargetDestructionException("oops", "again"); + monitor.destructionError(ex); + EasyMock.expect(component.createInstanceWrapper()).andReturn(wrapper); + wrapper.start(); + wrapper.stop(); + EasyMock.expectLastCall().andThrow(ex); + EasyMock.replay(component, wrapper, monitor); + + Object id = new Object(); + scopeContainer.onEvent(new HttpSessionStart(this, id)); + workContext.setIdentifier(Scope.SESSION, id); + assertSame(wrapper, scopeContainer.getWrapper(component)); + scopeContainer.onEvent(new HttpSessionEnd(this, id)); + EasyMock.verify(component, wrapper, monitor); +*/ + } + + public void testDestroyOrder() throws Exception { +/* + Object session = new Object(); + workContext.setIdentifier(Scope.SESSION, session); + + IMocksControl control = EasyMock.createStrictControl(); + InstanceWrapper wrapper1 = control.createMock(InstanceWrapper.class); + InstanceWrapper wrapper2 = control.createMock(InstanceWrapper.class); + InstanceWrapper wrapper3 = control.createMock(InstanceWrapper.class); + AtomicComponent component1 = control.createMock(AtomicComponent.class); + AtomicComponent component2 = control.createMock(AtomicComponent.class); + AtomicComponent component3 = control.createMock(AtomicComponent.class); + + component1.addListener(scopeContainer); + component2.addListener(scopeContainer); + component3.addListener(scopeContainer); + EasyMock.expect(component1.createInstanceWrapper()).andReturn(wrapper1); + wrapper1.start(); + EasyMock.expect(component2.createInstanceWrapper()).andReturn(wrapper2); + wrapper2.start(); + EasyMock.expect(component3.createInstanceWrapper()).andReturn(wrapper3); + wrapper3.start(); + wrapper3.stop(); + wrapper2.stop(); + wrapper1.stop(); + control.replay(); + + scopeContainer.register(component1, null); + scopeContainer.register(component2, null); + scopeContainer.register(component3, null); + scopeContainer.onEvent(new HttpSessionStart(this, session)); + assertSame(wrapper1, scopeContainer.getWrapper(component1)); + assertSame(wrapper2, scopeContainer.getWrapper(component2)); + assertSame(wrapper3, scopeContainer.getWrapper(component3)); + scopeContainer.onEvent(new HttpSessionEnd(this, session)); + control.verify(); +*/ + } + + public void testReuseSession() throws Exception { +/* + Object session = new Object(); + workContext.setIdentifier(Scope.SESSION, session); + + IMocksControl control = EasyMock.createStrictControl(); + InstanceWrapper wrapper1 = control.createMock(InstanceWrapper.class); + InstanceWrapper wrapper2 = control.createMock(InstanceWrapper.class); + AtomicComponent component1 = control.createMock(AtomicComponent.class); + + component1.addListener(scopeContainer); + EasyMock.expect(component1.createInstanceWrapper()).andReturn(wrapper1); + wrapper1.start(); + wrapper1.stop(); + EasyMock.expect(component1.createInstanceWrapper()).andReturn(wrapper2); + wrapper2.start(); + wrapper2.stop(); + control.replay(); + + scopeContainer.register(component1, null); + scopeContainer.onEvent(new HttpSessionStart(this, session)); + assertSame(wrapper1, scopeContainer.getWrapper(component1)); + scopeContainer.onEvent(new HttpSessionEnd(this, session)); + + scopeContainer.onEvent(new HttpSessionStart(this, session)); + assertSame(wrapper2, scopeContainer.getWrapper(component1)); + scopeContainer.onEvent(new HttpSessionEnd(this, session)); + control.verify(); +*/ + } + + protected void setUp() throws Exception { + super.setUp(); + component = EasyMock.createStrictMock(AtomicComponent.class); + wrapper = EasyMock.createStrictMock(InstanceWrapper.class); + + workContext = new SimpleWorkContext(); + monitor = EasyMock.createStrictMock(ScopeContainerMonitor.class); + scopeContainer = new HttpSessionScopeContainer(workContext, monitor); + scopeContainer.start(); + + component.addListener(scopeContainer); + EasyMock.replay(component); + scopeContainer.register(component, null); + EasyMock.reset(component); + } +} diff --git a/sandbox/rfeng/minicore/src/test/java/org/apache/tuscany/core/component/scope/BasicRequestScopeTestCase.java b/sandbox/rfeng/minicore/src/test/java/org/apache/tuscany/core/component/scope/BasicRequestScopeTestCase.java new file mode 100644 index 0000000000..4aad2dc0c6 --- /dev/null +++ b/sandbox/rfeng/minicore/src/test/java/org/apache/tuscany/core/component/scope/BasicRequestScopeTestCase.java @@ -0,0 +1,167 @@ +/* + * 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; +import org.easymock.EasyMock; + +import org.apache.tuscany.spi.component.AtomicComponent; +import org.apache.tuscany.spi.component.InstanceWrapper; +import org.apache.tuscany.spi.component.ScopeContainer; +import org.apache.tuscany.spi.component.ScopeContainerMonitor; + +/** + * @version $$Rev$$ $$Date$$ + */ +public abstract class BasicRequestScopeTestCase extends TestCase { + private ScopeContainerMonitor monitor; + private ScopeContainer scopeContainer; + private AtomicComponent component; + private InstanceWrapper wrapper; + + public void testLifecycleManagement() throws Exception { +/* + EasyMock.expect(component.createInstanceWrapper()).andReturn(wrapper); + wrapper.start(); + EasyMock.replay(component, wrapper); + assertSame(wrapper, scopeContainer.getWrapper(component)); + assertSame(wrapper, scopeContainer.getWrapper(component)); + EasyMock.verify(component, wrapper); +*/ + } + + public void testGetAssociatedInstance() throws Exception { +/* + EasyMock.expect(component.createInstanceWrapper()).andReturn(wrapper); + wrapper.start(); + EasyMock.replay(component, wrapper); + assertSame(wrapper, scopeContainer.getWrapper(component)); + assertSame(wrapper, scopeContainer.getAssociatedWrapper(component)); + EasyMock.verify(component, wrapper); +*/ + } + + public void testGetAssociatedInstanceNonExistent() throws Exception { +/* + URI id = URI.create("oops"); + EasyMock.expect(component.getUri()).andReturn(id); + EasyMock.replay(component); + + try { + scopeContainer.getAssociatedWrapper(component); + fail(); + } catch (TargetNotFoundException e) { + assertEquals(id.toString(), e.getMessage()); + EasyMock.verify(component); + } +*/ + } + + public void testRequestIsolation() throws Exception { +/* + IMocksControl control = EasyMock.createStrictControl(); + InstanceWrapper wrapper1 = control.createMock(InstanceWrapper.class); + InstanceWrapper wrapper2 = control.createMock(InstanceWrapper.class); + AtomicComponent component1 = control.createMock(AtomicComponent.class); + AtomicComponent component2 = control.createMock(AtomicComponent.class); + + EasyMock.expect(component1.createInstanceWrapper()).andReturn(wrapper1); + wrapper1.start(); + wrapper1.stop(); + EasyMock.expect(component2.createInstanceWrapper()).andReturn(wrapper2); + wrapper2.start(); + wrapper2.stop(); + control.replay(); + + scopeContainer.register(component1, null); + scopeContainer.register(component2, null); + scopeContainer.onEvent(new RequestStart(this)); + assertSame(wrapper1, scopeContainer.getWrapper(component1)); + scopeContainer.onEvent(new RequestEnd(this)); + scopeContainer.onEvent(new RequestStart(this)); + assertSame(wrapper2, scopeContainer.getWrapper(component2)); + scopeContainer.onEvent(new RequestEnd(this)); + control.verify(); +*/ + } + + public void testDestroyErrorMonitor() throws Exception { +/* + TargetDestructionException ex = new TargetDestructionException("oops", "again"); + monitor.destructionError(ex); + EasyMock.expect(component.createInstanceWrapper()).andReturn(wrapper); + wrapper.start(); + wrapper.stop(); + EasyMock.expectLastCall().andThrow(ex); + EasyMock.replay(component, wrapper, monitor); + + scopeContainer.onEvent(new RequestStart(this)); + assertSame(wrapper, scopeContainer.getWrapper(component)); + scopeContainer.onEvent(new RequestEnd(this)); + EasyMock.verify(component, wrapper, monitor); +*/ + } + + public void testDestroyOrder() throws Exception { +/* + IMocksControl control = EasyMock.createStrictControl(); + InstanceWrapper wrapper1 = control.createMock(InstanceWrapper.class); + InstanceWrapper wrapper2 = control.createMock(InstanceWrapper.class); + InstanceWrapper wrapper3 = control.createMock(InstanceWrapper.class); + AtomicComponent component1 = control.createMock(AtomicComponent.class); + AtomicComponent component2 = control.createMock(AtomicComponent.class); + AtomicComponent component3 = control.createMock(AtomicComponent.class); + + EasyMock.expect(component1.createInstanceWrapper()).andReturn(wrapper1); + wrapper1.start(); + EasyMock.expect(component2.createInstanceWrapper()).andReturn(wrapper2); + wrapper2.start(); + EasyMock.expect(component3.createInstanceWrapper()).andReturn(wrapper3); + wrapper3.start(); + wrapper3.stop(); + wrapper2.stop(); + wrapper1.stop(); + control.replay(); + + scopeContainer.register(component1, null); + scopeContainer.register(component2, null); + scopeContainer.register(component3, null); + scopeContainer.onEvent(new RequestStart(this)); + assertSame(wrapper1, scopeContainer.getWrapper(component1)); + assertSame(wrapper2, scopeContainer.getWrapper(component2)); + assertSame(wrapper3, scopeContainer.getWrapper(component3)); + scopeContainer.onEvent(new RequestEnd(this)); + control.verify(); +*/ + } + + protected void setUp() throws Exception { + super.setUp(); + component = EasyMock.createStrictMock(AtomicComponent.class); + wrapper = EasyMock.createStrictMock(InstanceWrapper.class); + monitor = EasyMock.createStrictMock(ScopeContainerMonitor.class); + scopeContainer = new RequestScopeContainer(monitor); + scopeContainer.start(); + + component.addListener(scopeContainer); + EasyMock.replay(component); + scopeContainer.register(component, null); + EasyMock.reset(component); + } +} diff --git a/sandbox/rfeng/minicore/src/test/java/org/apache/tuscany/core/component/scope/BasicStatelessScopeTestCase.java b/sandbox/rfeng/minicore/src/test/java/org/apache/tuscany/core/component/scope/BasicStatelessScopeTestCase.java new file mode 100644 index 0000000000..a7cab71ade --- /dev/null +++ b/sandbox/rfeng/minicore/src/test/java/org/apache/tuscany/core/component/scope/BasicStatelessScopeTestCase.java @@ -0,0 +1,89 @@ +/* + * 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; +import org.easymock.EasyMock; +import org.easymock.IMocksControl; + +import org.apache.tuscany.spi.component.AtomicComponent; +import org.apache.tuscany.spi.component.InstanceWrapper; +import org.apache.tuscany.spi.model.Scope; + +/** + * Unit tests for the composite scope container + * + * @version $Rev$ $Date$ + */ +public class BasicStatelessScopeTestCase<T> extends TestCase { + private StatelessScopeContainer<String> scopeContainer; + private String contextId; + private IMocksControl control; + private AtomicComponent<T> component; + private InstanceWrapper<T> wrapper; + + public void testCorrectScope() { + assertEquals(Scope.STATELESS, scopeContainer.getScope()); + } + + public void testInstanceCreation() throws Exception { + @SuppressWarnings("unchecked") + InstanceWrapper<T> wrapper2 = control.createMock(InstanceWrapper.class); + + EasyMock.expect(component.createInstanceWrapper()).andReturn(wrapper); + wrapper.start(); + EasyMock.expect(component.createInstanceWrapper()).andReturn(wrapper2); + wrapper2.start(); + control.replay(); + + assertSame(wrapper, scopeContainer.getWrapper(component, contextId)); + assertSame(wrapper2, scopeContainer.getWrapper(component, contextId)); + control.verify(); + } + + public void testGetAssociatedInstance() throws Exception { + control.replay(); + try { + // always throws an exception, which is the semantic for stateless implementations + scopeContainer.getAssociatedWrapper(component, contextId); + fail(); + } catch (UnsupportedOperationException e) { + // ok + } + control.verify(); + } + + public void testReturnWrapper() throws Exception { + wrapper.stop(); + control.replay(); + scopeContainer.returnWrapper(component, wrapper, contextId); + control.verify(); + } + + @SuppressWarnings("unchecked") + protected void setUp() throws Exception { + super.setUp(); + scopeContainer = new StatelessScopeContainer<String>(null); + contextId = "context"; + + control = EasyMock.createStrictControl(); + component = control.createMock(AtomicComponent.class); + wrapper = control.createMock(InstanceWrapper.class); + } +} diff --git a/sandbox/rfeng/minicore/src/test/java/org/apache/tuscany/core/component/scope/CompositeScopeInitDestroyErrorTestCase.java b/sandbox/rfeng/minicore/src/test/java/org/apache/tuscany/core/component/scope/CompositeScopeInitDestroyErrorTestCase.java new file mode 100644 index 0000000000..01c426e917 --- /dev/null +++ b/sandbox/rfeng/minicore/src/test/java/org/apache/tuscany/core/component/scope/CompositeScopeInitDestroyErrorTestCase.java @@ -0,0 +1,93 @@ +/* + * 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.net.URI; + +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 org.apache.tuscany.spi.component.InstanceWrapper; +import org.apache.tuscany.spi.component.TargetResolutionException; +import org.apache.tuscany.spi.component.GroupInitializationException; + +import junit.framework.TestCase; +import org.apache.tuscany.core.component.event.ComponentStart; +import org.apache.tuscany.core.component.event.ComponentStop; +import org.easymock.EasyMock; + +/** + * @version $Rev$ $Date$ + */ +public class CompositeScopeInitDestroyErrorTestCase extends TestCase { + private URI groupId; + + public void testInitializeError() throws Exception { + CompositeScopeContainer scope = new CompositeScopeContainer(null); + scope.start(); + + ObjectCreationException ex = new ObjectCreationException(""); + AtomicComponent component = EasyMock.createMock(AtomicComponent.class); + EasyMock.expect(component.createInstanceWrapper()).andThrow(ex); + EasyMock.expect(component.isEagerInit()).andStubReturn(true); + EasyMock.expect(component.getInitLevel()).andStubReturn(1); + EasyMock.replay(component); + scope.register(component, groupId); + try { + scope.startContext(groupId, groupId); + } catch (GroupInitializationException e) { + assertSame(ex, e.getCauses().get(0)); + } + EasyMock.verify(component); + } + + public void testDestroyErrorMonitor() throws Exception { + InstanceWrapper wrapper = EasyMock.createMock(InstanceWrapper.class); + wrapper.start(); + wrapper.stop(); + EasyMock.expectLastCall().andThrow(new TargetDestructionException("", "")); + EasyMock.replay(wrapper); + + 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.createInstanceWrapper()).andReturn(wrapper); + EasyMock.expect(component.isEagerInit()).andStubReturn(true); + EasyMock.expect(component.getInitLevel()).andStubReturn(1); + EasyMock.replay(component); + scope.register(component, groupId); + scope.startContext(groupId, groupId); + scope.stopContext(groupId); + EasyMock.verify(monitor); + EasyMock.verify(component); + EasyMock.verify(wrapper); + } + + + protected void setUp() throws Exception { + super.setUp(); + groupId = URI.create("composite"); + } +} diff --git a/sandbox/rfeng/minicore/src/test/java/org/apache/tuscany/core/component/scope/ConversationalScopeContainerDestroyOnExpirationTestCase.java b/sandbox/rfeng/minicore/src/test/java/org/apache/tuscany/core/component/scope/ConversationalScopeContainerDestroyOnExpirationTestCase.java new file mode 100644 index 0000000000..caf0c0cc5e --- /dev/null +++ b/sandbox/rfeng/minicore/src/test/java/org/apache/tuscany/core/component/scope/ConversationalScopeContainerDestroyOnExpirationTestCase.java @@ -0,0 +1,72 @@ +/* + * 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; + +import org.apache.tuscany.core.component.SimpleWorkContext; +import org.apache.tuscany.spi.component.AtomicComponent; +import org.apache.tuscany.spi.component.InstanceWrapper; +import org.apache.tuscany.spi.component.WorkContext; +import org.apache.tuscany.spi.event.RuntimeEventListener; +import org.apache.tuscany.spi.services.store.Store; +import org.apache.tuscany.spi.services.store.StoreExpirationEvent; +import org.easymock.EasyMock; +import org.easymock.IAnswer; + +/** + * @version $Rev$ $Date$ + */ +public class ConversationalScopeContainerDestroyOnExpirationTestCase extends TestCase { + private Store store; + private RuntimeEventListener listener; + private WorkContext context; + private InstanceWrapper wrapper; + 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.addListener(EasyMock.isA(RuntimeEventListener.class)); + EasyMock.expectLastCall().andStubAnswer(new IAnswer<Object>(){ + public Object answer() throws Throwable { + listener = (RuntimeEventListener) EasyMock.getCurrentArguments()[0]; + return null; + } + }); + wrapper.stop(); + EasyMock.replay(store); + EasyMock.replay(wrapper); + + new ConversationalScopeContainer(store, context, null); + listener.onEvent(new StoreExpirationEvent(this, component, wrapper)); + EasyMock.verify(store); + EasyMock.verify(wrapper); + } + + protected void setUp() throws Exception { + super.setUp(); + store = EasyMock.createMock(Store.class); + wrapper = EasyMock.createMock(InstanceWrapper.class); + component = EasyMock.createMock(AtomicComponent.class); + context = new SimpleWorkContext(); + } +} diff --git a/sandbox/rfeng/minicore/src/test/java/org/apache/tuscany/core/component/scope/ConversationalScopeContainerMaxAgeTestCase.java b/sandbox/rfeng/minicore/src/test/java/org/apache/tuscany/core/component/scope/ConversationalScopeContainerMaxAgeTestCase.java new file mode 100644 index 0000000000..e896b410d0 --- /dev/null +++ b/sandbox/rfeng/minicore/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 junit.framework.TestCase; + +import org.apache.tuscany.core.component.SimpleWorkContext; +import org.apache.tuscany.spi.component.AtomicComponent; +import org.apache.tuscany.spi.component.InstanceWrapper; +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 org.easymock.EasyMock; + +/** + * @version $Rev$ $Date$ + */ +public class ConversationalScopeContainerMaxAgeTestCase extends TestCase { + + private ScopeContainer container; + private WorkContext context; + private Store store; + private AtomicComponent component; + private InstanceWrapper wrapper; + + public void testMaxAgeUpdate() throws Exception { +/* + context.setIdentifier(Scope.CONVERSATION, "12345"); + assertSame(wrapper, container.getWrapper(component)); + EasyMock.verify(store); +*/ + } + + protected void setUp() throws Exception { + super.setUp(); + context = new SimpleWorkContext(); + component = EasyMock.createMock(AtomicComponent.class); + EasyMock.expect(component.getMaxIdleTime()).andReturn(-1L).atLeastOnce(); + EasyMock.expect(component.getMaxAge()).andReturn(600000L).atLeastOnce(); + EasyMock.replay(component); + + wrapper = EasyMock.createMock(InstanceWrapper.class); + + store = EasyMock.createMock(Store.class); + EasyMock.expect(store.readRecord(EasyMock.isA(SCAObject.class), EasyMock.isA(String.class))).andReturn(wrapper); + 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(); + } +} diff --git a/sandbox/rfeng/minicore/src/test/java/org/apache/tuscany/core/component/scope/ConversationalScopeContainerMaxIdleTimeTestCase.java b/sandbox/rfeng/minicore/src/test/java/org/apache/tuscany/core/component/scope/ConversationalScopeContainerMaxIdleTimeTestCase.java new file mode 100644 index 0000000000..9ac97bff32 --- /dev/null +++ b/sandbox/rfeng/minicore/src/test/java/org/apache/tuscany/core/component/scope/ConversationalScopeContainerMaxIdleTimeTestCase.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 junit.framework.TestCase; + +import org.apache.tuscany.core.component.SimpleWorkContext; +import org.apache.tuscany.spi.component.AtomicComponent; +import org.apache.tuscany.spi.component.InstanceWrapper; +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 org.easymock.EasyMock; + +/** + * @version $Rev$ $Date$ + */ +public class ConversationalScopeContainerMaxIdleTimeTestCase extends TestCase { + + private ScopeContainer container; + private WorkContext context; + private Store store; + private AtomicComponent component; + private InstanceWrapper wrapper; + + public void testMaxIdleTimeUpdate() throws Exception { +/* + context.setIdentifier(Scope.CONVERSATION, "12345"); + assertSame(wrapper, container.getWrapper(component)); + EasyMock.verify(store); +*/ + } + + protected void setUp() throws Exception { + super.setUp(); + wrapper = EasyMock.createMock(InstanceWrapper.class); + context = new SimpleWorkContext(); + component = EasyMock.createMock(AtomicComponent.class); + EasyMock.expect(component.getMaxIdleTime()).andStubReturn(600000L); + EasyMock.replay(component); + store = EasyMock.createMock(Store.class); + EasyMock.expect(store.readRecord(EasyMock.isA(SCAObject.class), EasyMock.isA(String.class))).andReturn(wrapper); + store.addListener(EasyMock.isA(RuntimeEventListener.class)); + store.updateRecord(EasyMock.isA(SCAObject.class), + EasyMock.isA(String.class), + EasyMock.eq(wrapper), + 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(); + } + +} diff --git a/sandbox/rfeng/minicore/src/test/java/org/apache/tuscany/core/component/scope/ConversationalScopeContainerPersistenceTestCase.java b/sandbox/rfeng/minicore/src/test/java/org/apache/tuscany/core/component/scope/ConversationalScopeContainerPersistenceTestCase.java new file mode 100644 index 0000000000..a8960c4575 --- /dev/null +++ b/sandbox/rfeng/minicore/src/test/java/org/apache/tuscany/core/component/scope/ConversationalScopeContainerPersistenceTestCase.java @@ -0,0 +1,189 @@ +/* + * 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; + +import org.apache.tuscany.core.component.SimpleWorkContext; +import org.apache.tuscany.core.services.store.memory.MemoryStore; +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 org.easymock.EasyMock; + +/** + * @version $Rev$ $Date$ + */ +public abstract class ConversationalScopeContainerPersistenceTestCase extends TestCase { + private ConversationalScopeContainer container; + private WorkContext context; + + public void testNotYetPersistedInMemory() throws Exception { +/* + InstanceWrapper wrapper = EasyMock.createMock(InstanceWrapper.class); + wrapper.start(); + EasyMock.replay(wrapper); + + String id = UUID.randomUUID().toString(); + context.setIdentifier(Scope.CONVERSATION, id); + AtomicComponent component = EasyMock.createMock(AtomicComponent.class); + component.addListener(EasyMock.eq(container)); + EasyMock.expect(component.createInstanceWrapper()).andReturn(wrapper); + EasyMock.expect(component.getMaxAge()).andReturn(600000L).atLeastOnce(); + EasyMock.replay(component); + + container.register(component, null); + assertSame(wrapper, container.getWrapper(component)); + EasyMock.verify(component); + EasyMock.verify(wrapper); +*/ + } + + 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, null); + InstanceWrapper fooWrapper = EasyMock.createMock(InstanceWrapper.class); + InstanceWrapper fooWrapper2 = EasyMock.createMock(InstanceWrapper.class); + container.persistNew(component, id, fooWrapper, System.currentTimeMillis() + 100000); + assertEquals(fooWrapper, container.getWrapper(component)); + container.persistNew(component, id2, fooWrapper2, System.currentTimeMillis() + 100000); + context.setIdentifier(Scope.CONVERSATION, id2); + assertEquals(fooWrapper2, container.getWrapper(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, null); + InstanceWrapper fooWrapper = EasyMock.createMock(InstanceWrapper.class); + container.persistNew(component, id, fooWrapper, System.currentTimeMillis() + 100000); + assertEquals(fooWrapper, container.getWrapper(component)); + container.persist(component, id, fooWrapper, System.currentTimeMillis() + 100000); + assertEquals(fooWrapper, container.getWrapper(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.getUri()).andReturn(URI.create("foo")).atLeastOnce(); + EasyMock.replay(component); + container.register(component, null); + InstanceWrapper fooWrapper = EasyMock.createMock(InstanceWrapper.class); + container.persistNew(component, id, fooWrapper, System.currentTimeMillis() + 100000); + assertEquals(fooWrapper, container.getWrapper(component)); + container.remove(component); + try { + container.getAssociatedWrapper(component); + fail(); + } catch (TargetNotFoundException e) { + //expected + } + EasyMock.verify(component); +*/ + } + + public void testRecreateAfterRemoveInMemory() throws Exception { +/* + InstanceWrapper wrapper = EasyMock.createMock(InstanceWrapper.class); + wrapper.start(); + EasyMock.replay(wrapper); + + 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(); + EasyMock.expect(component.getMaxIdleTime()).andReturn(-1L).atLeastOnce(); + EasyMock.expect(component.createInstanceWrapper()).andReturn(wrapper); + EasyMock.replay(component); + + container.register(component, null); + InstanceWrapper fooWrapper = EasyMock.createMock(InstanceWrapper.class); + container.persistNew(component, id, fooWrapper, System.currentTimeMillis() + 100000); + assertEquals(fooWrapper, container.getWrapper(component)); + container.remove(component); + assertSame(wrapper, container.getWrapper(component)); + 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.getUri()).andReturn(URI.create("foo")).atLeastOnce(); + EasyMock.expect(component.getMaxIdleTime()).andReturn(-1L).atLeastOnce(); + component.addListener(EasyMock.eq(container)); + EasyMock.replay(component); + container.register(component, null); + + InstanceWrapper fooWrapper = EasyMock.createMock(InstanceWrapper.class); + container.persistNew(component, id, fooWrapper, System.currentTimeMillis() + 100000); + assertEquals(fooWrapper, container.getAssociatedWrapper(component)); + assertEquals(fooWrapper, container.getAssociatedWrapper(component)); + context.setIdentifier(Scope.CONVERSATION, id2); + try { + container.getAssociatedWrapper(component); + fail(); + } catch (TargetNotFoundException e) { + //expected + } + EasyMock.verify(component); +*/ + } + + protected void setUp() throws Exception { + super.setUp(); + context = new SimpleWorkContext(); + 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(); + } +} diff --git a/sandbox/rfeng/minicore/src/test/java/org/apache/tuscany/core/component/scope/InstanceWrapperBaseTestCase.java b/sandbox/rfeng/minicore/src/test/java/org/apache/tuscany/core/component/scope/InstanceWrapperBaseTestCase.java new file mode 100644 index 0000000000..c583ff19c6 --- /dev/null +++ b/sandbox/rfeng/minicore/src/test/java/org/apache/tuscany/core/component/scope/InstanceWrapperBaseTestCase.java @@ -0,0 +1,64 @@ +/* + * 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 InstanceWrapperBaseTestCase extends TestCase { + private static final Object INSTANCE = new Object(); + private InstanceWrapperBase<Object> wrapper; + + public void testLifecycle() throws Exception { + assertFalse(wrapper.isStarted()); + try { + wrapper.getInstance(); + fail(); + } catch (AssertionError e) { + // expected + } + wrapper.start(); + assertTrue(wrapper.isStarted()); + assertSame(INSTANCE, wrapper.getInstance()); + wrapper.stop(); + assertFalse(wrapper.isStarted()); + try { + wrapper.getInstance(); + fail(); + } catch (AssertionError e) { + // expected + } + } + + public void testNullCheck() { + try { + new InstanceWrapperBase<Object>(null); + fail(); + } catch (AssertionError e) { + // expected + } + } + + protected void setUp() throws Exception { + super.setUp(); + wrapper = new InstanceWrapperBase<Object>(INSTANCE); + } +} diff --git a/sandbox/rfeng/minicore/src/test/java/org/apache/tuscany/core/component/scope/WorkContextTestCase.java b/sandbox/rfeng/minicore/src/test/java/org/apache/tuscany/core/component/scope/WorkContextTestCase.java new file mode 100644 index 0000000000..94feca05be --- /dev/null +++ b/sandbox/rfeng/minicore/src/test/java/org/apache/tuscany/core/component/scope/WorkContextTestCase.java @@ -0,0 +1,166 @@ +/* + * 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.Component; +import org.apache.tuscany.spi.component.WorkContext; + +import junit.framework.TestCase; +import org.apache.tuscany.core.component.SimpleWorkContext; +import org.easymock.EasyMock; + +/** + * @version $Rev$ $Date$ + */ +public class WorkContextTestCase extends TestCase { + + public void testSetCurrentAtomicComponent() throws Exception { + WorkContext ctx = new SimpleWorkContext(); + 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 SimpleWorkContext(); + assertNull(ctx.getCurrentAtomicComponent()); + } + + public void testIndentifier() throws Exception { + WorkContext ctx = new SimpleWorkContext(); + Object id = new Object(); + ctx.setIdentifier(this, id); + assertEquals(id, ctx.getIdentifier(this)); + } + + public void testClearIndentifier() throws Exception { + WorkContext ctx = new SimpleWorkContext(); + Object id = new Object(); + ctx.setIdentifier(this, id); + ctx.clearIdentifier(this); + assertNull(ctx.getIdentifier(this)); + } + + public void testClearIndentifiers() throws Exception { + WorkContext ctx = new SimpleWorkContext(); + 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 SimpleWorkContext(); + ctx.clearIdentifier(this); + } + + public void testNullIndentifier() throws Exception { + WorkContext ctx = new SimpleWorkContext(); + Object id = new Object(); + ctx.setIdentifier(this, id); + ctx.clearIdentifier(null); + assertEquals(id, ctx.getIdentifier(this)); + } + + public void testNoIndentifier() throws Exception { + WorkContext ctx = new SimpleWorkContext(); + assertNull(ctx.getIdentifier(this)); + } + + public void testSetGetCorrelationId() { + WorkContext context = new SimpleWorkContext(); + context.setCorrelationId("msg-005"); + assertEquals(context.getCorrelationId(), "msg-005"); + context.setCorrelationId(null); + assertNull(context.getCorrelationId()); + } + + public void testSetGetCorrelationIdInNewThread() throws InterruptedException { + WorkContext context = new SimpleWorkContext(); + context.setCorrelationId("msg-005"); + assertEquals(context.getCorrelationId(), "msg-005"); + context.setIdentifier("TX", "002"); + ChildThread t = new ChildThread(context); + t.start(); + t.join(); + assertTrue(t.passed); + context.setCorrelationId(null); + assertNull(context.getCorrelationId()); + } + + public void testCurrentAtomicComponentDoesNotPropagateToChildThread() throws InterruptedException { + // NOTE should behaviour be to propagate? + WorkContext context = new SimpleWorkContext(); + context.setCurrentAtomicComponent(EasyMock.createNiceMock(AtomicComponent.class)); + TestCurrentAtomicComponentChildThread t = new TestCurrentAtomicComponentChildThread(context); + t.start(); + t.join(); + assertTrue(t.passed); + context.setCurrentAtomicComponent(null); + assertNull(context.getCurrentAtomicComponent()); + } + + 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.getCorrelationId()); + 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; + } + } + + } + + +} |