diff options
Diffstat (limited to 'org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/ArchiveTest.java')
-rw-r--r-- | org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/ArchiveTest.java | 395 |
1 files changed, 175 insertions, 220 deletions
diff --git a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/ArchiveTest.java b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/ArchiveTest.java index 6ce092d3c1..1fc898d546 100644 --- a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/ArchiveTest.java +++ b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/ArchiveTest.java @@ -1,48 +1,17 @@ /* - * Copyright (C) 2012 Google Inc. - * and other copyright owners as documented in the project's IP log. + * Copyright (C) 2012 Google Inc. and others * - * This program and the accompanying materials are made available - * under the terms of the Eclipse Distribution License v1.0 which - * accompanies this distribution, is reproduced below, and is - * available at http://www.eclipse.org/org/documents/edl-v10.php + * This program and the accompanying materials are made available under the + * terms of the Eclipse Distribution License v. 1.0 which is available at + * https://www.eclipse.org/org/documents/edl-v10.php. * - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or - * without modification, are permitted provided that the following - * conditions are met: - * - * - Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * - Redistributions in binary form must reproduce the above - * copyright notice, this list of conditions and the following - * disclaimer in the documentation and/or other materials provided - * with the distribution. - * - * - Neither the name of the Eclipse Foundation, Inc. nor the - * names of its contributors may be used to endorse or promote - * products derived from this software without specific prior - * written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND - * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, - * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT - * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER - * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) - * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * SPDX-License-Identifier: BSD-3-Clause */ package org.eclipse.jgit.pgm; +import static java.nio.charset.StandardCharsets.UTF_8; import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; import static org.junit.Assume.assumeNoException; @@ -52,17 +21,14 @@ import java.io.ByteArrayInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; -import java.io.InputStreamReader; import java.io.IOException; +import java.io.InputStreamReader; import java.io.OutputStream; -import java.lang.Object; -import java.lang.String; import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import java.util.concurrent.Callable; -import java.util.concurrent.Executors; import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; import java.util.concurrent.Future; import java.util.zip.ZipEntry; import java.util.zip.ZipInputStream; @@ -71,9 +37,7 @@ import org.eclipse.jgit.api.Git; import org.eclipse.jgit.dircache.DirCache; import org.eclipse.jgit.lib.CLIRepositoryTestCase; import org.eclipse.jgit.lib.FileMode; -import org.eclipse.jgit.pgm.CLIGitCommand; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; public class ArchiveTest extends CLIRepositoryTestCase { @@ -89,25 +53,26 @@ public class ArchiveTest extends CLIRepositoryTestCase { emptyTree = db.resolve("HEAD^{tree}").abbreviate(12).name(); } - @Ignore("Some versions of java.util.zip refuse to write an empty ZIP") @Test public void testEmptyArchive() throws Exception { - final byte[] result = CLIGitCommand.rawExecute( // - "git archive --format=zip " + emptyTree, db); + byte[] result = CLIGitCommand.executeRaw( + "git archive --format=zip " + emptyTree, db).outBytes(); assertArrayEquals(new String[0], listZipEntries(result)); } @Test public void testEmptyTar() throws Exception { - final byte[] result = CLIGitCommand.rawExecute( // - "git archive --format=tar " + emptyTree, db); + byte[] result = CLIGitCommand.executeRaw( + "git archive --format=tar " + emptyTree, db).outBytes(); assertArrayEquals(new String[0], listTarEntries(result)); } @Test public void testUnrecognizedFormat() throws Exception { - final String[] expect = new String[] { "fatal: Unknown archive format 'nonsense'" }; - final String[] actual = execute("git archive --format=nonsense " + emptyTree); + String[] expect = new String[] { + "fatal: Unknown archive format 'nonsense'", "" }; + String[] actual = executeUnchecked( + "git archive --format=nonsense " + emptyTree); assertArrayEquals(expect, actual); } @@ -120,9 +85,9 @@ public class ArchiveTest extends CLIRepositoryTestCase { git.add().addFilepattern("c").call(); git.commit().setMessage("populate toplevel").call(); - final byte[] result = CLIGitCommand.rawExecute( // - "git archive --format=zip HEAD", db); - assertArrayEquals(new String[] { "a", "c" }, // + byte[] result = CLIGitCommand.executeRaw( + "git archive --format=zip HEAD", db).outBytes(); + assertArrayEquals(new String[] { "a", "c" }, listZipEntries(result)); } @@ -135,20 +100,16 @@ public class ArchiveTest extends CLIRepositoryTestCase { @Test public void testDefaultFormatIsTar() throws Exception { commitGreeting(); - final byte[] result = CLIGitCommand.rawExecute( // - "git archive HEAD", db); - assertArrayEquals(new String[] { "greeting" }, // + byte[] result = CLIGitCommand.executeRaw( + "git archive HEAD", db).outBytes(); + assertArrayEquals(new String[] { "greeting" }, listTarEntries(result)); } - private static String shellQuote(String s) { - return "'" + s.replace("'", "'\\''") + "'"; - } - @Test public void testFormatOverridesFilename() throws Exception { - final File archive = new File(db.getWorkTree(), "format-overrides-name.tar"); - final String path = archive.getAbsolutePath(); + File archive = new File(db.getWorkTree(), "format-overrides-name.tar"); + String path = archive.getAbsolutePath(); commitGreeting(); assertArrayEquals(new String[] { "" }, @@ -162,8 +123,8 @@ public class ArchiveTest extends CLIRepositoryTestCase { @Test public void testUnrecognizedExtensionMeansTar() throws Exception { - final File archive = new File(db.getWorkTree(), "example.txt"); - final String path = archive.getAbsolutePath(); + File archive = new File(db.getWorkTree(), "example.txt"); + String path = archive.getAbsolutePath(); commitGreeting(); assertArrayEquals(new String[] { "" }, @@ -176,8 +137,8 @@ public class ArchiveTest extends CLIRepositoryTestCase { @Test public void testNoExtensionMeansTar() throws Exception { - final File archive = new File(db.getWorkTree(), "example"); - final String path = archive.getAbsolutePath(); + File archive = new File(db.getWorkTree(), "example"); + String path = archive.getAbsolutePath(); commitGreeting(); assertArrayEquals(new String[] { "" }, @@ -189,8 +150,8 @@ public class ArchiveTest extends CLIRepositoryTestCase { @Test public void testExtensionMatchIsAnchored() throws Exception { - final File archive = new File(db.getWorkTree(), "two-extensions.zip.bak"); - final String path = archive.getAbsolutePath(); + File archive = new File(db.getWorkTree(), "two-extensions.zip.bak"); + String path = archive.getAbsolutePath(); commitGreeting(); assertArrayEquals(new String[] { "" }, @@ -202,8 +163,8 @@ public class ArchiveTest extends CLIRepositoryTestCase { @Test public void testZipExtension() throws Exception { - final File archiveWithDot = new File(db.getWorkTree(), "greeting.zip"); - final File archiveNoDot = new File(db.getWorkTree(), "greetingzip"); + File archiveWithDot = new File(db.getWorkTree(), "greeting.zip"); + File archiveNoDot = new File(db.getWorkTree(), "greetingzip"); commitGreeting(); execute("git archive " + @@ -218,8 +179,8 @@ public class ArchiveTest extends CLIRepositoryTestCase { @Test public void testTarExtension() throws Exception { - final File archive = new File(db.getWorkTree(), "tarball.tar"); - final String path = archive.getAbsolutePath(); + File archive = new File(db.getWorkTree(), "tarball.tar"); + String path = archive.getAbsolutePath(); commitGreeting(); assertArrayEquals(new String[] { "" }, @@ -234,8 +195,8 @@ public class ArchiveTest extends CLIRepositoryTestCase { commitGreeting(); for (String ext : Arrays.asList("tar.gz", "tgz")) { - final File archiveWithDot = new File(db.getWorkTree(), "tarball." + ext); - final File archiveNoDot = new File(db.getWorkTree(), "tarball" + ext); + File archiveWithDot = new File(db.getWorkTree(), "tarball." + ext); + File archiveNoDot = new File(db.getWorkTree(), "tarball" + ext); execute("git archive " + shellQuote("--output=" + archiveWithDot.getAbsolutePath()) + " " + @@ -253,8 +214,8 @@ public class ArchiveTest extends CLIRepositoryTestCase { commitGreeting(); for (String ext : Arrays.asList("tar.bz2", "tbz", "tbz2")) { - final File archiveWithDot = new File(db.getWorkTree(), "tarball." + ext); - final File archiveNoDot = new File(db.getWorkTree(), "tarball" + ext); + File archiveWithDot = new File(db.getWorkTree(), "tarball." + ext); + File archiveNoDot = new File(db.getWorkTree(), "tarball" + ext); execute("git archive " + shellQuote("--output=" + archiveWithDot.getAbsolutePath()) + " " + @@ -272,8 +233,8 @@ public class ArchiveTest extends CLIRepositoryTestCase { commitGreeting(); for (String ext : Arrays.asList("tar.xz", "txz")) { - final File archiveWithDot = new File(db.getWorkTree(), "tarball." + ext); - final File archiveNoDot = new File(db.getWorkTree(), "tarball" + ext); + File archiveWithDot = new File(db.getWorkTree(), "tarball." + ext); + File archiveNoDot = new File(db.getWorkTree(), "tarball" + ext); execute("git archive " + shellQuote("--output=" + archiveWithDot.getAbsolutePath()) + " " + @@ -302,8 +263,8 @@ public class ArchiveTest extends CLIRepositoryTestCase { git.add().addFilepattern("b").call(); git.commit().setMessage("add subdir").call(); - final byte[] result = CLIGitCommand.rawExecute( // - "git archive --format=zip master", db); + byte[] result = CLIGitCommand.executeRaw( + "git archive --format=zip master", db).outBytes(); String[] expect = { "a", "b.c", "b0c", "b/", "b/a", "b/b", "c" }; String[] actual = listZipEntries(result); @@ -328,8 +289,8 @@ public class ArchiveTest extends CLIRepositoryTestCase { git.add().addFilepattern("b").call(); git.commit().setMessage("add subdir").call(); - final byte[] result = CLIGitCommand.rawExecute( // - "git archive --format=tar master", db); + byte[] result = CLIGitCommand.executeRaw( + "git archive --format=tar master", db).outBytes(); String[] expect = { "a", "b.c", "b0c", "b/", "b/a", "b/b", "c" }; String[] actual = listTarEntries(result); @@ -349,9 +310,9 @@ public class ArchiveTest extends CLIRepositoryTestCase { @Test public void testArchivePrefixOption() throws Exception { commitBazAndFooSlashBar(); - byte[] result = CLIGitCommand.rawExecute( - "git archive --prefix=x/ --format=zip master", db); - String[] expect = { "x/baz", "x/foo/", "x/foo/bar" }; + byte[] result = CLIGitCommand.executeRaw( + "git archive --prefix=x/ --format=zip master", db).outBytes(); + String[] expect = { "x/", "x/baz", "x/foo/", "x/foo/bar" }; String[] actual = listZipEntries(result); Arrays.sort(expect); @@ -362,9 +323,9 @@ public class ArchiveTest extends CLIRepositoryTestCase { @Test public void testTarPrefixOption() throws Exception { commitBazAndFooSlashBar(); - byte[] result = CLIGitCommand.rawExecute( - "git archive --prefix=x/ --format=tar master", db); - String[] expect = { "x/baz", "x/foo/", "x/foo/bar" }; + byte[] result = CLIGitCommand.executeRaw( + "git archive --prefix=x/ --format=tar master", db).outBytes(); + String[] expect = { "x/", "x/baz", "x/foo/", "x/foo/bar" }; String[] actual = listTarEntries(result); Arrays.sort(expect); @@ -381,18 +342,18 @@ public class ArchiveTest extends CLIRepositoryTestCase { @Test public void testPrefixDoesNotNormalizeDoubleSlash() throws Exception { commitFoo(); - byte[] result = CLIGitCommand.rawExecute( - "git archive --prefix=x// --format=zip master", db); - String[] expect = { "x//foo" }; + byte[] result = CLIGitCommand.executeRaw( + "git archive --prefix=x// --format=zip master", db).outBytes(); + String[] expect = { "x/", "x//foo" }; assertArrayEquals(expect, listZipEntries(result)); } @Test public void testPrefixDoesNotNormalizeDoubleSlashInTar() throws Exception { commitFoo(); - final byte[] result = CLIGitCommand.rawExecute( // - "git archive --prefix=x// --format=tar master", db); - String[] expect = { "x//foo" }; + byte[] result = CLIGitCommand.executeRaw( + "git archive --prefix=x// --format=tar master", db).outBytes(); + String[] expect = { "x/", "x//foo" }; assertArrayEquals(expect, listTarEntries(result)); } @@ -408,8 +369,8 @@ public class ArchiveTest extends CLIRepositoryTestCase { @Test public void testPrefixWithoutTrailingSlash() throws Exception { commitBazAndFooSlashBar(); - byte[] result = CLIGitCommand.rawExecute( - "git archive --prefix=my- --format=zip master", db); + byte[] result = CLIGitCommand.executeRaw( + "git archive --prefix=my- --format=zip master", db).outBytes(); String[] expect = { "my-baz", "my-foo/", "my-foo/bar" }; String[] actual = listZipEntries(result); @@ -421,8 +382,8 @@ public class ArchiveTest extends CLIRepositoryTestCase { @Test public void testTarPrefixWithoutTrailingSlash() throws Exception { commitBazAndFooSlashBar(); - final byte[] result = CLIGitCommand.rawExecute( // - "git archive --prefix=my- --format=tar master", db); + byte[] result = CLIGitCommand.executeRaw( + "git archive --prefix=my- --format=tar master", db).outBytes(); String[] expect = { "my-baz", "my-foo/", "my-foo/bar" }; String[] actual = listTarEntries(result); @@ -441,8 +402,8 @@ public class ArchiveTest extends CLIRepositoryTestCase { git.submoduleAdd().setURI("./.").setPath("b").call().close(); git.commit().setMessage("add submodule").call(); - final byte[] result = CLIGitCommand.rawExecute( // - "git archive --format=zip master", db); + byte[] result = CLIGitCommand.executeRaw( + "git archive --format=zip master", db).outBytes(); String[] expect = { ".gitmodules", "a", "b/", "c" }; String[] actual = listZipEntries(result); @@ -461,8 +422,8 @@ public class ArchiveTest extends CLIRepositoryTestCase { git.submoduleAdd().setURI("./.").setPath("b").call().close(); git.commit().setMessage("add submodule").call(); - final byte[] result = CLIGitCommand.rawExecute( // - "git archive --format=tar master", db); + byte[] result = CLIGitCommand.executeRaw( + "git archive --format=tar master", db).outBytes(); String[] expect = { ".gitmodules", "a", "b/", "c" }; String[] actual = listTarEntries(result); @@ -491,8 +452,8 @@ public class ArchiveTest extends CLIRepositoryTestCase { git.commit().setMessage("three files with different modes").call(); - final byte[] zipData = CLIGitCommand.rawExecute( // - "git archive --format=zip master", db); + byte[] zipData = CLIGitCommand.executeRaw( + "git archive --format=zip master", db).outBytes(); writeRaw("zip-with-modes.zip", zipData); assertContainsEntryWithMode("zip-with-modes.zip", "-rw-", "plain"); assertContainsEntryWithMode("zip-with-modes.zip", "-rwx", "executable"); @@ -520,8 +481,8 @@ public class ArchiveTest extends CLIRepositoryTestCase { git.commit().setMessage("three files with different modes").call(); - final byte[] archive = CLIGitCommand.rawExecute( // - "git archive --format=tar master", db); + byte[] archive = CLIGitCommand.executeRaw( + "git archive --format=tar master", db).outBytes(); writeRaw("with-modes.tar", archive); assertTarContainsEntry("with-modes.tar", "-rw-r--r--", "plain"); assertTarContainsEntry("with-modes.tar", "-rwxr-xr-x", "executable"); @@ -531,74 +492,74 @@ public class ArchiveTest extends CLIRepositoryTestCase { @Test public void testArchiveWithLongFilename() throws Exception { - String filename = ""; - final List<String> l = new ArrayList<String>(); + StringBuilder filename = new StringBuilder(); + List<String> l = new ArrayList<>(); for (int i = 0; i < 20; i++) { - filename = filename + "1234567890/"; - l.add(filename); + filename.append("1234567890/"); + l.add(filename.toString()); } - filename = filename + "1234567890"; - l.add(filename); - writeTrashFile(filename, "file with long path"); + filename.append("1234567890"); + l.add(filename.toString()); + writeTrashFile(filename.toString(), "file with long path"); git.add().addFilepattern("1234567890").call(); git.commit().setMessage("file with long name").call(); - final byte[] result = CLIGitCommand.rawExecute( // - "git archive --format=zip HEAD", db); - assertArrayEquals(l.toArray(new String[l.size()]), + byte[] result = CLIGitCommand.executeRaw( + "git archive --format=zip HEAD", db).outBytes(); + assertArrayEquals(l.toArray(new String[0]), listZipEntries(result)); } @Test public void testTarWithLongFilename() throws Exception { - String filename = ""; - final List<String> l = new ArrayList<String>(); + StringBuilder filename = new StringBuilder(); + List<String> l = new ArrayList<>(); for (int i = 0; i < 20; i++) { - filename = filename + "1234567890/"; - l.add(filename); + filename.append("1234567890/"); + l.add(filename.toString()); } - filename = filename + "1234567890"; - l.add(filename); - writeTrashFile(filename, "file with long path"); + filename.append("1234567890"); + l.add(filename.toString()); + writeTrashFile(filename.toString(), "file with long path"); git.add().addFilepattern("1234567890").call(); git.commit().setMessage("file with long name").call(); - final byte[] result = CLIGitCommand.rawExecute( // - "git archive --format=tar HEAD", db); - assertArrayEquals(l.toArray(new String[l.size()]), + byte[] result = CLIGitCommand.executeRaw( + "git archive --format=tar HEAD", db).outBytes(); + assertArrayEquals(l.toArray(new String[0]), listTarEntries(result)); } @Test public void testArchivePreservesContent() throws Exception { - final String payload = "“The quick brown fox jumps over the lazy dog!”"; + String payload = "“The quick brown fox jumps over the lazy dog!”"; writeTrashFile("xyzzy", payload); git.add().addFilepattern("xyzzy").call(); git.commit().setMessage("add file with content").call(); - final byte[] result = CLIGitCommand.rawExecute( // - "git archive --format=zip HEAD", db); - assertArrayEquals(new String[] { payload }, // + byte[] result = CLIGitCommand.executeRaw( + "git archive --format=zip HEAD", db).outBytes(); + assertArrayEquals(new String[] { payload }, zipEntryContent(result, "xyzzy")); } @Test public void testTarPreservesContent() throws Exception { - final String payload = "“The quick brown fox jumps over the lazy dog!”"; + String payload = "“The quick brown fox jumps over the lazy dog!”"; writeTrashFile("xyzzy", payload); git.add().addFilepattern("xyzzy").call(); git.commit().setMessage("add file with content").call(); - final byte[] result = CLIGitCommand.rawExecute( // - "git archive --format=tar HEAD", db); - assertArrayEquals(new String[] { payload }, // + byte[] result = CLIGitCommand.executeRaw( + "git archive --format=tar HEAD", db).outBytes(); + assertArrayEquals(new String[] { payload }, tarEntryContent(result, "xyzzy")); } private Process spawnAssumingCommandPresent(String... cmdline) { - final File cwd = db.getWorkTree(); - final ProcessBuilder procBuilder = new ProcessBuilder(cmdline) // - .directory(cwd) // + File cwd = db.getWorkTree(); + ProcessBuilder procBuilder = new ProcessBuilder(cmdline) + .directory(cwd) .redirectErrorStream(true); Process proc = null; try { @@ -612,15 +573,15 @@ public class ArchiveTest extends CLIRepositoryTestCase { } private BufferedReader readFromProcess(Process proc) throws Exception { - return new BufferedReader( // - new InputStreamReader(proc.getInputStream(), "UTF-8")); + return new BufferedReader( + new InputStreamReader(proc.getInputStream(), UTF_8)); } - private void grepForEntry(String name, String mode, String... cmdline) // + private void grepForEntry(String name, String mode, String... cmdline) throws Exception { - final Process proc = spawnAssumingCommandPresent(cmdline); + Process proc = spawnAssumingCommandPresent(cmdline); proc.getOutputStream().close(); - final BufferedReader reader = readFromProcess(proc); + BufferedReader reader = readFromProcess(proc); try { String line; while ((line = reader.readLine()) != null) @@ -635,16 +596,16 @@ public class ArchiveTest extends CLIRepositoryTestCase { } private void assertMagic(long offset, byte[] magicBytes, File file) throws Exception { - BufferedInputStream in = new BufferedInputStream( - new FileInputStream(file)); - try { - in.skip(offset); + try (BufferedInputStream in = new BufferedInputStream( + new FileInputStream(file))) { + if (offset > 0) { + long skipped = in.skip(offset); + assertEquals(offset, skipped); + } byte[] actual = new byte[magicBytes.length]; in.read(actual); assertArrayEquals(magicBytes, actual); - } finally { - in.close(); } } @@ -672,79 +633,73 @@ public class ArchiveTest extends CLIRepositoryTestCase { assertMagic(new byte[] { (byte) 0xfd, '7', 'z', 'X', 'Z', 0 }, file); } - private void assertContainsEntryWithMode(String zipFilename, String mode, String name) // + private void assertContainsEntryWithMode(String zipFilename, String mode, String name) throws Exception { grepForEntry(name, mode, "zipinfo", zipFilename); } - private void assertTarContainsEntry(String tarfile, String mode, String name) // + private void assertTarContainsEntry(String tarfile, String mode, String name) throws Exception { grepForEntry(name, mode, "tar", "tvf", tarfile); } - private void writeRaw(String filename, byte[] data) // + private void writeRaw(String filename, byte[] data) throws IOException { - final File path = new File(db.getWorkTree(), filename); - final OutputStream out = new FileOutputStream(path); - try { + File path = new File(db.getWorkTree(), filename); + try (OutputStream out = new FileOutputStream(path)) { out.write(data); - } finally { - out.close(); } } private static String[] listZipEntries(byte[] zipData) throws IOException { - final List<String> l = new ArrayList<String>(); - final ZipInputStream in = new ZipInputStream( // - new ByteArrayInputStream(zipData)); + List<String> l = new ArrayList<>(); + try (ZipInputStream in = new ZipInputStream( + new ByteArrayInputStream(zipData))) { + ZipEntry e; + while ((e = in.getNextEntry()) != null) + l.add(e.getName()); + } + return l.toArray(new String[0]); + } - ZipEntry e; - while ((e = in.getNextEntry()) != null) - l.add(e.getName()); - in.close(); - return l.toArray(new String[l.size()]); - } - - private static Future<Object> writeAsync(final OutputStream stream, final byte[] data) { - final ExecutorService executor = Executors.newSingleThreadExecutor(); - - return executor.submit(new Callable<Object>() { // - public Object call() throws IOException { - try { - stream.write(data); - return null; - } finally { - stream.close(); - } + private static Future<Object> writeAsync(OutputStream stream, byte[] data) { + ExecutorService executor = Executors.newSingleThreadExecutor(); + + return executor.submit(() -> { + try { + stream.write(data); + return null; + } finally { + stream.close(); } }); } private String[] listTarEntries(byte[] tarData) throws Exception { - final List<String> l = new ArrayList<String>(); - final Process proc = spawnAssumingCommandPresent("tar", "tf", "-"); - final BufferedReader reader = readFromProcess(proc); - final OutputStream out = proc.getOutputStream(); - - // Dump tarball to tar stdin in background - final Future<?> writing = writeAsync(out, tarData); - - try { - String line; - while ((line = reader.readLine()) != null) - l.add(line); - - return l.toArray(new String[l.size()]); - } finally { - writing.get(); - reader.close(); - proc.destroy(); + List<String> l = new ArrayList<>(); + Process proc = spawnAssumingCommandPresent("tar", "tf", "-"); + try (BufferedReader reader = readFromProcess(proc)) { + OutputStream out = proc.getOutputStream(); + + // Dump tarball to tar stdin in background + Future<?> writing = writeAsync(out, tarData); + + try { + String line; + while ((line = reader.readLine()) != null) + l.add(line); + + return l.toArray(new String[0]); + } finally { + writing.get(); + proc.destroy(); + } } } - private static String[] zipEntryContent(byte[] zipData, String path) // + private static String[] zipEntryContent(byte[] zipData, String path) throws IOException { - final ZipInputStream in = new ZipInputStream( // + ZipInputStream in = new ZipInputStream( new ByteArrayInputStream(zipData)); ZipEntry e; while ((e = in.getNextEntry()) != null) { @@ -752,37 +707,37 @@ public class ArchiveTest extends CLIRepositoryTestCase { continue; // found! - final List<String> l = new ArrayList<String>(); - final BufferedReader reader = new BufferedReader( // - new InputStreamReader(in, "UTF-8")); + List<String> l = new ArrayList<>(); + BufferedReader reader = new BufferedReader( + new InputStreamReader(in, UTF_8)); String line; while ((line = reader.readLine()) != null) l.add(line); - return l.toArray(new String[l.size()]); + return l.toArray(new String[0]); } // not found return null; } - private String[] tarEntryContent(byte[] tarData, String path) // + private String[] tarEntryContent(byte[] tarData, String path) throws Exception { - final List<String> l = new ArrayList<String>(); - final Process proc = spawnAssumingCommandPresent("tar", "Oxf", "-", path); - final BufferedReader reader = readFromProcess(proc); - final OutputStream out = proc.getOutputStream(); - final Future<?> writing = writeAsync(out, tarData); - - try { - String line; - while ((line = reader.readLine()) != null) - l.add(line); - - return l.toArray(new String[l.size()]); - } finally { - writing.get(); - reader.close(); - proc.destroy(); + List<String> l = new ArrayList<>(); + Process proc = spawnAssumingCommandPresent("tar", "Oxf", "-", path); + try (BufferedReader reader = readFromProcess(proc)) { + OutputStream out = proc.getOutputStream(); + Future<?> writing = writeAsync(out, tarData); + + try { + String line; + while ((line = reader.readLine()) != null) + l.add(line); + + return l.toArray(new String[0]); + } finally { + writing.get(); + proc.destroy(); + } } } } |