1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.jxpath.ri.compiler;
18
19 import org.apache.commons.jxpath.JXPathContext;
20 import org.apache.commons.jxpath.JXPathTestCase;
21 import org.apache.commons.jxpath.Variables;
22
23
24
25
26
27
28
29
30 public class CoreOperationTest extends JXPathTestCase {
31 private JXPathContext context;
32
33 public void setUp() {
34 if (context == null) {
35 context = JXPathContext.newContext(null);
36 Variables vars = context.getVariables();
37 vars.declareVariable("integer", new Integer(1));
38 vars.declareVariable("array", new double[] { 0.25, 0.5, 0.75 });
39 vars.declareVariable("nan", new Double(Double.NaN));
40 }
41 }
42
43 public void testInfoSetTypes() {
44
45
46 assertXPathValue(context, "1", new Double(1.0));
47 assertXPathPointer(context, "1", "1");
48 assertXPathValueIterator(context, "1", list(new Double(1.0)));
49
50 assertXPathPointerIterator(context, "1", list("1"));
51
52 assertXPathValue(context, "-1", new Double(-1.0));
53 assertXPathValue(context, "2 + 2", new Double(4.0));
54 assertXPathValue(context, "3 - 2", new Double(1.0));
55 assertXPathValue(context, "1 + 2 + 3 - 4 + 5", new Double(7.0));
56 assertXPathValue(context, "3 * 2", new Double(3.0 * 2.0));
57 assertXPathValue(context, "3 div 2", new Double(3.0 / 2.0));
58 assertXPathValue(context, "5 mod 2", new Double(1.0));
59
60
61 assertXPathValue(context, "5.9 mod 2.1", new Double(1.0));
62
63 assertXPathValue(context, "5 mod -2", new Double(1.0));
64 assertXPathValue(context, "-5 mod 2", new Double(-1.0));
65 assertXPathValue(context, "-5 mod -2", new Double(-1.0));
66 assertXPathValue(context, "1 < 2", Boolean.TRUE);
67 assertXPathValue(context, "1 > 2", Boolean.FALSE);
68 assertXPathValue(context, "1 <= 1", Boolean.TRUE);
69 assertXPathValue(context, "1 >= 2", Boolean.FALSE);
70 assertXPathValue(context, "3 > 2 > 1", Boolean.FALSE);
71 assertXPathValue(context, "3 > 2 and 2 > 1", Boolean.TRUE);
72 assertXPathValue(context, "3 > 2 and 2 < 1", Boolean.FALSE);
73 assertXPathValue(context, "3 < 2 or 2 > 1", Boolean.TRUE);
74 assertXPathValue(context, "3 < 2 or 2 < 1", Boolean.FALSE);
75 assertXPathValue(context, "1 = 1", Boolean.TRUE);
76 assertXPathValue(context, "1 = '1'", Boolean.TRUE);
77 assertXPathValue(context, "1 > 2 = 2 > 3", Boolean.TRUE);
78 assertXPathValue(context, "1 > 2 = 0", Boolean.TRUE);
79 assertXPathValue(context, "1 = 2", Boolean.FALSE);
80
81 assertXPathValue(context, "$integer", new Double(1), Double.class);
82
83 assertXPathValue(context, "2 + 3", "5.0", String.class);
84
85 assertXPathValue(context, "2 + 3", Boolean.TRUE, boolean.class);
86
87 assertXPathValue(context, "'true'", Boolean.TRUE, Boolean.class);
88 }
89
90 public void testNodeSetOperations() {
91 assertXPathValue(context, "$array > 0", Boolean.TRUE, Boolean.class);
92 assertXPathValue(context, "$array >= 0", Boolean.TRUE, Boolean.class);
93 assertXPathValue(context, "$array = 0", Boolean.FALSE, Boolean.class);
94 assertXPathValue(context, "$array = 0.25", Boolean.TRUE, Boolean.class);
95 assertXPathValue(context, "$array = 0.5", Boolean.TRUE, Boolean.class);
96 assertXPathValue(context, "$array = 0.50000", Boolean.TRUE, Boolean.class);
97 assertXPathValue(context, "$array = 0.75", Boolean.TRUE, Boolean.class);
98 assertXPathValue(context, "$array < 1", Boolean.TRUE, Boolean.class);
99 assertXPathValue(context, "$array <= 1", Boolean.TRUE, Boolean.class);
100 assertXPathValue(context, "$array = 1", Boolean.FALSE, Boolean.class);
101 assertXPathValue(context, "$array > 1", Boolean.FALSE, Boolean.class);
102 assertXPathValue(context, "$array < 0", Boolean.FALSE, Boolean.class);
103 }
104
105 public void testEmptyNodeSetOperations() {
106 assertXPathValue(context, "/idonotexist = 0", Boolean.FALSE, Boolean.class);
107 assertXPathValue(context, "/idonotexist != 0", Boolean.FALSE, Boolean.class);
108 assertXPathValue(context, "/idonotexist < 0", Boolean.FALSE, Boolean.class);
109 assertXPathValue(context, "/idonotexist > 0", Boolean.FALSE, Boolean.class);
110 assertXPathValue(context, "/idonotexist >= 0", Boolean.FALSE, Boolean.class);
111 assertXPathValue(context, "/idonotexist <= 0", Boolean.FALSE, Boolean.class);
112 assertXPathValue(context, "$array[position() < 1] = 0", Boolean.FALSE, Boolean.class);
113 assertXPathValue(context, "$array[position() < 1] != 0", Boolean.FALSE, Boolean.class);
114 assertXPathValue(context, "$array[position() < 1] < 0", Boolean.FALSE, Boolean.class);
115 assertXPathValue(context, "$array[position() < 1] > 0", Boolean.FALSE, Boolean.class);
116 assertXPathValue(context, "$array[position() < 1] >= 0", Boolean.FALSE, Boolean.class);
117 assertXPathValue(context, "$array[position() < 1] <= 0", Boolean.FALSE, Boolean.class);
118 }
119
120 public void testNan() {
121 assertXPathValue(context, "$nan > $nan", Boolean.FALSE, Boolean.class);
122 assertXPathValue(context, "$nan < $nan", Boolean.FALSE, Boolean.class);
123 assertXPathValue(context, "$nan >= $nan", Boolean.FALSE, Boolean.class);
124 assertXPathValue(context, "$nan <= $nan", Boolean.FALSE, Boolean.class);
125 assertXPathValue(context, "$nan >= $nan and $nan <= $nan", Boolean.FALSE, Boolean.class);
126 assertXPathValue(context, "$nan = $nan", Boolean.FALSE, Boolean.class);
127 assertXPathValue(context, "$nan != $nan", Boolean.FALSE, Boolean.class);
128 assertXPathValue(context, "$nan > 0", Boolean.FALSE, Boolean.class);
129 assertXPathValue(context, "$nan < 0", Boolean.FALSE, Boolean.class);
130 assertXPathValue(context, "$nan >= 0", Boolean.FALSE, Boolean.class);
131 assertXPathValue(context, "$nan <= 0", Boolean.FALSE, Boolean.class);
132 assertXPathValue(context, "$nan >= 0 and $nan <= 0", Boolean.FALSE, Boolean.class);
133 assertXPathValue(context, "$nan = 0", Boolean.FALSE, Boolean.class);
134 assertXPathValue(context, "$nan != 0", Boolean.FALSE, Boolean.class);
135 assertXPathValue(context, "$nan > 1", Boolean.FALSE, Boolean.class);
136 assertXPathValue(context, "$nan < 1", Boolean.FALSE, Boolean.class);
137 assertXPathValue(context, "$nan >= 1", Boolean.FALSE, Boolean.class);
138 assertXPathValue(context, "$nan <= 1", Boolean.FALSE, Boolean.class);
139 assertXPathValue(context, "$nan >= 1 and $nan <= 1", Boolean.FALSE, Boolean.class);
140 assertXPathValue(context, "$nan = 1", Boolean.FALSE, Boolean.class);
141 assertXPathValue(context, "$nan != 1", Boolean.FALSE, Boolean.class);
142 }
143 }