KickJava   Java API By Example, From Geeks To Geeks.

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


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 JavaDoc;
9 import java.io.File JavaDoc;
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 JavaDoc;
20 /*
21 import org.exoplatform.services.xml.querying.impl.xtas.WellFormedUniFormTree;
22 import org.exoplatform.services.xml.querying.impl.xtas.BaseStatement;
23 import org.exoplatform.services.xml.querying.impl.xtas.SimpleStatement;
24 import org.exoplatform.services.xml.querying.impl.xtas.Query;
25 import org.exoplatform.services.xml.querying.impl.xtas.XTASMLStatement;
26 import org.exoplatform.services.xml.querying.impl.xtas.QueryType;
27 */

28
29 /**
30  * Test Case for Query related things
31  * @version $Id: TestQuery.java 566 2005-01-25 12:50:49Z kravchuk $
32  */

33 public class TestQuery extends TestCase
34 {
35
36     public final ByteArrayInputStream JavaDoc source =
37         new ByteArrayInputStream JavaDoc( "<root><child1>child1Value</child1><child2 attr1=\"child2Attr\"/></root>".getBytes());
38
39     public TestQuery(String JavaDoc 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 JavaDoc f1 = "tmp/out1.xml";
54             String JavaDoc f2 = "tmp/out2.xml";
55             String JavaDoc _xml = "<test/>";
56
57             BaseStatement qc = new SimpleStatement ();
58             qc.setDestinationId(f1);
59             Query q = new Query ( qc );
60
61             q.setInputStream( new ByteArrayInputStream JavaDoc(_xml.getBytes()) );
62             q.execute();
63
64             q.serialize();
65
66             // And one more serialize f1->f2
67
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 JavaDoc e) {
77 // e.printStackTrace();
78
fail( "testSerialize() ERROR: "+e.toString());
79         }
80
81     }
82
83     public void testCascadeSelect()
84     {
85        try {
86
87             String JavaDoc f1 = "tmp/out1.xml";
88             String JavaDoc 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 JavaDoc e) {
97 // e.printStackTrace();
98
fail( "testCascadeSelect() ERROR: "+e.toString());
99         }
100
101     }
102
103
104     public void testUpdate()
105     {
106        try {
107
108           String JavaDoc updVal = "Updated value";
109           BaseStatement qc = new SimpleStatement (QueryType.UPDATE, "//root/child1/node()", updVal);
110           Query q = new Query ( qc );
111 // WellFormedUniFormTree wfTree = new WellFormedUniFormTree ();
112
// wfTree.init( source );
113
// q.setInput(wfTree);
114

115           q.setInputStream( source );
116
117           q.execute();
118 // System.out.println(q.getResult().toString());
119

120           // (Update without writting)
121

122           // Get updated value...
123
BaseStatement qc1 = new SimpleStatement (QueryType.SELECT, "//root/child1/node()");
124 // Query q1 = q.createNext( qc1 );
125

126 // Query q1 = new Query( qc1, q.getResult() );
127
// q1.execute();
128
// System.out.println(q1.getResult().toString());
129
// assertEquals( "Update Not equal " , updVal, q1.getResult().toString() );
130

131        } catch ( Exception JavaDoc e) {
132
133             fail( "testUpdate() ERROR: "+e.toString());
134         }
135     }
136
137     public void testDelete()
138     {
139        try {
140
141           String JavaDoc f1 = "tmp/out1.xml";
142           BaseStatement qc = new SimpleStatement (QueryType.DELETE, "//root/child1", (String JavaDoc)null, null, f1);
143
144           Query q = new Query ( qc );
145           q.setInputStream( source );
146           q.execute();
147           q.serialize();
148
149           // Try to Get deleted value...
150
BaseStatement qc1 = new SimpleStatement (QueryType.SELECT, "//root/child1", (String JavaDoc) null, f1, null);
151           Query q1 = new Query ( qc1 );
152           q1.execute();
153 // System.out.println(q1.getResult().toString());
154

155           assertEquals( "Bad delete " , "", q1.getResult().toString().trim());
156
157        } catch ( Exception JavaDoc 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 JavaDoc f1 = "tmp/out1.xml";
171           String JavaDoc 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           // Get updated value...
179
BaseStatement qc1 = new SimpleStatement (QueryType.SELECT, "//root/child3/node()", (String JavaDoc)null, f1, null);
180           Query q1 = new Query ( qc1 );
181           q1.execute();
182           assertEquals( "Bad Append " , "NewChild", q1.getResult().toString());
183
184        } catch ( Exception JavaDoc 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 // assertEquals( "<root><branch/></root>", util.packXmlString(q.getResult().toString()));
207

208        } catch ( Exception JavaDoc e) {
209
210             fail( "testPrepareNext() ERROR: "+e.toString());
211         }
212
213     }
214
215     public void testLoadFormLocalFile()
216     {
217
218        try {
219
220            // File with external DTD...
221
String JavaDoc f1 = "tmp/employees.xml";
222            // File with internal DTD...
223
String JavaDoc f2 = "tmp/employees1.xml";
224
225           BaseStatement qc = new SimpleStatement (QueryType.SELECT, "/*", (String JavaDoc)null, f1, "tmp/employees-out.xml");
226           Query q = new Query ( qc );
227           q.execute();
228           q.serialize();
229
230 // assertEquals( "Bad Append " , "NewChild", q1.getResult().toString());
231

232        } catch ( Exception JavaDoc e) {
233
234             fail( "testLoadFormLocalFile() ERROR: "+e.toString());
235         }
236
237     }
238 /*
239     public void testGetAttribute()
240     {
241
242        try {
243
244           String f1 = "tmp/mapping1.xml";
245
246           Statement qc = new SimpleStatement(QueryType.SELECT, "/mapping/class/map-to[@xml='bean']/@*", (String)null, f1, "tmp/mapping1-out.xml");
247           Query q = new Query( qc );
248           q.execute();
249           q.serialize();
250
251 // System.out.println(q.getResult());
252
253 // @todo Does not worked!
254 // Xalan bug 10616
255 // All have a copy-of inside an instruction (xsl:attribute, xsl:comment,
256 // xsl:processing-instruction) that is supposed to have text-only constructors.
257 // Xalan stringifies the whole node-set or RTF instead of taking only top-level
258 // text nodes and discarding the rest.
259
260
261 // assertEquals( "@TODO :" , "bean", q.getResult().toString());
262
263        } catch ( Exception e) {
264
265             fail( "testGetAttribute() ERROR: "+e.toString());
266         }
267
268     }
269
270
271     public void testSerializeComments()
272     {
273
274
275        try {
276
277            // File with external DTD...
278           String f1 = "tmp/employees1.xml";
279           String f2 = "tmp/employees-out.xml";
280           String updStr = "<employee><firstname>Gennady</firstname></employee>";
281           Statement qc = new SimpleStatement(QueryType.APPEND, "text()", updStr, f1, f2);
282           Query q = new Query( qc );
283           q.execute();
284           q.serialize();
285
286 // assertEquals( "Bad Append " , "NewChild", q1.getResult().toString());
287
288        } catch ( Exception e) {
289
290             fail( "testXMLwithDTDSerialize() ERROR: "+e.toString());
291         }
292
293     }
294 */

