commit
52a2ec05e1
@ -63,17 +63,17 @@ public class FieldAccessorExpr implements Evaluable {
|
|||||||
if (ExpressionUtils.isError(o)) {
|
if (ExpressionUtils.isError(o)) {
|
||||||
return o; // bubble the error up
|
return o; // bubble the error up
|
||||||
} else if (o == null) {
|
} else if (o == null) {
|
||||||
return new EvalError("Cannot retrieve field from null");
|
return null;
|
||||||
} else if (o instanceof HasFields) {
|
} else if (o instanceof HasFields) {
|
||||||
return ((HasFields) o).getField(_fieldName, bindings);
|
return ((HasFields) o).getField(_fieldName, bindings);
|
||||||
} else if (o instanceof JSONObject) {
|
} else if (o instanceof JSONObject) {
|
||||||
try {
|
try {
|
||||||
return ((JSONObject) o).get(_fieldName);
|
return ((JSONObject) o).get(_fieldName);
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
return new EvalError("Object does not have any field, including " + _fieldName);
|
return null;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return new EvalError("Object does not have any field, including " + _fieldName);
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -99,7 +99,7 @@ public class GrelTests extends RefineTest {
|
|||||||
public void testEvalError() {
|
public void testEvalError() {
|
||||||
String tests[] = {
|
String tests[] = {
|
||||||
// "1=1", // TODO: Throws NullPointerException
|
// "1=1", // TODO: Throws NullPointerException
|
||||||
"a.value",
|
"value.datePart()",
|
||||||
};
|
};
|
||||||
for (String test : tests) {
|
for (String test : tests) {
|
||||||
try {
|
try {
|
||||||
@ -166,6 +166,26 @@ public class GrelTests extends RefineTest {
|
|||||||
parseEval(bindings, test);
|
parseEval(bindings, test);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetJsonFieldExists() throws ParsingException {
|
||||||
|
String test[] = { "\"[{\\\"one\\\": \\\"1\\\"}]\".parseJson()[0].one", "1" };
|
||||||
|
parseEval(bindings, test);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetJsonFieldAbsent() throws ParsingException {
|
||||||
|
String test = "\"[{\\\"one\\\": \\\"1\\\"}]\".parseJson()[0].two";
|
||||||
|
Evaluable eval = MetaParser.parse("grel:" + test);
|
||||||
|
Assert.assertNull(eval.evaluate(bindings));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testGetFieldFromNull() throws ParsingException {
|
||||||
|
String test = "null.value";
|
||||||
|
Evaluable eval = MetaParser.parse("grel:" + test);
|
||||||
|
Assert.assertNull(eval.evaluate(bindings));
|
||||||
|
}
|
||||||
|
|
||||||
// to demonstrate bug fixing for #1204
|
// to demonstrate bug fixing for #1204
|
||||||
@Test
|
@Test
|
||||||
@ -182,11 +202,11 @@ public class GrelTests extends RefineTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void parseEval(Properties bindings, String[] test)
|
private void parseEval(Properties bindings, String[] test)
|
||||||
throws ParsingException {
|
throws ParsingException {
|
||||||
Evaluable eval = MetaParser.parse("grel:" + test[0]);
|
Evaluable eval = MetaParser.parse("grel:" + test[0]);
|
||||||
Object result = eval.evaluate(bindings);
|
Object result = eval.evaluate(bindings);
|
||||||
Assert.assertEquals(result.toString(), test[1],
|
Assert.assertEquals(result.toString(), test[1],
|
||||||
"Wrong result for expression: "+test[0]);
|
"Wrong result for expression: "+test[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user