FileFilter implements ZipEntryFilter { void handleChanged(@NonNull File outputDir, @NonNull File changedFile) throws IOException { String archivePath = getArchivePath(changedFile); PackagingFileAction action = getPackagingAction(archivePath); switch (action) { case EXCLUDE: return; case MERGE: mergeAll(outputDir, archivePath); return; case PICK_FIRST: copy(changedFile, outputDir, archivePath); return; case NONE: copy(changedFile, outputDir, archivePath); } } FileFilter(
            @NonNull List<SubStream> expandedFolders,
            @Nullable PackagingOptions packagingOptions); @Override boolean checkEntry(@NonNull String archivePath); void handleRemoved(@NonNull File outputDir, @NonNull String removedFilePath);  }
@Test public void testSimpleCopy() throws IOException { mFileFilter = new FileFilter( mPackagedJarExpansionSubStreams, mPackagingOptions); assertFalse(new File(mMergedFolder, "foo/text.properties").exists()); File changedFile = createFile(mExpandedJar1, "foo/text.properties"); mFileFilter.handleChanged(mMergedFolder, changedFile); assertTrue(new File(mMergedFolder, "foo/text.properties").exists()); }
@Test public void testSimpleExclusion() throws IOException { setExcludes(ImmutableSet.of(FileUtils.join("foo", "text.properties"))); mFileFilter = new FileFilter( mPackagedJarExpansionSubStreams, mPackagingOptions); mFileFilter.handleChanged(mMergedFolder, createFile(mExpandedJar1, "foo/text.properties")); assertMergedFilesCount(0); }
@Test public void testExclusionFromMultipleFiles() throws IOException { setExcludes(ImmutableSet.of(FileUtils.join("foo", "text.properties"))); mFileFilter = new FileFilter( mPackagedJarExpansionSubStreams, mPackagingOptions); mFileFilter.handleChanged(mMergedFolder, createFile(mExpandedJar1, "foo/text.properties")); mFileFilter.handleChanged(mMergedFolder, createFile(mExpandedJar2, "foo/text.properties")); assertMergedFilesCount(0); }
@Test public void testMultipleExclusions() throws IOException { setExcludes(ImmutableSet.of( FileUtils.join("foo", "text.properties"), FileUtils.join("bar", "other.properties"))); mFileFilter = new FileFilter( mPackagedJarExpansionSubStreams, mPackagingOptions); mFileFilter.handleChanged(mMergedFolder, createFile(mExpandedJar1, "foo/text.properties")); mFileFilter.handleChanged(mMergedFolder, createFile(mExpandedJar2, "bar/other.properties")); assertMergedFilesCount(0); }
@Test public void textNonExclusion() throws IOException { setExcludes(ImmutableSet.of( FileUtils.join("foo", "text.properties"), FileUtils.join("bar", "other.properties"))); mFileFilter = new FileFilter( mPackagedJarExpansionSubStreams, mPackagingOptions); mFileFilter.handleChanged(mMergedFolder, createFile(mExpandedJar1, "foo/text.properties")); mFileFilter.handleChanged(mMergedFolder, createFile(mExpandedJar2, "bar/other.properties")); mFileFilter.handleChanged(mMergedFolder, createFile(mExpandedJar2, "bar/foo.properties")); assertMergedFilesCount(1); assertTrue(new File(mMergedFolder, "bar/foo.properties").exists()); }
@Test public void testSingleMerge() throws IOException { setMerges(ImmutableSet.of( FileUtils.join("foo", "text.properties"))); mFileFilter = new FileFilter( mPackagedJarExpansionSubStreams, mPackagingOptions); createFile(mExpandedJar1, "foo/text.properties", "one"); File secondFile = createFile(mExpandedJar2, "foo/text.properties", "two"); mFileFilter.handleChanged(mMergedFolder, secondFile); File mergedFile = new File(mMergedFolder, "foo/text.properties"); assertTrue(mergedFile.exists()); assertContentInAnyOrder( Files.asCharSource(mergedFile, Charset.defaultCharset()).read() , ImmutableList.of("one", "two")); }
@Test public void testMultipleMerges() throws IOException { setMerges(ImmutableSet.of( FileUtils.join("foo" , "text.properties"))); mFileFilter = new FileFilter( mPackagedJarExpansionSubStreams, mPackagingOptions); createFile(mExpandedJar1, "foo/text.properties", "one"); File secondFile = createFile(mExpandedJar2, "foo/text.properties", "two"); createFile(mExpandedJar3, "foo/text.properties", "three"); mFileFilter.handleChanged(mMergedFolder, secondFile); File mergedFile = new File(mMergedFolder, "foo/text.properties"); assertTrue(mergedFile.exists()); assertContentInAnyOrder( Files.asCharSource(mergedFile, Charset.defaultCharset()).read() , ImmutableList.of("one", "two", "three")); }
@Test public void testMergeAddon() throws IOException { setMerges(ImmutableSet.of( FileUtils.join("foo", "text.properties"))); mFileFilter = new FileFilter( mPackagedJarExpansionSubStreams, mPackagingOptions); createFile(mExpandedJar1, "foo/text.properties", "one"); File secondFile = createFile(mExpandedJar2, "foo/text.properties", "two"); mFileFilter.handleChanged(mMergedFolder, secondFile); File mergedFile = new File(mMergedFolder, "foo/text.properties"); assertTrue(mergedFile.exists()); assertContentInAnyOrder( Files.asCharSource(mergedFile, Charset.defaultCharset()).read() , ImmutableList.of("one", "two")); File thirdFile = createFile(mExpandedJar3, "foo/text.properties", "three"); mFileFilter.handleChanged(mMergedFolder, thirdFile); assertTrue(mergedFile.exists()); assertContentInAnyOrder( Files.asCharSource(mergedFile, Charset.defaultCharset()).read() , ImmutableList.of("one", "two", "three")); }
@Test public void testMergeUpdate() throws IOException { setMerges(ImmutableSet.of( FileUtils.join("foo", "text.properties"))); mFileFilter = new FileFilter( mPackagedJarExpansionSubStreams, mPackagingOptions); createFile(mExpandedJar1, "foo/text.properties", "one"); File secondFile = createFile(mExpandedJar2, "foo/text.properties", "two"); createFile(mExpandedJar3, "foo/text.properties", "three"); mFileFilter.handleChanged(mMergedFolder, secondFile); File mergedFile = new File(mMergedFolder, "foo/text.properties"); assertTrue(mergedFile.exists()); assertContentInAnyOrder( Files.asCharSource(mergedFile, Charset.defaultCharset()).read() , ImmutableList.of("one", "two", "three")); assertTrue(secondFile.delete()); secondFile = createFile(mExpandedJar2, "foo/text.properties", "deux"); mFileFilter.handleChanged(mMergedFolder, secondFile); assertTrue(mergedFile.exists()); assertContentInAnyOrder( Files.asCharSource(mergedFile, Charset.defaultCharset()).read() , ImmutableList.of("one", "deux", "three")); }
@Test public void testPickFirst() throws IOException { setPickFirsts(ImmutableSet.of("foo/text.properties")); mFileFilter = new FileFilter( mPackagedJarExpansionSubStreams, mPackagingOptions); mFileFilter.handleChanged(mMergedFolder, createFile(mExpandedJar1, "foo/text.properties", "one")); mFileFilter.handleChanged(mMergedFolder, createFile(mExpandedJar2, "foo/text.properties", "two")); mFileFilter.handleChanged(mMergedFolder, createFile(mExpandedJar3, "foo/text.properties", "three")); File mergedFile = new File(mMergedFolder, "foo/text.properties"); assertTrue(mergedFile.exists()); String mergedContent = Files.asCharSource(mergedFile, Charset.defaultCharset()).read(); assertTrue(mergedContent.equals("one") || mergedContent.equals("two") || mergedContent.equals("three")); }
@Test public void testPickFirstUpdate() throws IOException { setPickFirsts(ImmutableSet.of("foo/text.properties")); mFileFilter = new FileFilter( mPackagedJarExpansionSubStreams, mPackagingOptions); File firstFile = createFile(mExpandedJar1, "foo/text.properties", "one"); File secondFile = createFile(mExpandedJar2, "foo/text.properties", "two"); File thirdFile = createFile(mExpandedJar3, "foo/text.properties", "three"); mFileFilter.handleChanged(mMergedFolder, firstFile); mFileFilter.handleChanged(mMergedFolder, secondFile); mFileFilter.handleChanged(mMergedFolder, thirdFile); File mergedFile = new File(mMergedFolder, "foo/text.properties"); assertTrue(mergedFile.exists()); String mergedContent = Files.asCharSource(mergedFile, Charset.defaultCharset()).read(); if (mergedContent.equals("one")) { assertTrue(firstFile.delete()); createFile(mExpandedJar1, "foo/text.properties", "un"); mFileFilter.handleChanged(mMergedFolder, firstFile); assertEquals("un", Files.asCharSource(mergedFile, Charset.defaultCharset()).read()); } if (mergedContent.equals("two")) { assertTrue(thirdFile.delete()); createFile(mExpandedJar2, "foo/text.properties", "deux"); mFileFilter.handleChanged(mMergedFolder, secondFile); assertEquals("deux", Files.asCharSource(mergedFile, Charset.defaultCharset()).read()); } if (mergedContent.equals("three")) { assertTrue(thirdFile.delete()); createFile(mExpandedJar3, "foo/text.properties", "trois"); mFileFilter.handleChanged(mMergedFolder, thirdFile); assertEquals("trois", Files.asCharSource(mergedFile, Charset.defaultCharset()).read()); } }