TimeLimitMaintainerTask implements MaintainerTask { @Override public void run() { File messagesFolder = logFilePath.getLogFilePath().toFile(); int noDeletedFiles = deleteFilesAboveFileLimit(messagesFolder); deletedFileProvider.setDeletedFilesCount(noDeletedFiles); logMaintenanceResult(noDeletedFiles); } TimeLimitMaintainerTask(); @Override void run(); @Override void logParameters();  }
@Test public final void testRun() { Whitebox.setInternalState(underTest, "timeLimitInSeconds", 10); Whitebox.setInternalState(underTest, "simpleDateFormat", new SimpleDateFormat("yyyyMMddHHmmss")); DeletedFileProvider deletedFileProvider = new DeletedFileProvider(); Whitebox.setInternalState(underTest, "deletedFileProvider", deletedFileProvider); File file1 = Mockito.mock(File.class); given(file1.getName()).willReturn("20130701151510.0000resp.txt"); File file2 = Mockito.mock(File.class); given(file2.getName()).willReturn("20130701151510.0000req.txt"); File file3 = Mockito.mock(File.class); given(file3.getName()).willReturn("20130701151513.0000req.txt"); File file4 = Mockito.mock(File.class); given(file4.getName()).willReturn("20130701151513.0000resp.txt"); File[] messageFiles = new File[4]; messageFiles[0] = file1; messageFiles[1] = file2; messageFiles[2] = file3; messageFiles[3] = file4; given(logFilePath.getLogFilePath()).willReturn(path); given(path.toFile()).willReturn(logFolder); given(logFolder.listFiles(fileFilter)).willReturn(messageFiles); Calendar cal = Calendar.getInstance(); cal.set(2013, 6, 1, 15, 15, 21); Date date = cal.getTime(); given(currentDateProvider.getCurrentDate()).willReturn(date); underTest.run(); verify(file1).delete(); verify(file2).delete(); verify(file3, never()).delete(); verify(file4, never()).delete(); }
@Test public final void testRunShouldSendMessageToIndexDeletion() { Whitebox.setInternalState(underTest, "timeLimitInSeconds", 10); Whitebox.setInternalState(underTest, "simpleDateFormat", new SimpleDateFormat("yyyyMMddHHmmss")); File file1 = Mockito.mock(File.class); given(file1.getAbsolutePath()).willReturn("20130701151510.0000resp.txt"); given(file1.getName()).willReturn("20130701151510.0000resp.txt"); File[] messageFiles = new File[1]; messageFiles[0] = file1; given(logFilePath.getLogFilePath()).willReturn(path); given(path.toFile()).willReturn(logFolder); given(logFolder.listFiles(fileFilter)).willReturn(messageFiles); given(file1.delete()).willReturn(true); Calendar cal = Calendar.getInstance(); cal.set(2013, 6, 1, 15, 15, 21); Date date = cal.getTime(); given(currentDateProvider.getCurrentDate()).willReturn(date); underTest.run(); verify(indexDeletionProcessor).process("20130701151510.0000resp.txt"); }
@Test public final void testRunShouldSaveDeleteFilesCountWhenItDeleteFiles() { Whitebox.setInternalState(underTest, "timeLimitInSeconds", 10); Whitebox.setInternalState(underTest, "simpleDateFormat", new SimpleDateFormat("yyyyMMddHHmmss")); DeletedFileProvider deletedFileProvider = new DeletedFileProvider(); Whitebox.setInternalState(underTest, "deletedFileProvider", deletedFileProvider); File file1 = Mockito.mock(File.class); given(file1.getName()).willReturn("20130701151510.0000resp.txt"); File file2 = Mockito.mock(File.class); given(file2.getName()).willReturn("20130701151510.0000req.txt"); File file3 = Mockito.mock(File.class); given(file3.getName()).willReturn("20130701151513.0000req.txt"); File file4 = Mockito.mock(File.class); given(file4.getName()).willReturn("20130701151513.0000resp.txt"); File[] messageFiles = new File[4]; messageFiles[0] = file1; messageFiles[1] = file2; messageFiles[2] = file3; messageFiles[3] = file4; given(logFilePath.getLogFilePath()).willReturn(path); given(path.toFile()).willReturn(logFolder); given(logFolder.listFiles(fileFilter)).willReturn(messageFiles); Calendar cal = Calendar.getInstance(); cal.set(2013, 6, 1, 15, 15, 21); Date date = cal.getTime(); given(currentDateProvider.getCurrentDate()).willReturn(date); underTest.run(); int actual = ((DeletedFileProvider) Whitebox.getInternalState(underTest, "deletedFileProvider")).getDeletedFilesCount(); Assert.assertEquals(actual, 2); }