Browse Source

Started compliance checks.

pull/6/head
Kalle Stenflo 13 years ago
parent
commit
0669d4d039
  1. 6
      json-path/src/main/java/com/jayway/jsonpath/reader/filter/FilterFactory.java
  2. 4
      json-path/src/main/java/com/jayway/jsonpath/reader/filter/PassthroughFilter.java
  3. 211
      json-path/src/test/java/com/jayway/jsonpath/ComplianceTests.java
  4. 2
      json-path/src/test/java/com/jayway/jsonpath/IssueTests.java

6
json-path/src/main/java/com/jayway/jsonpath/reader/filter/FilterFactory.java

@ -8,8 +8,8 @@ package com.jayway.jsonpath.reader.filter;
*/ */
public class FilterFactory { public class FilterFactory {
private final static Filter DOCUMENT_FILTER = new PassThrewFilter("$", false); private final static Filter DOCUMENT_FILTER = new PassThroughFilter("$", false);
private final static Filter ALL_ARRAY_ITEMS_FILTER = new PassThrewFilter("[*]", true); private final static Filter ALL_ARRAY_ITEMS_FILTER = new PassThroughFilter("[*]", true);
private final static Filter WILDCARD_FILTER = new WildcardFilter("*"); private final static Filter WILDCARD_FILTER = new WildcardFilter("*");
private final static Filter SCAN_FILTER = new ScanFilter(".."); private final static Filter SCAN_FILTER = new ScanFilter("..");
@ -23,7 +23,7 @@ public class FilterFactory {
return ALL_ARRAY_ITEMS_FILTER; return ALL_ARRAY_ITEMS_FILTER;
} else if ("*".equals(pathFragment)) { } else if ("*".equals(pathFragment) || "['*']".equals(pathFragment)) {
return WILDCARD_FILTER; return WILDCARD_FILTER;

4
json-path/src/main/java/com/jayway/jsonpath/reader/filter/PassThrewFilter.java → json-path/src/main/java/com/jayway/jsonpath/reader/filter/PassthroughFilter.java

@ -8,11 +8,11 @@ import com.jayway.jsonpath.spi.JsonProvider;
* Date: 11/4/11 * Date: 11/4/11
* Time: 10:15 PM * Time: 10:15 PM
*/ */
public class PassThrewFilter extends Filter { public class PassThroughFilter extends Filter {
private boolean isArrayFilter; private boolean isArrayFilter;
public PassThrewFilter(String condition, boolean isArrayFilter) { public PassThroughFilter(String condition, boolean isArrayFilter) {
super(condition); super(condition);
this.isArrayFilter = isArrayFilter; this.isArrayFilter = isArrayFilter;
} }

211
json-path/src/test/java/com/jayway/jsonpath/ComplianceTests.java

@ -0,0 +1,211 @@
package com.jayway.jsonpath;
import org.hamcrest.Matchers;
import org.junit.Test;
import java.util.List;
import java.util.Map;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertThat;
/**
* test defined in http://jsonpath.googlecode.com/svn/trunk/tests/jsonpath-test-js.html
*/
public class ComplianceTests {
@Test
public void test_one() throws Exception {
String json = "{ a: \"a\",\n" +
" b: \"b\",\n" +
" \"c d\": \"e\" \n" +
" }";
assertThat(JsonPath.<String>read(json, "$.a"), is(equalTo("a")));
assertThat(JsonPath.<List<String>>read(json, "$.*"), hasItems("a", "b", "e"));
assertThat(JsonPath.<List<String>>read(json, "$['*']"), hasItems("a", "b", "e"));
//assertThat(JsonPath.<String>read(json, "$['a']"), is(equalTo("a"))); //high
//assertThat(JsonPath.<String>read(json, "$.'c d'"), is(equalTo("e"))); //low
//assertThat(JsonPath.<List<String>>read(json, "$[*]"), hasItems("a", "b", "e")); //low
}
@Test
public void test_two() throws Exception {
String json = "[ 1, \"2\", 3.14, true, null ]";
assertThat(JsonPath.<Integer>read(json, "$[0]"), is(equalTo(1)));
assertThat(JsonPath.<Integer>read(json, "$[4]"), is(equalTo(null)));
assertThat(JsonPath.<List<Comparable>>read(json, "$[*]"), hasItems((Comparable)new Integer(1), (Comparable)new String("2"), (Comparable)new Double(3.14), (Comparable)new Boolean(true), null));
assertThat(JsonPath.<Boolean>read(json, "$[-1:]"), is(equalTo(null)));
}
@Test
public void test_three() throws Exception {
String json = "{ points: [\n" +
" { id: \"i1\", x: 4, y: -5 },\n" +
" { id: \"i2\", x: -2, y: 2, z: 1 },\n" +
" { id: \"i3\", x: 8, y: 3 },\n" +
" { id: \"i4\", x: -6, y: -1 },\n" +
" { id: \"i5\", x: 0, y: 2, z: 1 },\n" +
" { id: \"i6\", x: 1, y: 4 }\n" +
" ]\n" +
" }";
assertThat(JsonPath.<Map<String, Comparable>>read(json, "$.points[1]"), allOf(
Matchers.<String, Comparable>hasEntry("id", "i2"),
Matchers.<String, Comparable>hasEntry("x", -2),
Matchers.<String, Comparable>hasEntry("y", 2),
Matchers.<String, Comparable>hasEntry("z", 1)
));
assertThat(JsonPath.<Integer>read(json, "$.points[4].x"), equalTo(0));
assertThat(JsonPath.<List<Integer>>read(json, "$.points[?(@.id = 'i4')].x"), hasItem(-6));
assertThat(JsonPath.<List<Integer>>read(json, "$.points[*].x"), hasItems(4, -2, 8, -6, 0, 1));
assertThat(JsonPath.<List<String>>read(json, "$.points[?(@.z)].id"), hasItems("i2", "i5"));
assertThat(JsonPath.<String>read(json, "$.points[(@.length-1)].id"), equalTo("i6"));
//assertThat(JsonPath.<List<Integer>>read(json, "$['points'][?(@.x * @.x + @.y * @.y > 50)].id"), hasItems(?)); //low
}
@Test
public void test_four() throws Exception {
}
/*
--one
{ "o": { a: "a",
b: "b",
"c d": "e"
},
"p": [ "$.a",
"$['a']",
"$.'c d'",
"$.*",
"$['*']" ,
"$[*]"
]
},
--two
{ "o": [ 1, "2", 3.14, true, null ],
"p": [ "$[0]",
"$[4]",
"$[*]",
"$[-1:]"
]
},
--three
{ "o": { points: [
{ id: "i1", x: 4, y: -5 },
{ id: "i2", x: -2, y: 2, z: 1 },
{ id: "i3", x: 8, y: 3 },
{ id: "i4", x: -6, y: -1 },
{ id: "i5", x: 0, y: 2, z: 1 },
{ id: "i6", x: 1, y: 4 }
]
},
"p": [ "$.points[1]",
"$.points[4].x",
"$.points[?(@.id=='i4')].x",
"$.points[*].x",
"$['points'][?(@.x*@.x+@.y*@.y > 50)].id",
"$.points[?(@.z)].id",
"$.points[(@.length-1)].id"
]
},
--four
{ "o": { "menu": {
"header": "SVG Viewer",
"items": [
{"id": "Open"},
{"id": "OpenNew", "label": "Open New"},
null,
{"id": "ZoomIn", "label": "Zoom In"},
{"id": "ZoomOut", "label": "Zoom Out"},
{"id": "OriginalView", "label": "Original View"},
null,
{"id": "Quality"},
{"id": "Pause"},
{"id": "Mute"},
null,
{"id": "Find", "label": "Find..."},
{"id": "FindAgain", "label": "Find Again"},
{"id": "Copy"},
{"id": "CopyAgain", "label": "Copy Again"},
{"id": "CopySVG", "label": "Copy SVG"},
{"id": "ViewSVG", "label": "View SVG"},
{"id": "ViewSource", "label": "View Source"},
{"id": "SaveAs", "label": "Save As"},
null,
{"id": "Help"},
{"id": "About", "label": "About Adobe CVG Viewer..."}
]
}
},
"p": [ "$.menu.items[?(@ && @.id && !@.label)].id",
"$.menu.items[?(@ && @.label && /SVG/.test(@.label))].id",
"$.menu.items[?(!@)]",
"$..[0]"
]
},
--five
{ "o": { a: [1,2,3,4],
b: [5,6,7,8]
},
"p": [ "$..[0]",
"$..[-1:]",
"$..[?(@%2==0)]"
]
},
{ "o": { lin: {color:"red", x:2, y:3},
cir: {color:"blue", x:5, y:2, r:1 },
arc: {color:"green", x:2, y:4, r:2, phi0:30, dphi:120 },
pnt: {x:0, y:7 }
},
"p": [ "$.'?(@.color)'.x",
"$['lin','cir'].color"
]
},
{ "o": { lin: {color:"red", x:2, y:3},
cir: {color:"blue", x:5, y:2, r:1 },
arc: {color:"green", x:2, y:4, r:2, phi0:30, dphi:120 },
pnt: {x:0, y:7 }
},
"p": [ "$.'?(@.color)'.x",
"$['lin','arc'].color"
]
},
{ "o": { text: [ "hello", "world2.0"] },
"p": [ "$.text[?(@.length > 5)]",
"$.text[?(@.charAt(0) == 'h')]"
]
},
{ "o": { a: { a:2, b:3 },
b: { a:4, b:5 },
c: { a: { a:6, b:7}, c:8}
},
"p": [ "$..a"
]
},
{ "o": { a: [ { a:5, '@':2, '$':3 }, // issue 7: resolved by escaping the '@' character
{ a:6, '@':3, '$':4 }, // in a JSONPath expression.
{ a:7, '@':4, '$':5 }
]
},
"p": [ "$.a[?(@['\\@']==3)]",
"$.a[?(@['$']==5)]"
]
}
*/
}

2
json-path/src/test/java/com/jayway/jsonpath/Issues.java → json-path/src/test/java/com/jayway/jsonpath/IssueTests.java

@ -10,7 +10,7 @@ import static junit.framework.Assert.assertNull;
* Date: 2/29/12 * Date: 2/29/12
* Time: 8:42 AM * Time: 8:42 AM
*/ */
public class Issues { public class IssueTests {
@Test @Test
public void issue_7() throws Exception { public void issue_7() throws Exception {
Loading…
Cancel
Save