Tests.java
Go to the documentation of this file.
1 /*
2  * Copyright 2006-2012 The FLWOR Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package api_test;
17 
18 
19 import java.io.*;
20 import java.net.URI;
21 import java.net.URISyntaxException;
22 import java.util.Properties;
23 import javax.xml.namespace.QName;
24 import javax.xml.xquery.*;
25 import org.zorbaxquery.api.*;
26 import org.zorbaxquery.api.xqj.*;
27 
28 public class Tests {
29 
30  static
31  {
32  System.loadLibrary ( "zorba_api" );
33  }
34 
35  static String getFile(String filename) throws FileNotFoundException, IOException {
36  File file = new File(filename);
37  FileInputStream fileStream = new FileInputStream(file);
38  InputStreamReader reader = new InputStreamReader( fileStream, "UTF8" );
39  BufferedReader buffer = new BufferedReader( reader );
40  String line;
41  StringBuilder builder = new StringBuilder();
42  while ( (line = buffer.readLine()) != null )
43  {
44  builder.append(line);
45  }
46  return builder.toString();
47  }
48 
49  static boolean checkResult(String expected, String result) {
50  System.out.println("Result:");
51  System.out.println(result);
52  System.out.println("Expected:");
53  System.out.println(expected);
54  return expected.equals(result);
55  }
56 
57  static boolean test_1() throws XQException
58  {
59  ZorbaXQDataSource xqds = new ZorbaXQDataSource();
60  XQConnection xqc = xqds.getConnection();
61  XQExpression xqe = xqc.createExpression();
62  org.zorbaxquery.api.xqj.ZorbaXQResultSequence xqs = (org.zorbaxquery.api.xqj.ZorbaXQResultSequence) xqe.executeQuery("1,2,3");
63  ZorbaXQStaticCollectionManager colManager = xqs.getStaticCollectionManager();
64  xqc.close();
65  xqc.close();
66  return true;
67  }
68 
69 
70  static boolean test_2 () throws XQException
71  {
72  StringBuilder strBuilder = new StringBuilder();
73  try {
74  BufferedReader in = new BufferedReader(new FileReader("managers/module1.xq"));
75  String str;
76  while ((str = in.readLine()) != null) {
77  strBuilder.append(str);
78  }
79  in.close();
80  } catch (Exception e) {
81  throw new XQException("Error reading file for test: " + e.getLocalizedMessage());
82  }
83  ZorbaXQDataSource xqds = new ZorbaXQDataSource();
84  XQConnection xqc = xqds.getConnection();
85  XQExpression xqe = xqc.createExpression();
86  org.zorbaxquery.api.xqj.ZorbaXQResultSequence xqs = (org.zorbaxquery.api.xqj.ZorbaXQResultSequence) xqe.executeQuery(strBuilder.toString());
87  ZorbaXQStaticCollectionManager colManager = xqs.getStaticCollectionManager();
88  boolean resultAdding = false;
89  boolean resultDeleting = true;
90  URI uri;
91  QName qname;
92  XQItemType type = null;
93  try {
94  uri = new URI("http://www.mod2.com/");
95  qname = new QName("http://www.mod2.com/", "coll");
96  type = xqc.createAtomicType( XQItemType.XQBASETYPE_QNAME, qname, uri);
97  } catch (URISyntaxException e) {
98  throw new XQException("Error creating QName: " + e.getLocalizedMessage());
99  }
100  XQItem colName = xqc.createItemFromString("coll", type);
101  colManager.createCollection(colName);
102  resultAdding = colManager.isAvailableCollection(colName);
103  colManager.deleteCollection(colName);
104  resultDeleting = !colManager.isAvailableCollection(colName);
105  xqc.close();
106  return resultAdding && resultDeleting;
107  }
108 
109  static boolean test_3a() throws XQException
110  {
111  StringBuilder strBuilder = new StringBuilder();
112  try {
113  BufferedReader in = new BufferedReader(new FileReader("managers/module1.xq"));
114  String str;
115  while ((str = in.readLine()) != null) {
116  strBuilder.append(str);
117  }
118  in.close();
119  } catch (Exception e) {
120  throw new XQException("Error reading file for test: " + e.getLocalizedMessage());
121  }
122  ZorbaXQDataSource xqds = new ZorbaXQDataSource();
123  org.zorbaxquery.api.xqj.ZorbaXQConnection xqc = (org.zorbaxquery.api.xqj.ZorbaXQConnection) xqds.getConnection();
124  XQExpression xqe = xqc.createExpression();
125  org.zorbaxquery.api.xqj.ZorbaXQResultSequence xqs = (org.zorbaxquery.api.xqj.ZorbaXQResultSequence) xqe.executeQuery(strBuilder.toString());
126  ZorbaXQStaticCollectionManager colManager = xqs.getStaticCollectionManager();
127  URI uri;
128  QName qname;
129  XQItemType type = null;
130  try {
131  uri = new URI("http://www.mod2.com/");
132  qname = new QName("http://www.mod2.com/", "coll");
133  type = xqc.createAtomicType( XQItemType.XQBASETYPE_QNAME, qname, uri);
134  } catch (URISyntaxException e) {
135  throw new XQException("Error creating QName: " + e.getLocalizedMessage());
136  }
137  XQItem colName = xqc.createItemFromString("coll", type);
138  colManager.createCollection(colName);
139  ZorbaXQCollection collection = colManager.getCollection(colName);
140 
141  ZorbaXQXmlDataManager manager = xqc.getXmlDataManager();
142  XQSequence data = manager.parseXML("<books><book>Book 1</book><book>Book 2</book></books>");
143  collection.insertNodesFirst(data);
144 
145  colManager.deleteCollection(colName);
146  boolean resultDeleting = !colManager.isAvailableCollection(colName);
147  xqc.close();
148  return resultDeleting;
149  }
150 
151  static boolean test_3b() throws XQException
152  {
153  StringBuilder strBuilder = new StringBuilder();
154  try {
155  BufferedReader in = new BufferedReader(new FileReader("managers/module1.xq"));
156  String str;
157  while ((str = in.readLine()) != null) {
158  strBuilder.append(str);
159  }
160  in.close();
161  } catch (Exception e) {
162  throw new XQException("Error reading file for test: " + e.getLocalizedMessage());
163  }
164  InMemoryStore store = InMemoryStore.getInstance();
165  Zorba zorba = Zorba.getInstance ( store );
166  XQuery query = zorba.compileQuery(strBuilder.toString());
167  StaticCollectionManager manager = query.getStaticCollectionManager();
168 
169 
170  ItemFactory factory = zorba.getItemFactory();
171  Item name = factory.createQName("http://www.mod2.com/", "coll");
172 
173  manager.createCollection(name);
174  Collection collection = manager.getCollection(name);
175 
176  XmlDataManager xmlManager = zorba.getXmlDataManager();
177  Item data = xmlManager.parseXMLtoItem("<books><book>Book 1</book><book>Book 2</book></books>");
178  ItemSequence sequence = new ItemSequence(data);
179  collection.insertNodesLast(sequence);
180 
181  zorba.shutdown();
182  InMemoryStore.shutdown ( store );
183 
184  return true;
185  }
186 
187  static boolean test_4() throws XQException
188  {
189  ZorbaXQDataSource xqds = new ZorbaXQDataSource();
190  org.zorbaxquery.api.xqj.ZorbaXQConnection xqc = (org.zorbaxquery.api.xqj.ZorbaXQConnection) xqds.getConnection();
191  XQExpression xqe = xqc.createExpression();
192  ZorbaXQXmlDataManager xmlManager = xqc.getXmlDataManager();
193  ZorbaXQCollectionManager colManager = xmlManager.getCollectionManager();
194  xqc.close();
195  xqc.close();
196  return true;
197  }
198 
199  static boolean test_5() throws XQException
200  {
201  ZorbaXQDataSource xqds = new ZorbaXQDataSource();
202  org.zorbaxquery.api.xqj.ZorbaXQConnection xqc = (org.zorbaxquery.api.xqj.ZorbaXQConnection) xqds.getConnection();
203  ZorbaXQXmlDataManager xmlManager = xqc.getXmlDataManager();
204  ZorbaXQCollectionManager colManager = xmlManager.getCollectionManager();
205  boolean resultAdding = false;
206  boolean resultDeleting = true;
207  URI uri;
208  QName qname;
209  XQItemType type = null;
210  try {
211  uri = new URI("http://www.mod2.com/");
212  qname = new QName("http://www.mod2.com/", "col2");
213  type = xqc.createAtomicType( XQItemType.XQBASETYPE_QNAME, qname, uri);
214  } catch (URISyntaxException e) {
215  throw new XQException("Error creating QName: " + e.getLocalizedMessage());
216  }
217  XQItem colName = xqc.createItemFromString("col2", type);
218  colManager.createCollection(colName);
219  resultAdding = colManager.isAvailableCollection(colName);
220  colManager.deleteCollection(colName);
221  resultDeleting = !colManager.isAvailableCollection(colName);
222  xqc.close();
223  return resultAdding && resultDeleting;
224  }
225  static boolean test_6a() throws XQException
226  {
227  InMemoryStore store = InMemoryStore.getInstance();
228  Zorba zorba = Zorba.getInstance ( store );
229  XmlDataManager xmlManager = new XmlDataManager(zorba.getXmlDataManager());
230  CollectionManager manager = new CollectionManager(xmlManager.getCollectionManager());
231  ItemFactory factory = zorba.getItemFactory();
232  Item name = factory.createQName("http://www.zorba-xquery.com/", "aaa");
233  manager.createCollection(name);
234  boolean resultAdding = manager.isAvailableCollection(name);
235  Collection collection = null;
236  //Item data = new Item();
237  if (resultAdding) {
238  collection = manager.getCollection(name);
239  Item data = xmlManager.parseXMLtoItem("<books><book>Book 1</book><book>Book 2</book></books>");
240  collection.insertNodesLast(new ItemSequence(data));
241  }
242  collection.delete();
243 
244  zorba.shutdown();
245  InMemoryStore.shutdown ( store );
246  return true;
247  }
248 
249  static boolean test_6b() throws XQException
250  {
251  ZorbaXQDataSource xqds = new ZorbaXQDataSource();
252  org.zorbaxquery.api.xqj.ZorbaXQConnection xqc = (org.zorbaxquery.api.xqj.ZorbaXQConnection) xqds.getConnection();
253  ZorbaXQXmlDataManager xmlManager = xqc.getXmlDataManager();
254  ZorbaXQCollectionManager colManager = xmlManager.getCollectionManager();
255  URI uri;
256  QName qname;
257  XQItemType type = null;
258  try {
259  uri = new URI("http://www.mod2.com/");
260  qname = new QName("http://www.mod2.com/", "col2");
261  type = xqc.createAtomicType( XQItemType.XQBASETYPE_QNAME, qname, uri);
262  } catch (URISyntaxException e) {
263  throw new XQException("Error creating QName: " + e.getLocalizedMessage());
264  }
265  XQItem colName = xqc.createItemFromString("col2", type);
266  colManager.createCollection(colName);
267  ZorbaXQCollection collection = colManager.getCollection(colName);
268  colName.close();
269  XQSequence data = xmlManager.parseXML("<books><book>Book 1</book><book>Book 2</book></books>");
270  collection.insertNodesLast(data);
271  xqc.close();
272  return true;
273  }
274 
275  static boolean test_7() throws XQException
276  {
277  InMemoryStore store = InMemoryStore.getInstance();
278  Zorba zorba = Zorba.getInstance ( store );
279  String test = "Hello world!";
280  ZorbaReaderWrapper stream = new ZorbaReaderWrapper(new StringReader("'"+test+"'"));
281  XQuery query = zorba.compileQuery(stream);
282  SerializationOptions opts = new SerializationOptions();
283  opts.setOmitXMLDeclaration(SerializationOptions.OmitXMLDeclaration.ZORBA_API_OMIT_XML_DECLARATION_YES);
284  String queryResult = query.execute(opts);
285  boolean result = checkResult(test, queryResult);
286  zorba.shutdown();
287  InMemoryStore.shutdown ( store );
288  return result;
289  }
290 
291  static boolean test_8() throws XQException, UnsupportedEncodingException
292  {
293  InMemoryStore store = InMemoryStore.getInstance();
294  Zorba zorba = Zorba.getInstance ( store );
295  StringBuilder test = new StringBuilder();
296  for (int i=0; i<20; i++) {
297  test.append("1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"); // 100 chars
298  }
299  String queryString = "'" + test.toString() + "'";
300  ZorbaInputWrapper stream = new ZorbaInputWrapper(new ByteArrayInputStream(queryString.getBytes("UTF-8")));
301  XQuery query = zorba.compileQuery(stream);
302  SerializationOptions opts = new SerializationOptions();
303  opts.setOmitXMLDeclaration(SerializationOptions.OmitXMLDeclaration.ZORBA_API_OMIT_XML_DECLARATION_YES);
304  String queryResult = query.execute(opts);
305  boolean result = checkResult(test.toString(), queryResult);
306  zorba.shutdown();
307  InMemoryStore.shutdown ( store );
308  return result;
309  }
310 
311  static boolean test_9() throws XQException, UnsupportedEncodingException
312  {
313  InMemoryStore store = InMemoryStore.getInstance();
314  Zorba zorba = Zorba.getInstance ( store );
315  String test = "<Hello><ab/><ax/>World</Hello>";
316  ZorbaInputWrapper stream = new ZorbaInputWrapper(new ByteArrayInputStream(test.getBytes("UTF-8")));
317  SerializationOptions opts = new SerializationOptions();
318  opts.setIndent(SerializationOptions.Indent.ZORBA_API_INDENT_NO);
319  opts.setOmitXMLDeclaration(SerializationOptions.OmitXMLDeclaration.ZORBA_API_OMIT_XML_DECLARATION_YES);
320  XQuery query = zorba.compileQuery(stream);
321  String queryResult = query.execute(opts);
322  boolean result = checkResult(test, queryResult);
323  query.destroy();
324  zorba.shutdown();
325  InMemoryStore.shutdown ( store );
326  return result;
327  }
328 
329  static boolean test_10() throws XQException, UnsupportedEncodingException
330  {
331  InMemoryStore store = InMemoryStore.getInstance();
332  Zorba zorba = Zorba.getInstance ( store );
333  String test = "<Hello><ab/><ax/>World</Hello>";
334  ZorbaInputWrapper stream = new ZorbaInputWrapper(new ByteArrayInputStream(test.getBytes("UTF-8")));
335  XQuery query = zorba.compileQuery(stream);
336  ByteArrayOutputStream out = new ByteArrayOutputStream();
337  ZorbaOutputWrapper OStream = new ZorbaOutputWrapper(out);
338  SerializationOptions opts = new SerializationOptions();
339  opts.setIndent(SerializationOptions.Indent.ZORBA_API_INDENT_NO);
340  opts.setOmitXMLDeclaration(SerializationOptions.OmitXMLDeclaration.ZORBA_API_OMIT_XML_DECLARATION_YES);
341  query.execute(OStream, opts);
342  boolean result = checkResult(test, out.toString());
343  query.destroy();
344  zorba.shutdown();
345  InMemoryStore.shutdown ( store );
346  return result;
347  }
348 
349  static boolean test_11a() throws XQException, FileNotFoundException, IOException
350  {
351  InMemoryStore store = InMemoryStore.getInstance();
352  Zorba zorba = Zorba.getInstance ( store );
353  String test = getFile("managers/utf.txt");
354  org.zorbaxquery.api.ZorbaReaderWrapper stream = new org.zorbaxquery.api.ZorbaReaderWrapper(new StringReader("'"+test+"'"));
355  XQuery query = zorba.compileQuery(stream);
356  SerializationOptions opts = new SerializationOptions();
357  opts.setOmitXMLDeclaration(SerializationOptions.OmitXMLDeclaration.ZORBA_API_OMIT_XML_DECLARATION_YES);
358  String queryResult = query.execute(opts);
359  boolean result = checkResult(test, queryResult);
360  zorba.shutdown();
361  InMemoryStore.shutdown ( store );
362  return result;
363  }
364 
365  static boolean test_11b() throws XQException, FileNotFoundException, IOException
366  {
367  String test = getFile("managers/utf.txt");
368  ZorbaXQDataSource xqds = new ZorbaXQDataSource();
369  XQConnection xqc = xqds.getConnection();
370  XQExpression xqe = xqc.createExpression();
371  org.zorbaxquery.api.xqj.ZorbaXQResultSequence xqs = (org.zorbaxquery.api.xqj.ZorbaXQResultSequence) xqe.executeQuery("'"+test+"'");
372  ZorbaXQStaticCollectionManager colManager = xqs.getStaticCollectionManager();
373  Properties prpts = new Properties();
374  prpts.setProperty("omit-xml-declaration", "yes");
375  String queryResult =xqs.getSequenceAsString(prpts);
376  boolean result = checkResult(test, queryResult);
377  xqc.close();
378  xqc.close();
379  return result;
380  }
381 
382 } // class Test_Zorba
383