JsonPathTest.java
/***************************************************************************
Copyright 2014 Emily Estes
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
***************************************************************************/
package net.metanotion.json;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import net.metanotion.functor.Block;
public final class JsonPathTest {
private static final Logger logger = LoggerFactory.getLogger(JsonPathTest.class);
public static void main(final String[] args) throws Exception {
final StreamingParser parser = new StreamingParser();
final Block<Object,Object> b = new Block<Object, Object>() {
@Override public Object eval(final Object val) {
if(val instanceof Map.Entry) {
logger.debug(":: {} - {}", ((Map.Entry) val).getKey(), ((Map.Entry) val).getValue());
} else {
logger.debug(":: {}", val);
}
return null;
}
};
JsonPath<Object> jp = new JsonPath<>()
.add("testPairs", b) // this will match
.add("numbers", b) // this will match
.add("test", b) // this will match
.add("keyPairs{}", b) // this will match
.add("test2[]", b) // this will match
.add("[10]", b) // this will never match
.add("test[0].foo['bar 1']", b) // this will never match
.add("test[0].foo['bar\\' 1']", b) // this will never match
.add("{}[0].foo['bar\\' 1']", b) // this will never match
.add("_test[0].foo['bar\\' 1']", b) // this will never match
.add("_te_st[0].fo1o['bar\\' 1']", b) // this will never match
.add("test[0]. foo['bar\\' 1']", b) // this will never match
.add("tdb[1].employees[]", b) // this will match
.add("tdb2[].employees[]", b) // this will match
.add("objMap{}.val", b) // this will match
.add("*.a[5]", b) // this will never match
.add("*[1]", b) // this will never match
.add("objMap", b); // this will overlap match with "objMap{}.val"
try (final Reader in = new InputStreamReader(new FileInputStream(args[0]), "UTF-8")) {
parser.parse(in, jp);
}
JsonPath<Object> fail = new JsonPath<>();
try (final BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(args[1]), "UTF-8"))) {
String line = in.readLine();
while (line != null) {
try {
fail.add(line, b);
throw new AssertionError("Invalid Json Path expression accepted. '" + line + "'");
} catch (final IllegalArgumentException iae) {
// Expected.
}
line = in.readLine();
}
}
}
}