@Test public void testJsonWritingWithMultipleWrite() throws Exception { // when this.writer.open(this.executionContext); this.writer.write(this.items); this.writer.write(Arrays.asList(trade3, trade4)); this.writer.close(); // then assertFileEquals( new File(EXPECTED_FILE_DIRECTORY + "expected-trades-with-multiple-writes.json"), this.resource.getFile()); }
@Test public void testJsonWriting() throws Exception { // when this.writer.open(this.executionContext); this.writer.write(this.items); this.writer.close(); // then assertFileEquals( new File(EXPECTED_FILE_DIRECTORY + "expected-trades.json"), this.resource.getFile()); }
@Test public void testWriteWithDelete() throws Exception { // given this.writer.setShouldDeleteIfExists(true); // when this.writer.open(this.executionContext); this.writer.write(Collections.singletonList(this.trade1)); this.writer.close(); this.writer.open(this.executionContext); this.writer.write(Collections.singletonList(this.trade2)); this.writer.close(); // then assertFileEquals( new File(EXPECTED_FILE_DIRECTORY + "expected-trades2.json"), this.resource.getFile()); }
@Test public void testForcedWrite() throws Exception { // given this.writer.setForceSync(true); // when this.writer.open(this.executionContext); this.writer.write(Collections.singletonList(this.trade1)); this.writer.close(); // then assertFileEquals( new File(EXPECTED_FILE_DIRECTORY + "expected-trades1.json"), this.resource.getFile()); }
@Test public void testJsonWritingWithEnclosingObject() throws Exception { // given this.writer.setHeaderCallback(writer -> writer.write("{\"trades\":[")); this.writer.setFooterCallback(writer -> writer.write(JsonFileItemWriter.DEFAULT_LINE_SEPARATOR + "]}")); // when this.writer.open(this.executionContext); this.writer.write(this.items); this.writer.close(); // then assertFileEquals( new File(EXPECTED_FILE_DIRECTORY + "expected-trades-with-wrapper-object.json"), this.resource.getFile()); }
@Test public void testItemMarshallingFailure() throws Exception { this.writer.setJsonObjectMarshaller(item -> { throw new IllegalArgumentException("Bad item"); }); this.writer.open(this.executionContext); try { this.writer.write(Collections.singletonList(this.trade1)); fail(); } catch (IllegalArgumentException iae) { assertEquals("Bad item", iae.getMessage()); } finally { this.writer.close(); } assertFileEquals( new File(EXPECTED_FILE_DIRECTORY + "empty-trades.json"), this.resource.getFile()); }
@Test public void itemsShouldBeMarshalledToJsonWithTheJsonObjectMarshaller() throws Exception { // given JsonFileItemWriter<String> writer = new JsonFileItemWriter<>(this.resource, this.jsonObjectMarshaller); // when writer.open(new ExecutionContext()); writer.write(Arrays.asList("foo", "bar")); writer.close(); // then Mockito.verify(this.jsonObjectMarshaller).marshal("foo"); Mockito.verify(this.jsonObjectMarshaller).marshal("bar"); } }
@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"); File outputFile = toBeCreated.getFile(); //enable easy content reading and auto-delete the file assertFalse("output file does not exist yet", toBeCreated.exists()); this.writer.setResource(toBeCreated); this.writer.setAppendAllowed(true); this.writer.afterPropertiesSet(); this.writer.open(this.executionContext); assertTrue("output file was created", toBeCreated.exists()); this.writer.write(Collections.singletonList(this.trade1)); this.writer.close(); assertFileEquals( new File(EXPECTED_FILE_DIRECTORY + "expected-trades1.json"), outputFile); outputFile.delete(); }
@Test public void testRestart() throws Exception { this.writer.open(this.executionContext); // write some lines this.writer.write(Collections.singletonList(this.trade1)); // get restart data this.writer.update(this.executionContext); // close template this.writer.close(); // init with correct data this.writer.open(this.executionContext); // write more lines this.writer.write(Collections.singletonList(this.trade2)); // get statistics this.writer.update(this.executionContext); // close template this.writer.close(); // verify what was written to the file assertFileEquals( new File(EXPECTED_FILE_DIRECTORY+ "expected-trades.json"), this.resource.getFile()); // 2 lines were written to the file in total assertEquals(2, this.executionContext.getLong("tradesItemWriter.written")); }
@Test public void testJsonWritingWithPrettyPrinting() throws Exception { // given this.writer = new JsonFileItemWriterBuilder<Trade>() .name("tradesItemWriter") .resource(this.resource) .jsonObjectMarshaller(getJsonObjectMarshallerWithPrettyPrint()) .build(); // when this.writer.open(this.executionContext); this.writer.write(this.items); this.writer.close(); // when assertFileEquals( new File(EXPECTED_FILE_DIRECTORY + getExpectedPrettyPrintedFile()), this.resource.getFile()); }