writer.setResource(this.resource); writer.setSaveState(this.saveState); writer.setShouldDeleteIfEmpty(this.shouldDeleteIfEmpty);
@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()); } }
/** * 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 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")); }
@Override public void setResource(Resource resource){ String fileName = resource.getFilename(); try { File file = new File(writeDirectory.getFile(), fileName); file.createNewFile(); if(file.exists()) logger.info("File created for writing file name: " + file); else logger.error("File not created for writing file name: " + file); super.setResource(new FileSystemResource(file)); } catch (IOException e) { logger.error("Error attempting to create file " + fileName + "for writing raw output.", e); e.printStackTrace(); } }
@Bean public StaxEventItemWriter<Customer> customerItemWriter() throws Exception { XStreamMarshaller marshaller = new XStreamMarshaller(); Map<String, Class> aliases = new HashMap<>(); aliases.put("customer", Customer.class); marshaller.setAliases(aliases); StaxEventItemWriter<Customer> itemWriter = new StaxEventItemWriter<>(); itemWriter.setRootTagName("customers"); itemWriter.setMarshaller(marshaller); String customerOutputPath = File.createTempFile("customerOutput", ".xml").getAbsolutePath(); System.out.println(">> Output Path: " + customerOutputPath); itemWriter.setResource(new FileSystemResource(customerOutputPath)); itemWriter.afterPropertiesSet(); return itemWriter; }
@Bean public FlatFileItemWriter<Customer> customerItemWriter() throws Exception { FlatFileItemWriter<Customer> itemWriter = new FlatFileItemWriter<>(); itemWriter.setLineAggregator(new CustomerLineAggregator()); String customerOutputPath = File.createTempFile("customerOutput", ".out").getAbsolutePath(); System.out.println(">> Output Path: " + customerOutputPath); itemWriter.setResource(new FileSystemResource(customerOutputPath)); itemWriter.afterPropertiesSet(); return itemWriter; }
@Bean public FlatFileItemWriter<Customer> jsonItemWriter() throws Exception { FlatFileItemWriter<Customer> itemWriter = new FlatFileItemWriter<>(); itemWriter.setLineAggregator(new CustomerLineAggregator()); String customerOutputPath = File.createTempFile("customerOutput", ".out").getAbsolutePath(); System.out.println(">> Output Path: " + customerOutputPath); itemWriter.setResource(new FileSystemResource(customerOutputPath)); itemWriter.afterPropertiesSet(); return itemWriter; }
@Bean public FlatFileItemWriter<Customer> customerItemWriter() throws Exception { FlatFileItemWriter<Customer> itemWriter = new FlatFileItemWriter<>(); itemWriter.setLineAggregator(new CustomerLineAggregator()); String customerOutputPath = File.createTempFile("customerOutput", ".out").getAbsolutePath(); System.out.println(">> Output Path: " + customerOutputPath); itemWriter.setResource(new FileSystemResource(customerOutputPath)); itemWriter.afterPropertiesSet(); return itemWriter; }
@Bean public FlatFileItemWriter<Customer> customerItemWriter() throws Exception { FlatFileItemWriter<Customer> itemWriter = new FlatFileItemWriter<>(); itemWriter.setLineAggregator(new CustomerLineAggregator()); String customerOutputPath = File.createTempFile("customerOutput", ".out").getAbsolutePath(); System.out.println(">> Output Path: " + customerOutputPath); itemWriter.setResource(new FileSystemResource(customerOutputPath)); itemWriter.afterPropertiesSet(); return itemWriter; }
@Bean ItemWriter<StudentDTO> databaseCsvItemWriter(Environment environment) { FlatFileItemWriter<StudentDTO> csvFileWriter = new FlatFileItemWriter<>(); String exportFileHeader = environment.getRequiredProperty(PROPERTY_CSV_EXPORT_FILE_HEADER); StringHeaderWriter headerWriter = new StringHeaderWriter(exportFileHeader); csvFileWriter.setHeaderCallback(headerWriter); String exportFilePath = environment.getRequiredProperty(PROPERTY_CSV_EXPORT_FILE_PATH); csvFileWriter.setResource(new FileSystemResource(exportFilePath)); LineAggregator<StudentDTO> lineAggregator = createStudentLineAggregator(); csvFileWriter.setLineAggregator(lineAggregator); return csvFileWriter; }
@Bean ItemWriter<StudentDTO> databaseCsvItemWriter(Environment environment) { FlatFileItemWriter<StudentDTO> csvFileWriter = new FlatFileItemWriter<>(); String exportFileHeader = environment.getRequiredProperty(PROPERTY_CSV_EXPORT_FILE_HEADER); StringHeaderWriter headerWriter = new StringHeaderWriter(exportFileHeader); csvFileWriter.setHeaderCallback(headerWriter); String exportFilePath = environment.getRequiredProperty(PROPERTY_CSV_EXPORT_FILE_PATH); csvFileWriter.setResource(new FileSystemResource(exportFilePath)); LineAggregator<StudentDTO> lineAggregator = createStudentLineAggregator(); csvFileWriter.setLineAggregator(lineAggregator); return csvFileWriter; }
itemWriter.setResource(new FileSystemResource(targetResource)); itemWriter.setLineAggregator(lineAggregator(null)); itemWriter.setEncoding("UTF-8");
writer.setResource(this.resource); writer.setSaveState(this.saveState); writer.setShouldDeleteIfEmpty(this.shouldDeleteIfEmpty);