@Before public void setUp() throws Exception { File directory = new File("target/data"); directory.mkdirs(); outputFile = File.createTempFile(ClassUtils.getShortName(this.getClass()), ".xml", directory); resource = new FileSystemResource(outputFile); writer.setResource(resource); writer.setMarshaller(getMarshaller()); writer.afterPropertiesSet(); writer.open(new ExecutionContext()); }
@Before public void setUp() throws Exception { File directory = new File("target/data"); directory.mkdirs(); outputFile = File.createTempFile(ClassUtils.getShortName(this.getClass()), ".xml", directory); resource = new FileSystemResource(outputFile); writer.setResource(resource); writer.setMarshaller(getMarshaller()); writer.setRootTagName("{urn:org.springframework.batch.io.oxm.domain}trades"); writer.afterPropertiesSet(); writer.open(new ExecutionContext()); }
/** * Item is written to the output file with namespace. */ @Test public void testWriteRootTagWithNamespace() throws Exception { writer.setRootTagName("{http://www.springframework.org/test}root"); writer.afterPropertiesSet(); writer.open(executionContext); writer.write(items); writer.close(); String content = getOutputFileContent(); assertTrue("Wrong content: " + content, content .contains(("<root xmlns=\"http://www.springframework.org/test\">"))); assertTrue("Wrong content: " + content, content.contains(TEST_STRING)); assertTrue("Wrong content: " + content, content.contains(("</root>"))); }
private void initWriterForSimpleCallbackTests() throws Exception { writer = createItemWriter(); writer.setHeaderCallback(new StaxWriterCallback() { @Override public void write(XMLEventWriter writer) throws IOException { XMLEventFactory factory = XMLEventFactory.newInstance(); try { writer.add(factory.createStartElement("ns", "http://www.springframework.org/test", "group")); } catch (XMLStreamException e) { throw new RuntimeException(e); } } }); writer.setFooterCallback(new StaxWriterCallback() { @Override public void write(XMLEventWriter writer) throws IOException { XMLEventFactory factory = XMLEventFactory.newInstance(); try { writer.add(factory.createEndElement("ns", "http://www.springframework.org/test", "group")); } catch (XMLStreamException e) { throw new RuntimeException(e); } } }); writer.setRootTagName("{http://www.springframework.org/test}ns:testroot"); writer.afterPropertiesSet(); }
/** * Item is written to the output file with additional namespaces and prefix. */ @Test public void testWriteRootTagWithAdditionalNamespace() throws Exception { writer.setRootTagName("{http://www.springframework.org/test}ns:root"); marshaller.setNamespace("urn:org.test.foo"); marshaller.setNamespacePrefix("foo"); writer.setRootElementAttributes(Collections.singletonMap("xmlns:foo", "urn:org.test.foo")); writer.afterPropertiesSet(); writer.open(executionContext); writer.write(items); writer.close(); String content = getOutputFileContent(); assertTrue("Wrong content: " + content, content .contains(("<ns:root xmlns:ns=\"http://www.springframework.org/test\" " + "xmlns:foo=\"urn:org.test.foo\">"))); assertTrue("Wrong content: " + content, content.contains(FOO_TEST_STRING)); assertTrue("Wrong content: " + content, content.contains(("</ns:root>"))); assertTrue("Wrong content: " + content, content.contains(("<ns:root"))); }
/** * Item is written to the output file with namespace and prefix. */ @Test public void testWriteRootTagWithNamespaceAndPrefix() throws Exception { writer.setRootTagName("{http://www.springframework.org/test}ns:root"); writer.afterPropertiesSet(); marshaller.setNamespace(writer.getRootTagNamespace()); marshaller.setNamespacePrefix(writer.getRootTagNamespacePrefix()); writer.open(executionContext); writer.write(items); writer.close(); String content = getOutputFileContent(); assertTrue("Wrong content: " + content, content .contains(("<ns:root xmlns:ns=\"http://www.springframework.org/test\">"))); assertTrue("Wrong content: " + content, content.contains(NS_TEST_STRING)); assertTrue("Wrong content: " + content, content.contains(("</ns:root>"))); assertTrue("Wrong content: " + content, content.contains(("<ns:root"))); }
@Test public void testDeleteIfEmpty() throws Exception { ExecutionContext executionContext = new ExecutionContext(); StaxEventItemWriter<Foo> staxEventItemWriter = new StaxEventItemWriterBuilder<Foo>() .name("fooWriter") .resource(this.resource) .marshaller(this.marshaller) .shouldDeleteIfEmpty(true) .build(); staxEventItemWriter.afterPropertiesSet(); staxEventItemWriter.open(executionContext); staxEventItemWriter.write(Collections.emptyList()); staxEventItemWriter.update(executionContext); staxEventItemWriter.close(); File file = this.resource.getFile(); assertFalse(file.exists()); }
/** * Test with OXM Marshaller that closes the XMLEventWriter. */ // BATCH-2054 @Test public void testMarshallingClosingEventWriter() throws Exception { writer.setMarshaller(new SimpleMarshaller() { @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); } } }); writer.afterPropertiesSet(); writer.open(executionContext); writer.write(items); writer.write(items); }
/** * Namespace prefixes are properly initialized on restart. */ @Test public void testRootTagWithAdditionalNamespaceRestart() throws Exception { writer.setMarshaller(jaxbMarshaller); writer.setRootTagName("{urn:org.test.foo}foo:root"); writer.setRootElementAttributes(Collections.singletonMap("xmlns:ns", "http://www.springframework.org/test")); writer.afterPropertiesSet(); writer.open(executionContext); writer.write(jaxbItems); writer.update(executionContext); writer.close(); writer = createItemWriter(); writer.setMarshaller(jaxbMarshaller); writer.setRootTagName("{urn:org.test.foo}foo:root"); writer.setRootElementAttributes(Collections.singletonMap("xmlns:ns", "http://www.springframework.org/test")); writer.afterPropertiesSet(); writer.open(executionContext); writer.write(jaxbItems); writer.update(executionContext); writer.close(); String content = getOutputFileContent(); assertEquals("Wrong content: " + content, "<foo:root xmlns:foo=\"urn:org.test.foo\" xmlns:ns=\"http://www.springframework.org/test\"><ns:item/><ns:item/></foo:root>", content); }
@Test(expected = ItemStreamException.class) public void testOverwriteOutput() throws Exception { StaxEventItemWriter<Foo> staxEventItemWriter = new StaxEventItemWriterBuilder<Foo>() .name("fooWriter") .marshaller(marshaller) .resource(this.resource) .overwriteOutput(false) .build(); staxEventItemWriter.afterPropertiesSet(); ExecutionContext executionContext = new ExecutionContext(); staxEventItemWriter.open(executionContext); staxEventItemWriter.write(this.items); staxEventItemWriter.update(executionContext); staxEventItemWriter.close(); File output = this.resource.getFile(); assertTrue(output.exists()); executionContext = new ExecutionContext(); staxEventItemWriter.open(executionContext); }
/** * @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; }
/** * Namespace prefixes are properly initialized on restart. */ @Test public void testRootTagWithNamespaceRestart() throws Exception { writer.setMarshaller(jaxbMarshaller); writer.setRootTagName("{http://www.springframework.org/test}root"); writer.afterPropertiesSet(); writer.open(executionContext); writer.write(jaxbItems); writer.update(executionContext); writer.close(); writer = createItemWriter(); writer.setMarshaller(jaxbMarshaller); writer.setRootTagName("{http://www.springframework.org/test}root"); writer.afterPropertiesSet(); writer.open(executionContext); writer.write(jaxbItems); writer.update(executionContext); writer.close(); String content = getOutputFileContent(); assertEquals("Wrong content: " + content, "<root xmlns=\"http://www.springframework.org/test\"><item/><item/></root>", content); }
/** * @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; } }
/** * Namespace prefixes are properly initialized on restart. */ @Test public void testRootTagWithNamespaceAndPrefixRestart() throws Exception { writer.setMarshaller(jaxbMarshaller); writer.setRootTagName("{http://www.springframework.org/test}ns:root"); writer.afterPropertiesSet(); writer.open(executionContext); writer.write(jaxbItems); writer.update(executionContext); writer.close(); writer = createItemWriter(); writer.setMarshaller(jaxbMarshaller); writer.setRootTagName("{http://www.springframework.org/test}ns:root"); writer.afterPropertiesSet(); writer.open(executionContext); writer.write(jaxbItems); writer.update(executionContext); writer.close(); String content = getOutputFileContent(); assertEquals("Wrong content: " + content, "<ns:root xmlns:ns=\"http://www.springframework.org/test\"><ns:item/><ns:item/></ns:root>", content); }
.build(); staxEventItemWriter.afterPropertiesSet();
@Bean public StaxEventItemWriter<Customer> customerItemWriter() throws Exception { XStreamMarshaller marshaller = new XStreamMarshaller(); Map<String, Class> aliases = new HashMap<>(); aliases.put("customer", Customer.class); marshaller.setAliases(aliases); StaxEventItemWriter<Customer> itemWriter = new StaxEventItemWriter<>(); itemWriter.setRootTagName("customers"); itemWriter.setMarshaller(marshaller); String customerOutputPath = File.createTempFile("customerOutput", ".xml").getAbsolutePath(); System.out.println(">> Output Path: " + customerOutputPath); itemWriter.setResource(new FileSystemResource(customerOutputPath)); itemWriter.afterPropertiesSet(); return itemWriter; }
@Bean public StaxEventItemWriter<Customer> xmlItemWriter() throws Exception { XStreamMarshaller marshaller = new XStreamMarshaller(); Map<String, Class> aliases = new HashMap<>(); aliases.put("customer", Customer.class); marshaller.setAliases(aliases); StaxEventItemWriter<Customer> itemWriter = new StaxEventItemWriter<>(); itemWriter.setRootTagName("customers"); itemWriter.setMarshaller(marshaller); String customerOutputPath = File.createTempFile("customerOutput", ".xml").getAbsolutePath(); System.out.println(">> Output Path: " + customerOutputPath); itemWriter.setResource(new FileSystemResource(customerOutputPath)); itemWriter.afterPropertiesSet(); return itemWriter; }