295
296     public void testXMLwithDTDSerialize()
297     {
298
299 // It is does not work properly for now !!!
300
// @todo DTD/XSD supporting
301

302        try {
303
304            // File with external DTD...
305
String JavaDoc f1 = "tmp/employees.xml";
306           String JavaDoc f2 = "tmp/employees-dtd.xml";
307           String JavaDoc 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 // assertEquals( "Bad Append " , "NewChild", q1.getResult().toString());
314

315        } catch ( Exception JavaDoc e) {
316
317             fail( "testXMLwithDTDSerialize() ERROR: "+e.toString());
318         }
319
320     }
321
322     public void testUnapropriateState()
323     {
324        try {
325
326             String JavaDoc f1 = "tmp/out1.xml";
327             String JavaDoc f2 = "tmp/out2.xml";
328             String JavaDoc _xml = "<test/>";
329
330             BaseStatement qc = new SimpleStatement ();
331             qc.setDestinationId(f1);
332             Query q = new Query ( qc );
333 // q.setInput( new WellFormedUniFormTree (_xml.getBytes()) );
334

335 // WellFormedUniFormTree wfTree = new WellFormedUniFormTree ();
336
// wfTree.init( new ByteArrayInputStream(_xml.getBytes()) );
337
// q.setInput(wfTree);
338

339             q.setInputStream(new ByteArrayInputStream JavaDoc(_xml.getBytes()));
340             q.execute();
341             q.serialize();
342
343             // And one more serialize f1->f2
344
q.loadSource( new LocalFile( f1 ) );
345             q.setDestination( f2 );
346             q.execute();
347             q.serialize();
348         } catch ( QueryRunTimeException e) {
349 // e.printStackTrace();
350
return;
351
352         } catch ( Exception JavaDoc 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 JavaDoc f1 = "tmp/multiquery1.xml";
365
366             BaseStatement qc = new XTASMLStatement (f1);
367
368 //System.out.println(qc);
369

370 // qc.setDestinationId(f1);
371
Query q = new Query ( qc );
372
373 // q.setInputStream(new ByteArrayInputStream(_xml.getBytes()));
374
q.execute();
375 // q.serialize();
376

377         } catch ( QueryRunTimeException e) {
378 // e.printStackTrace();
379
return;
380
381         } catch ( Exception JavaDoc e) {
382             fail( "testUnapropriateState() ERROR: "+e.toString());
383         }
384 // fail( "testUnapropriateState() ERROR: QueryRunTimeException(Bad state) must be trown!");
385

386     }
387
388 }
389
Popular Tags