@Override public void open(ExecutionContext executionContext) throws ItemStreamException { this.delegate.open(executionContext); }
@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 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[], LineDescriptor)</code> method * * @throws Exception */ @Test public void testWriteRecord() throws Exception { writer.open(executionContext); writer.write(Collections.singletonList("1")); String lineFromFile = readLine(); assertEquals("1", lineFromFile); }
@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 testWriteRecordWithrecordSeparator() throws Exception { writer.setLineSeparator("|"); writer.open(executionContext); writer.write(Arrays.asList(new String[] { "1", "2" })); String lineFromFile = readLine(); assertEquals("1|2|", lineFromFile); }
/** * 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); }
/** * Regular usage of <code>write(String)</code> method * * @throws Exception */ @Test public void testWriteWithConverter() throws Exception { writer.setLineAggregator(new LineAggregator<String>() { @Override public String aggregate(String item) { return "FOO:" + item; } }); String data = "string"; writer.open(executionContext); writer.write(Collections.singletonList(data)); String lineFromFile = readLine(); // converter not used if input is String assertEquals("FOO:" + data, lineFromFile); }
@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 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)); }
@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 testDefaultStreamContext() throws Exception { writer = new FlatFileItemWriter<>(); writer.setResource(new FileSystemResource(outputFile)); writer.setLineAggregator(new PassThroughLineAggregator<>()); writer.afterPropertiesSet(); writer.setSaveState(true); writer.open(executionContext); writer.update(executionContext); assertNotNull(executionContext); assertEquals(2, executionContext.entrySet().size()); assertEquals(0, executionContext.getLong(ClassUtils.getShortName(FlatFileItemWriter.class) + ".current.count")); }
@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 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); }