@Test public void testModifiedFilter() throws InterruptedException { final long currentTimeMillis = System.currentTimeMillis(); final String validName = "valid"; when(fileFilter.accept(any(File.class))).thenReturn(true); when(file.listFiles(any(FileFilter.class))).thenAnswer(new Answer<File[]>() { @Override public File[] answer(InvocationOnMock invocationOnMock) throws Throwable { File tooOld = mock(File.class); when(tooOld.lastModified()).thenReturn(currentTimeMillis - 1); when(tooOld.isFile()).thenReturn(true); File valid = mock(File.class); when(valid.getName()).thenReturn(validName); when(valid.getParentFile()).thenReturn(file); when(valid.lastModified()).thenReturn(currentTimeMillis); when(valid.isFile()).thenReturn(true); File tooNew = mock(File.class); when(tooNew.lastModified()).thenReturn(System.currentTimeMillis() + TimeUnit.HOURS.toMillis(1)); when(tooNew.isFile()).thenReturn(true); FileFilter filter = (FileFilter) invocationOnMock.getArguments()[0]; List<File> result = new ArrayList<File>(); for (File file : new File[]{tooOld, valid, tooNew}) { if (filter.accept(file)) { result.add(file); } } return result.toArray(new File[result.size()]); } }); listFileCollector = new ListFileCollector(file, fileFilter, currentTimeMillis); Thread.sleep(1); Iterable<DataPacket> dataPackets = listFileCollector.getDataPackets(); Iterator<DataPacket> iterator = dataPackets.iterator(); assertTrue(iterator.hasNext()); assertEquals(validName, iterator.next().getAttributes().get("filename")); assertFalse(iterator.hasNext()); }
@Override public Iterable<DataPacket> getDataPackets() { long maxLastModified = System.currentTimeMillis() - 1; List<DataPacket> dataPackets = new ArrayList<>(); FileFilter fileFilter; if (filterModified) { ParcelableFileFilter modifiedCompoundFilter = new OrFileFilter(new DirectoryFileFilter(), new LastModifiedFileFilter(minModifiedTime, maxLastModified)); fileFilter = new AndFileFilter(modifiedCompoundFilter, this.fileFilter); } else { fileFilter = this.fileFilter; } listRecursive(baseDir, fileFilter, dataPackets); minModifiedTime = maxLastModified + 1; return dataPackets; }
ListFileCollector implements DataCollector { @Override public Iterable<DataPacket> getDataPackets() { long maxLastModified = System.currentTimeMillis() - 1; List<DataPacket> dataPackets = new ArrayList<>(); FileFilter fileFilter; if (filterModified) { ParcelableFileFilter modifiedCompoundFilter = new OrFileFilter(new DirectoryFileFilter(), new LastModifiedFileFilter(minModifiedTime, maxLastModified)); fileFilter = new AndFileFilter(modifiedCompoundFilter, this.fileFilter); } else { fileFilter = this.fileFilter; } listRecursive(baseDir, fileFilter, dataPackets); minModifiedTime = maxLastModified + 1; return dataPackets; } }
ListFileCollector implements DataCollector { @Override public Iterable<DataPacket> getDataPackets() { long maxLastModified = System.currentTimeMillis() - 1; List<DataPacket> dataPackets = new ArrayList<>(); FileFilter fileFilter; if (filterModified) { ParcelableFileFilter modifiedCompoundFilter = new OrFileFilter(new DirectoryFileFilter(), new LastModifiedFileFilter(minModifiedTime, maxLastModified)); fileFilter = new AndFileFilter(modifiedCompoundFilter, this.fileFilter); } else { fileFilter = this.fileFilter; } listRecursive(baseDir, fileFilter, dataPackets); minModifiedTime = maxLastModified + 1; return dataPackets; } ListFileCollector(File baseDir, ParcelableFileFilter fileFilter);  ListFileCollector(File baseDir, ParcelableFileFilter fileFilter, long minModifiedTime); protected  ListFileCollector(File baseDir, ParcelableFileFilter fileFilter, boolean filterModified, long minModifiedTime); }
ListFileCollector implements DataCollector { @Override public Iterable<DataPacket> getDataPackets() { long maxLastModified = System.currentTimeMillis() - 1; List<DataPacket> dataPackets = new ArrayList<>(); FileFilter fileFilter; if (filterModified) { ParcelableFileFilter modifiedCompoundFilter = new OrFileFilter(new DirectoryFileFilter(), new LastModifiedFileFilter(minModifiedTime, maxLastModified)); fileFilter = new AndFileFilter(modifiedCompoundFilter, this.fileFilter); } else { fileFilter = this.fileFilter; } listRecursive(baseDir, fileFilter, dataPackets); minModifiedTime = maxLastModified + 1; return dataPackets; } ListFileCollector(File baseDir, ParcelableFileFilter fileFilter);  ListFileCollector(File baseDir, ParcelableFileFilter fileFilter, long minModifiedTime); protected  ListFileCollector(File baseDir, ParcelableFileFilter fileFilter, boolean filterModified, long minModifiedTime); @Override Iterable<DataPacket> getDataPackets(); @Override int describeContents(); @Override void writeToParcel(Parcel dest, int flags); }
ListFileCollector implements DataCollector { @Override public Iterable<DataPacket> getDataPackets() { long maxLastModified = System.currentTimeMillis() - 1; List<DataPacket> dataPackets = new ArrayList<>(); FileFilter fileFilter; if (filterModified) { ParcelableFileFilter modifiedCompoundFilter = new OrFileFilter(new DirectoryFileFilter(), new LastModifiedFileFilter(minModifiedTime, maxLastModified)); fileFilter = new AndFileFilter(modifiedCompoundFilter, this.fileFilter); } else { fileFilter = this.fileFilter; } listRecursive(baseDir, fileFilter, dataPackets); minModifiedTime = maxLastModified + 1; return dataPackets; } ListFileCollector(File baseDir, ParcelableFileFilter fileFilter);  ListFileCollector(File baseDir, ParcelableFileFilter fileFilter, long minModifiedTime); protected  ListFileCollector(File baseDir, ParcelableFileFilter fileFilter, boolean filterModified, long minModifiedTime); @Override Iterable<DataPacket> getDataPackets(); @Override int describeContents(); @Override void writeToParcel(Parcel dest, int flags); static final Creator<ListFileCollector> CREATOR; }