From bd4be89d2a0132c1286c4be993292a460fa35442 Mon Sep 17 00:00:00 2001 From: antelder Date: Fri, 30 Oct 2009 09:02:28 +0000 Subject: Tag 2.0-M4 RC1 git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@831233 13f79535-47bb-0310-9956-ffa450edef68 --- .../src/test/java/itest/ClientNode.java | 92 +++++++ .../src/test/java/itest/ServiceNode.java | 58 ++++ .../test/java/itest/StopStartNodesTestCase.java | 102 +++++++ .../src/test/java/itest/TestCaseRunner.java | 292 +++++++++++++++++++++ .../src/test/java/itest/TwoNodesTestCase.java | 105 ++++++++ 5 files changed, 649 insertions(+) create mode 100644 tags/java/sca/2.0-M4-RC1/itest/nodes/two-nodes-test/src/test/java/itest/ClientNode.java create mode 100644 tags/java/sca/2.0-M4-RC1/itest/nodes/two-nodes-test/src/test/java/itest/ServiceNode.java create mode 100644 tags/java/sca/2.0-M4-RC1/itest/nodes/two-nodes-test/src/test/java/itest/StopStartNodesTestCase.java create mode 100644 tags/java/sca/2.0-M4-RC1/itest/nodes/two-nodes-test/src/test/java/itest/TestCaseRunner.java create mode 100644 tags/java/sca/2.0-M4-RC1/itest/nodes/two-nodes-test/src/test/java/itest/TwoNodesTestCase.java (limited to 'tags/java/sca/2.0-M4-RC1/itest/nodes/two-nodes-test/src/test') diff --git a/tags/java/sca/2.0-M4-RC1/itest/nodes/two-nodes-test/src/test/java/itest/ClientNode.java b/tags/java/sca/2.0-M4-RC1/itest/nodes/two-nodes-test/src/test/java/itest/ClientNode.java new file mode 100644 index 0000000000..5f9453a5d6 --- /dev/null +++ b/tags/java/sca/2.0-M4-RC1/itest/nodes/two-nodes-test/src/test/java/itest/ClientNode.java @@ -0,0 +1,92 @@ +/* + * 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 itest; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import itest.nodes.Helloworld; + +import java.io.File; + +import javax.xml.namespace.QName; + +import org.apache.tuscany.sca.node.Node; +import org.apache.tuscany.sca.node.NodeFactory; +import org.apache.tuscany.sca.node.configuration.NodeConfiguration; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Ignore; +import org.junit.Test; +import org.oasisopen.sca.client.SCAClient; + +/** + * This shows how to test the Calculator service component. + */ +public class ClientNode { + private final static String SCA11_NS = "http://docs.oasis-open.org/ns/opencsa/sca/200903"; + private static Node clientNode; + private static TestCaseRunner runner; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + runner = new TestCaseRunner(ServiceNode.class); + runner.beforeClass(); + System.setProperty("org.apache.tuscany.sca.contribution.processor.ValidationSchemaExtensionPoint.enabled", + "false"); + NodeFactory factory = NodeFactory.newInstance(); + NodeConfiguration conf = + factory.createNodeConfiguration().setURI("clientNode"). + addBinding(new QName(SCA11_NS, "binding.sca"), "http://localhost:8085/sca https://localhost:9085/sca") + .addBinding(new QName(SCA11_NS, "binding.ws"), "http://localhost:8086/ws") + .addContribution("client", new File("../helloworld-client/target/classes").toURI().toString()); + clientNode = factory.createNode(conf).start(); + Thread.sleep(1000); + } + + @Test + public void testCalculator() throws Exception { + + Helloworld client = clientNode.getService(Helloworld.class, "HelloworldClient"); + assertNotNull(client); + assertEquals("Hi Hello Petra", client.sayHello("Petra")); + } + + @Test + @Ignore("SCAClient needs to leverage the EndpointRegistry to invoke services that are not hosted on the local node") + public void testCalculatorClientAPI() throws Exception { + Helloworld service = SCAClient.getService(Helloworld.class, "HelloworldService"); + assertNotNull(service); + assertEquals("Hello Petra", service.sayHello("Petra")); + + Helloworld client = SCAClient.getService(Helloworld.class, "HelloworldClient"); + assertNotNull(client); + assertEquals("Hi Hello Petra", client.sayHello("Petra")); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + if (clientNode != null) { + clientNode.stop(); + } + if (runner != null) { + runner.afterClass(); + } + } +} diff --git a/tags/java/sca/2.0-M4-RC1/itest/nodes/two-nodes-test/src/test/java/itest/ServiceNode.java b/tags/java/sca/2.0-M4-RC1/itest/nodes/two-nodes-test/src/test/java/itest/ServiceNode.java new file mode 100644 index 0000000000..b413825106 --- /dev/null +++ b/tags/java/sca/2.0-M4-RC1/itest/nodes/two-nodes-test/src/test/java/itest/ServiceNode.java @@ -0,0 +1,58 @@ +/* + * 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 itest; + +import java.io.File; + +import javax.xml.namespace.QName; + +import org.apache.tuscany.sca.node.Node; +import org.apache.tuscany.sca.node.NodeFactory; +import org.apache.tuscany.sca.node.configuration.NodeConfiguration; +import org.junit.AfterClass; +import org.junit.BeforeClass; + +/** + * This shows how to test the Calculator service component. + */ +public class ServiceNode { + private final static String SCA11_NS = "http://docs.oasis-open.org/ns/opencsa/sca/200903"; + private static Node serviceNode; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + System.setProperty("org.apache.tuscany.sca.contribution.processor.ValidationSchemaExtensionPoint.enabled", + "false"); + NodeFactory factory = NodeFactory.newInstance(); + NodeConfiguration conf = + factory.createNodeConfiguration().setURI("serviceNode") + .addBinding(new QName(SCA11_NS, "binding.sca"), "http://localhost:8087/sca") + .addBinding(new QName(SCA11_NS, "binding.ws"), "http://localhost:8088/ws") + .addContribution("service", new File("../helloworld-service/target/classes").toURI().toString()); + serviceNode = factory.createNode(conf).start(); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + if (serviceNode != null) { + serviceNode.stop(); + } + } +} diff --git a/tags/java/sca/2.0-M4-RC1/itest/nodes/two-nodes-test/src/test/java/itest/StopStartNodesTestCase.java b/tags/java/sca/2.0-M4-RC1/itest/nodes/two-nodes-test/src/test/java/itest/StopStartNodesTestCase.java new file mode 100644 index 0000000000..4a41ae8133 --- /dev/null +++ b/tags/java/sca/2.0-M4-RC1/itest/nodes/two-nodes-test/src/test/java/itest/StopStartNodesTestCase.java @@ -0,0 +1,102 @@ +/* + * 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 itest; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.fail; +import itest.nodes.Helloworld; + +import java.io.File; + +import org.apache.tuscany.sca.node.Contribution; +import org.apache.tuscany.sca.node.Node; +import org.apache.tuscany.sca.node.NodeFactory; +import org.junit.AfterClass; +import org.junit.Test; + +/** + * Tests using two nodes and stopping and restarting a service node. + */ +public class StopStartNodesTestCase { + + private static Node serviceNode; + private static Node clientNode; + + @Test + public void testCalculator() throws Exception { + NodeFactory factory = NodeFactory.newInstance(); + serviceNode = factory.createNode(new Contribution("service", getJar("../helloworld-service/target"))); + serviceNode.start(); + clientNode = factory.createNode(new Contribution("client", getJar("../helloworld-client/target"))); + clientNode.start(); + + Helloworld service = serviceNode.getService(Helloworld.class, "HelloworldService"); + assertNotNull(service); + assertEquals("Hello Petra", service.sayHello("Petra")); + + Helloworld client = clientNode.getService(Helloworld.class, "HelloworldClient"); + assertNotNull(client); + assertEquals("Hi Hello Petra", client.sayHello("Petra")); + + serviceNode.stop(); + + client = clientNode.getService(Helloworld.class, "HelloworldClient"); + assertNotNull(client); + try { + client.sayHello("Petra"); + fail(); + } catch (Exception e) { + // expected + // TODO: better exception than NPE + } + + serviceNode = factory.createNode(new Contribution("service", getJar("../helloworld-service/target"))); + serviceNode.start(); + + client = clientNode.getService(Helloworld.class, "HelloworldClient"); + assertNotNull(client); + assertEquals("Hi Hello Petra", client.sayHello("Petra")); + } + + /** + * Get the jar in the target folder without being dependent on the version name to + * make tuscany releases easier + */ + private static String getJar(String targetDirectory) { + File f = new File(targetDirectory); + for (File file : f.listFiles()) { + if (file.getName().endsWith(".jar")) { + return file.toURI().toString(); + } + } + throw new IllegalStateException("Can't find jar in: " + targetDirectory); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + if (serviceNode != null) { + serviceNode.stop(); + } + if (clientNode != null) { + clientNode.stop(); + } + } +} diff --git a/tags/java/sca/2.0-M4-RC1/itest/nodes/two-nodes-test/src/test/java/itest/TestCaseRunner.java b/tags/java/sca/2.0-M4-RC1/itest/nodes/two-nodes-test/src/test/java/itest/TestCaseRunner.java new file mode 100644 index 0000000000..7dfa6e66cc --- /dev/null +++ b/tags/java/sca/2.0-M4-RC1/itest/nodes/two-nodes-test/src/test/java/itest/TestCaseRunner.java @@ -0,0 +1,292 @@ +/* + * 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 itest; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Constructor; +import java.lang.reflect.Method; +import java.net.URL; +import java.net.URLClassLoader; +import java.security.AccessController; +import java.security.PrivilegedAction; + +/** + * A helper class that can be used to run an SCA JUnit test case. The test case will run in an isolated class loader. + * + * @version $Rev$ $Date$ + */ +public class TestCaseRunner { + + private ClassLoader classLoader; + private Class testSuiteClass; + private Object testSuite; + private Class testResultClass; + private Class testCaseClass; + private Object testCase; + + private Class beforeAnnotation; + private Class beforeClassAnnotation; + private Class afterAnnotation; + private Class afterClassAnnotation; + private Class junit4AdapterClass; + private Class junit3TestCaseClass; + + /** + * Constructs a new TestCase runner. + * + * @param testClass + */ + public TestCaseRunner(Class testClass) { + try { + ClassLoader tccl = Thread.currentThread().getContextClassLoader(); + classLoader = testClass.getClassLoader(); + if (classLoader instanceof URLClassLoader) { + URL[] urls = ((URLClassLoader)classLoader).getURLs(); + classLoader = new URLClassLoader(urls, classLoader.getParent()); + } else if (classLoader == tccl || classLoader.getParent() == tccl) { + classLoader = new URLClassLoader(new URL[0], classLoader); + } else { + classLoader = tccl; + } + + try { + // Thread.currentThread().setContextClassLoader(classLoader); + // Allow privileged access to set class loader. Requires RuntimePermission + // setContextClassLoader in security policy. + final ClassLoader finalClassLoader = classLoader; + AccessController.doPrivileged(new PrivilegedAction() { + public Object run() { + Thread.currentThread().setContextClassLoader(finalClassLoader); + return null; + } + }); + + testCaseClass = Class.forName(testClass.getName(), true, classLoader); + testCase = testCaseClass.newInstance(); + ClassLoader testClassLoader = testCaseClass.getClassLoader(); + + junit3TestCaseClass = Class.forName("junit.framework.TestCase", true, testClassLoader); + + testSuiteClass = Class.forName("junit.framework.TestSuite", true, testClassLoader); + Constructor testSuiteConstructor = testSuiteClass.getConstructor(Class.class); + testSuite = testSuiteConstructor.newInstance(testCaseClass); + + testResultClass = Class.forName("junit.framework.TestResult", true, testClassLoader); + + try { + beforeAnnotation = Class.forName("org.junit.Before", true, testClassLoader); + afterAnnotation = Class.forName("org.junit.After", true, testClassLoader); + beforeClassAnnotation = Class.forName("org.junit.BeforeClass", true, testClassLoader); + afterClassAnnotation = Class.forName("org.junit.AfterClass", true, testClassLoader); + junit4AdapterClass = Class.forName("junit.framework.JUnit4TestAdapter", true, testClassLoader); + } catch (Exception e) { + // Unexpected + throw new AssertionError(e); + } + } catch (Throwable e) { + e.printStackTrace(); + } finally { + // Thread.currentThread().setContextClassLoader(tccl); + // Allow privileged access to set class loader. Requires RuntimePermission + // setContextClassLoader in security policy. + final ClassLoader finaltccl = tccl; + AccessController.doPrivileged(new PrivilegedAction() { + public Object run() { + Thread.currentThread().setContextClassLoader(finaltccl); + return null; + } + }); + } + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + /** + * Run the test case + */ + public void run() { + ClassLoader tccl = Thread.currentThread().getContextClassLoader(); + try { + // Thread.currentThread().setContextClassLoader(classLoader); + // Allow privileged access to set class loader. Requires RuntimePermission + // setContextClassLoader in security policy. + final ClassLoader finalClassLoader = classLoader; + AccessController.doPrivileged(new PrivilegedAction() { + public Object run() { + Thread.currentThread().setContextClassLoader(finalClassLoader); + return null; + } + }); + + if (junit3TestCaseClass.isAssignableFrom(testCaseClass)) { + Object testResult = testResultClass.newInstance(); + Method runMethod = testSuiteClass.getMethod("run", testResultClass); + runMethod.invoke(testSuite, testResult); + } else { + Object junit4Adapter = junit4AdapterClass.getConstructor(Class.class).newInstance(testCaseClass); + Object testResult = testResultClass.newInstance(); + Method runMethod = junit4AdapterClass.getMethod("run", testResultClass); + runMethod.invoke(junit4Adapter, testResult); + } + } catch (Exception e) { + throw new RuntimeException(e); + } finally { + // Thread.currentThread().setContextClassLoader(tccl); + // Allow privileged access to set class loader. Requires RuntimePermission + // setContextClassLoader in security policy. + final ClassLoader finaltccl = tccl; + AccessController.doPrivileged(new PrivilegedAction() { + public Object run() { + Thread.currentThread().setContextClassLoader(finaltccl); + return null; + } + }); + } + } + + /** + * Invoke the setUp method + */ + public void setUp() { + execute("setUp"); + } + + /** + * Invoke the before methods + */ + public void before() { + execute(beforeAnnotation); + } + + /** + * Invoke the beforeClass methods + */ + public void beforeClass() { + execute(beforeClassAnnotation); + } + + /** + * Invoke the tearDown method + */ + public void tearDown() { + execute("tearDown"); + } + + /** + * Invoke the after methods + */ + public void after() { + execute(afterAnnotation); + } + + /** + * Invoke the afterClass methods + */ + public void afterClass() { + execute(afterClassAnnotation); + } + + /** + * Invoke the specified test method. + */ + public void run(String methodName) { + execute(methodName); + } + + /** + * Invoke the methods annotated with the specified annotation. + */ + private void execute(Class annotationClass) { + if (annotationClass == null) { + throw new RuntimeException(new NoSuchMethodException()); + } + ClassLoader tccl = Thread.currentThread().getContextClassLoader(); + try { + // Thread.currentThread().setContextClassLoader(classLoader); + // Allow privileged access to set class loader. Requires RuntimePermission + // setContextClassLoader in security policy. + final ClassLoader finalClassLoader = classLoader; + AccessController.doPrivileged(new PrivilegedAction() { + public Object run() { + Thread.currentThread().setContextClassLoader(finalClassLoader); + return null; + } + }); + + for (Method method : testCaseClass.getDeclaredMethods()) { + for (Annotation annotation : method.getAnnotations()) { + if (annotation.annotationType() == annotationClass) { + method.invoke(testCase); + } + } + } + } catch (Exception e) { + throw new RuntimeException(e); + } finally { + // Thread.currentThread().setContextClassLoader(tccl); + // Allow privileged access to set class loader. Requires RuntimePermission + // setContextClassLoader in security policy. + final ClassLoader finaltccl = tccl; + AccessController.doPrivileged(new PrivilegedAction() { + public Object run() { + Thread.currentThread().setContextClassLoader(finaltccl); + return null; + } + }); + } + } + + /** + * Invoke the specified method + */ + private void execute(String methodName) { + ClassLoader tccl = Thread.currentThread().getContextClassLoader(); + try { + // Thread.currentThread().setContextClassLoader(classLoader); + // Allow privileged access to set class loader. Requires RuntimePermission + // setContextClassLoader in security policy. + final ClassLoader finalClassLoader = classLoader; + AccessController.doPrivileged(new PrivilegedAction() { + public Object run() { + Thread.currentThread().setContextClassLoader(finalClassLoader); + return null; + } + }); + Method setUpMethod = testCaseClass.getDeclaredMethod(methodName); + setUpMethod.setAccessible(true); + setUpMethod.invoke(testCase); + } catch (Exception e) { + throw new RuntimeException(e); + } finally { + // Thread.currentThread().setContextClassLoader(tccl); + // Allow privileged access to set class loader. Requires RuntimePermission + // setContextClassLoader in security policy. + final ClassLoader finaltccl = tccl; + AccessController.doPrivileged(new PrivilegedAction() { + public Object run() { + Thread.currentThread().setContextClassLoader(finaltccl); + return null; + } + }); + } + } + +} + diff --git a/tags/java/sca/2.0-M4-RC1/itest/nodes/two-nodes-test/src/test/java/itest/TwoNodesTestCase.java b/tags/java/sca/2.0-M4-RC1/itest/nodes/two-nodes-test/src/test/java/itest/TwoNodesTestCase.java new file mode 100644 index 0000000000..769aad1940 --- /dev/null +++ b/tags/java/sca/2.0-M4-RC1/itest/nodes/two-nodes-test/src/test/java/itest/TwoNodesTestCase.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 itest; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import itest.nodes.Helloworld; + +import java.io.File; + +import org.apache.tuscany.sca.node.Node; +import org.apache.tuscany.sca.node.NodeFactory; +import org.apache.tuscany.sca.node.configuration.NodeConfiguration; +import org.junit.AfterClass; +import org.junit.BeforeClass; +import org.junit.Test; +import org.oasisopen.sca.client.SCAClient; + +/** + * This shows how to test the Calculator service component. + */ +public class TwoNodesTestCase { + + private static Node serviceNode; + private static Node clientNode; + + @BeforeClass + public static void setUpBeforeClass() throws Exception { + System.setProperty("org.apache.tuscany.sca.contribution.processor.ValidationSchemaExtensionPoint.enabled", + "false"); + NodeFactory factory = NodeFactory.newInstance(); + NodeConfiguration configuration1 = + factory.createNodeConfiguration().setURI("serviceNode") + .addContribution("service", getJar("../helloworld-service/target")); + serviceNode = factory.createNode(configuration1).start(); + + NodeConfiguration configuration2 = + factory.createNodeConfiguration().setURI("clientNode") + .addContribution("client", getJar("../helloworld-client/target")); + clientNode = factory.createNode(configuration2).start(); + } + + /** + * Get the jar in the target folder without being dependent on the version name to + * make tuscany releases easier + */ + private static String getJar(String targetDirectory) { + File f = new File(targetDirectory); + for (File file : f.listFiles()) { + if (file.getName().endsWith(".jar")) { + return file.toURI().toString(); + } + } + throw new IllegalStateException("Can't find jar in: " + targetDirectory); + } + + @Test + public void testCalculator() throws Exception { + Helloworld service = serviceNode.getService(Helloworld.class, "HelloworldService"); + assertNotNull(service); + assertEquals("Hello Petra", service.sayHello("Petra")); + + Helloworld client = clientNode.getService(Helloworld.class, "HelloworldClient"); + assertNotNull(client); + assertEquals("Hi Hello Petra", client.sayHello("Petra")); + } + + @Test + public void testCalculatorClientAPI() throws Exception { + Helloworld service = SCAClient.getService(Helloworld.class, "HelloworldService"); + assertNotNull(service); + assertEquals("Hello Petra", service.sayHello("Petra")); + + Helloworld client = SCAClient.getService(Helloworld.class, "HelloworldClient"); + assertNotNull(client); + assertEquals("Hi Hello Petra", client.sayHello("Petra")); + } + + @AfterClass + public static void tearDownAfterClass() throws Exception { + if (serviceNode != null) { + serviceNode.stop(); + } + if (clientNode != null) { + clientNode.stop(); + } + } +} -- cgit v1.2.3