    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.JXPathTestCase;
    19  import org.apache.commons.jxpath.ri.Parser;
    20
    21  /***
    22   * Tests the determination of whether an expression is context dependent.
    23   *
    24   * @author Dmitri Plotnikov
    25   * @version $Revision: 1.5 $ $Date: 2004/02/29 14:17:42 $
    26   */
    27
    28  public class ContextDependencyTest extends JXPathTestCase {
    29      public ContextDependencyTest(String name) {
    30          super(name);
    31      }
    32
    33      public void testContextDependency() {
    34          testContextDependency("1", false);
    35          testContextDependency("$x", false);
    36          testContextDependency("/foo", false);
    37          testContextDependency("foo", true);
    38          testContextDependency("/foo[3]", false);
    39          testContextDependency("/foo[$x]", false);
    40          testContextDependency("/foo[bar]", true);
    41          testContextDependency("3 + 5", false);
    42          testContextDependency("test:func(3, 5)", true);
    43          testContextDependency("test:func(3, foo)", true);
    44      }
    45
    46      public void testContextDependency(String xpath, boolean expected) {
    47          Expression expr =
    48              (Expression) Parser.parseExpression(xpath, new TreeCompiler());
    49
    50          assertEquals(
    51              "Context dependency <" + xpath + ">",
    52              expected,
    53              expr.isContextDependent());
    54      }
    55  }