1 package org.exoplatform.services.xml.querying.impl.xtas; 2 3 import junit.framework.TestCase; 4 import junit.framework.Test; 5 import junit.framework.TestSuite; 6 7 8 import java.io.ByteArrayInputStream ; 9 import java.io.File ; 10 11 import org.exoplatform.services.xml.querying.QueryRunTimeException; 12 import org.exoplatform.services.xml.querying.impl.xtas.BaseStatement; 13 import org.exoplatform.services.xml.querying.impl.xtas.Query; 14 import org.exoplatform.services.xml.querying.impl.xtas.QueryType; 15 import org.exoplatform.services.xml.querying.impl.xtas.SimpleStatement; 16 import org.exoplatform.services.xml.querying.impl.xtas.WellFormedUniFormTree; 17 import org.exoplatform.services.xml.querying.impl.xtas.XTASMLStatement; 18 import org.exoplatform.services.xml.querying.impl.xtas.resource.plugins.LocalFile; 19 import org.xml.sax.InputSource ; 20 28 29 33 public class TestQuery extends TestCase 34 { 35 36 public final ByteArrayInputStream source = 37 new ByteArrayInputStream ( "<root><child1>child1Value</child1><child2 attr1=\"child2Attr\"/></root>".getBytes()); 38 39 public TestQuery(String name) 40 { 41 super(name); 42 } 43 public static Test suite() 44 { 45 return new TestSuite(TestQuery.class); 46 } 47 48 49 public void testSerialize() 50 { 51 try { 52 53 String f1 = "tmp/out1.xml"; 54 String f2 = "tmp/out2.xml"; 55 String _xml = "<test/>"; 56 57 BaseStatement qc = new SimpleStatement (); 58 qc.setDestinationId(f1); 59 Query q = new Query ( qc ); 60 61 q.setInputStream( new ByteArrayInputStream (_xml.getBytes()) ); 62 q.execute(); 63 64 q.serialize(); 65 66 q.prepare( new SimpleStatement () ); 68 q.loadSource( new LocalFile( f1 ) ); 69 q.setDestination( f2 ); 70 q.execute(); 71 q.serialize(); 72 73 assertEquals( "Serialize1 Not equal ", util.XML_DECLARATION+_xml, util.getFileContent(f1) ); 74 assertEquals( "Serialize2 Not equal ", util.XML_DECLARATION+_xml, util.getFileContent(f2) ); 75 76 } catch ( Exception e) { 77 fail( "testSerialize() ERROR: "+e.toString()); 79 } 80 81 } 82 83 public void testCascadeSelect() 84 { 85 try { 86 87 String f1 = "tmp/out1.xml"; 88 String f2 = "tmp/out2.xml"; 89 90 BaseStatement qc = new XTASMLStatement ("tmp/query1.xml"); 91 Query q = new Query ( qc ); 92 q.execute(); 93 q.serialize(); 94 95 96 } catch ( Exception e) { 97 fail( "testCascadeSelect() ERROR: "+e.toString()); 99 } 100 101 } 102 103 104 public void testUpdate() 105 { 106 try { 107 108 String updVal = "Updated value"; 109 BaseStatement qc = new SimpleStatement (QueryType.UPDATE, "//root/child1/node()", updVal); 110 Query q = new Query ( qc ); 111 115 q.setInputStream( source ); 116 117 q.execute(); 118 120 122 BaseStatement qc1 = new SimpleStatement (QueryType.SELECT, "//root/child1/node()"); 124 126 131 } catch ( Exception e) { 132 133 fail( "testUpdate() ERROR: "+e.toString()); 134 } 135 } 136 137 public void testDelete() 138 { 139 try { 140 141 String f1 = "tmp/out1.xml"; 142 BaseStatement qc = new SimpleStatement (QueryType.DELETE, "//root/child1", (String )null, null, f1); 143 144 Query q = new Query ( qc ); 145 q.setInputStream( source ); 146 q.execute(); 147 q.serialize(); 148 149 BaseStatement qc1 = new SimpleStatement (QueryType.SELECT, "//root/child1", (String ) null, f1, null); 151 Query q1 = new Query ( qc1 ); 152 q1.execute(); 153 155 assertEquals( "Bad delete " , "", q1.getResult().toString().trim()); 156 157 } catch ( Exception e) { 158 159 e.printStackTrace(); 160 fail( "testDelete() ERROR: "+e.toString()); 161 } 162 163 } 164 165 public void testAppend() 166 { 167 168 try { 169 170 String f1 = "tmp/out1.xml"; 171 String appVal = "<child3>NewChild</child3>"; 172 BaseStatement qc = new SimpleStatement (QueryType.APPEND, "//root/child1", appVal, null, f1); 173 Query q = new Query ( qc ); 174 q.setInputStream(source); 175 q.execute(); 176 q.serialize(); 177 178 BaseStatement qc1 = new SimpleStatement (QueryType.SELECT, "//root/child3/node()", (String )null, f1, null); 180 Query q1 = new Query ( qc1 ); 181 q1.execute(); 182 assertEquals( "Bad Append " , "NewChild", q1.getResult().toString()); 183 184 } catch ( Exception e) { 185 186 fail( "testAppend() ERROR: "+e.toString()); 187 } 188 189 } 190 191 192 public void testPrepareNext() 193 { 194 195 try { 196 197 WellFormedUniFormTree initTree = new WellFormedUniFormTree (); 198 initTree.initRoot("root"); 199 Query q = new Query (); 200 q.setInput(initTree); 201 q.prepare( SimpleStatement.select("/")); 202 q.execute(); 203 q.prepareNext( SimpleStatement.append("root/text()", null, "<branch/>")); 204 q.execute(); 205 206 208 } catch ( Exception e) { 209 210 fail( "testPrepareNext() ERROR: "+e.toString()); 211 } 212 213 } 214 215 public void testLoadFormLocalFile() 216 { 217 218 try { 219 220 String f1 = "tmp/employees.xml"; 222 String f2 = "tmp/employees1.xml"; 224 225 BaseStatement qc = new SimpleStatement (QueryType.SELECT, "/*", (String )null, f1, "tmp/employees-out.xml"); 226 Query q = new Query ( qc ); 227 q.execute(); 228 q.serialize(); 229 230 232 } catch ( Exception e) { 233 234 fail( "testLoadFormLocalFile() ERROR: "+e.toString()); 235 } 236 237 } 238 295 296 public void testXMLwithDTDSerialize() 297 { 298 299 302 try { 303 304 String f1 = "tmp/employees.xml"; 306 String f2 = "tmp/employees-dtd.xml"; 307 String updStr = "<employee><firstname>Gennady</firstname></employee>"; 308 BaseStatement qc = new SimpleStatement (QueryType.APPEND, "text()", updStr, f1, f2); 309 Query q = new Query ( qc ); 310 q.execute(); 311 q.serialize(); 312 313 315 } catch ( Exception e) { 316 317 fail( "testXMLwithDTDSerialize() ERROR: "+e.toString()); 318 } 319 320 } 321 322 public void testUnapropriateState() 323 { 324 try { 325 326 String f1 = "tmp/out1.xml"; 327 String f2 = "tmp/out2.xml"; 328 String _xml = "<test/>"; 329 330 BaseStatement qc = new SimpleStatement (); 331 qc.setDestinationId(f1); 332 Query q = new Query ( qc ); 333 335 339 q.setInputStream(new ByteArrayInputStream (_xml.getBytes())); 340 q.execute(); 341 q.serialize(); 342 343 q.loadSource( new LocalFile( f1 ) ); 345 q.setDestination( f2 ); 346 q.execute(); 347 q.serialize(); 348 } catch ( QueryRunTimeException e) { 349 return; 351 352 } catch ( Exception e) { 353 fail( "testUnapropriateState() ERROR: "+e.toString()); 354 } 355 fail( "testUnapropriateState() ERROR: QueryRunTimeException(Bad state) must be trown!"); 356 357 } 358 359 public void testMultiInstruction() 360 { 361 try { 362 363 364 String f1 = "tmp/multiquery1.xml"; 365 366 BaseStatement qc = new XTASMLStatement (f1); 367 368 370 Query q = new Query ( qc ); 372 373 q.execute(); 375 377 } catch ( QueryRunTimeException e) { 378 return; 380 381 } catch ( Exception e) { 382 fail( "testUnapropriateState() ERROR: "+e.toString()); 383 } 384 386 } 387 388 } 389 | Popular Tags |