mixedmodeltest.java
来自「JXPath」· Java 代码 · 共 532 行 · 第 1/2 页
JAVA
532 行
"list[2]/int",
new Integer(1),
"/list[2]/int");
}
public void testListMap() {
assertXPathValueAndPointer(
context,
"list[3]/string",
"string",
"/list[3][@name='string']");
}
public void testListList() {
/** @todo: what is this supposed to do? Should we stick to XPath,
* in which case [1] is simply ignored, or Java, in which case
* it is supposed to extract the first element from the list?
*/
// assertXPathValueAndPointer(context,
// "list[4][1]",
// "string2",
// "/list[4][1]");
assertXPathValueAndPointer(
context,
"list[4]/.[1]",
"string2",
"/list[4]/.[1]");
}
public void testListDocument() {
assertXPathValueAndPointer(
context,
"list[5]/vendor/location/address/city",
"Fruit Market",
"/list[5]/vendor[1]/location[2]/address[1]/city[1]");
}
public void testListElement() {
assertXPathValueAndPointer(
context,
"list[6]/location/address/city",
"Fruit Market",
"/list[6]/location[2]/address[1]/city[1]");
}
public void testListContainer() {
assertXPathValueAndPointer(
context,
"list[7]/vendor/location/address/city",
"Fruit Market",
"/list[7]/vendor[1]/location[2]/address[1]/city[1]");
}
public void testNull() {
assertXPathPointerLenient(context, "$null", "$null");
assertXPathPointerLenient(context, "$null[3]", "$null[3]");
assertXPathPointerLenient(
context,
"$testnull/nothing",
"$testnull/nothing");
assertXPathPointerLenient(
context,
"$testnull/nothing[2]",
"$testnull/nothing[2]");
assertXPathPointerLenient(context, "beans[8]/int", "/beans[8]/int");
assertXPathValueIterator(
context,
"$testnull/nothing[1]",
Collections.EMPTY_LIST);
JXPathContext ctx = JXPathContext.newContext(new TestNull());
assertXPathValue(ctx, "nothing", null);
assertXPathValue(ctx, "child/nothing", null);
assertXPathValue(ctx, "array[2]", null);
assertXPathValueLenient(ctx, "nothing/something", null);
assertXPathValueLenient(ctx, "array[2]/something", null);
}
public void testRootAsCollection() {
assertXPathValue(context, ".[1]/string", "string");
}
public void testCreatePath() {
context = JXPathContext.newContext(new TestBean());
context.setFactory(new TestMixedModelFactory());
TestBean bean = (TestBean) context.getContextBean();
bean.setMap(null);
assertXPathCreatePath(
context,
"/map[@name='TestKey5']/nestedBean/int",
new Integer(1),
"/map[@name='TestKey5']/nestedBean/int");
bean.setMap(null);
assertXPathCreatePath(
context,
"/map[@name='TestKey5']/beans[2]/int",
new Integer(1),
"/map[@name='TestKey5']/beans[2]/int");
}
/**
* Test JXPath.iterate() with map containing an array
*/
public void testIterateArray() {
Map map = new HashMap();
map.put("foo", new String[] { "a", "b", "c" });
JXPathContext context = JXPathContext.newContext(map);
assertXPathValueIterator(context, "foo", list("a", "b", "c"));
}
public void testIteratePointersArray() {
Map map = new HashMap();
map.put("foo", new String[] { "a", "b", "c" });
JXPathContext context = JXPathContext.newContext(map);
Iterator it = context.iteratePointers("foo");
List actual = new ArrayList();
while (it.hasNext()) {
Pointer ptr = (Pointer) it.next();
actual.add(context.getValue(ptr.asPath()));
}
assertEquals(
"Iterating pointers <" + "foo" + ">",
list("a", "b", "c"),
actual);
}
public void testIteratePointersArrayElementWithVariable() {
Map map = new HashMap();
map.put("foo", new String[] { "a", "b", "c" });
JXPathContext context = JXPathContext.newContext(map);
context.getVariables().declareVariable("x", new Integer(2));
Iterator it = context.iteratePointers("foo[$x]");
List actual = new ArrayList();
while (it.hasNext()) {
Pointer ptr = (Pointer) it.next();
actual.add(context.getValue(ptr.asPath()));
}
assertEquals("Iterating pointers <" + "foo" + ">", list("b"), actual);
}
public void testIterateVector() {
Map map = new HashMap();
Vector vec = new Vector();
vec.add(new HashMap());
vec.add(new HashMap());
map.put("vec", vec);
JXPathContext context = JXPathContext.newContext(map);
assertXPathPointerIterator(
context,
"/vec",
list("/.[@name='vec'][1]", "/.[@name='vec'][2]"));
}
public void testErrorProperty() {
context.getVariables().declareVariable(
"e",
new ExceptionPropertyTestBean());
boolean ex = false;
try {
assertXPathValue(context, "$e/errorString", null);
}
catch (Throwable t) {
ex = true;
}
assertTrue("Legitimate exception accessing property", ex);
assertXPathPointer(context, "$e/errorString", "$e/errorString");
assertXPathPointerLenient(
context,
"$e/errorStringArray[1]",
"$e/errorStringArray[1]");
assertXPathPointerIterator(
context,
"$e/errorString",
list("$e/errorString"));
assertXPathPointerIterator(
context,
"$e//error",
Collections.EMPTY_LIST);
}
public void testMatrix() {
assertXPathValueAndPointer(
context,
"$matrix[1]/.[1]",
new Integer(3),
"$matrix[1]/.[1]");
context.setValue("$matrix[1]/.[1]", new Integer(2));
assertXPathValueAndPointer(
context,
"matrix[1]/.[1]",
new Integer(3),
"/matrix[1]/.[1]");
context.setValue("matrix[1]/.[1]", "2");
assertXPathValue(context, "matrix[1]/.[1]", new Integer(2));
context.getVariables().declareVariable(
"wholebean",
context.getContextBean());
assertXPathValueAndPointer(
context,
"$wholebean/matrix[1]/.[1]",
new Integer(2),
"$wholebean/matrix[1]/.[1]");
boolean ex = false;
try {
context.setValue("$wholebean/matrix[1]/.[2]", "4");
}
catch (Exception e) {
ex = true;
}
assertTrue("Exception setting value of non-existent element", ex);
ex = false;
try {
context.setValue("$wholebean/matrix[2]/.[1]", "4");
}
catch (Exception e) {
ex = true;
}
assertTrue("Exception setting value of non-existent element", ex);
}
public void testCreatePathAndSetValueWithMatrix() {
context.setValue("matrix", null);
// Calls factory.createObject(..., TestMixedModelBean, "matrix")
// Calls factory.createObject(..., nestedBean, "strings", 2)
assertXPathCreatePathAndSetValue(
context,
"/matrix[1]/.[1]",
new Integer(4),
"/matrix[1]/.[1]");
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?