Search in sources :

Example 1 with JarFilterOptions

use of com.google.devtools.build.android.ideinfo.JarFilter.JarFilterOptions in project bazel by bazelbuild.

the class JarFilterTest method fullIntegrationTest.

@Test
public void fullIntegrationTest() throws Exception {
    File fooJava = folder.newFile("Foo.java");
    Files.write("package com.google.foo; class Foo { class Inner {} }".getBytes(UTF_8), fooJava);
    File barJava = folder.newFile("Bar.java");
    Files.write("package com.google.foo.bar; class Bar {}".getBytes(UTF_8), barJava);
    File srcJar = folder.newFile("gen.srcjar");
    try (ZipOutputStream zo = new ZipOutputStream(new FileOutputStream(srcJar))) {
        zo.putNextEntry(new ZipEntry("com/google/foo/gen/Gen.java"));
        zo.write("package gen; class Gen {}".getBytes(UTF_8));
        zo.closeEntry();
        zo.putNextEntry(new ZipEntry("com/google/foo/gen/Gen2.java"));
        zo.write("package gen; class Gen2 {}".getBytes(UTF_8));
        zo.closeEntry();
    }
    File src3Jar = folder.newFile("gen3.srcjar");
    try (ZipOutputStream zo = new ZipOutputStream(new FileOutputStream(src3Jar))) {
        zo.putNextEntry(new ZipEntry("com/google/foo/gen/Gen3.java"));
        zo.write("package gen; class Gen3 {}".getBytes(UTF_8));
        zo.closeEntry();
    }
    File filterJar = folder.newFile("foo.jar");
    try (ZipOutputStream zo = new ZipOutputStream(new FileOutputStream(filterJar))) {
        zo.putNextEntry(new ZipEntry("com/google/foo/Foo.class"));
        zo.closeEntry();
        zo.putNextEntry(new ZipEntry("com/google/foo/Foo$Inner.class"));
        zo.closeEntry();
        zo.putNextEntry(new ZipEntry("com/google/foo/bar/Bar.class"));
        zo.closeEntry();
        zo.putNextEntry(new ZipEntry("gen/Gen.class"));
        zo.closeEntry();
        zo.putNextEntry(new ZipEntry("gen/Gen2.class"));
        zo.closeEntry();
        zo.putNextEntry(new ZipEntry("gen/Gen3.class"));
        zo.closeEntry();
        zo.putNextEntry(new ZipEntry("com/google/foo/Foo2.class"));
        zo.closeEntry();
    }
    File filterSrcJar = folder.newFile("foo-src.jar");
    try (ZipOutputStream zo = new ZipOutputStream(new FileOutputStream(filterSrcJar))) {
        zo.putNextEntry(new ZipEntry("com/google/foo/Foo.java"));
        zo.closeEntry();
        zo.putNextEntry(new ZipEntry("com/google/foo/bar/Bar.java"));
        zo.closeEntry();
        zo.putNextEntry(new ZipEntry("gen/Gen.java"));
        zo.closeEntry();
        zo.putNextEntry(new ZipEntry("gen/Gen2.java"));
        zo.closeEntry();
        zo.putNextEntry(new ZipEntry("gen/Gen3.java"));
        zo.closeEntry();
        zo.putNextEntry(new ZipEntry("com/google/foo/Foo2.java"));
        zo.closeEntry();
        zo.putNextEntry(new ZipEntry("com/google/foo/bar/Bar2.java"));
        zo.closeEntry();
    }
    File filteredJar = folder.newFile("foo-filtered-gen.jar");
    File filteredSourceJar = folder.newFile("foo-filtered-gen-src.jar");
    String[] args = new String[] { "--keep_java_files", fooJava.getPath() + File.pathSeparator + barJava.getPath(), "--keep_source_jars", Joiner.on(File.pathSeparator).join(srcJar.getPath(), src3Jar.getPath()), "--filter_jars", filterJar.getPath(), "--filter_source_jars", filterSrcJar.getPath(), "--filtered_jar", filteredJar.getPath(), "--filtered_source_jar", filteredSourceJar.getPath() };
    JarFilterOptions options = JarFilter.parseArgs(args);
    JarFilter.main(options);
    List<String> filteredJarNames = Lists.newArrayList();
    try (ZipFile zipFile = new ZipFile(filteredJar)) {
        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry zipEntry = entries.nextElement();
            filteredJarNames.add(zipEntry.getName());
        }
    }
    List<String> filteredSourceJarNames = Lists.newArrayList();
    try (ZipFile zipFile = new ZipFile(filteredSourceJar)) {
        Enumeration<? extends ZipEntry> entries = zipFile.entries();
        while (entries.hasMoreElements()) {
            ZipEntry zipEntry = entries.nextElement();
            filteredSourceJarNames.add(zipEntry.getName());
        }
    }
    assertThat(filteredJarNames).containsExactly("com/google/foo/Foo.class", "com/google/foo/Foo$Inner.class", "com/google/foo/bar/Bar.class", "gen/Gen.class", "gen/Gen2.class", "gen/Gen3.class");
    assertThat(filteredSourceJarNames).containsExactly("com/google/foo/Foo.java", "com/google/foo/bar/Bar.java", "gen/Gen.java", "gen/Gen2.java", "gen/Gen3.java");
}
Also used : JarFilterOptions(com.google.devtools.build.android.ideinfo.JarFilter.JarFilterOptions) ZipFile(java.util.zip.ZipFile) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) File(java.io.File) ZipFile(java.util.zip.ZipFile) Test(org.junit.Test)

Aggregations

JarFilterOptions (com.google.devtools.build.android.ideinfo.JarFilter.JarFilterOptions)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 ZipEntry (java.util.zip.ZipEntry)1 ZipFile (java.util.zip.ZipFile)1 ZipOutputStream (java.util.zip.ZipOutputStream)1 Test (org.junit.Test)1