simplepathinterpretertest.java
来自「JXPath」· Java 代码 · 共 650 行 · 第 1/2 页
JAVA
650 行
"/map[@name='foo']/bar",
"BbDdNn");
// existingProperty(node)[@name=missingProperty]/anotherStep
assertNullPointer("/vendor[@name='foo']/bar",
"/vendor[@name='foo']/bar",
"BbMnNn");
// existingProperty(node)[@name=missingProperty][index]/anotherStep
assertNullPointer("/vendor[@name='foo'][3]/bar",
"/vendor[@name='foo'][3]/bar",
"BbMnNn");
// Existing dynamic property + existing property
assertValueAndPointer("/map[@name='Key2'][@name='name']",
"Name 6",
"/map[@name='Key2']/name",
"BbDdBb",
"BbDdBbB");
// Existing dynamic property + existing property + index
assertValueAndPointer("/map[@name='Key2'][@name='strings'][2]",
"String 2",
"/map[@name='Key2']/strings[2]",
"BbDdBb",
"BbDdBbB");
// bean/map/map/property
assertValueAndPointer("map[@name='Key5'][@name='key']/name",
"Name 9",
"/map[@name='Key5'][@name='key']/name",
"BbDdDdBb",
"BbDdDdBbB");
assertNullPointer("map[@name='Key2'][@name='foo']",
"/map[@name='Key2'][@name='foo']",
"BbDdBn");
assertNullPointer("map[@name='Key2'][@name='foo'][@name='bar']",
"/map[@name='Key2'][@name='foo'][@name='bar']",
"BbDdBnNn");
// bean/map/node
assertValueAndPointer("map[@name='Key4'][@name='fruitco']",
context.getValue("/vendor"),
"/map[@name='Key4']",
"BbDdM");
}
public void testDoPredicatesStandard() {
// bean/map/collection/node
assertValueAndPointer("map[@name='Key3'][@name='fruitco']",
context.getValue("/vendor"),
"/map[@name='Key3'][3]",
"BbDdCM");
// bean/map/collection/missingNode
assertNullPointer("map[@name='Key3'][@name='foo']",
"/map[@name='Key3'][4][@name='foo']",
"BbDdCDd");
// bean/map/node
assertValueAndPointer("map[@name='Key4'][@name='fruitco']",
context.getValue("/vendor"),
"/map[@name='Key4']",
"BbDdM");
// bean/map/emptyCollection[@name=foo]
assertNullPointer("map[@name='Key6'][@name='fruitco']",
"/map[@name='Key6'][@name='fruitco']",
"BbDdCn");
// bean/node[@name=foo][index]
assertValueAndPointer("/vendor/contact[@name='jack'][2]",
"Jack Black",
"/vendor/contact[4]",
"BbMM");
// bean/node[@name=foo][missingIndex]
assertNullPointer("/vendor/contact[@name='jack'][5]",
"/vendor/contact[@name='jack'][5]",
"BbMnNn");
// bean/node/.[@name=foo][index]
assertValueAndPointer("/vendor/contact/.[@name='jack']",
"Jack",
"/vendor/contact[2]",
"BbMM");
}
public void testDoPredicateIndex() {
// Existing dynamic property + existing property + index
assertValueAndPointer("/map[@name='Key2'][@name='strings'][2]",
"String 2",
"/map[@name='Key2']/strings[2]",
"BbDdBb",
"BbDdBbB");
// existingProperty[@name=collectionProperty][index]
assertValueAndPointer("/nestedBean[@name='strings'][2]",
bean.getNestedBean().getStrings()[1],
"/nestedBean/strings[2]",
"BbBb",
"BbBbB");
// existingProperty[@name=missingProperty][index]
assertNullPointer("/nestedBean[@name='foo'][3]",
"/nestedBean[@name='foo'][3]",
"BbBn");
// existingProperty[@name=collectionProperty][missingIndex]
assertNullPointer("/nestedBean[@name='strings'][5]",
"/nestedBean/strings[5]",
"BbBbE");
// map[@name=collectionProperty][index]
assertValueAndPointer("/map[@name='Key3'][2]",
new Integer(2),
"/map[@name='Key3'][2]",
"BbDd",
"BbDdB");
// map[@name=collectionProperty][missingIndex]
assertNullPointer("/map[@name='Key3'][5]",
"/map[@name='Key3'][5]",
"BbDdE");
// map[@name=collectionProperty][missingIndex]/property
assertNullPointer("/map[@name='Key3'][5]/foo",
"/map[@name='Key3'][5]/foo",
"BbDdENn");
// map[@name=map][@name=collection][index]
assertValueAndPointer("/map[@name='Key5'][@name='strings'][2]",
"String 2",
"/map[@name='Key5'][@name='strings'][2]",
"BbDdDd",
"BbDdDdB");
// map[@name=map][@name=collection][missingIndex]
assertNullPointer("/map[@name='Key5'][@name='strings'][5]",
"/map[@name='Key5'][@name='strings'][5]",
"BbDdDdE");
// Existing dynamic property + indexing
assertValueAndPointer("/map[@name='Key3'][2]",
new Integer(2),
"/map[@name='Key3'][2]",
"BbDd",
"BbDdB");
// Existing dynamic property + indexing
assertValueAndPointer("/map[@name='Key3'][1]/name",
"some",
"/map[@name='Key3'][1]/name",
"BbDdBb",
"BbDdBbB");
// map[@name=missingProperty][index]
assertNullPointer("/map[@name='foo'][3]",
"/map[@name='foo'][3]",
"BbDdE");
// collectionProperty[index]
assertValueAndPointer("/integers[2]",
new Integer(2),
"/integers[2]",
"Bb",
"BbB");
// existingProperty/collectionProperty[index]
assertValueAndPointer("/nestedBean/strings[2]",
bean.getNestedBean().getStrings()[1],
"/nestedBean/strings[2]",
"BbBb",
"BbBbB");
// existingProperty[index]/existingProperty
assertValueAndPointer("/list[3]/int",
new Integer(1),
"/list[3]/int",
"BbBb",
"BbBbB");
// existingProperty[missingIndex]
assertNullPointer("/list[6]",
"/list[6]",
"BbE");
// existingProperty/missingProperty[index]
assertNullPointer("/nestedBean/foo[3]",
"/nestedBean/foo[3]",
"BbBn");
// map[@name=missingProperty][index]
assertNullPointer("/map/foo[3]",
"/map[@name='foo'][3]",
"BbDdE");
// existingProperty/collectionProperty[missingIndex]
assertNullPointer("/nestedBean/strings[5]",
"/nestedBean/strings[5]",
"BbBbE");
// map/collectionProperty[missingIndex]/property
assertNullPointer("/map/Key3[5]/foo",
"/map[@name='Key3'][5]/foo",
"BbDdENn");
// map[@name=map]/collection[index]
assertValueAndPointer("/map[@name='Key5']/strings[2]",
"String 2",
"/map[@name='Key5'][@name='strings'][2]",
"BbDdDd",
"BbDdDdB");
// map[@name=map]/collection[missingIndex]
assertNullPointer("/map[@name='Key5']/strings[5]",
"/map[@name='Key5'][@name='strings'][5]",
"BbDdDdE");
// scalarPropertyAsCollection[index]
assertValueAndPointer("/int[1]",
new Integer(1),
"/int",
"Bb",
"BbB");
// scalarPropertyAsCollection[index]
assertValueAndPointer(".[1]/int",
new Integer(1),
"/int",
"Bb",
"BbB");
}
public void testInterpretExpressionPath() {
context.getVariables().declareVariable("array", new String[]{"Value1"});
context.getVariables().declareVariable("testnull", new TestNull());
assertNullPointer("$testnull/nothing[2]",
"$testnull/nothing[2]",
"VBbE");
}
private void assertValueAndPointer(
String path, Object expectedValue, String expectedPath,
String expectedSignature)
{
assertValueAndPointer(
path,
expectedValue,
expectedPath,
expectedSignature,
expectedSignature);
}
private void assertValueAndPointer(
String path, Object expectedValue, String expectedPath,
String expectedSignature, String expectedValueSignature)
{
Object value = context.getValue(path);
assertEquals("Checking value: " + path, expectedValue, value);
Pointer pointer = context.getPointer(path);
assertEquals("Checking pointer: " + path,
expectedPath, pointer.toString());
assertEquals("Checking signature: " + path,
expectedSignature, pointerSignature(pointer));
Pointer vPointer = ((NodePointer) pointer).getValuePointer();
assertEquals("Checking value pointer signature: " + path,
expectedValueSignature, pointerSignature(vPointer));
}
private void assertNullPointer(String path, String expectedPath,
String expectedSignature)
{
Pointer pointer = context.getPointer(path);
assertNotNull("Null path exists: " + path,
pointer);
assertEquals("Null path as path: " + path,
expectedPath, pointer.asPath());
assertEquals("Checking Signature: " + path,
expectedSignature, pointerSignature(pointer));
Pointer vPointer = ((NodePointer) pointer).getValuePointer();
assertTrue("Null path is null: " + path,
!((NodePointer) vPointer).isActual());
assertEquals("Checking value pointer signature: " + path,
expectedSignature + "N", pointerSignature(vPointer));
}
/**
* Since we need to test the internal Signature of a pointer,
* we will get a signature which will contain a single character
* per pointer in the chain, representing that pointer's type.
*/
private String pointerSignature(Pointer pointer) {
if (pointer == null) {
return "";
}
char type = '?';
if (pointer instanceof NullPointer) { type = 'N'; }
else if (pointer instanceof NullPropertyPointer) { type = 'n'; }
else if (pointer instanceof NullElementPointer) { type = 'E'; }
else if (pointer instanceof VariablePointer) { type = 'V'; }
else if (pointer instanceof CollectionPointer) { type = 'C'; }
else if (pointer instanceof BeanPointer) { type = 'B'; }
else if (pointer instanceof BeanPropertyPointer) { type = 'b'; }
else if (pointer instanceof DynamicPointer) { type = 'D'; }
else if (pointer instanceof DynamicPropertyPointer) { type = 'd'; }
else if (pointer instanceof DOMNodePointer) { type = 'M'; }
else {
System.err.println("UNKNOWN TYPE: " + pointer.getClass());
}
NodePointer parent =
((NodePointer) pointer).getImmediateParentPointer();
return pointerSignature(parent) + type;
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?