Repository: bigtop Updated Branches: refs/heads/master 833180eaf -> c16ff3a6c
BIGTOP-2443: inject unzip method to ZipInputStream, accepting regex for include Signed-off-by: Konstantin Boudnik <[email protected]> Project: http://git-wip-us.apache.org/repos/asf/bigtop/repo Commit: http://git-wip-us.apache.org/repos/asf/bigtop/commit/c16ff3a6 Tree: http://git-wip-us.apache.org/repos/asf/bigtop/tree/c16ff3a6 Diff: http://git-wip-us.apache.org/repos/asf/bigtop/diff/c16ff3a6 Branch: refs/heads/master Commit: c16ff3a6c3277e054ffeb09583463bf91b30b6c9 Parents: 833180e Author: Kengo Seki <[email protected]> Authored: Wed Aug 10 06:30:10 2016 +0000 Committer: Konstantin Boudnik <[email protected]> Committed: Tue Sep 13 12:05:51 2016 -0700 ---------------------------------------------------------------------- .../org/apache/bigtop/itest/JarContent.groovy | 24 +++-- .../apache/bigtop/itest/JarContentTest.groovy | 92 ++++++++++++++++++++ 2 files changed, 110 insertions(+), 6 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/bigtop/blob/c16ff3a6/bigtop-test-framework/src/main/groovy/org/apache/bigtop/itest/JarContent.groovy ---------------------------------------------------------------------- diff --git a/bigtop-test-framework/src/main/groovy/org/apache/bigtop/itest/JarContent.groovy b/bigtop-test-framework/src/main/groovy/org/apache/bigtop/itest/JarContent.groovy index 59edb40..a348513 100644 --- a/bigtop-test-framework/src/main/groovy/org/apache/bigtop/itest/JarContent.groovy +++ b/bigtop-test-framework/src/main/groovy/org/apache/bigtop/itest/JarContent.groovy @@ -20,6 +20,7 @@ package org.apache.bigtop.itest import java.util.jar.JarEntry import java.util.jar.JarFile +import java.util.regex.Pattern import java.util.zip.ZipInputStream import java.util.zip.ZipException import org.apache.commons.logging.LogFactory @@ -198,13 +199,10 @@ public abstract class JarContent { * Adding an ability to unpack a content of an given ZipInputStream * to specified destination with given pattern * @param dest directory where the content will be unpacked - * @param includes regexps to include resources to be unpacked + * @param includes string(s) or regexp(s) to include resources to be unpacked */ - ZipInputStream.metaClass.unzip = { String dest, String includes -> - //in metaclass added methods, 'delegate' is the object on which - //the method is called. Here it's the file to unzip + def unzip = { InputStream result, String dest, includes, predicate -> if (includes == null) includes = ""; - def result = delegate def destFile = new File(dest) if (!destFile.exists()) { destFile.mkdir(); @@ -212,7 +210,7 @@ public abstract class JarContent { result.withStream { def entry while (entry = result.nextEntry) { - if (!entry.name.contains(includes)) { + if (!predicate(entry.name, includes)) { continue }; if (!entry.isDirectory()) { @@ -232,6 +230,20 @@ public abstract class JarContent { } } } + + ZipInputStream.metaClass.unzip = { String dest, String includes -> + //in metaclass added methods, 'delegate' is the object on which + //the method is called. Here it's the file to unzip + unzip(delegate, dest, includes) { dst, inc -> dst.contains(inc) } } + + ZipInputStream.metaClass.unzip = { String dest, List<String> includes -> + unzip(delegate, dest, includes) { dst, inc -> inc.any { dst.contains(it) } } } + + ZipInputStream.metaClass.unzip = { String dest, Pattern includes -> + unzip(delegate, dest, includes) { dst, inc -> (dst =~ inc) as boolean } } + + ZipInputStream.metaClass.unzip = { String dest, List<Pattern> includes -> + unzip(delegate, dest, includes) { dst, inc -> inc.any { (dst =~ it) as boolean } } } } } http://git-wip-us.apache.org/repos/asf/bigtop/blob/c16ff3a6/bigtop-test-framework/src/test/groovy/org/apache/bigtop/itest/JarContentTest.groovy ---------------------------------------------------------------------- diff --git a/bigtop-test-framework/src/test/groovy/org/apache/bigtop/itest/JarContentTest.groovy b/bigtop-test-framework/src/test/groovy/org/apache/bigtop/itest/JarContentTest.groovy index 9543b46..3c31230 100644 --- a/bigtop-test-framework/src/test/groovy/org/apache/bigtop/itest/JarContentTest.groovy +++ b/bigtop-test-framework/src/test/groovy/org/apache/bigtop/itest/JarContentTest.groovy @@ -18,6 +18,8 @@ package org.apache.bigtop.itest +import groovy.io.FileType +import java.util.zip.ZipInputStream import org.junit.Test import static org.junit.Assert.assertTrue import static org.junit.Assert.assertEquals @@ -60,6 +62,96 @@ class JarContentTest { } @Test + void testUnzipSingleString() { + def destination = System.properties['buildDir'] ?: 'target/local.unpack.dir' + URL url = JarContent.getJarURL(String.class) + ZipInputStream zis = new ZipInputStream(url.openConnection().getInputStream()) + zis.unzip(destination, 'visitor') + File dir = new File(destination) + int count = 0 + boolean result = true + dir.eachFileRecurse(FileType.FILES) { + if (it.name.endsWith(".class")) + count++ + if (!it.path.contains("visitor")) + result = false + } + assertTrue('Expect more than one file', count > 1); + assertTrue('Expect that all paths contain the specified string', result); + dir.deleteDir() + } + + @Test + void testUnzipMultipleStrings() { + def destination = System.properties['buildDir'] ?: 'target/local.unpack.dir' + URL url = JarContent.getJarURL(String.class) + ZipInputStream zis = new ZipInputStream(url.openConnection().getInputStream()) + zis.unzip(destination, ['visitor', 'tree']) + File dir = new File(destination) + int count = 0 + boolean result = true + dir.eachFileRecurse(FileType.FILES) { + if (it.name.endsWith(".class")) + count++ + if (!it.path.contains("visitor") && !it.path.contains("tree")) + result = false + } + assertTrue('Expect more than one file', count > 1); + assertTrue('Expect that all paths contain at least one of the specified string', result); + dir.deleteDir() + } + + @Test + void testUnzipSingleRegex() { + def destination = System.properties['buildDir'] ?: 'target/local.unpack.dir' + URL url = JarContent.getJarURL(String.class) + ZipInputStream zis = new ZipInputStream(url.openConnection().getInputStream()) + // This will unzip sun/security/x509/GeneralSubtree.class and sun/security/x509/GeneralSubtrees.class + // but not sun/reflect/generics/tree/* + zis.unzip(destination, ~/[^\/]tree[^\/]/) + File dir = new File(destination) + int count = 0 + boolean posChkResult = true + boolean negChkResult = true + dir.eachFileRecurse(FileType.FILES) { + if (it.name.endsWith(".class")) + count++ + if (!it.path.contains("tree")) + posChkResult = false + if (it.path.contains("/tree/")) + negChkResult = false + } + assertTrue('Expect more than one file', count > 1); + assertTrue('Expect that all paths contain the string "tree"', posChkResult); + assertTrue('Expect that all paths do not contain the string "/tree/"', negChkResult); + dir.deleteDir() + } + + @Test + void testUnzipMultipleRegexes() { + def destination = System.properties['buildDir'] ?: 'target/local.unpack.dir' + URL url = JarContent.getJarURL(String.class) + ZipInputStream zis = new ZipInputStream(url.openConnection().getInputStream()) + zis.unzip(destination, [~/[^\/]visitor[^\/]/, ~/[^\/]tree[^\/]/]) + File dir = new File(destination) + int count = 0 + boolean posChkResult = true + boolean negChkResult = true + dir.eachFileRecurse(FileType.FILES) { + if (it.name.endsWith(".class")) + count++ + if (!it.path.contains("visitor") && !it.path.contains("tree")) + posChkResult = false + if (it.path.contains("/visitor/") || it.path.contains("/tree/")) + negChkResult = false + } + assertTrue('Expect more than one file', count > 1); + assertTrue('Expect that all paths contain either "visitor" or "tree"', posChkResult); + assertTrue('Expect that all paths do not contain both "/visitor/" and "/tree/"', negChkResult); + dir.deleteDir() + } + + @Test void testGetJarName() { assertEquals("Should've find tools.jar file", 'tools.jar',
