/** * @return output file content as String */ private String getOutputFileContent() throws IOException { return getOutputFileContent("UTF-8"); }
/** * Item is written to the output file only after flush. */ @Test public void testWriteAndFlush() throws Exception { writer.open(executionContext); writer.write(items); writer.close(); String content = getOutputFileContent(); assertTrue("Wrong content: " + content, content.contains(TEST_STRING)); }
@Test public void testWriteAndForceFlush() throws Exception { writer.setForceSync(true); writer.open(executionContext); writer.write(items); writer.close(); String content = getOutputFileContent(); assertTrue("Wrong content: " + content, content.contains(TEST_STRING)); }
/** * Resource is not deleted when items have been written and shouldDeleteIfEmpty flag is set. */ @Test public void testDeleteIfEmptyRecordsWritten() throws Exception { writer.setShouldDeleteIfEmpty(true); writer.open(executionContext); writer.write(items); writer.close(); String content = getOutputFileContent(); assertTrue("Wrong content: " + content, content.contains(TEST_STRING)); }
/** * Item is written to the output file only after flush. */ @Test public void testWriteWithHeader() throws Exception { writer.setHeaderCallback(new StaxWriterCallback() { @Override public void write(XMLEventWriter writer) throws IOException { XMLEventFactory factory = XMLEventFactory.newInstance(); try { writer.add(factory.createStartElement("", "", "header")); writer.add(factory.createEndElement("", "", "header")); } catch (XMLStreamException e) { throw new RuntimeException(e); } } }); writer.open(executionContext); writer.write(items); String content = getOutputFileContent(); assertTrue("Wrong content: " + content, content.contains(("<header/>"))); assertTrue("Wrong content: " + content, content.contains(TEST_STRING)); }
/** * 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>"))); }
/** * 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"))); }
String outputFile = getOutputFileContent(); assertEquals(2, StringUtils.countOccurrencesOf(outputFile, TEST_STRING)); assertTrue(outputFile.contains("<root>" + TEST_STRING + TEST_STRING + "</root>"));
/** * 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); }
writer.open(executionContext); writer.close(); String content = getOutputFileContent();
/** * 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 that the writer can restart if the previous execution deleted empty file. */ @Test public void testDeleteIfEmptyRestartAfterDelete() throws Exception { writer.setShouldDeleteIfEmpty(true); writer.open(executionContext); writer.update(executionContext); writer.close(); assertFalse(resource.getFile().exists()); writer = createItemWriter(); writer.setShouldDeleteIfEmpty(true); writer.open(executionContext); writer.write(items); writer.update(executionContext); writer.close(); String content = getOutputFileContent(); assertTrue("Wrong content: " + content, content.contains(TEST_STRING)); }
/** * Resource is not deleted when items have been written and shouldDeleteIfEmpty flag is set. */ @Test public void testDeleteIfEmptyRecordsWrittenRestart() throws Exception { writer.setShouldDeleteIfEmpty(true); writer.open(executionContext); writer.write(items); writer.update(executionContext); writer.close(); writer = createItemWriter(); writer.setShouldDeleteIfEmpty(true); writer.open(executionContext); writer.close(); String content = getOutputFileContent(); assertTrue("Wrong content: " + content, content.contains(TEST_STRING)); }
writer.update(executionContext); writer.close(); String content = getOutputFileContent(); assertTrue("Wrong content: " + content, content.contains(TEST_STRING));
/** * Restart scenario - content is appended to the output file after restart. */ @Test public void testRestart() throws Exception { writer.open(executionContext); // write item writer.write(items); writer.update(executionContext); writer.close(); // create new writer from saved restart data and continue writing writer = createItemWriter(); writer.open(executionContext); writer.write(items); writer.write(items); writer.close(); // check the output is concatenation of 'before restart' and 'after // restart' writes. String outputFile = getOutputFileContent(); assertEquals(3, StringUtils.countOccurrencesOf(outputFile, TEST_STRING)); assertEquals("<root>" + TEST_STRING + TEST_STRING + TEST_STRING + "</root>", outputFile.replace(" ", "")); }
/** * Test opening and closing corresponding tags in header- and footer callback (restart). */ @Test public void testOpenAndCloseTagsInCallbacksRestart() throws Exception { initWriterForSimpleCallbackTests(); writer.open(executionContext); writer.write(items); writer.update(executionContext); 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/><StaxEventItemWriter-testString/></ns:group></ns:testroot>", content); }
/** * Test opening and closing corresponding tags in complex header- and footer callback (restart). */ @Test public void testOpenAndCloseTagsInComplexCallbacksRestart() throws Exception { initWriterForComplexCallbackTests(); writer.open(executionContext); writer.write(items); writer.update(executionContext); initWriterForComplexCallbackTests(); writer.open(executionContext); writer.write(items); writer.close(); String content = getOutputFileContent(); assertEquals("Wrong content: " + content, "<ns:testroot xmlns:ns=\"http://www.springframework.org/test\">" + "<preHeader>PRE-HEADER</preHeader><ns:group><subGroup><postHeader>POST-HEADER</postHeader>" + "<StaxEventItemWriter-testString/><StaxEventItemWriter-testString/>" + "<preFooter>PRE-FOOTER</preFooter></subGroup></ns:group><postFooter>POST-FOOTER</postFooter>" + "</ns:testroot>", content); }
/** * 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); }
/** * 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); }
/** * 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); }