Browse Source

More test for parsing filters.

pull/183/merge
Kalle Stenflo 9 years ago
parent
commit
27baaeca01
  1. 5
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/FilterCompiler.java
  2. 2
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/RelationalOperator.java
  3. 13
      json-path/src/main/java/com/jayway/jsonpath/internal/filter/ValueNode.java
  4. 5
      json-path/src/main/java/com/jayway/jsonpath/internal/function/numeric/AbstractAggregation.java
  5. 25
      json-path/src/test/java/com/jayway/jsonpath/FilterCompilerTest.java

5
json-path/src/main/java/com/jayway/jsonpath/internal/filter/FilterCompiler.java

@ -85,6 +85,9 @@ public class FilterCompiler {
filter.incrementPosition(1); filter.incrementPosition(1);
ExpressionNode expressionNode = expStack.pop(); ExpressionNode expressionNode = expStack.pop();
if(!opsStack.isEmpty()){ if(!opsStack.isEmpty()){
if(expStack.isEmpty()){
throw new InvalidPathException("Expected expression on right hand side of operator");
}
ExpressionNode right = expStack.pop(); ExpressionNode right = expStack.pop();
expressionNode = ExpressionNode.createExpressionNode(expressionNode, opsStack.pop(), right); expressionNode = ExpressionNode.createExpressionNode(expressionNode, opsStack.pop(), right);
while(!opsStack.isEmpty()){ while(!opsStack.isEmpty()){
@ -110,7 +113,7 @@ public class FilterCompiler {
throw new InvalidPathException("Failed to parse filter " + filter.toString()); throw new InvalidPathException("Failed to parse filter " + filter.toString());
} }
} }
if(unbalancedBrackets > 0){ if(unbalancedBrackets != 0){
throw new InvalidPathException("Failed to parse filter. Brackets are not balanced. " + filter.toString()); throw new InvalidPathException("Failed to parse filter. Brackets are not balanced. " + filter.toString());
} }

2
json-path/src/main/java/com/jayway/jsonpath/internal/filter/RelationalOperator.java

@ -33,7 +33,7 @@ public enum RelationalOperator {
return operator; return operator;
} }
} }
throw new InvalidPathException("Operator not supported " + operatorString); throw new InvalidPathException("Operator " + operatorString + " not supported ");
} }
@Override @Override

13
json-path/src/main/java/com/jayway/jsonpath/internal/filter/ValueNode.java

@ -239,9 +239,6 @@ public abstract class ValueNode {
this.compiledPattern = pattern; this.compiledPattern = pattern;
} }
public String getPattern() {
return "/" + pattern + "/";
}
public Pattern getCompiledPattern() { public Pattern getCompiledPattern() {
return compiledPattern; return compiledPattern;
@ -262,7 +259,15 @@ public abstract class ValueNode {
@Override @Override
public String toString() { public String toString() {
return pattern;
String flags = "";
if((compiledPattern.flags() & Pattern.CASE_INSENSITIVE) == Pattern.CASE_INSENSITIVE){
flags = "i";
}
return "/" + pattern + "/" + flags;
} }
@Override @Override

5
json-path/src/main/java/com/jayway/jsonpath/internal/function/numeric/AbstractAggregation.java

@ -35,11 +35,6 @@ public abstract class AbstractAggregation implements Function {
Iterable<?> objects = ctx.configuration().jsonProvider().toIterable(model); Iterable<?> objects = ctx.configuration().jsonProvider().toIterable(model);
for (Object obj : objects) { for (Object obj : objects) {
// Object unwraped = ctx.configuration().jsonProvider().unwrap(obj);
// if (unwraped instanceof Number) {
// Number value = (Number) unwraped;
// next(value);
// }
if (obj instanceof Number) { if (obj instanceof Number) {
Number value = (Number) obj; Number value = (Number) obj;
next(value); next(value);

25
json-path/src/test/java/com/jayway/jsonpath/FilterCompilerTest.java

@ -9,7 +9,7 @@ public class FilterCompilerTest {
@Test @Test
public void filter_compiler_test() { public void valid_filters_compile() {
assertThat(compile("[?(@)]").toString()).isEqualTo("[?(@)]"); assertThat(compile("[?(@)]").toString()).isEqualTo("[?(@)]");
assertThat(compile("[?(@)]").toString()).isEqualTo("[?(@)]"); assertThat(compile("[?(@)]").toString()).isEqualTo("[?(@)]");
assertThat(compile("[?(@.firstname)]").toString()).isEqualTo("[?(@['firstname'])]"); assertThat(compile("[?(@.firstname)]").toString()).isEqualTo("[?(@['firstname'])]");
@ -40,5 +40,28 @@ public class FilterCompilerTest {
assertThat(compile("[?(@.message.%min()==10)]").toString()).isEqualTo("[?(@['message'].%min() == 10)]"); assertThat(compile("[?(@.message.%min()==10)]").toString()).isEqualTo("[?(@['message'].%min() == 10)]");
assertThat(compile("[?(10 == @.message.%min())]").toString()).isEqualTo("[?(10 == @['message'].%min())]"); assertThat(compile("[?(10 == @.message.%min())]").toString()).isEqualTo("[?(10 == @['message'].%min())]");
assertThat(compile("[?(((@)))]").toString()).isEqualTo("[?(@)]"); assertThat(compile("[?(((@)))]").toString()).isEqualTo("[?(@)]");
assertThat(compile("[?(@.name =~ /.*?/i)]").toString()).isEqualTo("[?(@['name'] =~ /.*?/i)]");
assertThat(compile("[?(@.name =~ /.*?/)]").toString()).isEqualTo("[?(@['name'] =~ /.*?/)]");
}
@Test
public void invalid_filters_does_not_compile() {
assertInvalidPathException("[?(@))]");
assertInvalidPathException("[?(@ FOO 1)]");
assertInvalidPathException("[?(@ || )]");
assertInvalidPathException("[?(@ == 'foo )]");
assertInvalidPathException("[?(@ == 1' )]");
}
private void assertInvalidPathException(String filter){
try {
compile(filter);
throw new AssertionError("Expected " + filter + " to throw InvalidPathException");
} catch (InvalidPathException e){
e.printStackTrace();
}
} }
} }

Loading…
Cancel
Save