Skip to content
Permalink
Browse files
Add test case proofing that JXPATH-177 is not a problem. Thanks to Ma…
…rtin Xarx and Uwe Barthel. This also fixes #5 from github.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/jxpath/trunk@1716199 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
britter committed Nov 24, 2015
1 parent 4787cce commit caa754ffd11c7b70de96e73984f7ac462bd25c93
Showing 2 changed files with 131 additions and 0 deletions.
@@ -0,0 +1,68 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.jx177;

import org.apache.commons.jxpath.Variables;

public class JXPathVariablesResolver implements Variables
{

private static final long serialVersionUID = -1106360826446119597L;

public static final String ROOT_VAR = "__root";

private final Object root;

public JXPathVariablesResolver(Object root)
{
this.root = root;
}

public boolean isDeclaredVariable(String varName)
{
if (varName == null)
{
throw new IllegalArgumentException("varName");
}
return varName.equals(ROOT_VAR);
}

public Object getVariable(String varName)
{
if (varName == null)
{
throw new IllegalArgumentException("varName");
}
if (!varName.equals(ROOT_VAR))
{
throw new IllegalArgumentException("Variable is not declared: " + varName);
}

return root;
}

public void declareVariable(String varName, Object value)
{
throw new UnsupportedOperationException();
}

public void undeclareVariable(String varName)
{
throw new UnsupportedOperationException();
}

}
@@ -0,0 +1,63 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.jxpath.ri.jx177;

import java.util.HashMap;
import java.util.Map;
import junit.framework.TestCase;
import org.apache.commons.jxpath.JXPathContext;
import org.apache.commons.jxpath.Pointer;

public class TestJxPath177 extends TestCase
{
Map model = new HashMap();
{

model.put("name", "ROOT name");
final HashMap x = new HashMap();
model.put("x", x);
x.put("name", "X name");
}

public void testJx177()
{
doTest("name", "ROOT name");
doTest("/x/name", "X name");
doTest("$__root/x/name", "X name");
}
public void testJx177_Union1()
{
doTest("$__root/x/name|name", "X name");

}
public void testJx177_Union2()
{
doTest("$__root/x/unexisting|name", "ROOT name");

}

private void doTest(String xp, String expected)
{
JXPathContext xpathContext = JXPathContext.newContext(model);
xpathContext.setVariables(new JXPathVariablesResolver(model));
Pointer p = xpathContext.getPointer(xp);
Object result = p.getNode();
assertNotNull(result);
assertEquals(expected, result);

}
}

0 comments on commit caa754f

Please sign in to comment.