diff options
author | 2015-06-09 16:39:59 +0000 | |
---|---|---|
committer | 2015-06-10 16:02:22 +0000 | |
commit | 1cad714e2157dd940cc75428e8bb4d682b1bef28 (patch) | |
tree | bf62a5e34762160cbefa096f95364666f840e445 /src/java_tools/singlejar/javatests/com/google | |
parent | 6b2766dd0e97e37fd56c1c538bc029f22e68dfcd (diff) |
Removed deprecated API features from ZipCombiner and improves slow read testing and support.
RELNOTES: Elements of ZipCombiner's API previously marked deprecated are removed.
--
MOS_MIGRATED_REVID=95543357
Diffstat (limited to 'src/java_tools/singlejar/javatests/com/google')
3 files changed, 126 insertions, 96 deletions
diff --git a/src/java_tools/singlejar/javatests/com/google/devtools/build/singlejar/ZipCombinerTest.java b/src/java_tools/singlejar/javatests/com/google/devtools/build/singlejar/ZipCombinerTest.java index 099454f458..94cb25f480 100644 --- a/src/java_tools/singlejar/javatests/com/google/devtools/build/singlejar/ZipCombinerTest.java +++ b/src/java_tools/singlejar/javatests/com/google/devtools/build/singlejar/ZipCombinerTest.java @@ -23,7 +23,6 @@ import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; -import com.google.common.base.Preconditions; import com.google.common.collect.ArrayListMultimap; import com.google.common.collect.ListMultimap; import com.google.devtools.build.singlejar.ZipCombiner.OutputMode; @@ -73,12 +72,6 @@ public class ZipCombinerTest { @Rule public TemporaryFolder tmp = new TemporaryFolder(); @Rule public ExpectedException thrown = ExpectedException.none(); - private InputStream sampleZipStream() { - ZipFactory factory = new ZipFactory(); - factory.addFile("hello.txt", "Hello World!"); - return factory.toInputStream(); - } - private File sampleZip() throws IOException { ZipFactory factory = new ZipFactory(); factory.addFile("hello.txt", "Hello World!"); @@ -98,25 +91,12 @@ public class ZipCombinerTest { return writeInputStreamToFile(factory.toInputStream()); } - private InputStream sampleZipWithOneUncompressedEntryStream() { - ZipFactory factory = new ZipFactory(); - factory.addFile("hello.txt", "Hello World!", false); - return factory.toInputStream(); - } - private File sampleZipWithOneUncompressedEntry() throws IOException { ZipFactory factory = new ZipFactory(); factory.addFile("hello.txt", "Hello World!", false); return writeInputStreamToFile(factory.toInputStream()); } - private InputStream sampleZipWithTwoUncompressedEntriesStream() { - ZipFactory factory = new ZipFactory(); - factory.addFile("hello.txt", "Hello World!", false); - factory.addFile("hello2.txt", "Hello World 2!", false); - return factory.toInputStream(); - } - private File sampleZipWithTwoUncompressedEntries() throws IOException { ZipFactory factory = new ZipFactory(); factory.addFile("hello.txt", "Hello World!", false); @@ -161,16 +141,6 @@ public class ZipCombinerTest { } @Test - public void testInputStreamZip() throws IOException { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - try (ZipCombiner zipCombiner = new ZipCombiner(out)) { - zipCombiner.addZip(sampleZipStream()); - } - FakeZipFile expectedResult = new FakeZipFile().addEntry("hello.txt", "Hello World!", true); - expectedResult.assertSame(out.toByteArray()); - } - - @Test public void testCompressedDontCare() throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); try (ZipCombiner zipCombiner = new ZipCombiner(out)) { @@ -279,55 +249,6 @@ public class ZipCombinerTest { assertNull(zipInput.getNextEntry()); } - //Returns an input stream that can only read one byte at a time. - private InputStream slowRead(final InputStream in) { - return new InputStream() { - @Override - public int read() throws IOException { - return in.read(); - } - @Override - public int read(byte b[], int off, int len) throws IOException { - Preconditions.checkArgument(b != null); - Preconditions.checkArgument((len >= 0) && (off >= 0)); - Preconditions.checkArgument(len <= b.length - off); - if (len == 0) { - return 0; - } - int value = read(); - if (value == -1) { - return -1; - } - b[off] = (byte) value; - return 1; - } - }; - } - - @Test - public void testDuplicateUncompressedEntryWithSlowRead() throws IOException { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - ZipCombiner zipCombiner = new ZipCombiner(out); - zipCombiner.addZip(slowRead(sampleZipWithOneUncompressedEntryStream())); - zipCombiner.addZip(slowRead(sampleZipWithOneUncompressedEntryStream())); - zipCombiner.close(); - ZipInputStream zipInput = new ZipInputStream(new ByteArrayInputStream(out.toByteArray())); - assertEntry(zipInput, "hello.txt", "Hello World!"); - assertNull(zipInput.getNextEntry()); - } - - @Test - public void testDuplicateEntryWithSlowRead() throws IOException { - ByteArrayOutputStream out = new ByteArrayOutputStream(); - ZipCombiner zipCombiner = new ZipCombiner(out); - zipCombiner.addZip(slowRead(sampleZipStream())); - zipCombiner.addZip(slowRead(sampleZipStream())); - zipCombiner.close(); - ZipInputStream zipInput = new ZipInputStream(new ByteArrayInputStream(out.toByteArray())); - assertEntry(zipInput, "hello.txt", "Hello World!"); - assertNull(zipInput.getNextEntry()); - } - @Test public void testBadZipFileNoEntry() throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); @@ -478,22 +399,6 @@ public class ZipCombinerTest { } @Test - public void testMergeStrategyWithUncompressedEntriesAndSlowRead() throws IOException { - MockZipEntryFilter mockFilter = new MockZipEntryFilter(); - mockFilter.behavior.put("hello.txt", new ConcatenateStrategy()); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - ZipCombiner zipCombiner = new ZipCombiner(mockFilter, out); - zipCombiner.addZip(slowRead(sampleZipWithOneUncompressedEntryStream())); - zipCombiner.addZip(slowRead(sampleZipWithTwoUncompressedEntriesStream())); - zipCombiner.close(); - assertEquals(Arrays.asList("hello.txt", "hello2.txt"), mockFilter.calls); - ZipInputStream zipInput = new ZipInputStream(new ByteArrayInputStream(out.toByteArray())); - assertEntry(zipInput, "hello2.txt", "Hello World 2!"); - assertEntry(zipInput, "hello.txt", "Hello World!\nHello World!"); - assertNull(zipInput.getNextEntry()); - } - - @Test public void testMergeStrategyWithSlowCopy() throws IOException { MockZipEntryFilter mockFilter = new MockZipEntryFilter(); mockFilter.behavior.put("hello.txt", new SlowConcatenateStrategy()); diff --git a/src/java_tools/singlejar/javatests/com/google/devtools/build/zip/SlowZipReader.java b/src/java_tools/singlejar/javatests/com/google/devtools/build/zip/SlowZipReader.java new file mode 100644 index 0000000000..cdc4fa9f67 --- /dev/null +++ b/src/java_tools/singlejar/javatests/com/google/devtools/build/zip/SlowZipReader.java @@ -0,0 +1,87 @@ +// Copyright 2015 Google Inc. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +package com.google.devtools.build.zip; + +import static com.google.common.base.Preconditions.checkArgument; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.Charset; + +/** + * A test ZIP file reader that provides slow {@link InputStream}s for reading sections of the file. + * + * <p>Providing InputStreams that do not read or skip the requested amount allows testing that the + * parsing code properly handles cases where the data is not immediately available. + */ +public class SlowZipReader extends ZipReader { + + /** + * Opens a zip file for raw acceess. Passes through to {@link ZipReader#ZipReader(File)}. + */ + public SlowZipReader(File file) throws IOException { + super(file); + } + + /** + * Opens a zip file for raw acceess. Passes through to {@link ZipReader#ZipReader(File, Charset)}. + */ + public SlowZipReader(File file, Charset charset) throws IOException { + super(file, charset); + } + + /** + * Opens a zip file for raw acceess. Passes through to + * {@link ZipReader#ZipReader(File, Charset, boolean)}. + */ + public SlowZipReader(File file, Charset charset, boolean strictEntries) throws IOException { + super(file, charset, strictEntries); + } + + /** + * Returns a new slow {@link InputStream} positioned at fileOffset. + * + * @throws IOException if an I/O error has occurred + */ + @Override + protected InputStream getStreamAt(long fileOffset) throws IOException { + final InputStream in = super.getStreamAt(fileOffset); + return new InputStream() { + @Override + public int read() throws IOException { + return in.read(); + } + @Override + public int read(byte b[], int off, int len) throws IOException { + checkArgument(b != null); + checkArgument((len >= 0) && (off >= 0)); + checkArgument(len <= b.length - off); + if (len == 0) { + return 0; + } + int value = read(); + if (value == -1) { + return -1; + } + b[off] = (byte) value; + return 1; + } + @Override + public long skip(long n) throws IOException { + return super.skip(1); + } + }; + } +} diff --git a/src/java_tools/singlejar/javatests/com/google/devtools/build/zip/ZipReaderTest.java b/src/java_tools/singlejar/javatests/com/google/devtools/build/zip/ZipReaderTest.java index dc2652ec03..69be251c45 100644 --- a/src/java_tools/singlejar/javatests/com/google/devtools/build/zip/ZipReaderTest.java +++ b/src/java_tools/singlejar/javatests/com/google/devtools/build/zip/ZipReaderTest.java @@ -348,7 +348,7 @@ public class ZipReaderTest { } @Test public void testSimultaneousReads() throws IOException { - byte[] expectedFooData = "This if file foo. It contains a foo.".getBytes(UTF_8); + byte[] expectedFooData = "This is file foo. It contains a foo.".getBytes(UTF_8); byte[] expectedBarData = "This is a different file bar. It contains only a bar." .getBytes(UTF_8); try (ZipOutputStream zout = new ZipOutputStream(new FileOutputStream(test))) { @@ -385,6 +385,44 @@ public class ZipReaderTest { } } + @Test public void testSlowRead() throws IOException { + byte[] expectedFooData = "This is file foo. It contains a foo.".getBytes(UTF_8); + byte[] expectedBarData = "This is a different file bar. It contains only a bar." + .getBytes(UTF_8); + try (ZipOutputStream zout = new ZipOutputStream(new FileOutputStream(test))) { + ZipEntry foo = new ZipEntry("foo"); + foo.setComment("foo comment."); + foo.setMethod(ZipEntry.DEFLATED); + zout.putNextEntry(foo); + zout.write(expectedFooData); + zout.closeEntry(); + + ZipEntry bar = new ZipEntry("bar"); + bar.setComment("bar comment."); + bar.setMethod(ZipEntry.DEFLATED); + zout.putNextEntry(bar); + zout.write(expectedBarData); + zout.closeEntry(); + } + + try (ZipReader reader = new SlowZipReader(test, UTF_8)) { + ZipFileEntry fooEntry = reader.getEntry("foo"); + ZipFileEntry barEntry = reader.getEntry("bar"); + InputStream fooIn = reader.getInputStream(fooEntry); + InputStream barIn = reader.getInputStream(barEntry); + byte[] fooData = new byte[expectedFooData.length]; + byte[] barData = new byte[expectedBarData.length]; + ZipUtil.readFully(fooIn, fooData, 0, 10); + ZipUtil.readFully(barIn, barData, 0, 10); + ZipUtil.readFully(fooIn, fooData, 10, 10); + ZipUtil.readFully(barIn, barData, 10, 10); + ZipUtil.readFully(fooIn, fooData, 20, fooData.length - 20); + ZipUtil.readFully(barIn, barData, 20, barData.length - 20); + assertThat(fooData).isEqualTo(expectedFooData); + assertThat(barData).isEqualTo(expectedBarData); + } + } + @Test public void testZip64() throws IOException { // Generated with: 'echo "foo" > entry; zip -fz -q out.zip entry' byte[] data = new byte[] { |