ConstitParseSampleStream extends FilterObjectStream<byte[], Parse> { public Parse read() throws IOException { if (parses.isEmpty()) { byte[] xmlbytes = samples.read(); if (xmlbytes != null) { List<Parse> producedParses = new ArrayList<Parse>(); try { saxParser.parse(new ByteArrayInputStream(xmlbytes), new ConstitDocumentHandler(producedParses)); } catch (SAXException e) { throw (IOException) new IOException(e.getMessage()).initCause(e); } parses.addAll(producedParses); } } if (parses.size() > 0) { return parses.remove(0); } else { return null; } } protected  ConstitParseSampleStream(ObjectStream<byte[]> samples); Parse read();  }
@Test public void testThereIsExactlyOneSent() throws IOException { ObjectStream<Parse> samples = new ConstitParseSampleStream(ObjectStreamUtils.createObjectStream(getSample1())); Assert.assertNotNull(samples.read()); Assert.assertNull(samples.read()); Assert.assertNull(samples.read()); }
@Test public void testTokensAreCorrect() throws IOException { ObjectStream<Parse> samples = new ConstitParseSampleStream(ObjectStreamUtils.createObjectStream(getSample1())); Parse p = samples.read(); Parse[] tagNodes = p.getTagNodes(); String[] tokens = new String[tagNodes.length]; for (int ti=0;ti<tagNodes.length;ti++){ tokens[ti] = tagNodes[ti].getCoveredText(); } Assert.assertArrayEquals(sample1Tokens, tokens); }