1 package org.exoplatform.services.xml.querying.impl.xtas; 2 3 import java.io.ByteArrayInputStream ; 4 import java.io.FileInputStream ; 5 import junit.framework.TestCase; 6 import junit.framework.Test; 7 import junit.framework.TestSuite; 8 9 10 import org.xml.sax.InputSource ; 11 import java.io.*; 12 import java.util.Iterator ; 13 import java.util.ArrayList ; 14 import java.util.NoSuchElementException ; 15 import org.xml.sax.InputSource ; 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 ; 22 import org.w3c.dom.Document ; 23 import org.w3c.dom.NodeList ; 24 25 import javax.xml.parsers.DocumentBuilder ; 26 import javax.xml.parsers.DocumentBuilderFactory ; 27 32 33 34 public class TestUniFormTree extends TestCase { 35 36 public final String str = "<root><child1>child1Value</child1><child2 attr1=\"child2Attr\"/></root>"; 37 public final ByteArrayInputStream source = 38 new ByteArrayInputStream ( str.getBytes()); 39 40 public TestUniFormTree(String 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 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 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 attrs = new java.util.Properties (); 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 95 } catch ( Exception e ) { 96 e.printStackTrace(); 97 fail( " testNewFromStream() failed ! "+e.toString()); 98 99 } 100 } 101 102 public void testAsNodeList() 103 { 104 try { 105 106 String str1 = "<child1>child1Value</child1><child2 attr1=\"child2Attr\"/>"; 107 108 UniFormTreeFragment elem = new UniFormTreeFragment (); 109 elem.init( new ByteArrayInputStream ( str1.getBytes()) ); 110 NodeList nl = elem.getAsNodeList(); 111 assertTrue("Num of children !=2", nl.getLength()==2 ); 112 113 } catch ( Exception e ) { 114 e.printStackTrace(); 115 fail( " testNewFromStream() failed ! "+e.toString()); 116 117 } 118 } 119 120 public void testNewFromDocument() 121 { 122 try { 123 124 DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance(); 125 dfactory.setNamespaceAware(true); 126 DocumentBuilder docBuilder = dfactory.newDocumentBuilder(); 127 Document doc = docBuilder.parse( source ); 128 129 WellFormedUniFormTree elem = new WellFormedUniFormTree (); 130 elem.init( doc ); 131 elem.getAsDOM(); 132 133 135 assertEquals( "", util.packXmlString(str), util.packXmlString(elem.toString()) ); 136 137 } catch ( Exception e ) { 138 e.printStackTrace(); 139 fail( " testNewFromDocument() failed ! "+e.toString()); 140 141 } 142 } 143 144 145 public void testNewFromLocalFile() 146 { 147 try { 148 149 String f1 = "tmp/employees.xml"; 151 String f2 = "tmp/employees1.xml"; 153 154 156 WellFormedUniFormTree elem = new WellFormedUniFormTree (); 158 elem.init( new InputSource (f1) ); 159 161 163 164 167 } catch ( Exception e ) { 168 fail( " testNewFromLocalFile() failed ! "+e.toString()); 169 170 } 171 } 172 173 174 public void testSimpleAdd() 176 { 177 try { 178 179 WellFormedUniFormTree tree = new WellFormedUniFormTree (); 180 181 Document doc = Utils.createDocument(); 182 Element root = doc.createElement("root"); 183 doc.appendChild( root ); 184 185 doc.getDocumentElement().setAttribute("attr", "val"); 186 Element item = doc.createElement("node1"); 187 doc.getDocumentElement().appendChild( item ); 188 item.appendChild( doc.createTextNode("val1") ); 189 Element item1 = doc.createElement("node2"); 190 item.appendChild( item1 ); 191 item1.appendChild( doc.createTextNode("val2") ); 192 193 tree.init(doc); 194 195 String 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 e ) { 200 e.printStackTrace(); 201 fail( " testSimpleAdd() failed ! "+e.toString()); 202 203 } 204 } 205 206 207 } 208 | Popular Tags |