View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Test basic functionality of JXPath - infoset types,
25   * operations.
26   *
27   * @author Dmitri Plotnikov
28   * @version $Revision: 652845 $ $Date: 2008-05-02 19:46:46 +0200 (Fr, 02 Mai 2008) $
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          // Numbers
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          // This test produces a different result with Xalan?
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 }