    1   /*
    2    * Copyright 1999-2004 The Apache Software Foundation
    3    *
    4    * Licensed under the Apache License, Version 2.0 (the "License");
    5    * you may not use this file except in compliance with the License.
    6    * You may obtain a copy of the License at
    7    *
    8    *     http://www.apache.org/licenses/LICENSE-2.0
    9    *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  package org.apache.commons.jxpath.ri.compiler;
    17
    18  import org.apache.commons.jxpath.JXPathContext;
    19  import org.apache.commons.jxpath.JXPathTestCase;
    20  import org.apache.commons.jxpath.Variables;
    21
    22  /***
    23   * Test basic functionality of JXPath - infoset types,
    24   * operations.
    25   *
    26   * @author Dmitri Plotnikov
    27   * @version $Revision: 1.5 $ $Date: 2004/02/29 14:17:42 $
    28   */
    29
    30  public class CoreOperationTest extends JXPathTestCase {
    31      private JXPathContext context;
    32
    33      /***
    34       * Construct a new instance of this test case.
    35       *
    36       * @param name Name of the test case
    37       */
    38      public CoreOperationTest(String name) {
    39          super(name);
    40      }
    41
    42      public void setUp() {
    43          if (context == null) {
    44              context = JXPathContext.newContext(null);
    45              Variables vars = context.getVariables();
    46              vars.declareVariable("integer", new Integer(1));
    47          }
    48      }
    49
    50      public void testInfoSetTypes() {
    51
    52          // Numbers
    53          assertXPathValue(context, "1", new Double(1.0));
    54          assertXPathPointer(context, "1", "1");
    55          assertXPathValueIterator(context, "1", list(new Double(1.0)));
    56
    57          assertXPathPointerIterator(context, "1", list("1"));
    58
    59          assertXPathValue(context, "-1", new Double(-1.0));
    60          assertXPathValue(context, "2 + 2", new Double(4.0));
    61          assertXPathValue(context, "3 - 2", new Double(1.0));
    62          assertXPathValue(context, "1 + 2 + 3 - 4 + 5", new Double(7.0));
    63          assertXPathValue(context, "3 * 2", new Double(3.0 * 2.0));
    64          assertXPathValue(context, "3 div 2", new Double(3.0 / 2.0));
    65          assertXPathValue(context, "5 mod 2", new Double(1.0));
    66
    67          // This test produces a different result with Xalan?
    68          assertXPathValue(context, "5.9 mod 2.1", new Double(1.0));
    69
    70          assertXPathValue(context, "5 mod -2", new Double(1.0));
    71          assertXPathValue(context, "-5 mod 2", new Double(-1.0));
    72          assertXPathValue(context, "-5 mod -2", new Double(-1.0));
    73          assertXPathValue(context, "1 < 2", Boolean.TRUE);
    74          assertXPathValue(context, "1 > 2", Boolean.FALSE);
    75          assertXPathValue(context, "1 <= 1", Boolean.TRUE);
    76          assertXPathValue(context, "1 >= 2", Boolean.FALSE);
    77          assertXPathValue(context, "3 > 2 > 1", Boolean.FALSE);
    78          assertXPathValue(context, "3 > 2 and 2 > 1", Boolean.TRUE);
    79          assertXPathValue(context, "3 > 2 and 2 < 1", Boolean.FALSE);
    80          assertXPathValue(context, "3 < 2 or 2 > 1", Boolean.TRUE);
    81          assertXPathValue(context, "3 < 2 or 2 < 1", Boolean.FALSE);
    82          assertXPathValue(context, "1 = 1", Boolean.TRUE);
    83          assertXPathValue(context, "1 = '1'", Boolean.TRUE);
    84          assertXPathValue(context, "1 > 2 = 2 > 3", Boolean.TRUE);
    85          assertXPathValue(context, "1 > 2 = 0", Boolean.TRUE);
    86          assertXPathValue(context, "1 = 2", Boolean.FALSE);
    87
    88          assertXPathValue(context, "$integer", new Double(1), Double.class);
    89
    90          assertXPathValue(context, "2 + 3", "5.0", String.class);
    91
    92          assertXPathValue(context, "2 + 3", Boolean.TRUE, boolean.class);
    93
    94          assertXPathValue(context, "'true'", Boolean.TRUE, Boolean.class);
    95      }
    96  }