/** * Subclasses can override to customize the STAX result. * * @return a result for writing to */ protected Result createStaxResult() { return StaxUtils.getResult(eventWriter); }
/** * @param fragmentRootElementName name of the root element of the fragment */ public void setFragmentRootElementName(String fragmentRootElementName) { setFragmentRootElementNames(new String[] {fragmentRootElementName}); }
@Override protected void jumpToItem(int itemIndex) throws Exception { for (int i = 0; i < itemIndex; i++) { try { QName fragmentName = readToStartFragment(); readToEndFragment(fragmentName); } catch (NoSuchElementException e) { if (itemIndex == (i + 1)) { // we can presume a NoSuchElementException on the last item means the EOF was reached on the last run return; } else { // if NoSuchElementException occurs on an item other than the last one, this indicates a problem throw e; } } } }
/** * @return new instance of fully configured writer */ private StaxEventItemWriter<Object> createItemWriter() throws Exception { StaxEventItemWriter<Object> source = new StaxEventItemWriter<>(); source.setResource(resource); marshaller = new SimpleMarshaller(); source.setMarshaller(marshaller); source.setEncoding("UTF-8"); source.setRootTagName("root"); source.setVersion("1.0"); source.setOverwriteOutput(true); source.setSaveState(true); source.afterPropertiesSet(); return source; }
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; }
@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(expected = ItemStreamException.class) public void testStrictness() throws Exception { source.setResource(new NonExistentResource()); source.setStrict(true); source.afterPropertiesSet(); source.open(executionContext); }
@Before public void setUp() throws Exception { reader.setFragmentRootElementName("trade"); reader.setUnmarshaller(getUnmarshaller()); reader.afterPropertiesSet(); }
@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 opening and closing corresponding tags in header- and footer callback. */ @Test public void testOpenAndCloseTagsInCallbacks() throws Exception { initWriterForSimpleCallbackTests(); writer.open(executionContext); writer.write(items); writer.close(); String content = getOutputFileContent(); assertEquals("Wrong content: " + content, "<ns:testroot xmlns:ns=\"http://www.springframework.org/test\"><ns:group><StaxEventItemWriter-testString/></ns:group></ns:testroot>", content); }
@Override protected void pointReaderToOutput(ItemReader<CustomerCredit> reader) { StaxEventItemReader<?> xmlReader = (StaxEventItemReader<?>) reader; xmlReader.setResource(outputResource); }
@Override public Object unmarshal(Source source) throws XmlMappingException, IOException { List<XMLEvent> fragment = (List<XMLEvent>) super.unmarshal(source); if(fragment != null) { return new ItemCountAwareFragment(fragment); } else { return null; } } }
@Override public void open(ExecutionContext executionContext) { super.open(executionContext); openCalled = true; }
@Override public void marshal(Object graph, Result result) throws XmlMappingException, IOException { super.marshal(graph, result); try { StaxTestUtils.getXmlEventWriter(result).close(); } catch (Exception e) { throw new RuntimeException("Exception while writing to output file", e); } }
/** * @return output file content as String */ private String getOutputFileContent() throws IOException { return getOutputFileContent("UTF-8"); }
@Test // BATCH-1959 public void testTransactionalRestartWithMultiByteCharacterUTF8() throws Exception { testTransactionalRestartWithMultiByteCharacter("UTF-8"); }
/** * @return new instance of fully configured writer */ private StaxEventItemWriter<Object> createItemWriter() throws Exception { StaxEventItemWriter<Object> source = new StaxEventItemWriter<>(); source.setResource(resource); Marshaller marshaller = new SimpleMarshaller(); source.setMarshaller(marshaller); source.setEncoding("UTF-8"); source.setRootTagName("root"); source.setVersion("1.0"); source.setOverwriteOutput(true); source.setSaveState(true); source.afterPropertiesSet(); return source; } }
@Test public void testNonExistentResource() throws Exception { source.setResource(new NonExistentResource()); source.afterPropertiesSet(); source.setStrict(false); source.open(executionContext); assertNull(source.read()); }
@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(); }