/** * * Copyright 2005 The Apache Software Foundation * * Licensed 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.sdo.proxy; import java.io.IOException; import java.util.Date; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import junit.framework.TestCase; import org.osoa.sdo.DataObject; import org.osoa.sdo.Type; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.apache.tuscany.sdo.helper.DOMHelperImpl; import org.apache.tuscany.sdo.impl.DataFactoryImpl; import org.apache.tuscany.sdo.impl.TypeHelperImpl; import org.apache.tuscany.sdo.proxy.interfaces.DataTypes; import org.apache.tuscany.sdo.proxy.interfaces.Primitives; /** * @version $Rev$ $Date$ */ public class DOMHelperTestCase extends TestCase { private Primitives primitives; private DataTypes dataTypes; private DOMHelperImpl domHelper; private Document doc; public void testSavePrimitives() throws IOException { domHelper.save((DataObject) primitives, doc); Element e = doc.getDocumentElement(); assertEquals(Type.JAVA_NAMESPACE, e.getNamespaceURI()); assertEquals(Primitives.class.getName(), e.getLocalName()); assertEquals(8, e.getChildNodes().getLength()); // domHelper.writeTo(System.out, doc); } public void testLoadPrimitives() { Element rootElement = doc.createElementNS(Type.JAVA_NAMESPACE, Primitives.class.getName()); appendElement(rootElement, "boolean", "true"); doc.appendChild(rootElement); Primitives primitives = (Primitives) domHelper.load(doc); assertNotNull(primitives); assertEquals(true, primitives.isBoolean()); } private void appendElement(Element parent, String name, String text) { Element e = doc.createElementNS(null, name); e.appendChild(doc.createTextNode(text)); parent.appendChild(e); } public void testSaveDataTypes() throws IOException { dataTypes.setString("Hello World"); dataTypes.setBytes(new byte[]{ 0 , 1, 2, 3}); dataTypes.setDate(new Date()); domHelper.save((DataObject) dataTypes, doc); Element e = doc.getDocumentElement(); assertEquals(Type.JAVA_NAMESPACE, e.getNamespaceURI()); assertEquals(DataTypes.class.getName(), e.getLocalName()); assertEquals(3, e.getChildNodes().getLength()); // domHelper.writeTo(System.out, doc); } protected void setUp() throws Exception { super.setUp(); TypeHelperImpl typeHelper = new TypeHelperImpl(getClass().getClassLoader()); DataFactoryImpl dataFactory = new DataFactoryImpl(typeHelper); domHelper = new DOMHelperImpl(dataFactory); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); doc = builder.newDocument(); primitives = typeHelper.define(Primitives.class).newInstance(); dataTypes = typeHelper.define(DataTypes.class).newInstance(); } }