public void testSetReader() throws Exception { XmlPullParser xpp = factory.newPullParser(); assertEquals(true, xpp.getFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES)); xpp.setInput(null); assertEquals(XmlPullParser.START_DOCUMENT, xpp.getEventType()); try { xpp.next(); fail("exception was expected of next() if no input was set on parser"); } catch (XmlPullParserException ex) { } // make input suspectible to read ... ReaderWrapper reader = new ReaderWrapper( new StringReader("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><foo/>")); assertEquals("no read() called in just contructed reader", false, reader.calledRead()); xpp.setInput(reader); checkParserStateNs(xpp, 0, XmlPullParser.START_DOCUMENT, null, 0, null, null, null, false, -1); assertEquals("read() not called before next()", false, reader.calledRead()); xpp.next(); assertEquals("read() must be called after next()", true, reader.calledRead()); checkParserStateNs( xpp, 1, XmlPullParser.START_TAG, null, 0, "", "foo", null, true /*empty*/, 0); }
private static void copyReader(String out) throws Exception { CloseableRegistry r = new CloseableRegistry(); try { ReaderWrapper in = new ReaderWrapper(TEST_INPUT_FILE); r.register(in); assertEquals(VALUE.length, CopyCharsUtils.copy(in.getReader(), true, out)); } finally { r.close(); } }
@Test public void failReaderWriter() throws Exception { CopyCharsUtils.copy(VALUE, TEST_INPUT_FILE); CloseableRegistry r = new CloseableRegistry(); try { ReaderWrapper in = new ReaderWrapper(TEST_INPUT_FILE); r.register(in); WriterWrapper out = new WriterWrapper(TEST_OUTPUT_FILE); r.register(out); out.getWriter().close(); CopyCharsUtils.copy(in.getReader(), true, out.getWriter(), true); fail(); } catch (I18NException ex) { assertEquals(ex.getDetail(), Messages.CANNOT_COPY_CSTREAMS, ex.getI18NBoundMessage()); } finally { r.close(); } }
@Test public void copyReaderWriter() throws Exception { CopyCharsUtils.copy(VALUE, TEST_INPUT_FILE); CloseableRegistry r = new CloseableRegistry(); try { ReaderWrapper in = new ReaderWrapper(TEST_INPUT_FILE); r.register(in); WriterWrapper out = new WriterWrapper(TEST_OUTPUT_FILE); r.register(out); assertEquals( VALUE.length, CopyCharsUtils.copy( in.getReader(), true, out.getWriter(), true)); } finally { r.close(); } assertArrayEquals(VALUE, CopyCharsUtils.copy(TEST_OUTPUT_FILE)); }
@Test public void readShouldReadReaderCorrectlyAndShouldCloseStream() throws Exception { // Read content shorter than buffer size ... String content = "This is the way to grandma's house."; Reader reader = new StringReader(content); ReaderWrapper wrapper = new ReaderWrapper(reader); assertThat(wrapper.isClosed(), is(false)); assertThat(IoUtil.read(wrapper), is(content)); assertThat(wrapper.isClosed(), is(true)); // Read content longer than buffer size ... for (int i = 0; i != 10; ++i) { content += content; // note this doubles each time! } reader = new StringReader(content); wrapper = new ReaderWrapper(reader); assertThat(wrapper.isClosed(), is(false)); assertThat(IoUtil.read(wrapper), is(content)); assertThat(wrapper.isClosed(), is(true)); }