1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
/**
*
* 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>) 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>) 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();
}
}
|