OverwriteIfExistsWorker extends NioWorker { @Override public void put(byte[] data, File file) throws FsException { Args.notNull(file, "file"); if (file.exists() && !file.delete()) { throw new FsException("Could not delete [" + file + "]"); } super.put(data, file); }  @Override void put(byte[] data, File file);  }
@Override @Test public void testPutFileExists() throws Exception { FsWorker worker = createWorker(); String[] testFiles = createTestFiles(); worker.put(DATA.getBytes(), new File(baseDir, testFiles[0])); byte[] readBytes = worker.get(new File(baseDir, testFiles[0])); assertEquals(DATA, new String(readBytes)); }
@Test public void testPutFile_Does_Not_Exist() throws Exception { OverwriteIfExistsWorker worker = createWorker(); File failingFile = Mockito.mock(File.class); Mockito.when(failingFile.exists()).thenReturn(false); Mockito.when(failingFile.delete()).thenReturn(true); try { worker.put(DATA.getBytes(), failingFile); fail(); } catch (FsException expected) { } }
@Test public void testPutFile_Does_Not_Delete() throws Exception { OverwriteIfExistsWorker worker = createWorker(); File failingFile = Mockito.mock(File.class); Mockito.when(failingFile.exists()).thenReturn(true); Mockito.when(failingFile.delete()).thenReturn(false); try { worker.put(DATA.getBytes(), failingFile); fail(); } catch (FsException expected) { } }