CounterStorage { static long deleteObsoleteCounterFiles(String application) { final Calendar nowMinusOneYearAndADay = Calendar.getInstance(); nowMinusOneYearAndADay.add(Calendar.DAY_OF_YEAR, -getObsoleteStatsDays()); nowMinusOneYearAndADay.add(Calendar.DAY_OF_YEAR, -1); long diskUsage = 0; for (final File file : listSerGzFiles(application)) { boolean deleted = false; if (file.lastModified() < nowMinusOneYearAndADay.getTimeInMillis()) { deleted = file.delete(); } if (!deleted) { diskUsage += file.length(); } } return diskUsage; } CounterStorage(Counter counter); static void disableStorage();  }
@Test public void testDeleteObsoleteCounterFiles() throws IOException { final Counter counter = new Counter("http", null); counter.setApplication("test counter"); final File storageDir = Parameters.getStorageDirectory(counter.getApplication()); final File obsoleteFile = new File(storageDir, "obsolete.ser.gz"); final File notObsoleteFile = new File(storageDir, "notobsolete.ser.gz"); checkSetup(storageDir, obsoleteFile, notObsoleteFile); final Calendar nowMinus1YearAnd2Days = Calendar.getInstance(); nowMinus1YearAnd2Days.add(Calendar.YEAR, -1); nowMinus1YearAnd2Days.add(Calendar.DAY_OF_YEAR, -2); if (!obsoleteFile.setLastModified(nowMinus1YearAnd2Days.getTimeInMillis())) { fail("setLastModified"); } CounterStorage.deleteObsoleteCounterFiles(counter.getApplication()); if (obsoleteFile.exists()) { fail("obsolete file still exists"); } if (!notObsoleteFile.delete()) { notObsoleteFile.deleteOnExit(); } Utils.setProperty(Parameter.OBSOLETE_STATS_DAYS, "1"); CounterStorage.deleteObsoleteCounterFiles(counter.getApplication()); }