writer.setAppendAllowed(this.append); writer.setEncoding(this.encoding); writer.setFooterCallback(this.footerCallback);
@Test /** * If append=true a new output file should still be created on the first run (not restart). */ public void testAppendToNotYetExistingFile() throws Exception { Resource toBeCreated = new FileSystemResource("build/FlatFileItemWriterTests.out"); outputFile = toBeCreated.getFile(); //enable easy content reading and auto-delete the file assertFalse("output file does not exist yet", toBeCreated.exists()); writer.setResource(toBeCreated); writer.setAppendAllowed(true); writer.afterPropertiesSet(); writer.open(executionContext); assertTrue("output file was created", toBeCreated.exists()); writer.write(Collections.singletonList("test1")); writer.close(); assertEquals("test1", readLine()); } }
@Test public void testWriteWithAppend() throws Exception { writer.setAppendAllowed(true); writer.open(executionContext); writer.write(Collections.singletonList("test1")); writer.close(); assertEquals("test1", readLine()); closeReader(); writer.open(executionContext); writer.write(Collections.singletonList("test2")); assertEquals("test1", readLine()); assertEquals("test2", readLine()); }
@Test public void testWriteWithAppendAfterHeaders() throws Exception { writer.setHeaderCallback(new FlatFileHeaderCallback() { @Override public void writeHeader(Writer writer) throws IOException { writer.write("a\nb"); } }); writer.setAppendAllowed(true); writer.open(executionContext); writer.write(Collections.singletonList("test1")); writer.close(); assertEquals("a", readLine()); assertEquals("b", readLine()); assertEquals("test1", readLine()); closeReader(); writer.open(executionContext); writer.write(Collections.singletonList("test2")); assertEquals("a", readLine()); assertEquals("b", readLine()); assertEquals("test1", readLine()); assertEquals("test2", readLine()); }
@Test public void testWriteWithAppendRestartOnSecondChunk() throws Exception { // This should be overridden via the writer#setAppendAllowed(true) writer.setShouldDeleteIfExists(true); writer.setAppendAllowed(true); writer.open(executionContext); writer.write(Collections.singletonList("test1")); writer.close(); assertEquals("test1", readLine()); closeReader(); writer.open(executionContext); writer.write(Collections.singletonList(TEST_STRING)); writer.update(executionContext); writer.write(Collections.singletonList(TEST_STRING)); writer.close(); assertEquals("test1", readLine()); assertEquals(TEST_STRING, readLine()); assertEquals(TEST_STRING, readLine()); assertEquals(null, readLine()); writer.open(executionContext); writer.write(Collections.singletonList(TEST_STRING)); writer.close(); closeReader(); assertEquals("test1", readLine()); assertEquals(TEST_STRING, readLine()); assertEquals(TEST_STRING, readLine()); assertEquals(null, readLine()); }
writer.setAppendAllowed(this.append); writer.setEncoding(this.encoding); writer.setFooterCallback(this.footerCallback);