IN PROGRESS - issue BATCH-1246: Add support for a JSON Reader from text files which are JSON formatted
Added line mapper and record separator using Jackson to parse the line up to a map.
This commit is contained in:
@@ -0,0 +1,23 @@
|
||||
package org.springframework.batch.item.file.separator;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
public class JsonRecordSeparatorPolicyTests {
|
||||
|
||||
private JsonRecordSeparatorPolicy policy = new JsonRecordSeparatorPolicy();
|
||||
|
||||
@Test
|
||||
public void testIsEndOfRecord() {
|
||||
assertFalse(policy.isEndOfRecord("{\"a\":\"b\""));
|
||||
assertTrue(policy.isEndOfRecord("{\"a\":\"b\"} "));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testNestedObject() {
|
||||
assertFalse(policy.isEndOfRecord("{\"a\": {\"b\": 2}"));
|
||||
assertTrue(policy.isEndOfRecord("{\"a\": {\"b\": 2}} "));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package org.springframework.batch.item.file.transform;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.batch.item.file.FlatFileParseException;
|
||||
|
||||
public class JsonLineMapperTests {
|
||||
|
||||
private JsonLineMapper mapper = new JsonLineMapper();
|
||||
|
||||
@Test
|
||||
public void testMapLine() throws Exception {
|
||||
Map<String, Object> map = mapper.mapLine("{\"foo\": 1}", 1);
|
||||
assertEquals(1, map.get("foo"));
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void testMapNested() throws Exception {
|
||||
Map<String, Object> map = mapper.mapLine("{\"foo\": 1, \"bar\" : {\"foo\": 2}}", 1);
|
||||
assertEquals(1, map.get("foo"));
|
||||
assertEquals(2, ((Map<String, Object>) map.get("bar")).get("foo"));
|
||||
}
|
||||
|
||||
@Test(expected=FlatFileParseException.class)
|
||||
public void testMappingError() throws Exception {
|
||||
Map<String, Object> map = mapper.mapLine("{\"foo\": 1", 1);
|
||||
assertEquals(1, map.get("foo"));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user