Splitter { public void split(InputSource inputSource) throws IOException, SAXException { char[] leftOver = null; while ((parser = EDIReaderFactory.createEDIReader(inputSource, leftOver)) != null) { String outputFilename = handlerFactory.generateName(); logger.info("EDI interchange written to {}", outputFilename); parser.setContentHandler(new ScanningHandler()); try (Writer writer = new FileWriter(outputFilename)) { parser.setCopyWriter(writer); parser.parse(inputSource); } leftOver = parser.getTokenizer().getBuffered(); } } Splitter(Reader inputReader, String outputFileNamePattern); void run(); void split(InputSource inputSource); static void main(String[] args); static int getCount();  }
@Test public void testOneInterchangeOneDocumentIntoDOM() throws Exception { inputReader = new StringReader(EDITestData.getAnsiInterchange()); MyDomHandlerFactory factory = new MyDomHandlerFactory(); new SplittingHandler(factory).split(new InputSource(inputReader)); assertEquals(1, factory.getCreateCalls()); assertEquals(332, factory.getSAXEventsWritten()); factory.shutdown(); Document controlDom = generateDOM(EDITestData.getAnsiInputSource()); Diff diff = new Diff(controlDom, factory.getDom()); boolean identical = diff.identical(); if (!identical) { fail(diff.toString()); } }
@Test public void testTwoInterchangeOneDocumentEach() throws Exception { String ansiInterchange = EDITestData.getAnsiInterchange(); ansiInterchange = ansiInterchange + ansiInterchange; inputReader = new StringReader(ansiInterchange); MyHandlerFactory factory = new MyHandlerFactory(); new SplittingHandler(factory).split(new InputSource(inputReader)); assertEquals(2, factory.getCreateCalls()); assertEquals(664, factory.getSAXEventsWritten()); factory.getReaderThread().join(); assertEquals(662, factory.getSAXEventsRead()); assertEquals(2, factory.getInterchangeCount()); assertEquals(2, factory.getGroupCount()); assertEquals(2, factory.getTransactionCount()); }
@Test public void testTwoInterchangeTwoDocumentsEach() throws Exception { String ansiInterchange = EDITestData.getAnsiInterchange(2); ansiInterchange = ansiInterchange + ansiInterchange; MyHandlerFactory factory = new MyHandlerFactory(); new SplittingHandler(factory).split(new InputSource(new StringReader(ansiInterchange))); assertEquals(4, factory.getCreateCalls()); assertEquals(1328, factory.getSAXEventsWritten()); factory.getReaderThread().join(); assertEquals(1324, factory.getSAXEventsRead()); assertEquals(4, factory.getInterchangeCount()); assertEquals(4, factory.getGroupCount()); assertEquals(4, factory.getTransactionCount()); }
@Test public void testTwoInterchangeTwoDocumentsEachIntoDOMs() throws Exception { String ansiInterchange = EDITestData.getAnsiInterchange(2); ansiInterchange = ansiInterchange + ansiInterchange; inputReader = new StringReader(ansiInterchange); MyDomHandlerFactory factory = new MyDomHandlerFactory(); new SplittingHandler(factory).split(new InputSource(inputReader)); assertEquals(4, factory.getCreateCalls()); assertEquals(1328, factory.getSAXEventsWritten()); factory.shutdown(); Document controlDom = generateDOM(EDITestData.getAnsiInputSource()); Diff diff = new Diff(controlDom, factory.getDom()); boolean identical = diff.identical(); if (!identical) { fail(diff.toString()); } }
@Test public void testOneInterchangeOneDocument() throws Exception { inputReader = new StringReader(EDITestData.getAnsiInterchange()); MyHandlerFactory factory = new MyHandlerFactory(); new SplittingHandler(factory).split(new InputSource(inputReader)); assertEquals(1, factory.getCreateCalls()); assertEquals(332, factory.getSAXEventsWritten()); assertEquals(1, factory.getInterchangeCount()); assertEquals(1, factory.getGroupCount()); assertEquals(1, factory.getTransactionCount()); factory.getReaderThread().join(); assertEquals(331, factory.getSAXEventsRead()); }
@Test public void testOneInterchangeTwoGroupsOneDocumentEach() throws Exception { inputReader = new StringReader(EDITestData.getAnsiInterchange(2, 1)); MyHandlerFactory factory = new MyHandlerFactory(); new SplittingHandler(factory).split(new InputSource(inputReader)); assertEquals(2, factory.getCreateCalls()); assertEquals(664, factory.getSAXEventsWritten()); assertEquals(2, factory.getInterchangeCount()); assertEquals(2, factory.getGroupCount()); assertEquals(2, factory.getTransactionCount()); factory.getReaderThread().join(); assertEquals(662, factory.getSAXEventsRead()); }