Skip to content

Commit

Permalink
#29 Fix issue with complex JsonPath in filter
Browse files Browse the repository at this point in the history
  • Loading branch information
wanglingsong committed Aug 26, 2017
1 parent 84db967 commit fe67bf7
Show file tree
Hide file tree
Showing 24 changed files with 881 additions and 804 deletions.
2 changes: 1 addition & 1 deletion jsurfer-all/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<artifactId>jsurfer</artifactId>
<groupId>com.github.jsurfer</groupId>
<version>1.4</version>
<version>1.4.1</version>
</parent>
<modelVersion>4.0.0</modelVersion>

Expand Down
47 changes: 0 additions & 47 deletions jsurfer-all/src/test/java/org/jsfr/json/FastJsonParesrTest.java
Original file line number Diff line number Diff line change
@@ -1,64 +1,17 @@
package org.jsfr.json;

import org.jsfr.json.compiler.JsonPathCompiler;
import org.jsfr.json.provider.FastJsonProvider;
import org.junit.Before;
import org.junit.Test;

import java.util.Iterator;

import static org.junit.Assert.*;

/**
* Created by Leo on 2017/3/31.
*/
public class FastJsonParesrTest extends JsonSurferTest {

@Override
@Before
public void setUp() throws Exception {
provider = FastJsonProvider.INSTANCE;
surfer = new JsonSurfer(FastJsonParser.INSTANCE, provider);
}

@Test
public void testSurfingIterator() throws Exception {
Iterator<Object> iterator = surfer.iterator(read("sample.json"), JsonPathCompiler.compile("$.store.book[*]"));
int count = 0;
while (iterator.hasNext()) {
LOGGER.info("Iterator next: {}", iterator.next());
count++;
}
assertEquals(4, count);
}

@Test
public void testResumableParser() throws Exception {
SurfingConfiguration config = surfer.configBuilder()
.bind("$.store.book[0]", new JsonPathListener() {
@Override
public void onValue(Object value, ParsingContext context) {
LOGGER.info("First pause");
context.pause();
}
})
.bind("$.store.book[1]", new JsonPathListener() {
@Override
public void onValue(Object value, ParsingContext context) {
LOGGER.info("Second pause");
context.pause();
}
}).build();
ResumableParser parser = surfer.createResumableParser(read("sample.json"), config);
assertFalse(parser.resume());
LOGGER.info("Start parsing");
parser.parse();
LOGGER.info("Resume from the first pause");
assertTrue(parser.resume());
LOGGER.info("Resume from the second pause");
assertTrue(parser.resume());
LOGGER.info("Parsing stopped");
assertFalse(parser.resume());
}

}
88 changes: 0 additions & 88 deletions jsurfer-all/src/test/java/org/jsfr/json/GsonParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,106 +24,18 @@

package org.jsfr.json;

import com.google.gson.Gson;
import com.google.gson.stream.JsonReader;
import org.jsfr.json.compiler.JsonPathCompiler;
import org.jsfr.json.provider.GsonProvider;
import org.junit.Before;
import org.junit.Test;

import java.io.Reader;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;

import static org.junit.Assert.*;

/**
* Created by Leo on 2015/3/29.
*/
public class GsonParserTest extends JsonSurferTest {

@Override
@Before
public void setUp() throws Exception {
provider = GsonProvider.INSTANCE;
surfer = new JsonSurfer(GsonParser.INSTANCE, provider);
}

@Test
public void testLargeJsonRawGson() throws Exception {
final AtomicLong counter = new AtomicLong();
Gson gson = new Gson();
JsonReader reader = new JsonReader(read("allthethings.json"));
long start = System.currentTimeMillis();
reader.beginObject();
reader.nextName();
// $.builders
reader.beginObject();
while (reader.hasNext()) {
reader.nextName();
Map element = gson.fromJson(reader, Map.class);
Object value = ((Map) element.get("properties")).get("branch");
counter.incrementAndGet();
LOGGER.trace("JsonPath: {} value: {}", reader.getPath(), value);
}
LOGGER.info("Gson processes {} value in {} millisecond", counter.get(), System.currentTimeMillis() - start);
}

@Test
public void testGsonTypeBindingOne() throws Exception {
Reader reader = read("sample.json");
Book book = surfer.collectOne(reader, Book.class, JsonPathCompiler.compile("$..book[1]"));
assertEquals("Evelyn Waugh", book.getAuthor());
}

@Test
public void testGsonTypeBindingCollection() throws Exception {
Reader reader = read("sample.json");
Collection<Book> book = surfer.collectAll(reader, Book.class, JsonPathCompiler.compile("$..book[0,1]"));
assertEquals(2, book.size());
assertEquals("Nigel Rees", book.iterator().next().getAuthor());
}

@Test
public void testSurfingIterator() throws Exception {
Iterator<Object> iterator = surfer.iterator(read("sample.json"), JsonPathCompiler.compile("$.store.book[*]"));
int count = 0;
while (iterator.hasNext()) {
LOGGER.info("Iterator next: {}", iterator.next());
count++;
}
assertEquals(4, count);
}

@Test
public void testResumableParser() throws Exception {
SurfingConfiguration config = surfer.configBuilder()
.bind("$.store.book[0]", new JsonPathListener() {
@Override
public void onValue(Object value, ParsingContext context) {
LOGGER.info("First pause");
context.pause();
}
})
.bind("$.store.book[1]", new JsonPathListener() {
@Override
public void onValue(Object value, ParsingContext context) {
LOGGER.info("Second pause");
context.pause();
}
}).build();
ResumableParser parser = surfer.createResumableParser(read("sample.json"), config);
assertFalse(parser.resume());
LOGGER.info("Start parsing");
parser.parse();
LOGGER.info("Resume from the first pause");
assertTrue(parser.resume());
LOGGER.info("Resume from the second pause");
assertTrue(parser.resume());
LOGGER.info("Parsing stopped");
assertFalse(parser.resume());
}

}
91 changes: 2 additions & 89 deletions jsurfer-all/src/test/java/org/jsfr/json/JacksonParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,12 @@

