KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > xquery > testsuite > TestMisc


1 package gnu.xquery.testsuite;
2 import gnu.expr.*;
3 import gnu.mapping.*;
4 import gnu.xquery.lang.*;
5 import gnu.text.*;
6
7 public class TestMisc
8 {
9   static { XQuery.registerEnvironment(); }
10   static XQuery interp = XQuery.getInstance();
11   static Environment env = Environment.getCurrent();
12
13   static int expectedPasses = 0;
14   static int unexpectedPasses = 0;
15   static int expectedFailures = 0;
16   static int unexpectedFailures = 0;
17   static boolean verbose = false;
18   static String JavaDoc failureExpectedNext = null;
19
20   public static void main(String JavaDoc[] args)
21   {
22     // gnu.expr.ModuleExp.dumpZipPrefix = "kawa-zip-dump-";
23
// Compilation.debugPrintExpr = true;
24
// Compilation.debugPrintFinalExpr = true;
25

26     evalTest("3.5+1", "4.5");
27     evalTest("3.5+1 ,4*2.5", "4.5 10");
28     evalTest("3<5", "true");
29     evalTest("let $x:=3+4 return $x", "7");
30     evalTest("let $x:=3+4 return <a>{$x}</a>", "<a>7</a>");
31     // We resolve $request and $response to servlet request/response,
32
// but only when they're not lexially bound.
33
evalTest("let $request:=2, $response:=3 return ($request+$response)", "5");
34
35     evalTest("some $x in (1, 2, 3), $y in (2, 3, 4)"
36          + " satisfies $x + $y = 4",
37          "true");
38     evalTest("every $x in (1, 2, 3), $y in (2, 3, 4)"
39          + " satisfies $x + $y = 4",
40          "false");
41     evalTest("every $x in (11, 12, 13), $y in (2, 3, 4)"
42          + " satisfies $x > $y",
43          "true");
44
45     evalTest("for $y in (4,5,2+4) return <b>{10+$y}</b>",
46          "<b>14</b><b>15</b><b>16</b>");
47     evalTest("for $i in (1 to 10) where ($i mod 2)=1 return 20+$i",
48          "21 23 25 27 29");
49     evalTest("for $car at $i in ('Ford', 'Chevy')," +
50          "$pet at $j in ('Cat', 'Dog') " +
51          "return ($i, '/', $car, '/', $j, '/', $pet, ';')",
52          "1/Ford/1/Cat;1/Ford/2/Dog;2/Chevy/1/Cat;2/Chevy/2/Dog;");
53
54     evalTest("(3,4,5)[3]", "5");
55     evalTest("1,((2,3)[false()]),5", "1 5");
56     evalTest("1,((2 to 4)[true()]),5", "1 2 3 4 5");
57     evalTest("(for $y in (5,4) return <b>{10+$y}</b>)[2]", "<b>14</b>");
58     evalTest("for $a in (<a><b c='1' d='3'/><b c='2' d='6'/></a>)/b/@c"
59          + " return concat('c: ', $a, ' d: ', $a/../@d, ';')",
60          "c: 1 d: 3;c: 2 d: 6;");
61
62     String JavaDoc tabNsNodes = " xmlns:h=\"H\" xmlns:j=\"J\" xmlns:k=\"J\"";
63     evalTest("doc('tab.xml')/result",
64          "<result"+tabNsNodes+">\n" +
65          "<row>\n" +
66          "<fld1>a1</fld1>\n" +
67          "<fld2 align=\"right\"><!--ignore-this-comment-->12</fld2>\n" +
68          "</row>\n" +
69          "<row>\n" +
70          "<fld1 align=\"left\">b1</fld1>\n" +
71          "<fld2 align=\"right\">22</fld2>\n" +
72          "</row>\n" +
73          "<h:row>\n" +
74          "<j:fld1><![CDATA[c]]><![CDATA[1]]></j:fld1>\n" +
75          "<h:fld2><![CDATA[33]]></h:fld2>\n" +
76          "<j:fld3>44</j:fld3>\n" +
77          "<k:fld1>c2</k:fld1>\n" +
78          "</h:row>\n" +
79          "</result>");
80     evalTest("doc('tab.xml')/result/row/fld2",
81          "<fld2"+tabNsNodes+" align=\"right\"><!--ignore-this-comment-->12</fld2>"
82          +"<fld2"+tabNsNodes+" align=\"right\">22</fld2>");
83     evalTest("doc('tab.xml')/result/row[fld2]",
84          "<row"+tabNsNodes+">\n" +
85          "<fld1>a1</fld1>\n" +
86          "<fld2 align=\"right\"><!--ignore-this-comment-->12</fld2>\n</row>" +
87          "<row"+tabNsNodes+">\n" +
88          "<fld1 align=\"left\">b1</fld1>\n" +
89          "<fld2 align=\"right\">22</fld2>\n" +
90          "</row>");
91     evalTest("doc('tab.xml')/result/row/*",
92          "<fld1"+tabNsNodes+">a1</fld1><fld2"+tabNsNodes+" align=\"right\"><!--ignore-this-comment-->12</fld2><fld1"+tabNsNodes+" align=\"left\">b1</fld1><fld2"+tabNsNodes+" align=\"right\">22</fld2>");
93
94     evalTest("doc('tab.xml')/result/row[2]",
95          "<row"+tabNsNodes+">\n" +
96          "<fld1 align=\"left\">b1</fld1>\n" +
97          "<fld2 align=\"right\">22</fld2>\n" +
98          "</row>");
99     evalTest("for $x in doc('tab.xml')/result/row[2]/node()" +
100          " return ('[',$x,']')",
101          "[\n][<fld1"+tabNsNodes+" align=\"left\">b1</fld1>][\n" +
102          "][<fld2"+tabNsNodes+" align=\"right\">22</fld2>][\n]");
103     evalTest("for $x in doc('tab.xml')/result/row[2]/text()" +
104          " return ('[',$x,']')",
105          "[\n][\n][\n]");
106     evalTest("for $x in doc('tab.xml')/result/row[2]//text()" +
107          " return ('[',$x,']')",
108          "[\n][b1][\n][22][\n]");
109     evalTest("doc('tab.xml')/result/row/*[2]",
110          "<fld2"+tabNsNodes+" align=\"right\"><!--ignore-this-comment-->12</fld2>"
111          + "<fld2"+tabNsNodes+" align=\"right\">22</fld2>");
112
113     evalTest("for $x in <T>r1<fld1>a1</fld1><fld3/>r2<fld2>12</fld2></T>" +
114          " /node()" +
115          " return ('[',$x,']')",
116          "[r1][<fld1>a1</fld1>][<fld3 />][r2][<fld2>12</fld2>]");
117
118     evalTest("(doc('tab.xml')/result/row/*)[2]",
119          "<fld2"+tabNsNodes+" align=\"right\"><!--ignore-this-comment-->12</fld2>");
120     evalTest("(doc('tab.xml')/result/row/*)[position()>1]",
121          "<fld2"+tabNsNodes+" align=\"right\"><!--ignore-this-comment-->12</fld2>"
122          +"<fld1"+tabNsNodes+" align=\"left\">b1</fld1>"
123          +"<fld2"+tabNsNodes+" align=\"right\">22</fld2>");
124     evalTest("(doc('tab.xml')/result/row/*)[position()>1][2]",
125          "<fld1"+tabNsNodes+" align=\"left\">b1</fld1>");
126
127     evalTest("doc('tab.xml')/result/row/(fld2,fld1)",
128          "<fld1"+tabNsNodes+">a1</fld1>"
129          +"<fld2"+tabNsNodes+" align=\"right\"><!--ignore-this-comment-->12</fld2>"
130          +"<fld1"+tabNsNodes+" align=\"left\">b1</fld1>"
131          +"<fld2"+tabNsNodes+" align=\"right\">22</fld2>");
132
133     evalTest("string(doc('tab.xml'))",
134          "\n\na1\n12\n\n\nb1\n22\n\n\nc1\n33\n44\nc2\n\n\n");
135     evalTest("string(doc('tab.xml'))",
136          "\n\na1\n12\n\n\nb1\n22\n\n\nc1\n33\n44\nc2\n\n\n");
137     evalTest("string(doc('tab.xml')/result/row/fld1/@align)", "left");
138     evalTest("doc('tab.xml')/result/row/fld2/@align/string()",
139          "rightright");
140  
141     evalTest("for $x in children(<a>xy{3+4}kl<c>def</c>{9}{11}</a>)" +
142          " return ('[',$x,']')",
143          "[xy 7 kl][<c>def</c>][9 11]");
144     evalTest("children(<a>xy{3+4}kl<c>def</c>{9}{11}</a>)",
145          "xy 7 kl<c>def</c>9 11");
146
147     evalTest("<a>aab</a> ='aab'", "true");
148     evalTest("<a>abc</a>='abb'", "false");
149
150     evalTest("string(<a>{'aa''bb&#88;cc&#x5a;dd'}</a>)", "aa'bbXccZdd");
151
152     evalTest("doc('tab.xml')/result/row[fld1]",
153          "<row"+tabNsNodes+">\n" +
154          "<fld1>a1</fld1>\n" +
155          "<fld2 align=\"right\"><!--ignore-this-comment-->12</fld2>\n</row>" +
156          "<row"+tabNsNodes+">\n" +
157          "<fld1 align=\"left\">b1</fld1>\n" +
158          "<fld2 align=\"right\">22</fld2>\n" +
159          "</row>");
160     evalTest("doc('tab.xml')/result/row[fld3]", "");
161     evalTest("doc('tab.xml')/result/row/fld1[@align]",
162          "<fld1"+tabNsNodes+" align=\"left\">b1</fld1>");
163     evalTest("doc('tab.xml')/result/row/fld2[@align]",
164          "<fld2"+tabNsNodes+" align=\"right\"><!--ignore-this-comment-->12</fld2>"
165          +"<fld2"+tabNsNodes+" align=\"right\">22</fld2>");
166     evalTest("'a',doc('tab.xml')/result/row/fld1[@align='left']",
167          "a<fld1"+tabNsNodes+" align=\"left\">b1</fld1>");
168     evalTest("'a',doc('tab.xml')/result/row/fld1[@align='right']", "a");
169
170     evalTest("let $x:=12,\n" +
171          " $y:=<a>{$x+$x}</a>\n" +
172          " return <b atr1='11' atr2=\"{$x}\">{($y,99,$y)}</b>",
173          "<b atr1=\"11\" atr2=\"12\"><a>24</a>99<a>24</a></b>");
174
175     evalTest("let $el := 'elm' return "
176          + "document{element {$el} {attribute at{\"abc\"}, \"data\"}}/elm",
177          "<elm at=\"abc\">data</elm>");
178
179     evalTest("let $a := <a at1='val1'><b/><c/></a>,"
180          + " $b0 := <b/>,"
181          + " $b := $a/b return"
182          + " ($a is $a, $a << $b, $b >> $b,"
183          + " $a isnot $b, $b, $b0, $b is $b0)",
184          "true true false true <b /> <b /> false");
185     evalTest("let $a := <a at1='val1'><b/><c/></a>,"
186          + " $b := $a/b, $c := $a/c return"
187          + " for $n in distinct-nodes(($c, $a/@at1, $a, $c, $b, $b, $c))"
188          + " return ('[', $n, ']')",
189          "[<a at1=\"val1\"><b /><c /></a>][ at1=\"val1\"][<b />][<c />]");
190
191     // Boundary whitsapce (boundary-space) tests:
192
evalTest("declare boundary-space preserve;\n"
193          + "for $n in (<a> <b/> {' x '} </a>)/node() return ($n,';')",
194          " ;<b/>; x ;");
195     evalTest("declare boundary-space strip;\n"
196          + "for $n in (<a> <b/> {' x '} </a>)/node() return ($n,';')",
197          "<b/>; x ;");
198     evalTest("declare boundary-space strip;\n"
199          + "for $n in (<a> x <b/> y<c/>&#x20;</a>)/node() return ($n,';')",
200          " x ;<b/>; y;<c/>; ;");
201     evalTest("for $n in (<a> <b/> </a>)/node() return ($n,';')",
202          "<b/>;");
203     evalTest("<a> {3} {4} </a>", "<a>34</a>");
204     // This actually succeeds because evalTest ignores spaces.
205
// failureExpectedNext = "fix space handling in constructors";
206
evalTest("<a>{3,4}{5,6}</a>", "<a>3 45 6</a>");
207     failureExpectedNext = "fix space handling in constructors";
208     evalTest("let $x := <a>{3,4}{5,6}</a> return <b>{$x, $x}</b>",
209          "<ba><a>3 45 6</a><a>3 45 6</a></b>");
210     evalTest("for $n in <a><?xq doit?>abc<![CDATA[<X>]]>d<!--a comment--></a>/node()"
211          + " return ($n,';')",
212          "<?xq doit?>;abc<![CDATA[<X>]]>d;<!--a comment-->;");
213     evalTest("for $n in <a><?xq doit?>abc<![CDATA[<X>]]>d<!--a comment--></a>/node()"
214          + " return (string($n),';')",
215          "doit;abc&lt;X&gt;d;a comment;");
216     evalTest("string(<a><?xq doit?>abc<![CDATA[<X>]]>d<!--a comment--></a>)",
217          "abc&lt;X&gt;d");
218
219     // Simple namespace tests.
220
evalTest("declare namespace xx='XXX';\n <xx:a>XX</xx:a>",
221          "<xx:a xmlns:xx=\"XXX\">XX</xx:a>");
222     evalTest("declare namespace x1='XXX';\n declare namespace x2='XXX';\n"
223          + "(<top><x1:ab>X1</x1:ab><x2:ab>X2</x2:ab></top>)/x2:ab/string()",
224          "X1X2");
225     evalTest("declare namespace x1='XXX';\n declare namespace x2='YYY';\n"
226          + "string((<top><x1:ab>X1</x1:ab><x2:ab>X2</x2:ab></top>)/x2:ab)",
227          "X2");
228     evalTest("declare namespace x1='XXX';\n declare namespace x2='YYY';\n"
229          + "(<top><x1:ab>X1</x1:ab><x2:ab>X2</x2:ab></top>)/*/string()",
230          "X1X2");
231     evalTest("declare namespace x1='XXX';\n declare namespace x2='YYY';\n"
232          + "(<top><x1:ab>X1</x1:ab><x2:ab>X2</x2:ab></top>)/*:*/string()",
233          "X1X2");
234     evalTest("declare namespace x1='XXX';\n declare namespace x2='YYY';\n"
235          + "string((<top><x1:ab>X1</x1:ab><x2:ab>X2</x2:ab></top>)/x1:*)",
236          "X1");
237     evalTest("declare namespace x1='XXX';\n declare namespace x2='YYY';\n"
238          + "(<top><x1:ab>X1</x1:ab><x2:ab>X2</x2:ab></top>)/*:ab/string()",
239          "X1X2");
240     evalTest("declare namespace x1='XXX';\n declare namespace x2='YYY';\n"
241          + "string((<top><x1:ab>X1</x1:ab><x2:cd>X2</x2:cd></top>)/*:cd)",
242          "X2");
243     evalTest("declare namespace h='H';\n"
244          + "string(doc('tab.xml')/result/h:row)",
245          "\nc1\n33\n44\nc2\n");
246     evalTest("declare namespace xx='H';\n"
247          + "doc('tab.xml')/result/xx:row/string()",
248          "\nc1\n33\n44\nc2\n");
249     evalTest("doc('tab.xml')/result/*:row/string()",
250          "\na1\n12\n\nb1\n22\n\nc1\n33\n44\nc2\n");
251     evalTest("doc('tab.xml')/result/*:row/*:fld1/string()",
252          "a1b1c1c2");
253     evalTest("declare namespace k='J';\n"
254          + "doc('tab.xml')/result/*:row/k:fld1/string()",
255          "c1c2");
256     evalTest("declare namespace k='J';\n"
257          + "string(doc('tab.xml')/result/*:row[k:fld1])",
258          "\nc1\n33\n44\nc2\n");
259     evalTest("declare namespace m1 = 'bb'; declare namespace m2 = 'cc';"
260          + "let $m1:x := 3 return let $m2:x := 4 return"
261          + " <m2:a a:c='{$a:x}' xmlns:a='bb'>{ count($a:x) }</m2:a>",
262          "<m2:a xmlns:a=\"bb\" xmlns:m2=\"cc\" a:c=\"3\">1</m2:a>");
263
264     evalTest("doc('tab.xml')/result/row[1]/descendant::*",
265          "<fld1"+tabNsNodes+">a1</fld1>"
266          +"<fld2"+tabNsNodes+" align=\"right\"><!--ignore-this-comment-->12</fld2>");
267     evalTest("for $x in doc('tab.xml')/result/row[1]/descendant::node() return ($x,';')",
268          "\n;<fld1"+tabNsNodes+">a1</fld1>;a1;\n;"
269          + "<fld2"+tabNsNodes+" align=\"right\"><!--ignore-this-comment-->12</fld2>;<!--ignore-this-comment-->;12;\n;");
270     evalTest("doc('tab.xml')/result/row[1]/descendant::text()",
271          "a112");
272     evalTest("doc('tab.xml')/result/row[1]/descendant-or-self::*",
273          "<row"+tabNsNodes+"><fld1>a1</fld1>"
274          + "<fld2 align=\"right\"><!--ignore-this-comment-->12</fld2></row>"
275          + "<fld1"+tabNsNodes+">a1</fld1>"
276          + "<fld2"+tabNsNodes+" align=\"right\"><!--ignore-this-comment-->12</fld2>");
277
278     evalTest("for $n in doc('tab.xml')/result/* return node-name($n)",
279          "row row h:row");
280
281     evalTest("for $n in doc('tab.xml')/result/row/* "
282          + "return local-name-from-QName(node-name($n))",
283          "fld1 fld2 fld1 fld2");
284     evalTest("declare namespace h='H';\n"
285              +" for $n in doc('tab.xml')/result/*:row/* "
286          + "return (prefix-from-QName(node-name($n)),';')",
287          " ; ; ; ; j ; h ; j ; k ;");
288     evalTest("for $n in doc('tab.xml')/result/*:row/*:fld1 "
289          + "return <n>{namespace-uri-from-QName(node-name($n))}</n>",
290          "<n /><n /><n>J</n><n>J</n>");
291     evalTest("for $n in doc('tab.xml')/result/*:row/*:fld1 return "
292          + "('[', for $p in ('', 'k', 'h') return"
293          + " (namespace-uri-for-prefix($p,$n),';'), ']')",
294          "[;J;H;][;J;H;][;J;H;][;J;H;]");
295
296     // Based on bugs reported by Francois Leygues <vizawalou@wanadoo.fr>:
297
evalTest("let $bx := <b x='xx'></b> return"
298          + " let $x := <a>{for $y in $bx return $y}</a>"
299          + " return $x/b",
300          "<b x=\"xx\" />");
301     evalTest("element r {let $y := <b x='1'/>"
302          + " let $x:=<a>{$y}</a> return $x/b/@x}",
303          "<r x=\"1\" />");
304     evalTest("declare function local:x(){<a><b x='1'/><b x='2'/></a>};"
305          + " let $i := <a>{for $a in local:x()/b return $a}</a> return $i/b/@x",
306          " x=\"1\" x=\"2\"");
307
308     evalTest("declare function local:s(){ <a x='10'>{for $n in (<b x='2'/>) return ($n) }</a>};"
309          + " let $st := local:s()/b return ("
310          + " '[',$st/@x ,'] [',$st ,']')",
311          "[ x=\"2\"] [<b x=\"2\" />]");
312
313     // Testcase from <Seshukumar_Adiraju@infosys.com>:
314
evalTest("let $books := "
315          + "<books><book id='book1'/><book id='book2'/></books> "
316          + "for $book in $books/book return <p>{string($book/@id)}</p>",
317          "<p>book1</p><p>book2</p>");
318
319     evalTest("for $n in children(<a>xx<b/>yy</a>) return $n instance of node()",
320          "true true true");
321     evalTest("for $n in children(<a>xx<b/>yy</a>) return $n instance of text ( )",
322          "true false true");
323     evalTest("for $n in children(<a>xx<b/>yy</a>) return $n instance of element(a)",
324          "false false false");
325     evalTest("for $n in <a>xx<b/>yy</a>/node() return $n instance of element(b)",
326          "false true false");
327     // FIXME: evalTest("<a>xx<b/>yy</a>/node() instance of node()", "false");
328
evalTest("<a>xx<b/>yy</a>/node() instance of node()?", "false");
329     evalTest("<a>xx<b/>yy</a>/node() instance of node()+", "true");
330     evalTest("<a>xx<b/>yy</a>/node() instance of node()*", "true");
331     evalTest("<a>xx<b/>yy</a>/node() instance of item()+", "true");
332     evalTest("(3,4,5) instance of item()+", "true");
333     evalTest("('a','b') instance of string+", "true");
334     evalTest("(2,3) instance of string?", "false");
335     evalTest("(2,3) instance of string+", "false");
336     evalTest("() instance of string?", "true");
337     evalTest("() instance of string+", "false");
338     evalTest("() instance of string*", "true");
339     evalTest("('2') instance of string?", "true");
340     evalTest("('2') instance of string+", "true");
341     evalTest("('2') instance of string*", "true");
342     evalTest("('2','3') instance of string?", "false");
343     evalTest("('2','3') instance of string+", "true");
344     evalTest("('2','3') instance of string*", "true");
345
346     evalTest("declare namespace Int='class:java.lang.Integer';\n"
347          + "Int:toHexString(266)", "10a");
348     evalTest("declare namespace File='class:java.io.File';\n"
349          + "declare function local:make-file ($x as string) {File:new($x)};\n"
350          + "declare function local:parent ($x) {java.io.File:getParent($x)};\n"
351          + "local:parent(local:make-file('dir/mine.txt'))", "dir");
352     evalTest("java.lang.Integer:toHexString(255)", "ff");
353
354     // String functions
355
evalTest("substring('motor car', 6)", "car");
356     evalTest("substring('metadata', 4, 3)", "ada");
357     // evalTest("substring('metadata', -INF, 3)", "met");
358

359     evalTest("(1 to 20)[. mod 5 = 0]", "5 10 15 20");
360     evalTest("(1 to 20)[. mod 5 ge 3]", "3 4 8 9 13 14 18 19");
361     evalTest("1,(99 to 0),3", "1 3");
362     evalTest("-10 to -2", "-10 -9 -8 -7 -6 -5 -4 -3 -2");
363
364     String JavaDoc some_elements =
365       "let $top := <top><a/><b/><c/><d/></top>,"
366       + " $a:=$top/a, $b:=$top/b, $c:=$top/c, $d:=$top/d return ";
367     evalNodeNames(some_elements+"($b, $a) union ($a, $b)", "a;b;");
368     evalNodeNames(some_elements+"($b, $a) union ($b, $c)", "a;b;c;");
369     evalNodeNames(some_elements+"($b, $a) intersect ($a, $b)", "a;b;");
370     evalNodeNames(some_elements+"($b, $a) intersect ($b, $c)", "b;");
371     evalNodeNames(some_elements+"($b, $a) except ($a, $b)", "");
372     evalNodeNames(some_elements+"($b, $a) except ($b, $c)", "a;");
373     evalNodeNames(some_elements+"($b, $a, $b, $d) intersect ($b, $d)", "b;d;");
374     evalNodeNames(some_elements+"($b, $a, $b, $d) except ($b, $d)", "a;");
375     evalNodeNames(some_elements+"($b, $a, $b, $d) except ()", "a;b;d;");
376
377     // Check for catching errors:
378
evalTest("+ +", "*** syntax error - <string>:1:4: missing expression [XPST0003]");
379
380     evalTest("declare namespace x1='XXX",
381          "*** caught SyntaxException - <string>:1:22: "
382          + "unexpected end-of-file in string starting here [XPST0003]");
383
384     evalTest("unescaped-data('<?--->'),let $x:=unescaped-data('an &amp;oslash;') return <b>{unescaped-data('<![CDATA[saw]]>')} {$x}</b>",
385          "<?---><b><![CDATA[saw]]> an &oslash;</b>");
386
387     evalTestIdAttrs("doc('outline.xml')/book/part/chapter/ancestor::*",
388             "b1;P1;");
389     evalTestIdAttrs("doc('outline.xml')/book/part/"
390             +"chapter/ancestor-or-self::node()",
391             ";b1;P1;c1;c2;");
392     evalTestIdAttrs("doc('outline.xml')//"
393             +"section[@id='s1']/following-sibling::*",
394             "s2;s3;");
395     evalTestIdAttrs("doc('outline.xml')//chapter/self::*",
396             "c1;c2;");
397     evalTestIdAttrs("doc('outline.xml')//"
398             +"para[@id='p31']/preceding::*",
399             "s1;s11;s2;");
400     evalTestIdAttrs("doc('outline.xml')//"
401             +"section[@id='s5']/preceding-sibling::*",
402             "s4;");
403     evalTestIdAttrs("doc('outline.xml')//"
404             +"chapter[@id='c1']/following::*",
405             "c2;s4;s5;");
406     evalTestIdAttrs("doc('outline.xml')//"
407             +"section[@id='s1']/(/book)",
408             "b1;");
409     evalTestIdAttrs("doc('outline.xml')//"
410             +"section[@id='s1']/(//chapter)",
411             "c1;c2;");
412
413     evalTest("declare namespace XQuery = 'class:gnu.xquery.lang.XQuery';"
414          + "XQuery:eval-with-focus(XQuery:getInstance(),"
415          + " '<r pos=\"{position()}\">{.}</r>', (<b/>, 3))",
416          "<r pos=\"1\"><b /></r><r pos=\"2\">3</r>");
417     evalTest("declare namespace XQuery = 'class:gnu.xquery.lang.XQuery';"
418          + "XQuery:eval-with-focus(XQuery:getInstance(),"
419          + " '<r pos=\"{position()}\">{.}</r>', <b/>, 3, 4)",
420          "<r pos=\"3\"><b /></r>");
421
422     Object JavaDoc r;
423     String JavaDoc e = "<r pos='{position()}' size='{last()}'>{.}</r>";
424     try
425       {
426     r = toString(interp.evalWithFocus(e, interp.eval("2,3,4")));
427       }
428     catch (Throwable JavaDoc ex)
429       {
430     r = ex;
431       }
432     matchTest(e, r,
433           "<r pos=\"1\" size=\"3\">2</r>"
434           + "<r pos=\"2\" size=\"3\">3</r>"
435           + "<r pos=\"3\" size=\"3\">4</r>");
436     try
437       {
438     r = toString(interp.evalWithFocus(e, interp.eval("<b/>"), 4, 10));
439       }
440     catch (Throwable JavaDoc ex)
441       {
442     r = ex;
443       }
444     matchTest(e, r,
445           "<r pos=\"4\" size=\"10\"><b/></r>");
446
447     printSummary();
448   }
449
450   public static boolean printSummary ()
451   {
452     System.out.println("# of expected passes " + expectedPasses);
453     if (expectedFailures > 0)
454       System.out.println("# of expected failures " + expectedFailures);
455     if (unexpectedPasses > 0)
456       System.out.println("# of unexpected passes " + unexpectedPasses);
457     if (unexpectedFailures > 0)
458       System.out.println("# of unexpected failures " + unexpectedFailures);
459     return unexpectedFailures != 0;
460   }
461
462   /** True if the two string match, ignoring unquoted white-space. */
463   public static boolean matches(String JavaDoc str1, String JavaDoc str2)
464   {
465     int i = 0;
466     int j = 0;
467     char quote = 0;
468     for (;;)
469       {
470     char x, y;
471     for (;;)
472       {
473         if (i >= str1.length())
474           {
475         x = 0;
476         break;
477           }
478         x = str1.charAt(i++);
479         if (quote != 0 || ! Character.isWhitespace(x))
480           break;
481       }
482     for (;;)
483       {
484         if (j >= str2.length())
485           {
486         y = 0;
487         break;
488           }
489         y = str2.charAt(j++);
490         if (quote != 0 || ! Character.isWhitespace(y))
491           break;
492       }
493     if (x != y)
494       return false;
495     if (x == 0)
496       return true;
497     if (x == '\'' || x == '\"')
498       {
499         if (quote == 0)
500           quote = x;
501         else if (x == quote)
502           quote = 0;
503       }
504       }
505   }
506
507   private static void evalTestIdAttrs(String JavaDoc expr, String JavaDoc expected)
508   {
509     evalTest("for $x in (" + expr + ") return (string($x/@id),';')", expected);
510   }
511
512   private static void evalNodeNames(String JavaDoc expr, String JavaDoc expected)
513   {
514     evalTest("for $node in (" + expr + ") return concat(node-name($node),';')",
515          expected);
516   }
517
518   public static void evalTest(String JavaDoc expr, String JavaDoc expected)
519   {
520     Object JavaDoc result;
521     try
522       {
523     result = eval(expr);
524       }
525     catch (Throwable JavaDoc ex)
526       {
527     result = ex;
528       }
529     matchTest(expr, result, expected);
530   }
531
532   public static String JavaDoc toString (Object JavaDoc value)
533   {
534     CharArrayOutPort wr = new CharArrayOutPort();
535     gnu.xml.XMLPrinter xp = new gnu.xml.XMLPrinter(wr);
536     xp.writeObject(value);
537     xp.close();
538     return wr.toString();
539   }
540
541   public static void matchTest(String JavaDoc expr, Object JavaDoc returned, String JavaDoc expected)
542   {
543     String JavaDoc result;
544     Throwable JavaDoc throwable;
545     if (returned instanceof Throwable JavaDoc)
546       {
547     if (returned instanceof WrappedException)
548       {
549         throwable = ((WrappedException) returned).getException();
550         if (throwable != null)
551           returned = throwable;
552       }
553     throwable = (Throwable JavaDoc) returned;
554         // throwable.printStackTrace();
555
if (returned instanceof SyntaxException)
556       result = "*** caught SyntaxException - "
557         + ((SyntaxException) returned).getMessages().getErrors();
558     else
559       result = "*** caught " + returned.getClass().getName() + " ***";
560       }
561     else
562       {
563     result = returned.toString();
564     throwable = null;
565       }
566
567     boolean failureExpected = failureExpectedNext != null;
568     if (matches(expected, result))
569       {
570     if (failureExpected)
571       unexpectedPasses++;
572     else
573       expectedPasses++;
574     if (verbose || failureExpected)
575       System.out.println((failureExpected ? "XPASS: \"" : "PASS: \"")
576                  + expr + "\" evaluated to \"" + result + "\"");
577       }
578     else
579       {
580     if (failureExpectedNext != null)
581       expectedFailures++;
582     else
583       unexpectedFailures++;
584     if (verbose || ! failureExpected)
585       System.out.println((failureExpected ? "XFAIL: \"" : "FAIL: \"")
586                  + expr + "\" evaluated to \"" + result
587                  + "\" but expected \"" + expected + "\"");
588     if (verbose && ! failureExpected && throwable != null)
589       throwable.printStackTrace(System.out);
590       }
591     failureExpectedNext = null;
592   }
593
594   public static String JavaDoc eval(String JavaDoc expr)
595     throws Throwable JavaDoc
596   {
597     CharArrayOutPort out = new CharArrayOutPort();
598     InPort in = new CharArrayInPort(expr);
599     SourceMessages messages = new SourceMessages();
600
601     Compilation comp = interp.parse(in, messages, Language.PARSE_IMMEDIATE);
602     SourceError firstError = messages.getErrors();
603     if (firstError != null)
604       return "*** syntax error - " + firstError;
605
606     CallContext ctx = CallContext.getInstance();
607     gnu.lists.Consumer save = ctx.consumer;
608
609     try
610       {
611     ctx.consumer = interp.getOutputConsumer(out);
612     ModuleExp mod = comp.getModule();
613     mod.setName("atInteractiveLevel"); // FIXME
614
ModuleExp.evalModule(env, ctx, comp, null, null);
615       }
616     finally
617       {
618     ctx.consumer = save;
619       }
620
621     String JavaDoc result = new String JavaDoc(out.toCharArray());
622     out.close();
623     return result;
624   }
625
626 }
627
Popular Tags