@Test public void testNonExistantResource() throws Exception { Resource doesntExist = mock(Resource.class); when(doesntExist.getFile()).thenReturn(File.createTempFile("arbitrary", null)); when(doesntExist.exists()).thenReturn(false); writer.setResource(doesntExist); try { writer.open(executionContext); fail(); } catch (IllegalStateException e) { assertEquals("Output resource must exist", e.getMessage()); } }
/** * Resource is deleted when no items have been written and shouldDeleteIfEmpty flag is set. */ @Test public void testDeleteIfEmptyNoRecordsWritten() throws Exception { writer.setShouldDeleteIfEmpty(true); writer.open(executionContext); writer.close(); assertFalse("file should be deleted" + resource, resource.getFile().exists()); }
@Before public void setUp() throws Exception { File directory = new File("target/data"); directory.mkdirs(); outputFile = File.createTempFile(ClassUtils.getShortName(this.getClass()), ".xml", directory); resource = new FileSystemResource(outputFile); writer.setResource(resource); writer.setMarshaller(getMarshaller()); writer.afterPropertiesSet(); writer.open(new ExecutionContext()); }
@Before public void setUp() throws Exception { File directory = new File("target/data"); directory.mkdirs(); outputFile = File.createTempFile(ClassUtils.getShortName(this.getClass()), ".xml", directory); resource = new FileSystemResource(outputFile); writer.setResource(resource); writer.setMarshaller(getMarshaller()); writer.setRootTagName("{urn:org.springframework.batch.io.oxm.domain}trades"); writer.afterPropertiesSet(); writer.open(new ExecutionContext()); }
@Override public Void doInTransaction(TransactionStatus status) { writer.open(executionContext); try { writer.write(items); } catch (Exception e) { throw new UnexpectedInputException("Could not write data", e); } // get restart data writer.update(executionContext); return null; } });
/** * Item is written to the output file only after flush. */ @Test public void testWriteAndFlush() throws Exception { writer.open(executionContext); writer.write(items); writer.close(); String content = getOutputFileContent(); assertTrue("Wrong content: " + content, content.contains(TEST_STRING)); }
@Test public void testWriteAndForceFlush() throws Exception { writer.setForceSync(true); writer.open(executionContext); writer.write(items); writer.close(); String content = getOutputFileContent(); assertTrue("Wrong content: " + content, content.contains(TEST_STRING)); }
/** * Resource is not deleted when items have been written and shouldDeleteIfEmpty flag is set. */ @Test public void testDeleteIfEmptyRecordsWritten() throws Exception { writer.setShouldDeleteIfEmpty(true); writer.open(executionContext); writer.write(items); writer.close(); String content = getOutputFileContent(); assertTrue("Wrong content: " + content, content.contains(TEST_STRING)); }
/** * Resource is not deleted when items have been written and shouldDeleteIfEmpty flag is set. */ @Test public void testDeleteIfEmptyRecordsWrittenRestart() throws Exception { writer.setShouldDeleteIfEmpty(true); writer.open(executionContext); writer.write(items); writer.update(executionContext); writer.close(); writer = createItemWriter(); writer.setShouldDeleteIfEmpty(true); writer.open(executionContext); writer.close(); String content = getOutputFileContent(); assertTrue("Wrong content: " + content, content.contains(TEST_STRING)); }
/** * Test that the writer can restart if the previous execution deleted empty file. */ @Test public void testDeleteIfEmptyRestartAfterDelete() throws Exception { writer.setShouldDeleteIfEmpty(true); writer.open(executionContext); writer.update(executionContext); writer.close(); assertFalse(resource.getFile().exists()); writer = createItemWriter(); writer.setShouldDeleteIfEmpty(true); writer.open(executionContext); writer.write(items); writer.update(executionContext); writer.close(); String content = getOutputFileContent(); assertTrue("Wrong content: " + content, content.contains(TEST_STRING)); }
/** * Item is written to the output file with namespace. */ @Test public void testWriteRootTagWithNamespace() throws Exception { writer.setRootTagName("{http://www.springframework.org/test}root"); writer.afterPropertiesSet(); writer.open(executionContext); writer.write(items); writer.close(); String content = getOutputFileContent(); assertTrue("Wrong content: " + content, content .contains(("<root xmlns=\"http://www.springframework.org/test\">"))); assertTrue("Wrong content: " + content, content.contains(TEST_STRING)); assertTrue("Wrong content: " + content, content.contains(("</root>"))); }
/** * Test opening and closing corresponding tags in complex header- and footer callback (restart). */ @Test public void testOpenAndCloseTagsInComplexCallbacksRestart() throws Exception { initWriterForComplexCallbackTests(); writer.open(executionContext); writer.write(items); writer.update(executionContext); initWriterForComplexCallbackTests(); writer.open(executionContext); writer.write(items); writer.close(); String content = getOutputFileContent(); assertEquals("Wrong content: " + content, "<ns:testroot xmlns:ns=\"http://www.springframework.org/test\">" + "<preHeader>PRE-HEADER</preHeader><ns:group><subGroup><postHeader>POST-HEADER</postHeader>" + "<StaxEventItemWriter-testString/><StaxEventItemWriter-testString/>" + "<preFooter>PRE-FOOTER</preFooter></subGroup></ns:group><postFooter>POST-FOOTER</postFooter>" + "</ns:testroot>", content); }
/** * Test opening and closing corresponding tags in header- and footer callback (restart). */ @Test public void testOpenAndCloseTagsInCallbacksRestart() throws Exception { initWriterForSimpleCallbackTests(); writer.open(executionContext); writer.write(items); writer.update(executionContext); initWriterForSimpleCallbackTests(); writer.open(executionContext); writer.write(items); writer.close(); String content = getOutputFileContent(); assertEquals("Wrong content: " + content, "<ns:testroot xmlns:ns=\"http://www.springframework.org/test\">" + "<ns:group><StaxEventItemWriter-testString/><StaxEventItemWriter-testString/></ns:group></ns:testroot>", content); }
/** * Test opening and closing corresponding tags in header- and footer callback. */ @Test public void testOpenAndCloseTagsInCallbacks() throws Exception { initWriterForSimpleCallbackTests(); writer.open(executionContext); writer.write(items); writer.close(); String content = getOutputFileContent(); assertEquals("Wrong content: " + content, "<ns:testroot xmlns:ns=\"http://www.springframework.org/test\"><ns:group><StaxEventItemWriter-testString/></ns:group></ns:testroot>", content); }
@Test(expected = ItemStreamException.class) public void testOverwriteOutput() throws Exception { StaxEventItemWriter<Foo> staxEventItemWriter = new StaxEventItemWriterBuilder<Foo>() .name("fooWriter") .marshaller(marshaller) .resource(this.resource) .overwriteOutput(false) .build(); staxEventItemWriter.afterPropertiesSet(); ExecutionContext executionContext = new ExecutionContext(); staxEventItemWriter.open(executionContext); staxEventItemWriter.write(this.items); staxEventItemWriter.update(executionContext); staxEventItemWriter.close(); File output = this.resource.getFile(); assertTrue(output.exists()); executionContext = new ExecutionContext(); staxEventItemWriter.open(executionContext); }
/** * Test setting writer name. */ @Test public void testSetName() throws Exception { writer.setName("test"); writer.open(executionContext); writer.write(items); writer.update(executionContext); writer.close(); assertTrue("execution context keys should be prefixed with writer name", executionContext.containsKey("test.position")); }
@Test public void testDeleteIfEmpty() throws Exception { ExecutionContext executionContext = new ExecutionContext(); StaxEventItemWriter<Foo> staxEventItemWriter = new StaxEventItemWriterBuilder<Foo>() .name("fooWriter") .resource(this.resource) .marshaller(this.marshaller) .shouldDeleteIfEmpty(true) .build(); staxEventItemWriter.afterPropertiesSet(); staxEventItemWriter.open(executionContext); staxEventItemWriter.write(Collections.emptyList()); staxEventItemWriter.update(executionContext); staxEventItemWriter.close(); File file = this.resource.getFile(); assertFalse(file.exists()); }
/** * Test with OXM Marshaller that closes the XMLEventWriter. */ // BATCH-2054 @Test public void testMarshallingClosingEventWriter() throws Exception { writer.setMarshaller(new SimpleMarshaller() { @Override public void marshal(Object graph, Result result) throws XmlMappingException, IOException { super.marshal(graph, result); try { StaxTestUtils.getXmlEventWriter(result).close(); } catch (Exception e) { throw new RuntimeException("Exception while writing to output file", e); } } }); writer.afterPropertiesSet(); writer.open(executionContext); writer.write(items); writer.write(items); }
/** * Count of 'records written so far' is returned as statistics. */ @Test public void testStreamContext() throws Exception { writer.open(executionContext); final int NUMBER_OF_RECORDS = 10; assertFalse(executionContext.containsKey(ClassUtils.getShortName(StaxEventItemWriter.class) + ".record.count")); for (int i = 1; i <= NUMBER_OF_RECORDS; i++) { writer.write(items); writer.update(executionContext); long writeStatistics = executionContext.getLong(ClassUtils.getShortName(StaxEventItemWriter.class) + ".record.count"); assertEquals(i, writeStatistics); } }
@Test public void testTransactional() { StaxEventItemWriter<Foo> staxEventItemWriter = new StaxEventItemWriterBuilder<Foo>() .name("fooWriter") .resource(this.resource) .marshaller(this.marshaller) .transactional(true) .forceSync(true) .build(); ExecutionContext executionContext = new ExecutionContext(); staxEventItemWriter.open(executionContext); Object writer = ReflectionTestUtils.getField(staxEventItemWriter, "bufferedWriter"); assertTrue(writer instanceof TransactionAwareBufferedWriter); assertTrue((Boolean) ReflectionTestUtils.getField(writer, "forceSync")); }