001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.jxpath.ri.compiler;
018
019import org.apache.commons.jxpath.JXPathContext;
020import org.apache.commons.jxpath.JXPathTestCase;
021import org.apache.commons.jxpath.Variables;
022
023/**
024 * Test basic functionality of JXPath - infoset types,
025 * operations.
026 *
027 * @author Dmitri Plotnikov
028 * @version $Revision: 652845 $ $Date: 2008-05-02 13:46:46 -0400 (Fri, 02 May 2008) $
029 */
030public class CoreOperationTest extends JXPathTestCase {
031    private JXPathContext context;
032
033    public void setUp() {
034        if (context == null) {
035            context = JXPathContext.newContext(null);
036            Variables vars = context.getVariables();
037            vars.declareVariable("integer", new Integer(1));
038            vars.declareVariable("array", new double[] { 0.25, 0.5, 0.75 });
039            vars.declareVariable("nan", new Double(Double.NaN));
040        }
041    }
042
043    public void testInfoSetTypes() {
044
045        // Numbers
046        assertXPathValue(context, "1", new Double(1.0));
047        assertXPathPointer(context, "1", "1");
048        assertXPathValueIterator(context, "1", list(new Double(1.0)));
049
050        assertXPathPointerIterator(context, "1", list("1"));
051
052        assertXPathValue(context, "-1", new Double(-1.0));
053        assertXPathValue(context, "2 + 2", new Double(4.0));
054        assertXPathValue(context, "3 - 2", new Double(1.0));
055        assertXPathValue(context, "1 + 2 + 3 - 4 + 5", new Double(7.0));
056        assertXPathValue(context, "3 * 2", new Double(3.0 * 2.0));
057        assertXPathValue(context, "3 div 2", new Double(3.0 / 2.0));
058        assertXPathValue(context, "5 mod 2", new Double(1.0));
059
060        // This test produces a different result with Xalan?
061        assertXPathValue(context, "5.9 mod 2.1", new Double(1.0));
062
063        assertXPathValue(context, "5 mod -2", new Double(1.0));
064        assertXPathValue(context, "-5 mod 2", new Double(-1.0));
065        assertXPathValue(context, "-5 mod -2", new Double(-1.0));
066        assertXPathValue(context, "1 < 2", Boolean.TRUE);
067        assertXPathValue(context, "1 > 2", Boolean.FALSE);
068        assertXPathValue(context, "1 <= 1", Boolean.TRUE);
069        assertXPathValue(context, "1 >= 2", Boolean.FALSE);
070        assertXPathValue(context, "3 > 2 > 1", Boolean.FALSE);
071        assertXPathValue(context, "3 > 2 and 2 > 1", Boolean.TRUE);
072        assertXPathValue(context, "3 > 2 and 2 < 1", Boolean.FALSE);
073        assertXPathValue(context, "3 < 2 or 2 > 1", Boolean.TRUE);
074        assertXPathValue(context, "3 < 2 or 2 < 1", Boolean.FALSE);
075        assertXPathValue(context, "1 = 1", Boolean.TRUE);
076        assertXPathValue(context, "1 = '1'", Boolean.TRUE);
077        assertXPathValue(context, "1 > 2 = 2 > 3", Boolean.TRUE);
078        assertXPathValue(context, "1 > 2 = 0", Boolean.TRUE);
079        assertXPathValue(context, "1 = 2", Boolean.FALSE);
080
081        assertXPathValue(context, "$integer", new Double(1), Double.class);
082
083        assertXPathValue(context, "2 + 3", "5.0", String.class);
084
085        assertXPathValue(context, "2 + 3", Boolean.TRUE, boolean.class);
086
087        assertXPathValue(context, "'true'", Boolean.TRUE, Boolean.class);
088    }
089
090    public void testNodeSetOperations() {
091        assertXPathValue(context, "$array > 0", Boolean.TRUE, Boolean.class);
092        assertXPathValue(context, "$array >= 0", Boolean.TRUE, Boolean.class);
093        assertXPathValue(context, "$array = 0", Boolean.FALSE, Boolean.class);
094        assertXPathValue(context, "$array = 0.25", Boolean.TRUE, Boolean.class);
095        assertXPathValue(context, "$array = 0.5", Boolean.TRUE, Boolean.class);
096        assertXPathValue(context, "$array = 0.50000", Boolean.TRUE, Boolean.class);
097        assertXPathValue(context, "$array = 0.75", Boolean.TRUE, Boolean.class);
098        assertXPathValue(context, "$array < 1", Boolean.TRUE, Boolean.class);
099        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}