@Test public void testAfterPropertiesSetChecksMandatory() throws Exception { writer = new FlatFileItemWriter<>(); try { writer.afterPropertiesSet(); fail("Expected IllegalArgumentException"); } catch (IllegalArgumentException e) { // expected } }
@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")); }
@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> 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; }
@Override public void afterPropertiesSet() throws Exception { if (simpleLineAggregator == null){ this.simpleLineAggregator = new SimpleLineAggregator(); setLineAggregator(this.simpleLineAggregator); } super.afterPropertiesSet(); // we don't want transactional setTransactional(false); }
@Override public void afterPropertiesSet() throws Exception { // important to override this method or the writer will complain setLineAggregator(new MitabLineAggregator(mitabVersion, false)); super.afterPropertiesSet(); setTransactional(false); }