### Question:
XMLImporter extends Importer<T> { @Override public List<T> importStream(InputStream inputStream) throws Exception { return importStream(inputStream, DEFAULT_ENCODING); } XMLImporter(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 { XMLImporter<SamplePojo> xmlImporter = new XMLImporter<>(SamplePojo.class); List<SamplePojo> list = xmlImporter.importStream(XMLImporterTest.class.getClassLoader().getResourceAsStream("sample.xml")); assert list.size() == TestData.getData().size(); int index = 0; for (SamplePojo item : list) { SamplePojo ref = TestData.getData().get(index++); assert item.equals(ref); } }
@Test public void testImportStreamByItem() throws Exception { XMLImporter<SamplePojo> importer = new XMLImporter<>(SamplePojo.class); OnItemHandler<SamplePojo> handler = new OnItemHandler<SamplePojo>() { @Override public void onItem(SamplePojo samplePojo) { } }; importer.importStream(XMLImporterTest.class.getClassLoader().getResourceAsStream("sample.xml"), handler); }
@Test public void testImportStreamWithEncoding() throws Exception { XMLImporter<SamplePojo> xmlImporter = new XMLImporter<>(SamplePojo.class); List<SamplePojo> list = xmlImporter.importStream(XMLImporterTest.class.getClassLoader().getResourceAsStream("sampleUTF16.xml"), ENCODING_UTF_16); assert list.size() == TestData.getData().size(); int index = 0; for (SamplePojo item : list) { SamplePojo ref = TestData.getData().get(index++); assert item.equals(ref); } }
@Test public void testImportStreamByItemWithEncoding() throws Exception { XMLImporter<SamplePojo> importer = new XMLImporter<>(SamplePojo.class); OnItemHandler<SamplePojo> handler = new OnItemHandler<SamplePojo>() { @Override public void onItem(SamplePojo samplePojo) { } }; importer.importStream(XMLImporterTest.class.getClassLoader().getResourceAsStream("sampleUTF16.xml"), handler, ENCODING_UTF_16); }
@Test(expected = IOException.class) public void testImportStreamWithError() throws Exception { XMLImporter<SamplePojo> importer = new XMLImporter<>(SamplePojo.class); OnItemHandler<SamplePojo> handler = new OnItemHandler<SamplePojo>() { @Override public void onItem(SamplePojo samplePojo) { } }; importer.importStream(XMLImporterTest.class.getClassLoader().getResourceAsStream("sampleError.xml"), handler); }