### Question:
JSONImporter extends Importer<T> { @Override public List<T> importStream(InputStream inputStream) throws Exception { return importStream(inputStream, DEFAULT_ENCODING); } JSONImporter(Class dataClass); @Override List<T> importStream(InputStream inputStream); @Override List<T> importStream(InputStream inputStream, String charSetName); @Override void importStream(InputStream inputStream, OnItemHandler handler); @Override void importStream(InputStream inputStream, OnItemHandler handler, String charSetName);  }### Answer:
@Test public void testImportStream() throws Exception { JSONImporter<SamplePojo> importer = new JSONImporter(SamplePojo.class); List<SamplePojo> list = importer.importStream(JSONImporterTest.class.getClassLoader().getResourceAsStream("sample.json")); assert list.size() == TestData.getData().size(); int index = 0; for (SamplePojo item : list) { SamplePojo ref = TestData.getData().get(index++); assertEquals(ref, item); } }
@Test public void testImportStreamWithHandler() throws Exception { JSONImporter<SamplePojo> importer = new JSONImporter<>(SamplePojo.class); InputStream stream = JSONImporterTest.class.getClassLoader().getResourceAsStream("sample.json"); List<SamplePojo> items = new ArrayList<>(); importer.importStream(stream, o -> items.add((SamplePojo) o)); assertEquals(5, items.size()); }
@Test public void testImportStreamWithEncoding() throws Exception { JSONImporter<SamplePojo> importer = new JSONImporter(SamplePojo.class); List<SamplePojo> list = importer.importStream(JSONImporterTest.class.getClassLoader().getResourceAsStream("sampleUTF16.json"), ENCODING_UTF_16); assert list.size() == TestData.getData().size(); int index = 0; for (SamplePojo item : list) { SamplePojo ref = TestData.getData().get(index++); assertEquals(ref, item); } }
@Test(expected = Exception.class) public void testWithCorruptedJson() throws Exception { JSONImporter<SamplePojo> importer = new JSONImporter<>(SamplePojo.class); InputStream stream = new ByteArrayInputStream("{in".getBytes()); importer.importStream(stream, o -> { }); }