@Before public void setUp() throws Exception { this.executionContext = new ExecutionContext(); source = createNewInputSource(); }
/** * Statistics return the current record count. Calling read after end of input does not increase the counter. */ @Test public void testExecutionContext() throws Exception { final int NUMBER_OF_RECORDS = 2; source.open(executionContext); source.update(executionContext); for (int i = 0; i < NUMBER_OF_RECORDS; i++) { int recordCount = extractRecordCount(); assertEquals(i, recordCount); source.read(); source.update(executionContext); } assertEquals(NUMBER_OF_RECORDS, extractRecordCount()); source.read(); assertEquals(NUMBER_OF_RECORDS, extractRecordCount()); }
@Test public void testItemCountAwareFragmentRestart() throws Exception { StaxEventItemReader<ItemCountAwareFragment> source = createNewItemCountAwareInputSource(); source.afterPropertiesSet(); source.open(executionContext); assertEquals(1, source.read().getItemCount()); source.update(executionContext); source.close(); source = createNewItemCountAwareInputSource(); source.afterPropertiesSet(); source.open(executionContext); assertEquals(2, source.read().getItemCount()); assertNull(source.read()); // there are only two fragments source.close(); }
@Test public void testItemCountAwareFragment() throws Exception { StaxEventItemReader<ItemCountAwareFragment> source = createNewItemCountAwareInputSource(); source.afterPropertiesSet(); source.open(executionContext); assertEquals(1, source.read().getItemCount()); assertEquals(2, source.read().getItemCount()); assertNull(source.read()); // there are only two fragments source.close(); }
@Test public void testAfterPropertesSetException() throws Exception { source = createNewInputSource(); source.setFragmentRootElementName(""); try { source.afterPropertiesSet(); fail(); } catch (IllegalArgumentException e) { // expected } source = createNewInputSource(); source.setUnmarshaller(null); try { source.afterPropertiesSet(); fail(); } catch (IllegalArgumentException e) { // expected } }
/** * Save restart data and restore from it. */ @Test public void testRestart() throws Exception { source.open(executionContext); source.read(); source.update(executionContext); assertEquals(1, executionContext.getInt(ClassUtils.getShortName(StaxEventItemReader.class) + ".read.count")); List<XMLEvent> expectedAfterRestart = source.read(); source = createNewInputSource(); source.open(executionContext); List<XMLEvent> afterRestart = source.read(); assertEquals(expectedAfterRestart.size(), afterRestart.size()); }
@Test public void testMultiFragmentRestart() throws Exception { source.setResource(new ByteArrayResource(xmlMultiFragment.getBytes())); source.setFragmentRootElementNames(MULTI_FRAGMENT_ROOT_ELEMENTS); source.afterPropertiesSet(); source.open(executionContext); // see asserts in the mock unmarshaller assertNotNull(source.read()); assertNotNull(source.read()); source.update(executionContext); assertEquals(2, executionContext.getInt(ClassUtils.getShortName(StaxEventItemReader.class) + ".read.count")); source.close(); source = createNewInputSource(); source.setResource(new ByteArrayResource(xmlMultiFragment.getBytes())); source.setFragmentRootElementNames(MULTI_FRAGMENT_ROOT_ELEMENTS); source.afterPropertiesSet(); source.open(executionContext); assertNotNull(source.read()); assertNull(source.read()); // there are only three fragments source.close(); }
@Test public void testMultiFragmentNestedRestart() throws Exception { source.setResource(new ByteArrayResource(xmlMultiFragmentNested.getBytes())); source.setFragmentRootElementNames(MULTI_FRAGMENT_ROOT_ELEMENTS); source.afterPropertiesSet(); source.open(executionContext); // see asserts in the mock unmarshaller assertNotNull(source.read()); assertNotNull(source.read()); source.update(executionContext); assertEquals(2, executionContext.getInt(ClassUtils.getShortName(StaxEventItemReader.class) + ".read.count")); source.close(); source = createNewInputSource(); source.setResource(new ByteArrayResource(xmlMultiFragment.getBytes())); source.setFragmentRootElementNames(MULTI_FRAGMENT_ROOT_ELEMENTS); source.afterPropertiesSet(); source.open(executionContext); assertNotNull(source.read()); assertNull(source.read()); // there are only three fragments source.close(); }
/** * Test restart at end of file. */ @Test public void testRestartAtEndOfFile() throws Exception { source.open(executionContext); assertNotNull(source.read()); assertNotNull(source.read()); assertNull(source.read()); source.update(executionContext); source.close(); assertEquals(3, executionContext.getInt(ClassUtils.getShortName(StaxEventItemReader.class) + ".read.count")); source = createNewInputSource(); source.open(executionContext); assertNull(source.read()); }