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