AppendCommandExecution extends AbstractCommandExecution { @Override public void execute() throws CommandException { OutputStreamWriter fw = null; try { File f = new File( this.instr.getFilePath()); boolean append = f.exists() && f.length() > 0; fw = new OutputStreamWriter( new FileOutputStream( f, true ), StandardCharsets.UTF_8 ); if( append ) fw.append( "\n" ); fw.append( this.instr.getContent()); } catch( IOException e ) { throw new CommandException( e ); } finally { Utils.closeQuietly( fw ); } } AppendCommandExecution( AppendCommandInstruction instr ); @Override void execute();  }
@Test public void testExecute_success() throws Exception { File tf = this.folder.newFile(); Assert.assertEquals( 0, tf.length()); AppendCommandExecution exec = buildExecutor( "append this into " + tf.getAbsolutePath()); exec.execute(); Assert.assertNotSame( 0, tf.length()); Assert.assertEquals( "this", Utils.readFileContent( tf )); exec.execute(); Assert.assertEquals( "this\nthis", Utils.readFileContent( tf )); exec = buildExecutor( "append that into " + tf.getAbsolutePath()); exec.execute(); Assert.assertEquals( "this\nthis\nthat", Utils.readFileContent( tf )); }
@Test( expected = CommandException.class ) public void testExecute_failure() throws Exception { File tf = this.folder.newFolder(); AppendCommandExecution exec = buildExecutor( "append this into " + tf.getAbsolutePath()); exec.execute(); }