private void cleanup() { FileUtils.deleteQuietly(AGENT_PLUGINS_ZIP); FileUtils.deleteQuietly(AGENT_BINARY_JAR); FileUtils.deleteQuietly(AGENT_LAUNCHER_JAR); FileUtils.deleteQuietly(TFS_IMPL_JAR); new Lockfile(new File(AgentLauncherImpl.AGENT_BOOTSTRAPPER_LOCK_FILE)).delete(); }
@Test public void shouldReturnFalseIfLockFileAlreadyExists() throws IOException { File mockfile = mock(File.class); Lockfile lockfile = new Lockfile(mockfile); when(mockfile.exists()).thenReturn(true); when(mockfile.lastModified()).thenReturn(System.currentTimeMillis()); assertThat(lockfile.tryLock(), is(false)); verify(mockfile).exists(); }
@Test public void shouldNotAttemptToDeleteLockFileIfItDoesNotExist() { File mockfile = mock(File.class); Lockfile lockfile = new Lockfile(mockfile); when(mockfile.exists()).thenReturn(false); lockfile.delete(); verify(mockfile, never()).delete(); }
@Test public void shouldReturnFalseifUnableToSetLock() throws IOException { File mockfile = mock(File.class); Lockfile lockfile = spy(new Lockfile(mockfile)); when(mockfile.exists()).thenReturn(false); when(mockfile.getAbsolutePath()).thenReturn("/abcd/dummyFile"); doThrow(new IOException("dummy")).when(lockfile).setHooks(); assertThat(lockfile.tryLock(), is(false)); verify(mockfile).exists(); }
@Test public void shouldNotExistIfNotChangedRecently() { File mockfile = mock(File.class); Lockfile lockfile = new Lockfile(mockfile); Lockfile spy = spy(lockfile); doReturn(false).when(spy).lockFileChangedWithinMinutes(10); when(lockfile.exists()).thenReturn(true); assertThat(spy.exists(), is(false)); }
@Test public void shouldExistIfFileExistsAndChangedRecently() { File mockfile = mock(File.class); Lockfile lockfile = new Lockfile(mockfile); Lockfile spy = spy(lockfile); doReturn(true).when(spy).lockFileChangedWithinMinutes(10); when(lockfile.exists()).thenReturn(true); assertThat(spy.exists(), is(true)); }
@Test public void shouldReturnTrueIfCanSetLockAndDeleteLockFileWhenDeleteIsCalled() throws IOException { Lockfile lockfile = new Lockfile(LOCK_FILE); assertThat(lockfile.tryLock(), is(true)); lockfile.delete(); assertThat(LOCK_FILE.exists(), is(false)); }
@Test public void shouldNotDeleteLockFileIfTryLockHasFailed() throws IOException { FileUtils.touch(LOCK_FILE); Lockfile lockfile = new Lockfile(LOCK_FILE); assertThat(lockfile.tryLock(), is(false)); lockfile.delete(); assertThat(LOCK_FILE.exists(), is(true)); }