KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > xml > querying > impl > xtas > TestUniFormTree


1 package org.exoplatform.services.xml.querying.impl.xtas;
2
3 import java.io.ByteArrayInputStream JavaDoc;
4 import java.io.FileInputStream JavaDoc;
5 import junit.framework.TestCase;
6 import junit.framework.Test;
7 import junit.framework.TestSuite;
8
9
10 import org.xml.sax.InputSource JavaDoc;
11 import java.io.*;
12 import java.util.Iterator JavaDoc;
13 import java.util.ArrayList JavaDoc;
14 import java.util.NoSuchElementException JavaDoc;
15 import org.xml.sax.InputSource JavaDoc;
16
17 import org.exoplatform.services.xml.querying.impl.xtas.UniFormConverter;
18 import org.exoplatform.services.xml.querying.impl.xtas.UniFormTreeFragment;
19 import org.exoplatform.services.xml.querying.impl.xtas.WellFormedUniFormTree;
20 import org.exoplatform.services.xml.querying.impl.xtas.xml.Utils;
21 import org.w3c.dom.Element JavaDoc;
22 import org.w3c.dom.Document JavaDoc;
23 import org.w3c.dom.NodeList JavaDoc;
24
25 import javax.xml.parsers.DocumentBuilder JavaDoc;
26 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
27 /*
28 import org.exoplatform.services.xml.querying.impl.xtas.WellFormedUniFormTree;
29 import org.exoplatform.services.xml.querying.impl.xtas.UniFormTreeFragment;
30 import org.exoplatform.services.xml.querying.impl.xtas.UniFormConverter;
31 */

32
33
34 public class TestUniFormTree extends TestCase {
35
36     public final String JavaDoc str = "<root><child1>child1Value</child1><child2 attr1=\"child2Attr\"/></root>";
37     public final ByteArrayInputStream JavaDoc source =
38         new ByteArrayInputStream JavaDoc( str.getBytes());
39
40     public TestUniFormTree(String JavaDoc name)
41     {
42         super(name);
43     }
44
45     public static Test suite()
46     {
47         return new TestSuite(TestUniFormTree.class);
48     }
49
50     public void testNewDefault()
51     {
52        try {
53
54            WellFormedUniFormTree elem = new WellFormedUniFormTree ();
55            assertEquals( "", "", elem.toString() );
56
57         } catch ( Exception JavaDoc e ) {
58             e.printStackTrace();
59             fail( " testNewFromStream() failed ! "+e.toString());
60
61         }
62     }
63
64
65     public void testNewFromStream()
66     {
67        try {
68
69            UniFormTreeFragment elem = new UniFormTreeFragment ();
70            elem.init( source );
71
72            assertEquals( "", util.packXmlString(str), util.packXmlString(elem.toString()) );
73
74         } catch ( Exception JavaDoc e ) {
75             e.printStackTrace();
76             fail( " testNewFromStream() failed ! "+e.toString());
77
78         }
79     }
80
81     public void testToWellForm()
82     {
83        try {
84
85            UniFormTreeFragment elem = new UniFormTreeFragment ();
86            elem.init( source );
87            java.util.Properties JavaDoc attrs = new java.util.Properties JavaDoc();
88            attrs.setProperty("xtas:xpath", "tables/exo_user");
89            attrs.setProperty("xtas:resource", "?rootTable=EXO_USER");
90
91            WellFormedUniFormTree t = UniFormConverter.toWellForm(elem, "users", "xmlns:xtas=\"http://xtas.sourceforge.net\"", attrs);
92 //System.out.println(t);
93
// assertEquals( "", util.packXmlString(str), util.packXmlString(elem.toString()) );
94

95         } catch ( Exception JavaDoc e ) {
96             e.printStackTrace();
97             fail( " testNewFromStream() failed ! "+e.toString());
98
99         }
100     }
101
102     public void testAsNodeList()
103     {
104        try {
105
106           String JavaDoc str1 = "<child1>child1Value</child1><child2 attr1=\"child2Attr\"/>";
107
108            UniFormTreeFragment elem = new UniFormTreeFragment ();
109            elem.init( new ByteArrayInputStream JavaDoc( str1.getBytes()) );
110            NodeList JavaDoc nl = elem.getAsNodeList();
111            assertTrue("Num of children !=2", nl.getLength()==2 );
112
113         } catch ( Exception JavaDoc e ) {
114             e.printStackTrace();
115             fail( " testNewFromStream() failed ! "+e.toString());
116
117         }
118     }
119
120     public void testNewFromDocument()
121     {
122        try {
123
124           DocumentBuilderFactory JavaDoc dfactory = DocumentBuilderFactory.newInstance();
125           dfactory.setNamespaceAware(true);
126           DocumentBuilder JavaDoc docBuilder = dfactory.newDocumentBuilder();
127           Document JavaDoc doc = docBuilder.parse( source );
128            
129            WellFormedUniFormTree elem = new WellFormedUniFormTree ();
130            elem.init( doc );
131            elem.getAsDOM();
132
133 // System.out.println( elem.getAsDOM().getDocumentElement().getFirstChild() );
134

135            assertEquals( "", util.packXmlString(str), util.packXmlString(elem.toString()) );
136
137         } catch ( Exception JavaDoc e ) {
138             e.printStackTrace();
139             fail( " testNewFromDocument() failed ! "+e.toString());
140
141         }
142     }
143
144
145     public void testNewFromLocalFile()
146     {
147        try {
148
149            // File with external DTD...
150
String JavaDoc f1 = "tmp/employees.xml";
151            // File with internal DTD...
152
String JavaDoc f2 = "tmp/employees1.xml";
153
154 // UniFormTree elem = new UniFormTreeFragment();
155

156            // Well Formed !!!!!!!
157
WellFormedUniFormTree elem = new WellFormedUniFormTree ();
158            elem.init( new InputSource JavaDoc(f1) );
159 // elem.init( new FileInputStream(f1) );
160

161 // UniFormTree elem1 = new WellFormedUniFormTree( new FileInputStream(f2) );
162

163
164 // System.out.println(elem.toString());
165
// assertEquals( "", util.packXmlString(str), util.packXmlString(elem.getAsString()) );
166

167         } catch ( Exception JavaDoc e ) {
168             fail( " testNewFromLocalFile() failed ! "+e.toString());
169
170         }
171     }
172
173
174     // Mutable Tree is DEPRECATED!!
175
public void testSimpleAdd()
176     {
177        try {
178
179            WellFormedUniFormTree tree = new WellFormedUniFormTree ();
180
181            Document JavaDoc doc = Utils.createDocument();
182            Element JavaDoc root = doc.createElement("root");
183            doc.appendChild( root );
184          
185            doc.getDocumentElement().setAttribute("attr", "val");
186            Element JavaDoc item = doc.createElement("node1");
187            doc.getDocumentElement().appendChild( item );
188            item.appendChild( doc.createTextNode("val1") );
189            Element JavaDoc item1 = doc.createElement("node2");
190            item.appendChild( item1 );
191            item1.appendChild( doc.createTextNode("val2") );
192
193            tree.init(doc);
194
195            String JavaDoc testStr = "<root attr=\"val\"><node1>val1<node2>val2</node2></node1></root>";
196
197            assertEquals( "", util.packXmlString(testStr), util.packXmlString(tree.toString()) );
198
199         } catch ( Exception JavaDoc e ) {
200             e.printStackTrace();
201             fail( " testSimpleAdd() failed ! "+e.toString());
202
203         }
204     }
205
206
207 }
208
Popular Tags