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; }
@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(); }
Assert.notNull(this.resource, "A resource is required."); StaxEventItemReader<T> reader = new StaxEventItemReader<>(); reader.setName(this.name); reader.setSaveState(this.saveState); reader.setResource(this.resource); reader.setFragmentRootElementNames( this.fragmentRootElements.toArray(new String[this.fragmentRootElements.size()])); reader.setStrict(this.strict); reader.setUnmarshaller(this.unmarshaller); reader.setCurrentItemCount(this.currentItemCount); reader.setMaxItemCount(this.maxItemCount); reader.setXmlInputFactory(this.xmlInputFactory);
@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()); }
@Before public void setUp() throws Exception { reader.setFragmentRootElementName("trade"); reader.setUnmarshaller(getUnmarshaller()); reader.afterPropertiesSet(); }
@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); }
/** * Make sure the reader doesn't end up in inconsistent state if there's an error during unmarshalling (BATCH-1738). * After an error during <code>read</code> the next <code>read</code> call should continue with reading the next * fragment. */ @Test public void exceptionDuringUnmarshalling() throws Exception { source.setUnmarshaller(new TroublemakerUnmarshaller()); source.afterPropertiesSet(); source.open(executionContext); try { source.read(); fail(); } catch (UnmarshallingFailureException expected) { assert expected.getMessage() == TroublemakerUnmarshaller.MESSAGE; } try { source.read(); fail(); } catch (UnmarshallingFailureException expected) { assert expected.getMessage() == TroublemakerUnmarshaller.MESSAGE; } assertNull(source.read()); }
@Override protected void pointReaderToOutput(ItemReader<CustomerCredit> reader) { StaxEventItemReader<?> xmlReader = (StaxEventItemReader<?>) reader; xmlReader.setResource(outputResource); }
/** * Document with no fragments works OK. */ @Test public void testMoveCursorToNextFragmentOnMissing() throws XMLStreamException, FactoryConfigurationError, IOException { Resource resource = new ByteArrayResource(missingXml.getBytes()); XMLEventReader reader = StaxUtils.createXmlInputFactory().createXMLEventReader(resource.getInputStream()); assertFalse(source.moveCursorToNextFragment(reader)); }
@Override protected Unmarshaller getUnmarshaller() throws Exception { reader.setFragmentRootElementName("trade"); Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); marshaller.setClassesToBeBound(new Class<?>[] { Trade.class }); // marshaller.setSchema(new ClassPathResource("trade.xsd", Trade.class)); marshaller.afterPropertiesSet(); return marshaller; }
/** * @param fragmentRootElementName name of the root element of the fragment */ public void setFragmentRootElementName(String fragmentRootElementName) { setFragmentRootElementNames(new String[] {fragmentRootElementName}); }
public StaxEventItemReader() { setName(ClassUtils.getShortName(StaxEventItemReader.class)); }
@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 } }
@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); }
/** * Empty document works OK. */ @Test public void testMoveCursorToNextFragmentOnEmpty() throws XMLStreamException, FactoryConfigurationError, IOException { Resource resource = new ByteArrayResource(emptyXml.getBytes()); XMLEventReader reader = StaxUtils.createXmlInputFactory().createXMLEventReader(resource.getInputStream()); assertFalse(source.moveCursorToNextFragment(reader)); }
/** * @param fragmentRootElementName name of the root element of the fragment */ public void setFragmentRootElementName(String fragmentRootElementName) { setFragmentRootElementNames(new String[] {fragmentRootElementName}); }
public StaxEventItemReader() { setName(ClassUtils.getShortName(StaxEventItemReader.class)); }
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; }