@Override public Void doInTransaction(TransactionStatus status) { try { writer.write(itemsMultiByte); } catch (Exception e) { throw new UnexpectedInputException("Could not write data", e); } // get restart data writer.update(executionContext); return null; } });
@Override public Void doInTransaction(TransactionStatus status) { try { // write item writer.write(itemsMultiByte); } catch (Exception e) { throw new UnexpectedInputException("Could not write data", e); } // get restart data writer.update(executionContext); return null; } });
@Override public Void doInTransaction(TransactionStatus status) { try { // write item writer.write(items); } catch (Exception e) { throw new UnexpectedInputException("Could not write data", e); } // get restart data writer.update(executionContext); return null; } });
@Override public Void doInTransaction(TransactionStatus status) { try { writer.write(items); } catch (Exception e) { throw new UnexpectedInputException("Could not write data", e); } // get restart data writer.update(executionContext); return null; } });
@Override public Void doInTransaction(TransactionStatus status) { writer.open(executionContext); try { writer.write(items); } catch (Exception e) { throw new UnexpectedInputException("Could not write data", e); } // get restart data writer.update(executionContext); return null; } });
/** * 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)); }
@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 setting writer name. */ @Test public void testSetName() throws Exception { writer.setName("test"); writer.open(executionContext); writer.write(items); writer.update(executionContext); writer.close(); assertTrue("execution context keys should be prefixed with writer name", executionContext.containsKey("test.position")); }
/** * 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)); }
/** * 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); }
writer.update(executionContext); writer.close(); writer.open(executionContext);
@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); }
/** * Count of 'records written so far' is returned as statistics. */ @Test public void testStreamContext() throws Exception { writer.open(executionContext); final int NUMBER_OF_RECORDS = 10; assertFalse(executionContext.containsKey(ClassUtils.getShortName(StaxEventItemWriter.class) + ".record.count")); for (int i = 1; i <= NUMBER_OF_RECORDS; i++) { writer.write(items); writer.update(executionContext); long writeStatistics = executionContext.getLong(ClassUtils.getShortName(StaxEventItemWriter.class) + ".record.count"); assertEquals(i, writeStatistics); } }
/** * 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); }
/** * 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); }
staxEventItemWriter.update(executionContext); staxEventItemWriter.close();