@Before public void setUp() throws Exception { this.delegate = new FlatFileItemWriter<>(); this.delegate.setLineAggregator(new PassThroughLineAggregator<>()); this.file = File.createTempFile(MultiResourceItemWriterFlatFileTests.class.getSimpleName(), null); this.writer = null; }
@Before public void setUp() throws Exception { super.createFile(); delegate = new FlatFileItemWriter<>(); delegate.setLineAggregator(new PassThroughLineAggregator<>()); }
/** * Create temporary output file, define mock behaviour, set dependencies and initialize the object under test */ @Before public void setUp() throws Exception { outputFile = File.createTempFile("flatfile-test-output-", ".tmp"); writer.setResource(new FileSystemResource(outputFile)); writer.setLineSeparator("\n"); writer.setLineAggregator(new PassThroughLineAggregator<>()); writer.afterPropertiesSet(); writer.setSaveState(true); writer.setEncoding("UTF-8"); executionContext = new ExecutionContext(); }
@Test public void testOpenWithNonWritableFile() throws Exception { writer = new FlatFileItemWriter<>(); writer.setLineAggregator(new PassThroughLineAggregator<>()); FileSystemResource file = new FileSystemResource("build/no-such-file.foo"); writer.setResource(file); new File(file.getFile().getParent()).mkdirs(); file.getFile().createNewFile(); assertTrue("Test file must exist: " + file, file.exists()); assertTrue("Test file set to read-only: " + file, file.getFile().setReadOnly()); assertFalse("Should be readonly file: " + file, file.getFile().canWrite()); writer.afterPropertiesSet(); try { writer.open(executionContext); fail("Expected IllegalStateException"); } catch (IllegalStateException e) { String message = e.getMessage(); assertTrue("Message does not contain 'writable': " + message, message.indexOf("writable") >= 0); } }
@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 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 testFlags() throws Exception { Resource output = new FileSystemResource(File.createTempFile("foo", "txt")); FlatFileItemWriter<Foo> writer = new FlatFileItemWriterBuilder<Foo>() .name("foo") .resource(output) .shouldDeleteIfEmpty(true) .shouldDeleteIfExists(false) .saveState(false) .forceSync(true) .append(true) .transactional(false) .lineAggregator(new PassThroughLineAggregator<>()) .build(); assertFalse((Boolean) ReflectionTestUtils.getField(writer, "saveState")); assertTrue((Boolean) ReflectionTestUtils.getField(writer, "append")); assertFalse((Boolean) ReflectionTestUtils.getField(writer, "transactional")); assertTrue((Boolean) ReflectionTestUtils.getField(writer, "shouldDeleteIfEmpty")); assertFalse((Boolean) ReflectionTestUtils.getField(writer, "shouldDeleteIfExists")); assertTrue((Boolean) ReflectionTestUtils.getField(writer, "forceSync")); }