From e5b7380c874745c989d1816b8f552504f038e1bc Mon Sep 17 00:00:00 2001 From: lresende Date: Thu, 26 Sep 2013 20:33:20 +0000 Subject: 2.0 branch for possible maintenance release git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@1526672 13f79535-47bb-0310-9956-ffa450edef68 --- .../sca/databinding/impl/MediatorImplTestCase.java | 125 +++++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 sca-java-2.x/branches/2.0/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/impl/MediatorImplTestCase.java (limited to 'sca-java-2.x/branches/2.0/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/impl/MediatorImplTestCase.java') diff --git a/sca-java-2.x/branches/2.0/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/impl/MediatorImplTestCase.java b/sca-java-2.x/branches/2.0/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/impl/MediatorImplTestCase.java new file mode 100644 index 0000000000..953e539dd1 --- /dev/null +++ b/sca-java-2.x/branches/2.0/modules/databinding/src/test/java/org/apache/tuscany/sca/databinding/impl/MediatorImplTestCase.java @@ -0,0 +1,125 @@ +/* + * 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.sca.databinding.impl; + +import java.io.StringWriter; +import java.io.Writer; + +import org.apache.tuscany.sca.core.DefaultExtensionPointRegistry; +import org.apache.tuscany.sca.core.ExtensionPointRegistry; +import org.apache.tuscany.sca.databinding.DataBindingExtensionPoint; +import org.apache.tuscany.sca.databinding.DefaultDataBindingExtensionPoint; +import org.apache.tuscany.sca.databinding.DefaultTransformerExtensionPoint; +import org.apache.tuscany.sca.databinding.TransformationContext; +import org.apache.tuscany.sca.databinding.TransformerExtensionPoint; +import org.apache.tuscany.sca.databinding.xml.Node2String; +import org.apache.tuscany.sca.databinding.xml.Node2Writer; +import org.apache.tuscany.sca.databinding.xml.SAX2DOMPipe; +import org.apache.tuscany.sca.databinding.xml.String2SAX; +import org.apache.tuscany.sca.interfacedef.DataType; +import org.apache.tuscany.sca.interfacedef.impl.DataTypeImpl; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; + +/** + * Test case for MediatorImpl + * + * @version $Rev$ $Date$ + */ +public class MediatorImplTestCase { + private static final String IPO_XML = + "" + "" + + " " + + " Helen Zoe" + + " 47 Eden Street" + + " Cambridge" + + " CB1 1JR" + + " " + + " " + + " Robert Smith" + + " 8 Oak Avenue" + + " Old Town" + + "PA" + + " 95819" + + " " + + " " + + " " + + " Lapis necklace" + + " 1" + + "99.95" + + " Want this for the holidays" + + " 1999-12-05" + + " " + + " " + + ""; + + private MediatorImpl mediator; + + @Before + public void setUp() throws Exception { + ExtensionPointRegistry extensionPointRegistry = new DefaultExtensionPointRegistry(); + DataBindingExtensionPoint dataBindingRegistry = new DefaultDataBindingExtensionPoint(extensionPointRegistry); + TransformerExtensionPoint registry = new DefaultTransformerExtensionPoint(extensionPointRegistry); + + registry.addTransformer(new String2SAX(), true); + registry.addTransformer(new SAX2DOMPipe(extensionPointRegistry), true); + registry.addTransformer(new Node2String(extensionPointRegistry), true); + registry.addTransformer(new Node2Writer(extensionPointRegistry), true); + + mediator = new MediatorImpl(dataBindingRegistry, registry); + } + + private TransformationContext createTransformationContext(Class sourceType, Class targetType) { + TransformationContext context = new TransformationContextImpl(); + DataType sourceDataType = new DataTypeImpl(sourceType.getName(), sourceType, sourceType); + DataType targetDataType = new DataTypeImpl(targetType.getName(), targetType, targetType); + context.setSourceDataType(sourceDataType); + context.setTargetDataType(targetDataType); + return context; + } + + @Test + public void testTransform1() { + TransformationContext context = createTransformationContext(String.class, Node.class); + Object node = + mediator.mediate(IPO_XML, context.getSourceDataType(), context.getTargetDataType(), null); + Assert.assertTrue(node instanceof Document); + Element root = ((Document)node).getDocumentElement(); + Assert.assertEquals(root.getNamespaceURI(), "http://www.example.com/IPO"); + Assert.assertEquals(root.getLocalName(), "purchaseOrder"); + } + + @Test + public void testTransform2() { + TransformationContext context = createTransformationContext(String.class, Writer.class); + Writer writer = new StringWriter(); + mediator.mediate(IPO_XML, writer, context.getSourceDataType(), context.getTargetDataType(), null); + String str = writer.toString(); + Assert.assertTrue(str != null && str.indexOf("1999-12-05") != -1); + } + +} -- cgit v1.2.3