~zorba-coders/zorba/bug1024580

« back to all changes in this revision

Viewing changes to swig/csharp/tests/test07.cs

  • Committer: Rodolfo Ochoa
  • Date: 2012-09-08 16:36:08 UTC
  • mfrom: (10937.1.51 zorba)
  • Revision ID: rodolfo.ochoa@gmail.com-20120908163608-mz55ayoa8lznh7i3
MergeĀ fromĀ trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
using System;
 
17
using System.Text;
 
18
using System.IO;
 
19
using org.zorbaxquery.api;
 
20
 
 
21
namespace ZorbaApplication
 
22
{
 
23
    class Program
 
24
    {
 
25
        static string test(string query, string xml, string doc) {
 
26
            InMemoryStore store = InMemoryStore.getInstance();
 
27
            Zorba zorba = Zorba.getInstance(store);
 
28
 
 
29
            XmlDataManager dataManager = zorba.getXmlDataManager();
 
30
            Iterator docIter = dataManager.parseXML(xml);
 
31
            docIter.open();
 
32
            Item idoc = new Item();
 
33
            docIter.next(idoc);
 
34
            docIter.close();
 
35
            docIter.Dispose();
 
36
 
 
37
            DocumentManager docManager = dataManager.getDocumentManager();
 
38
            docManager.put(doc, idoc);
 
39
 
 
40
            XQuery xquery = zorba.compileQuery(query);
 
41
            string result = xquery.execute();
 
42
 
 
43
            xquery.destroy();
 
44
            xquery.Dispose();
 
45
            
 
46
            zorba.shutdown();
 
47
            InMemoryStore.shutdown(store);
 
48
            
 
49
            return result;
 
50
        }
 
51
 
 
52
        static string readFile(string file) {
 
53
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
 
54
            using (StreamReader sr = File.OpenText(file)) {
 
55
                string line = "";
 
56
                while ( (line = sr.ReadLine()) != null) {
 
57
                    sb.AppendLine(line);
 
58
                }
 
59
            }
 
60
            return sb.ToString();
 
61
        }
 
62
 
 
63
        static bool compareStrings(string a, string b) {
 
64
            string[] aMult = a.Split('\n');
 
65
            string[] bMult = b.Split('\n');
 
66
            if (aMult.Length > bMult.Length) {
 
67
                return false;
 
68
            }
 
69
            int i = 0;
 
70
            foreach (string line in aMult) {
 
71
              if (line.Equals(bMult[i++])) {
 
72
                  return false;
 
73
              }
 
74
            }
 
75
            return true;
 
76
        }
 
77
 
 
78
        static void Main(string[] args)
 
79
        {
 
80
            System.Console.WriteLine("Running: XQuery execute - parsing XML");
 
81
 
 
82
            string xml = readFile("books.xml");
 
83
            string doc = "my_fake_books.xml";
 
84
            string query = "doc('my_fake_books.xml')";
 
85
            string testResult=readFile("test07.result");;
 
86
            System.Console.WriteLine("XML: " + xml);
 
87
            System.Console.WriteLine("Query: " + query);
 
88
 
 
89
            string result;
 
90
            try {
 
91
                result = test(query, xml, doc);
 
92
            } catch(Exception e) {
 
93
                System.Console.WriteLine("Failed");
 
94
                Console.WriteLine("{0} Exception caught.", e);
 
95
                return;
 
96
            }
 
97
 
 
98
            System.Console.WriteLine("Expecting: " + testResult);
 
99
            System.Console.WriteLine("Result: " + result);
 
100
 
 
101
            if (compareStrings(result,testResult)) {
 
102
                System.Console.WriteLine("Success");
 
103
            } else {
 
104
                System.Console.WriteLine("Failed");
 
105
            }
 
106
 
 
107
            }
 
108
    }
 
109
}