package org.jsfr.json;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonToken;
import com.fasterxml.jackson.core.TreeNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.jsfr.json.compiler.JsonPathCompiler;
import org.jsfr.json.provider.JacksonProvider;
import org.junit.Before;
import org.junit.Test;

import java.io.Reader;
import java.util.Collection;
import java.util.Iterator;
import java.util.concurrent.atomic.AtomicLong;

import static org.junit.Assert.*;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.*;

Expand All @@ -48,89 +38,12 @@
*/
public class JacksonParserTest extends JsonSurferTest {

@Override
@Before
public void setUp() throws Exception {
provider = new JacksonProvider();
surfer = new JsonSurfer(JacksonParser.INSTANCE, provider);
}

@Test
public void testLargeJsonJackson() throws Exception {
final AtomicLong counter = new AtomicLong();
ObjectMapper om = new ObjectMapper();
JsonFactory f = new JsonFactory();
JsonParser jp = f.createParser(read("allthethings.json"));
long start = System.currentTimeMillis();
jp.nextToken();
jp.nextToken();
jp.nextToken();
while (jp.nextToken() == JsonToken.FIELD_NAME) {
if (jp.nextToken() == JsonToken.START_OBJECT) {
TreeNode tree = om.readTree(jp);
counter.incrementAndGet();
LOGGER.trace("value: {}", tree);
}
}
jp.close();
LOGGER.info("Jackson processes {} value in {} millisecond", counter.get(), System.currentTimeMillis() - start);
}

@Test
public void testJacksonTypeBindingOne() throws Exception {
Reader reader = read("sample.json");
Book book = surfer.collectOne(reader, Book.class, JsonPathCompiler.compile("$..book[1]"));
assertEquals("Evelyn Waugh", book.getAuthor());
}

@Test
public void testJacksonTypeBindingCollection() throws Exception {
Reader reader = read("sample.json");
Collection<Book> book = surfer.collectAll(reader, Book.class, JsonPathCompiler.compile("$..book[0,1]"));
assertEquals(2, book.size());
assertEquals("Nigel Rees", book.iterator().next().getAuthor());
}

@Test
public void testSurfingIterator() throws Exception {
Iterator<Object> iterator = surfer.iterator(read("sample.json"), JsonPathCompiler.compile("$.store.book[*]"));
int count = 0;
while (iterator.hasNext()) {
LOGGER.info("Iterator next: {}", iterator.next());
count++;
}
assertEquals(4, count);
}

@Test
public void testResumableParser() throws Exception {
SurfingConfiguration config = surfer.configBuilder()
.bind("$.store.book[0]", new JsonPathListener() {
@Override
public void onValue(Object value, ParsingContext context) {
LOGGER.info("First pause");
context.pause();
}
})
.bind("$.store.book[1]", new JsonPathListener() {
@Override
public void onValue(Object value, ParsingContext context) {
LOGGER.info("Second pause");
context.pause();
}
}).build();
ResumableParser parser = surfer.createResumableParser(read("sample.json"), config);
assertFalse(parser.resume());
LOGGER.info("Start parsing");
parser.parse();
LOGGER.info("Resume from the first pause");
assertTrue(parser.resume());
LOGGER.info("Resume from the second pause");
assertTrue(parser.resume());
LOGGER.info("Parsing stopped");
assertFalse(parser.resume());
}

@Test
public void testNonBlockingParser() throws Exception {
JsonPathListener mockListener = mock(JsonPathListener.class);
Expand Down
42 changes: 42 additions & 0 deletions jsurfer-all/src/test/java/org/jsfr/json/JsonSimpleParserTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package org.jsfr.json;

import org.jsfr.json.provider.JsonSimpleProvider;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;

/**
* Created by Leo on 2017/8/26.
*/
public class JsonSimpleParserTest extends JsonSurferTest {

@Before
public void setUp() throws Exception {
provider = JsonSimpleProvider.INSTANCE;
surfer = new JsonSurfer(JsonSimpleParser.INSTANCE, provider);
}

@Override
public void testTypeBindingOne() throws Exception {
// ignore
}

@Ignore
@Override
public void testTypeBindingCollection() throws Exception {
// ignore
}

@Test(expected = UnsupportedOperationException.class)
@Override
public void testSurfingIterator() throws Exception {
super.testSurfingIterator();
}

@Test(expected = UnsupportedOperationException.class)
@Override
public void testResumableParser() throws Exception {
super.testResumableParser();
}

}
Loading

0 comments on commit fe67bf7

Please sign in to comment.