@Override protected void pointReaderToOutput(ItemReader<CustomerCredit> reader) { StaxEventItemReader<?> xmlReader = (StaxEventItemReader<?>) reader; xmlReader.setResource(outputResource); }
@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 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); }
private StaxEventItemReader<List<XMLEvent>> createNewInputSource() { Resource resource = new ByteArrayResource(xml.getBytes()); StaxEventItemReader<List<XMLEvent>> newSource = new StaxEventItemReader<>(); newSource.setResource(resource); newSource.setFragmentRootElementName(FRAGMENT_ROOT_ELEMENT); newSource.setUnmarshaller(unmarshaller); newSource.setSaveState(true); return newSource; }
@Override protected void pointToEmptyInput(ItemReader<Foo> tested) throws Exception { StaxEventItemReader<Foo> reader = (StaxEventItemReader<Foo>) tested; reader.close(); reader.setResource(new ByteArrayResource("<foos />".getBytes())); reader.afterPropertiesSet(); reader.open(new ExecutionContext()); }
@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()); }
private StaxEventItemReader<ItemCountAwareFragment> createNewItemCountAwareInputSource() { Resource resource = new ByteArrayResource(xml.getBytes()); StaxEventItemReader<ItemCountAwareFragment> newSource = new StaxEventItemReader<>(); newSource.setResource(resource); newSource.setFragmentRootElementName(FRAGMENT_ROOT_ELEMENT); newSource.setUnmarshaller(new ItemCountAwareMockFragmentUnmarshaller()); newSource.setSaveState(true); return newSource; }
@Test(expected = ItemStreamException.class) public void testStrictness() throws Exception { source.setResource(new NonExistentResource()); source.setStrict(true); source.afterPropertiesSet(); source.open(executionContext); }
@Test public void testDtdXml() { String xmlWithDtd = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<!DOCTYPE rohit [\n<!ENTITY entityex SYSTEM \"file://" + new File("src/test/resources/org/springframework/batch/support/existing.txt").getAbsolutePath() + "\">\n]>\n<abc>&entityex;</abc>"; StaxEventItemReader<String> reader = new StaxEventItemReader<>(); reader.setName("foo"); reader.setResource(new ByteArrayResource(xmlWithDtd.getBytes())); reader.setUnmarshaller(new MockFragmentUnmarshaller() { @Override public Object unmarshal(Source source) throws XmlMappingException { try { XMLEventReader xmlEventReader = StaxTestUtils.getXmlEventReader(source); xmlEventReader.nextEvent(); xmlEventReader.nextEvent(); return xmlEventReader.getElementText(); } catch (Exception e) { throw new RuntimeException(e); } } }); reader.setFragmentRootElementName("abc"); reader.open(new ExecutionContext()); try { reader.read(); fail("Should fail when XML contains DTD"); } catch (Exception e) { Assert.assertThat(e.getMessage(), Matchers.containsString("Undeclared general entity \"entityex\"")); } }
@Before public void setUp() throws Exception { reader.setResource(resource); reader.setFragmentRootElementName("{urn:org.springframework.batch.io.oxm.domain}trade"); reader.setUnmarshaller(getUnmarshaller()); reader.afterPropertiesSet(); reader.open(new ExecutionContext()); }
@Test public void testNonExistentResource() throws Exception { source.setResource(new NonExistentResource()); source.afterPropertiesSet(); source.setStrict(false); source.open(executionContext); assertNull(source.read()); }
@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(); }
@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 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 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 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 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(); }