@Override public void close() throws ItemStreamException { this.delegate.close(); }
/** * Release resources and delete the temporary output file */ @After public void tearDown() throws Exception { if (reader != null) { reader.close(); } writer.close(); outputFile.delete(); }
private void writeStringTransactionCheck(final String expectedInTransaction) { PlatformTransactionManager transactionManager = new ResourcelessTransactionManager(); writer.open(executionContext); new TransactionTemplate(transactionManager).execute(new TransactionCallback<Void>() { @Override public Void doInTransaction(TransactionStatus status) { try { writer.write(Collections.singletonList(TEST_STRING)); assertEquals(expectedInTransaction, readLine()); } catch (Exception e) { throw new UnexpectedInputException("Could not write data", e); } return null; } }); writer.close(); }
@Test public void testDeleteOnExitNoRecordsWrittenAfterRestart() throws Exception { writer.setShouldDeleteIfEmpty(true); writer.open(executionContext); writer.write(Collections.singletonList("test2")); writer.update(executionContext); writer.close(); assertTrue(outputFile.exists()); writer.open(executionContext); writer.close(); assertTrue(outputFile.exists()); }
/** * Regular usage of <code>write(String)</code> method * * @throws Exception */ @Test public void testWriteString() throws Exception { writer.open(executionContext); writer.write(Collections.singletonList(TEST_STRING)); writer.close(); String lineFromFile = readLine(); assertEquals(TEST_STRING, lineFromFile); }
@Test public void testWriteHeaderAndDeleteOnExit() throws Exception { writer.setHeaderCallback(new FlatFileHeaderCallback() { @Override public void writeHeader(Writer writer) throws IOException { writer.write("a\nb"); } }); writer.setShouldDeleteIfEmpty(true); writer.open(executionContext); assertTrue(outputFile.exists()); writer.close(); assertFalse(outputFile.exists()); }
@Test public void testForcedWriteString() throws Exception { writer.setForceSync(true); writer.open(executionContext); writer.write(Collections.singletonList(TEST_STRING)); writer.close(); String lineFromFile = readLine(); assertEquals(TEST_STRING, lineFromFile); }
@Test public void testWriteStringWithEncodingAfterClose() throws Exception { writer.open(executionContext); writer.write(Collections.singletonList(TEST_STRING)); writer.close(); writer.setEncoding("UTF-8"); writer.open(executionContext); writer.write(Collections.singletonList(TEST_STRING)); String lineFromFile = readLine(); assertEquals(TEST_STRING, lineFromFile); }
@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 testDeleteOnExitReopen() throws Exception { writer.setShouldDeleteIfEmpty(true); writer.open(executionContext); writer.update(executionContext); assertTrue(outputFile.exists()); writer.close(); assertFalse(outputFile.exists()); writer.open(executionContext); writer.write(Collections.singletonList("test2")); assertEquals("test2", readLine()); }
@Test public void testWriteWithDelete() throws Exception { writer.open(executionContext); writer.write(Collections.singletonList("test1")); writer.close(); assertEquals("test1", readLine()); closeReader(); writer.setShouldDeleteIfExists(true); writer.open(executionContext); writer.write(Collections.singletonList("test2")); assertEquals("test2", readLine()); }
@Test public void testDelimitedOutputWithDefaultDelimiter() throws Exception { Resource output = new FileSystemResource(File.createTempFile("foo", "txt")); FlatFileItemWriter<Foo> writer = new FlatFileItemWriterBuilder<Foo>() .name("foo") .resource(output) .lineSeparator("$") .delimited() .names("first", "second", "third") .encoding("UTF-16LE") .headerCallback(writer1 -> writer1.append("HEADER")) .footerCallback(writer12 -> writer12.append("FOOTER")) .build(); ExecutionContext executionContext = new ExecutionContext(); writer.open(executionContext); writer.write(Arrays.asList(new Foo(1, 2, "3"), new Foo(4, 5, "6"))); writer.close(); assertEquals("HEADER$1,2,3$4,5,6$FOOTER", readLine("UTF-16LE", output)); }
@Test public void test() throws Exception { Resource output = new FileSystemResource(File.createTempFile("foo", "txt")); FlatFileItemWriter<Foo> writer = new FlatFileItemWriterBuilder<Foo>() .name("foo") .resource(output) .lineSeparator("$") .lineAggregator(new PassThroughLineAggregator<>()) .encoding("UTF-16LE") .headerCallback(writer1 -> writer1.append("HEADER")) .footerCallback(writer12 -> writer12.append("FOOTER")) .build(); ExecutionContext executionContext = new ExecutionContext(); writer.open(executionContext); writer.write(Arrays.asList(new Foo(1, 2, "3"), new Foo(4, 5, "6"))); writer.close(); assertEquals("HEADER$Foo{first=1, second=2, third='3'}$Foo{first=4, second=5, third='6'}$FOOTER", readLine("UTF-16LE", output)); }
@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 testWriteFooter() throws Exception { writer.setFooterCallback(new FlatFileFooterCallback() { @Override public void writeFooter(Writer writer) throws IOException { writer.write("a\nb"); } }); writer.open(executionContext); writer.write(Collections.singletonList(TEST_STRING)); writer.close(); assertEquals(TEST_STRING, readLine()); assertEquals("a", readLine()); assertEquals("b", readLine()); }
@Test public void testFormattedOutputWithDefaultFieldExtractor() throws Exception { Resource output = new FileSystemResource(File.createTempFile("foo", "txt")); FlatFileItemWriter<Foo> writer = new FlatFileItemWriterBuilder<Foo>() .name("foo") .resource(output) .lineSeparator("$") .formatted() .format("%2s%2s%2s") .names("first", "second", "third") .encoding("UTF-16LE") .headerCallback(writer1 -> writer1.append("HEADER")) .footerCallback(writer12 -> writer12.append("FOOTER")) .build(); ExecutionContext executionContext = new ExecutionContext(); writer.open(executionContext); writer.write(Arrays.asList(new Foo(1, 2, "3"), new Foo(4, 5, "6"))); writer.close(); assertEquals("HEADER$ 1 2 3$ 4 5 6$FOOTER", readLine("UTF-16LE", output)); }
@Test public void testDelimitedOutputWithDefaultFieldExtractor() throws Exception { Resource output = new FileSystemResource(File.createTempFile("foo", "txt")); FlatFileItemWriter<Foo> writer = new FlatFileItemWriterBuilder<Foo>() .name("foo") .resource(output) .lineSeparator("$") .delimited() .delimiter(";") .names("first", "second", "third") .encoding("UTF-16LE") .headerCallback(writer1 -> writer1.append("HEADER")) .footerCallback(writer12 -> writer12.append("FOOTER")) .build(); ExecutionContext executionContext = new ExecutionContext(); writer.open(executionContext); writer.write(Arrays.asList(new Foo(1, 2, "3"), new Foo(4, 5, "6"))); writer.close(); assertEquals("HEADER$1;2;3$4;5;6$FOOTER", readLine("UTF-16LE", output)); }
@Test public void testFormattedOutputWithCustomFieldExtractor() throws Exception { Resource output = new FileSystemResource(File.createTempFile("foo", "txt")); FlatFileItemWriter<Foo> writer = new FlatFileItemWriterBuilder<Foo>() .name("foo") .resource(output) .lineSeparator("$") .formatted() .format("%3s%3s") .fieldExtractor(item -> new Object[] {item.getFirst(), item.getThird()}) .encoding("UTF-16LE") .headerCallback(writer1 -> writer1.append("HEADER")) .footerCallback(writer12 -> writer12.append("FOOTER")) .build(); ExecutionContext executionContext = new ExecutionContext(); writer.open(executionContext); writer.write(Arrays.asList(new Foo(1, 2, "3"), new Foo(4, 5, "6"))); writer.close(); assertEquals("HEADER$ 1 3$ 4 6$FOOTER", readLine("UTF-16LE", output)); }
@Test public void testWriteHeader() throws Exception { writer.setHeaderCallback(new FlatFileHeaderCallback() { @Override public void writeHeader(Writer writer) throws IOException { writer.write("a\nb"); } }); writer.open(executionContext); writer.write(Collections.singletonList(TEST_STRING)); writer.close(); String lineFromFile = readLine(); assertEquals("a", lineFromFile); lineFromFile = readLine(); assertEquals("b", lineFromFile); lineFromFile = readLine(); assertEquals(TEST_STRING, lineFromFile); }
@Test public void testWriteStringWithBogusEncoding() throws Exception { writer.setTransactional(false); writer.setEncoding("BOGUS"); // writer.setShouldDeleteIfEmpty(true); try { writer.open(executionContext); fail("Expected ItemStreamException"); } catch (ItemStreamException e) { assertTrue(e.getCause() instanceof UnsupportedCharsetException); } writer.close(); // Try and write after the exception on open: writer.setEncoding("UTF-8"); writer.open(executionContext); writer.write(Collections.singletonList(TEST_STRING)); }