@Test public void testRead() throws Exception { QualifiedTrade result; List<QualifiedTrade> results = new ArrayList<>(); while ((result = reader.read()) != null) { results.add(result); } checkResults(results); }
/** * Regular usage scenario. ItemReader should pass XML fragments to unmarshaller wrapped with StartDocument and * EndDocument events. */ @Test public void testFragmentWrapping() throws Exception { source.afterPropertiesSet(); source.open(executionContext); // see asserts in the mock unmarshaller assertNotNull(source.read()); assertNotNull(source.read()); assertNull(source.read()); // there are only two fragments source.close(); }
@Test public void testMultiFragment() 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()); assertNotNull(source.read()); assertNull(source.read()); // there are only three fragments source.close(); }
@Test public void testRead() throws Exception { reader.setResource(new ClassPathResource(ClassUtils.addResourcePathToPackagePath(getClass(), "input.xml"))); reader.open(new ExecutionContext()); Trade result; List<Trade> results = new ArrayList<>(); while ((result = reader.read()) != null) { results.add(result); } checkResults(results); }
@Test public void testReadNested() throws Exception { reader.setResource(new ClassPathResource(ClassUtils .addResourcePathToPackagePath(getClass(), "input-nested.xml"))); reader.open(new ExecutionContext()); Trade result; List<Trade> results = new ArrayList<>(); while ((result = reader.read()) != null) { results.add(result); } checkResults(results); }
@Test public void testMultiFragmentNested() 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()); assertNotNull(source.read()); assertNull(source.read()); // there are only three fragments source.close(); }
@Test public void testFragmentNamespace() throws Exception { source.setResource(new ByteArrayResource(fooXml.getBytes())); source.afterPropertiesSet(); source.open(executionContext); // see asserts in the mock unmarshaller assertNotNull(source.read()); assertNotNull(source.read()); assertNull(source.read()); // there are only two fragments source.close(); }
/** * 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 testFragmentMixedNamespace() throws Exception { source.setResource(new ByteArrayResource(mixedXml.getBytes())); source.setFragmentRootElementName("{urn:org.test.bar}" + FRAGMENT_ROOT_ELEMENT); source.afterPropertiesSet(); source.open(executionContext); // see asserts in the mock unmarshaller assertNotNull(source.read()); assertNotNull(source.read()); assertNull(source.read()); // there are only two fragments source.close(); }
@Test public void testMultiFragmentNameSpace() throws Exception { source.setResource(new ByteArrayResource(xmlMultiFragment.getBytes())); source.setFragmentRootElementNames(new String[] {"{urn:org.test.bar}fragmentA", "fragmentB"}); source.afterPropertiesSet(); source.open(executionContext); // see asserts in the mock unmarshaller assertNotNull(source.read()); assertNotNull(source.read()); assertNull(source.read()); // there are only two fragments (one has wrong namespace) 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 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()); }
@Test public void testDirectoryResource() throws Exception { FileSystemResource resource = new FileSystemResource("build/data"); resource.getFile().mkdirs(); assertTrue(resource.getFile().isDirectory()); source.setResource(resource); source.afterPropertiesSet(); source.setStrict(false); source.open(executionContext); assertNull(source.read()); }
@Test public void testRuntimeFileCreation() throws Exception { source.setResource(new NonExistentResource()); source.afterPropertiesSet(); source.setResource(new ByteArrayResource(xml.getBytes())); source.open(executionContext); source.read(); }
@Test public void testFragmentInvalid() throws Exception { source.setResource(new ByteArrayResource(invalidXml.getBytes())); source.setFragmentRootElementName(FRAGMENT_ROOT_ELEMENT); source.afterPropertiesSet(); source.open(executionContext); // Should fail before it gets to the marshaller try { assertNotNull(source.read()); fail("Expected NonTransientResourceException"); } catch (NonTransientResourceException e) { // expected } assertNull(source.read()); // after an error there is no more output source.close(); }
@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(); }
/** * 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 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 public void testNonExistentResource() throws Exception { source.setResource(new NonExistentResource()); source.afterPropertiesSet(); source.setStrict(false); source.open(executionContext); assertNull(source.read()); }