summaryrefslogtreecommitdiffstats
path: root/sca-java-2.x/trunk/samples/implementation-extension/src/test
diff options
context:
space:
mode:
authorjsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68>2010-09-13 18:08:39 +0000
committerjsdelfino <jsdelfino@13f79535-47bb-0310-9956-ffa450edef68>2010-09-13 18:08:39 +0000
commit10450bdd620b7d622d38a899daf602277e629782 (patch)
treed740fee0987387a61a4e3f852f73996a543d1f4f /sca-java-2.x/trunk/samples/implementation-extension/src/test
parentd587bce05e96fde5fac8aa5ca76d997ce34bd0d0 (diff)
Cleanup EmbedUtil a bit. Use a builder pattern and closures to pass a node factory context around instead of being kept in a static. Change the DOM util to use a similar builder pattern as well.
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@996622 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sca-java-2.x/trunk/samples/implementation-extension/src/test')
-rw-r--r--sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/Xutil.java43
-rw-r--r--sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/impl/EmbedTestCase.java24
2 files changed, 28 insertions, 39 deletions
diff --git a/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/Xutil.java b/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/Xutil.java
index bdad745b6f..ce89f9dd8c 100644
--- a/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/Xutil.java
+++ b/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/Xutil.java
@@ -45,18 +45,15 @@ import org.w3c.dom.NodeList;
* Just for fun, a little bit of magic code and utility functions to help work with XML DOM.
*/
class Xutil {
- static class NodeBuilder {
- String ns;
- String name;
- NodeBuilder[] children;
- String text;
+ interface NodeBuilder {
+ Node build(Document doc);
}
/**
* Convert a name and a list of children to a document element.
*/
static Element xdom(String ns, String name, final NodeBuilder... nodes) {
- return (Element)node(elem(ns, name, nodes), db.newDocument());
+ return (Element)elem(ns, name, nodes).build(db.newDocument());
}
/**
@@ -64,21 +61,17 @@ class Xutil {
*/
static NodeBuilder elem(final String uri, final String n, final NodeBuilder... nodes) {
return new NodeBuilder() {
- {
- this.ns = uri;
- this.name = n;
- this.children = nodes;
+ public Node build(Document doc) {
+ final Element e = doc.createElementNS(uri, n);
+ for(final NodeBuilder n: nodes)
+ e.appendChild(n.build(doc));
+ return e;
}
};
}
static NodeBuilder elem(final String n, final NodeBuilder... nodes) {
- return new NodeBuilder() {
- {
- this.name = n;
- this.children = nodes;
- }
- };
+ return elem(null, n, nodes);
}
/**
@@ -86,8 +79,8 @@ class Xutil {
*/
static NodeBuilder text(final String t) {
return new NodeBuilder() {
- {
- this.text = t;
+ public Node build(final Document doc) {
+ return doc.createTextNode(t);
}
};
}
@@ -102,18 +95,6 @@ class Xutil {
}
}
- private static Element link(final Element e, final Document doc, final NodeBuilder... nodes) {
- for(final NodeBuilder c: nodes)
- e.appendChild(node(c, doc));
- return e;
- }
-
- private static Node node(NodeBuilder node, Document doc) {
- if(node.text != null)
- return doc.createTextNode(node.text);
- return link(doc.createElementNS(node.ns, node.name), doc, node.children);
- }
-
/**
* Convert an element to XML.
*/
@@ -144,7 +125,7 @@ class Xutil {
}
/**
- * A pure Java FP-style alternative to xpath.
+ * A pure Java FP-style alternative to xpath for DOM.
*/
interface Mapper<T> {
T map(final Element e);
diff --git a/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/impl/EmbedTestCase.java b/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/impl/EmbedTestCase.java
index f30b5c9853..406dbabee0 100644
--- a/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/impl/EmbedTestCase.java
+++ b/sca-java-2.x/trunk/samples/implementation-extension/src/test/java/sample/impl/EmbedTestCase.java
@@ -21,10 +21,12 @@ package sample.impl;
import static java.lang.System.out;
import static org.junit.Assert.assertEquals;
+import static sample.impl.EmbedUtil.build;
import static sample.impl.EmbedUtil.component;
import static sample.impl.EmbedUtil.composite;
import static sample.impl.EmbedUtil.contrib;
import static sample.impl.EmbedUtil.deploy;
+import static sample.impl.EmbedUtil.embedContext;
import static sample.impl.EmbedUtil.extensionPoints;
import static sample.impl.EmbedUtil.implementation;
import static sample.impl.EmbedUtil.node;
@@ -38,6 +40,7 @@ import org.apache.tuscany.sca.assembly.Composite;
import org.apache.tuscany.sca.contribution.Contribution;
import org.apache.tuscany.sca.interfacedef.wsdl.WSDLInterface;
import org.apache.tuscany.sca.node.Node;
+import org.apache.tuscany.sca.node.NodeFactory;
import org.apache.tuscany.sca.provider.ImplementationProvider;
import org.apache.tuscany.sca.provider.ProviderFactory;
import org.apache.tuscany.sca.runtime.RuntimeComponent;
@@ -61,23 +64,28 @@ import sample.WelloTest;
* @version $Rev$ $Date$
*/
public class EmbedTestCase {
+ static NodeFactory nf;
+ static EmbedUtil.Context ec;
static Node node;
+ @SuppressWarnings("unchecked")
@BeforeClass
public static void setUp() throws Exception {
+ nf = NodeFactory.newInstance();
+ ec = embedContext(nf);
// Load the test WSDL definitions (could also construct the WSDL
// and XSD models in code but that'd be quite painful, so just
// load them from XML for now)
- final Contribution contrib = contrib("test", here());
- WSDLInterface Hello_wsdl = wsdli("Hello.wsdl", "http://sample/hello", "Hello", contrib);
- WSDLInterface Upper_wsdl = wsdli("Upper.wsdl", "http://sample/upper", "Upper", contrib);
+ final Contribution contrib = build(contrib("test", here()), ec);
+ WSDLInterface Hello_wsdl = build(wsdli("Hello.wsdl", "http://sample/hello", "Hello", contrib), ec);
+ WSDLInterface Upper_wsdl = build(wsdli("Upper.wsdl", "http://sample/upper", "Upper", contrib), ec);
// Assemble a test composite model (see EmbedUtil
// for the little DSL used here, much more concise
// than using the assembly model interfaces)
final Composite comp =
- composite("http://sample", "test",
+ build(composite("http://sample", "test",
component("client-test",
implementation(ClientTest.class,
service(Client.class),
@@ -97,19 +105,19 @@ public class EmbedTestCase {
reference("upper", "upper-test")),
component("upper-test",
implementation(UpperTest.class,
- service(Upper.class))));
+ service(Upper.class)))), ec);
// Register a test instance of our sample implementation ProviderFactory
- providerFactories().addProviderFactory(testProviderFactory());
+ providerFactories(ec).addProviderFactory(testProviderFactory());
// Run with it
- node = node(deploy(contrib, comp));
+ node = node(nf, deploy(contrib, comp));
node.start();
}
static ProviderFactory<SampleImplementation> testProviderFactory() {
// This shows how to get called when a provider is created
- return new SampleProviderFactory(extensionPoints()) {
+ return new SampleProviderFactory(extensionPoints(ec)) {
public ImplementationProvider createImplementationProvider(RuntimeComponent comp, SampleImplementation impl) {
out.println("Creating a provider for component " + comp.getName());
return super.createImplementationProvider(comp, impl);