This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-compress.git
commit 05489c04aa388f561ba038fd4d450ddcdc39a78e Author: Gary Gregory <garydgreg...@gmail.com> AuthorDate: Mon Jan 15 11:29:31 2024 -0500 Javadoc - Remove extra whitespace - Use Objects.requireNonNull() - Format - Remove unused instance variable - Don't use deprecated method - Add comments - Add Javadoc --- .../zip/FileRandomAccessOutputStream.java | 9 +- .../archivers/zip/RandomAccessOutputStream.java | 1 + .../SeekableChannelRandomAccessOutputStream.java | 4 +- .../commons/compress/archivers/zip/ZipFile.java | 57 +++----- .../commons/compress/archivers/zip/ZipIoUtil.java | 28 ++-- .../archivers/zip/ZipSplitOutputStream.java | 5 +- .../zip/ZipSplitReadOnlySeekableByteChannel.java | 2 +- .../apache/commons/compress/archivers/ZipTest.java | 52 +------ .../zip/FileRandomAccessOutputStreamTest.java | 99 ++++++-------- .../zip/RandomAccessOutputStreamTest.java | 11 +- ...eekableChannelRandomAccessOutputStreamTest.java | 110 ++++++--------- .../archivers/zip/ZipArchiveOutputStreamTest.java | 4 +- .../compress/archivers/zip/ZipIoUtilTest.java | 152 +++++++++------------ 13 files changed, 205 insertions(+), 329 deletions(-) diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/FileRandomAccessOutputStream.java b/src/main/java/org/apache/commons/compress/archivers/zip/FileRandomAccessOutputStream.java index 6024520f3..1ee726f6c 100644 --- a/src/main/java/org/apache/commons/compress/archivers/zip/FileRandomAccessOutputStream.java +++ b/src/main/java/org/apache/commons/compress/archivers/zip/FileRandomAccessOutputStream.java @@ -22,11 +22,12 @@ import java.nio.channels.FileChannel; import java.nio.file.OpenOption; import java.nio.file.Path; import java.nio.file.StandardOpenOption; - +import java.util.Objects; /** - * {@link RandomAccessOutputStream} implementation based on Path file. + * {@link RandomAccessOutputStream} implementation based on a file. */ +// Keep package-private; consider for Apache Commons IO. class FileRandomAccessOutputStream extends RandomAccessOutputStream { private final FileChannel channel; @@ -34,7 +35,7 @@ class FileRandomAccessOutputStream extends RandomAccessOutputStream { private long position; FileRandomAccessOutputStream(final FileChannel channel) throws IOException { - this.channel = channel; + this.channel = Objects.requireNonNull(channel, "channel"); } FileRandomAccessOutputStream(final Path file) throws IOException { @@ -68,7 +69,7 @@ class FileRandomAccessOutputStream extends RandomAccessOutputStream { @Override public void writeFully(final byte[] b, final int off, final int len, final long atPosition) throws IOException { final ByteBuffer buf = ByteBuffer.wrap(b, off, len); - for (long currentPos = atPosition; buf.hasRemaining(); ) { + for (long currentPos = atPosition; buf.hasRemaining();) { final int written = this.channel.write(buf, currentPos); if (written <= 0) { throw new IOException("Failed to fully write to file: written=" + written); diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/RandomAccessOutputStream.java b/src/main/java/org/apache/commons/compress/archivers/zip/RandomAccessOutputStream.java index e47f4c632..3ff5cf67b 100644 --- a/src/main/java/org/apache/commons/compress/archivers/zip/RandomAccessOutputStream.java +++ b/src/main/java/org/apache/commons/compress/archivers/zip/RandomAccessOutputStream.java @@ -22,6 +22,7 @@ import java.io.OutputStream; /** * Abstraction over OutputStream which also allows random access writes. */ +// Keep package-private; consider for Apache Commons IO. abstract class RandomAccessOutputStream extends OutputStream { /** diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/SeekableChannelRandomAccessOutputStream.java b/src/main/java/org/apache/commons/compress/archivers/zip/SeekableChannelRandomAccessOutputStream.java index 29a1ec4c6..8a9f5b8bf 100644 --- a/src/main/java/org/apache/commons/compress/archivers/zip/SeekableChannelRandomAccessOutputStream.java +++ b/src/main/java/org/apache/commons/compress/archivers/zip/SeekableChannelRandomAccessOutputStream.java @@ -20,16 +20,14 @@ import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.SeekableByteChannel; - /** * {@link RandomAccessOutputStream} implementation for SeekableByteChannel. */ +// Keep package-private; consider for Apache Commons IO. class SeekableChannelRandomAccessOutputStream extends RandomAccessOutputStream { private final SeekableByteChannel channel; - private long position; - SeekableChannelRandomAccessOutputStream(final SeekableByteChannel channel) { this.channel = channel; } diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java b/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java index a8271e8a3..bd452e5f9 100644 --- a/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java +++ b/src/main/java/org/apache/commons/compress/archivers/zip/ZipFile.java @@ -178,10 +178,9 @@ public class ZipFile implements Closeable { /** * Sets max number of multi archive disks, default is 1 (no multi archive). * - * @param maxNumberOfDisks - * max number of multi archive disks + * @param maxNumberOfDisks max number of multi archive disks. * - * @return this instance + * @return this. */ public Builder setMaxNumberOfDisks(final long maxNumberOfDisks) { this.maxNumberOfDisks = maxNumberOfDisks; @@ -519,8 +518,7 @@ public class ZipFile implements Closeable { numberOfDisks = (buf.getShort() & 0xffff) + 1; } if (numberOfDisks > Math.min(maxNumberOfDisks, Integer.MAX_VALUE)) { - throw new IOException("Too many disks for zip archive, max=" + - Math.min(maxNumberOfDisks, Integer.MAX_VALUE) + " actual=" + numberOfDisks); + throw new IOException("Too many disks for zip archive, max=" + Math.min(maxNumberOfDisks, Integer.MAX_VALUE) + " actual=" + numberOfDisks); } if (numberOfDisks <= 1) { @@ -531,28 +529,23 @@ public class ZipFile implements Closeable { final Path parent = path.getParent(); final String basename = FilenameUtils.removeExtension(path.getFileName().toString()); - return ZipSplitReadOnlySeekableByteChannel.forPaths( - IntStream.range(0, (int) numberOfDisks) - .mapToObj(i -> { - if (i == numberOfDisks - 1) { - return path; - } - final Path lowercase = parent.resolve(String.format("%s.z%02d", basename, i + 1)); - if (Files.exists(lowercase)) { - return lowercase; - } - final Path uppercase = parent.resolve(String.format("%s.Z%02d", basename, i + 1)); - if (Files.exists(uppercase)) { - return uppercase; - } - return lowercase; - }) - .collect(Collectors.toList()), - openOptions - ); + return ZipSplitReadOnlySeekableByteChannel.forPaths(IntStream.range(0, (int) numberOfDisks).mapToObj(i -> { + if (i == numberOfDisks - 1) { + return path; + } + final Path lowercase = parent.resolve(String.format("%s.z%02d", basename, i + 1)); + if (Files.exists(lowercase)) { + return lowercase; + } + final Path uppercase = parent.resolve(String.format("%s.Z%02d", basename, i + 1)); + if (Files.exists(uppercase)) { + return uppercase; + } + return lowercase; + }).collect(Collectors.toList()), openOptions); } catch (final Throwable ex) { - IOUtils.closeQuietly(channel); - channels.forEach(IOUtils::closeQuietly); + org.apache.commons.io.IOUtils.closeQuietly(channel); + channels.forEach(org.apache.commons.io.IOUtils::closeQuietly); throw ex; } } @@ -560,8 +553,7 @@ public class ZipFile implements Closeable { /** * Searches for the and positions the stream at the start of the "End of central dir record". * - * @return - * true if it's Zip64 end of central directory or false if it's Zip32 + * @return true if it's Zip64 end of central directory or false if it's Zip32 */ private static boolean positionAtEndOfCentralDirectoryRecord(final SeekableByteChannel channel) throws IOException { final boolean found = tryToLocateSignature(channel, MIN_EOCD_SIZE, MAX_EOCD_SIZE, ZipArchiveOutputStream.EOCD_SIG); @@ -591,12 +583,8 @@ public class ZipFile implements Closeable { * Searches the archive backwards from minDistance to maxDistance for the given signature, positions the RandomaccessFile right at the signature if it has * been found. */ - private static boolean tryToLocateSignature( - final SeekableByteChannel channel, - final long minDistanceFromEnd, - final long maxDistanceFromEnd, - final byte[] sig - ) throws IOException { + private static boolean tryToLocateSignature(final SeekableByteChannel channel, final long minDistanceFromEnd, final long maxDistanceFromEnd, + final byte[] sig) throws IOException { final ByteBuffer wordBuf = ByteBuffer.allocate(ZipConstants.WORD); boolean found = false; long off = channel.size() - minDistanceFromEnd; @@ -694,7 +682,6 @@ public class ZipFile implements Closeable { private final ByteBuffer shortBbuf = ByteBuffer.wrap(shortBuf); - private long centralDirectoryStartDiskNumber, centralDirectoryStartRelativeOffset; private long centralDirectoryStartOffset; diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/ZipIoUtil.java b/src/main/java/org/apache/commons/compress/archivers/zip/ZipIoUtil.java index 2de5c1b1f..7de121fca 100644 --- a/src/main/java/org/apache/commons/compress/archivers/zip/ZipIoUtil.java +++ b/src/main/java/org/apache/commons/compress/archivers/zip/ZipIoUtil.java @@ -23,22 +23,18 @@ import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.nio.channels.SeekableByteChannel; - /** * IO utilities for Zip operations. - * - * Package private to potentially move to something reusable. */ +// Keep package-private; consider for Apache Commons IO. class ZipIoUtil { + /** * Writes full buffer to channel. * - * @param channel - * channel to write to - * @param buf - * buffer to write - * @throws IOException - * when writing fails or fails to write fully + * @param channel channel to write to + * @param buf buffer to write + * @throws IOException when writing fails or fails to write fully */ static void writeFully(final SeekableByteChannel channel, final ByteBuffer buf) throws IOException { while (buf.hasRemaining()) { @@ -53,17 +49,13 @@ class ZipIoUtil { /** * Writes full buffer to channel at specified position. * - * @param channel - * channel to write to - * @param buf - * buffer to write - * @param position - * position to write at - * @throws IOException - * when writing fails or fails to write fully + * @param channel channel to write to + * @param buf buffer to write + * @param position position to write at + * @throws IOException when writing fails or fails to write fully */ static void writeFullyAt(final FileChannel channel, final ByteBuffer buf, final long position) throws IOException { - for (long currentPosition = position; buf.hasRemaining(); ) { + for (long currentPosition = position; buf.hasRemaining();) { final int remaining = buf.remaining(); final int written = channel.write(buf, currentPosition); if (written <= 0) { diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/ZipSplitOutputStream.java b/src/main/java/org/apache/commons/compress/archivers/zip/ZipSplitOutputStream.java index 19af76f06..594927bde 100644 --- a/src/main/java/org/apache/commons/compress/archivers/zip/ZipSplitOutputStream.java +++ b/src/main/java/org/apache/commons/compress/archivers/zip/ZipSplitOutputStream.java @@ -43,7 +43,10 @@ final class ZipSplitOutputStream extends RandomAccessOutputStream { * 8.5.1 Capacities for split archives are as follows: * <p> * Maximum number of segments = 4,294,967,295 - 1 Maximum .ZIP segment size = 4,294,967,295 bytes (refer to section 8.5.6) Minimum segment size = 64K + * </p> + * <p> * Maximum PKSFX segment size = 2,147,483,647 bytes + * </p> */ private static final long ZIP_SEGMENT_MIN_SIZE = 64 * 1024L; private static final long ZIP_SEGMENT_MAX_SIZE = 4294967295L; @@ -57,9 +60,7 @@ final class ZipSplitOutputStream extends RandomAccessOutputStream { private long currentSplitSegmentBytesWritten; private boolean finished; private final byte[] singleByte = new byte[1]; - private final List<Long> diskToPosition = new ArrayList<>(); - private final TreeMap<Long, Path> positionToFiles = new TreeMap<>(); /** diff --git a/src/main/java/org/apache/commons/compress/archivers/zip/ZipSplitReadOnlySeekableByteChannel.java b/src/main/java/org/apache/commons/compress/archivers/zip/ZipSplitReadOnlySeekableByteChannel.java index 1547c55a9..29815d25a 100644 --- a/src/main/java/org/apache/commons/compress/archivers/zip/ZipSplitReadOnlySeekableByteChannel.java +++ b/src/main/java/org/apache/commons/compress/archivers/zip/ZipSplitReadOnlySeekableByteChannel.java @@ -226,7 +226,7 @@ public class ZipSplitReadOnlySeekableByteChannel extends MultiReadOnlySeekableBy * @since 1.22 */ public static SeekableByteChannel forPaths(final Path... paths) throws IOException { - return forPaths(Arrays.asList(paths), new OpenOption[]{ StandardOpenOption.READ }); + return forPaths(Arrays.asList(paths), new OpenOption[] { StandardOpenOption.READ }); } /** diff --git a/src/test/java/org/apache/commons/compress/archivers/ZipTest.java b/src/test/java/org/apache/commons/compress/archivers/ZipTest.java index 1aab3e5eb..d14749249 100644 --- a/src/test/java/org/apache/commons/compress/archivers/ZipTest.java +++ b/src/test/java/org/apache/commons/compress/archivers/ZipTest.java @@ -285,12 +285,7 @@ public final class ZipTest extends AbstractTest { zipArchiveOutputStream.write(data); zipArchiveOutputStream.closeArchiveEntry(); } - - try (ZipFile zipFile = ZipFile.builder() - .setPath(outputZipFile.toPath()) - .setMaxNumberOfDisks(Integer.MAX_VALUE) - .get() - ) { + try (ZipFile zipFile = ZipFile.builder().setPath(outputZipFile.toPath()).setMaxNumberOfDisks(Integer.MAX_VALUE).get()) { assertArrayEquals(data, IOUtils.toByteArray(zipFile.getInputStream(zipFile.getEntry("file01")))); assertArrayEquals(data, IOUtils.toByteArray(zipFile.getInputStream(zipFile.getEntry("file02")))); } @@ -315,14 +310,8 @@ public final class ZipTest extends AbstractTest { zipArchiveOutputStream.write(data1); zipArchiveOutputStream.closeArchiveEntry(); } - assertEquals(64 * 1024L - 1, Files.size(outputZipFile.toPath().getParent().resolve("artificialSplitZip.z01"))); - - try (ZipFile zipFile = ZipFile.builder() - .setPath(outputZipFile.toPath()) - .setMaxNumberOfDisks(Integer.MAX_VALUE) - .get() - ) { + try (ZipFile zipFile = ZipFile.builder().setPath(outputZipFile.toPath()).setMaxNumberOfDisks(Integer.MAX_VALUE).get()) { assertArrayEquals(data1, IOUtils.toByteArray(zipFile.getInputStream(zipFile.getEntry("file01")))); } } @@ -369,12 +358,7 @@ public final class ZipTest extends AbstractTest { zipArchiveOutputStream.write(data4); zipArchiveOutputStream.closeArchiveEntry(); } - - try (ZipFile zipFile = ZipFile.builder() - .setPath(outputZipFile.toPath()) - .setMaxNumberOfDisks(Integer.MAX_VALUE) - .get() - ) { + try (ZipFile zipFile = ZipFile.builder().setPath(outputZipFile.toPath()).setMaxNumberOfDisks(Integer.MAX_VALUE).get()) { assertArrayEquals(data1, IOUtils.toByteArray(zipFile.getInputStream(zipFile.getEntry("file01")))); assertArrayEquals(data2, IOUtils.toByteArray(zipFile.getInputStream(zipFile.getEntry("file02")))); assertArrayEquals(data3, IOUtils.toByteArray(zipFile.getInputStream(zipFile.getEntry("file03")))); @@ -386,7 +370,6 @@ public final class ZipTest extends AbstractTest { public void testBuildSplitZipTest() throws IOException { final File directoryToZip = getFilesToZip(); createTestSplitZipSegments(); - final File lastFile = newTempFile("splitZip.zip"); try (SeekableByteChannel channel = ZipSplitReadOnlySeekableByteChannel.buildFromLastSplitSegment(lastFile); InputStream inputStream = Channels.newInputStream(channel); @@ -414,11 +397,9 @@ public final class ZipTest extends AbstractTest { final File outputZipFile = newTempFile("splitZip.zip"); final long splitSize = 100 * 1024L; /* 100 KB */ try (ZipArchiveOutputStream zipArchiveOutputStream = new ZipArchiveOutputStream(outputZipFile, splitSize)) { - // create a file that has the same name of one of the created split segments final File sameNameFile = newTempFile("splitZip.z01"); sameNameFile.createNewFile(); - assertThrows(IOException.class, () -> addFilesToZip(zipArchiveOutputStream, directoryToZip)); } catch (final Exception e) { // Ignore: @@ -447,21 +428,17 @@ public final class ZipTest extends AbstractTest { @Test public void testCopyRawEntriesFromFile() throws IOException { - final File reference = createReferenceFile(Zip64Mode.Never, "expected."); - final File file1 = createTempFile("src1.", ".zip"); try (ZipArchiveOutputStream zos = new ZipArchiveOutputStream(file1)) { zos.setUseZip64(Zip64Mode.Never); createFirstEntry(zos).close(); } - final File file2 = createTempFile("src2.", ".zip"); try (ZipArchiveOutputStream zos1 = new ZipArchiveOutputStream(file2)) { zos1.setUseZip64(Zip64Mode.Never); createSecondEntry(zos1).close(); } - try (ZipFile zipFile1 = newZipFile(file1); ZipFile zipFile2 = newZipFile(file2)) { final File fileResult = createTempFile("file-actual.", ".zip"); @@ -478,19 +455,16 @@ public final class ZipTest extends AbstractTest { @Test public void testCopyRawZip64EntryFromFile() throws IOException { - final File reference = createTempFile("z64reference.", ".zip"); try (ZipArchiveOutputStream zos1 = new ZipArchiveOutputStream(reference)) { zos1.setUseZip64(Zip64Mode.Always); createFirstEntry(zos1); } - final File file1 = createTempFile("zip64src.", ".zip"); try (ZipArchiveOutputStream zos = new ZipArchiveOutputStream(file1)) { zos.setUseZip64(Zip64Mode.Always); createFirstEntry(zos).close(); } - final File fileResult = createTempFile("file-actual.", ".zip"); try (ZipFile zipFile1 = newZipFile(file1)) { try (ZipArchiveOutputStream zos2 = new ZipArchiveOutputStream(fileResult)) { @@ -598,9 +572,7 @@ public final class ZipTest extends AbstractTest { private void testInputStreamStatistics(final String fileName, final Map<String, List<Long>> expectedStatistics) throws IOException, ArchiveException { final File input = getFile(fileName); - final Map<String, List<List<Long>>> actualStatistics = new HashMap<>(); - // stream access try (InputStream fis = Files.newInputStream(input.toPath()); ArchiveInputStream<?> in = ArchiveStreamFactory.DEFAULT.createArchiveInputStream("zip", fis)) { @@ -608,7 +580,6 @@ public final class ZipTest extends AbstractTest { readStream(in, entry, actualStatistics); } } - // file access try (ZipFile zf = newZipFile(input)) { final Enumeration<ZipArchiveEntry> entries = zf.getEntries(); @@ -619,12 +590,10 @@ public final class ZipTest extends AbstractTest { } } } - // compare statistics of stream / file access for (final Map.Entry<String, List<List<Long>>> me : actualStatistics.entrySet()) { assertEquals(me.getValue().get(0), me.getValue().get(1), "Mismatch of stats for: " + me.getKey()); } - for (final Map.Entry<String, List<Long>> me : expectedStatistics.entrySet()) { assertEquals(me.getValue(), actualStatistics.get(me.getKey()).get(0), "Mismatch of stats with expected value for: " + me.getKey()); } @@ -684,17 +653,13 @@ public final class ZipTest extends AbstractTest { @Test public void testListAllFilesWithNestedArchive() throws Exception { final File input = getFile("OSX_ArchiveWithNestedArchive.zip"); - final List<String> results = new ArrayList<>(); final List<ZipException> expectedExceptions = new ArrayList<>(); - try (InputStream fis = Files.newInputStream(input.toPath()); ZipArchiveInputStream in = ArchiveStreamFactory.DEFAULT.createArchiveInputStream("zip", fis)) { - ZipArchiveEntry entry; while ((entry = in.getNextEntry()) != null) { results.add(entry.getName()); - final ZipArchiveInputStream nestedIn = ArchiveStreamFactory.DEFAULT.createArchiveInputStream("zip", in); try { ZipArchiveEntry nestedEntry; @@ -708,7 +673,6 @@ public final class ZipTest extends AbstractTest { // nested stream must not be closed here } } - assertTrue(results.contains("NestedArchiv.zip")); assertTrue(results.contains("test1.xml")); assertTrue(results.contains("test2.xml")); @@ -787,22 +751,20 @@ public final class ZipTest extends AbstractTest { final File output = newTempFile("bla.zip"); final File file1 = getFile("test1.xml"); final File file2 = getFile("test2.xml"); - try (OutputStream out = Files.newOutputStream(output.toPath())) { try (ArchiveOutputStream<ZipArchiveEntry> os = ArchiveStreamFactory.DEFAULT.createArchiveOutputStream("zip", out)) { + // entry 1 os.putArchiveEntry(new ZipArchiveEntry("testdata/test1.xml")); Files.copy(file1.toPath(), os); os.closeArchiveEntry(); - + // entry 2 os.putArchiveEntry(new ZipArchiveEntry("testdata/test2.xml")); Files.copy(file2.toPath(), os); os.closeArchiveEntry(); } } - // Unarchive the same final List<File> results = new ArrayList<>(); - try (InputStream fileInputStream = Files.newInputStream(output.toPath())) { try (ArchiveInputStream<ZipArchiveEntry> archiveInputStream = ArchiveStreamFactory.DEFAULT.createArchiveInputStream("zip", fileInputStream)) { ZipArchiveEntry entry; @@ -814,7 +776,6 @@ public final class ZipTest extends AbstractTest { } } } - assertEquals(results.size(), 2); File result = results.get(0); assertEquals(file1.length(), result.length()); @@ -832,7 +793,6 @@ public final class ZipTest extends AbstractTest { final byte[] file1Contents = Files.readAllBytes(getPath("test1.xml")); final byte[] file2Contents = Files.readAllBytes(getPath("test2.xml")); final List<byte[]> results = new ArrayList<>(); - try (SeekableInMemoryByteChannel channel = new SeekableInMemoryByteChannel()) { try (ZipArchiveOutputStream os = new ZipArchiveOutputStream(channel)) { os.putArchiveEntry(new ZipArchiveEntry("testdata/test1.xml")); @@ -843,10 +803,8 @@ public final class ZipTest extends AbstractTest { os.write(file2Contents); os.closeArchiveEntry(); } - // Unarchive the same try (ZipArchiveInputStream inputStream = ArchiveStreamFactory.DEFAULT.createArchiveInputStream("zip", new ByteArrayInputStream(channel.array()))) { - ZipArchiveEntry entry; while ((entry = inputStream.getNextEntry()) != null) { final byte[] result = new byte[(int) entry.getSize()]; diff --git a/src/test/java/org/apache/commons/compress/archivers/zip/FileRandomAccessOutputStreamTest.java b/src/test/java/org/apache/commons/compress/archivers/zip/FileRandomAccessOutputStreamTest.java index bc6cd240f..7cccd2514 100644 --- a/src/test/java/org/apache/commons/compress/archivers/zip/FileRandomAccessOutputStreamTest.java +++ b/src/test/java/org/apache/commons/compress/archivers/zip/FileRandomAccessOutputStreamTest.java @@ -35,8 +35,11 @@ import java.nio.file.Path; import org.apache.commons.compress.AbstractTempDirTest; import org.junit.jupiter.api.Test; - +/** + * Tests {@link FileRandomAccessOutputStream}. + */ public class FileRandomAccessOutputStreamTest extends AbstractTempDirTest { + @Test public void testChannelReturn() throws IOException { final Path file = newTempPath("testChannel"); @@ -49,19 +52,16 @@ public class FileRandomAccessOutputStreamTest extends AbstractTempDirTest { public void testWrite() throws IOException { final FileChannel channel = mock(FileChannel.class); final FileRandomAccessOutputStream stream = new FileRandomAccessOutputStream(channel); - when(channel.write((ByteBuffer) any())) - .thenAnswer(answer -> { - ((ByteBuffer) answer.getArgument(0)).position(5); - return 5; - }) - .thenAnswer(answer -> { + when(channel.write((ByteBuffer) any())).thenAnswer(answer -> { + ((ByteBuffer) answer.getArgument(0)).position(5); + return 5; + }).thenAnswer(answer -> { ((ByteBuffer) answer.getArgument(0)).position(6); return 6; }); stream.write("hello".getBytes(StandardCharsets.UTF_8)); stream.write("world\n".getBytes(StandardCharsets.UTF_8)); - verify(channel, times(2)) - .write((ByteBuffer) any()); + verify(channel, times(2)).write((ByteBuffer) any()); assertEquals(11, stream.position()); } @@ -70,23 +70,19 @@ public class FileRandomAccessOutputStreamTest extends AbstractTempDirTest { public void testWriteFullyAt_whenFullAtOnce_thenSucceed() throws IOException { final FileChannel channel = mock(FileChannel.class); final FileRandomAccessOutputStream stream = new FileRandomAccessOutputStream(channel); - when(channel.write((ByteBuffer) any(), eq(20L))) - .thenAnswer(answer -> { - ((ByteBuffer) answer.getArgument(0)).position(5); - return 5; - }); - when(channel.write((ByteBuffer) any(), eq(30L))) - .thenAnswer(answer -> { - ((ByteBuffer) answer.getArgument(0)).position(6); - return 6; - }); + when(channel.write((ByteBuffer) any(), eq(20L))).thenAnswer(answer -> { + ((ByteBuffer) answer.getArgument(0)).position(5); + return 5; + }); + when(channel.write((ByteBuffer) any(), eq(30L))).thenAnswer(answer -> { + ((ByteBuffer) answer.getArgument(0)).position(6); + return 6; + }); stream.writeFully("hello".getBytes(StandardCharsets.UTF_8), 20); stream.writeFully("world\n".getBytes(StandardCharsets.UTF_8), 30); - verify(channel, times(1)) - .write((ByteBuffer) any(), eq(20L)); - verify(channel, times(1)) - .write((ByteBuffer) any(), eq(30L)); + verify(channel, times(1)).write((ByteBuffer) any(), eq(20L)); + verify(channel, times(1)).write((ByteBuffer) any(), eq(30L)); assertEquals(0, stream.position()); } @@ -95,30 +91,24 @@ public class FileRandomAccessOutputStreamTest extends AbstractTempDirTest { public void testWriteFullyAt_whenFullButPartial_thenSucceed() throws IOException { final FileChannel channel = mock(FileChannel.class); final FileRandomAccessOutputStream stream = new FileRandomAccessOutputStream(channel); - when(channel.write((ByteBuffer) any(), eq(20L))) - .thenAnswer(answer -> { - ((ByteBuffer) answer.getArgument(0)).position(3); - return 3; - }); - when(channel.write((ByteBuffer) any(), eq(23L))) - .thenAnswer(answer -> { - ((ByteBuffer) answer.getArgument(0)).position(5); - return 2; - }); - when(channel.write((ByteBuffer) any(), eq(30L))) - .thenAnswer(answer -> { - ((ByteBuffer) answer.getArgument(0)).position(6); - return 6; - }); + when(channel.write((ByteBuffer) any(), eq(20L))).thenAnswer(answer -> { + ((ByteBuffer) answer.getArgument(0)).position(3); + return 3; + }); + when(channel.write((ByteBuffer) any(), eq(23L))).thenAnswer(answer -> { + ((ByteBuffer) answer.getArgument(0)).position(5); + return 2; + }); + when(channel.write((ByteBuffer) any(), eq(30L))).thenAnswer(answer -> { + ((ByteBuffer) answer.getArgument(0)).position(6); + return 6; + }); stream.writeFully("hello".getBytes(StandardCharsets.UTF_8), 20); stream.writeFully("world\n".getBytes(StandardCharsets.UTF_8), 30); - verify(channel, times(1)) - .write((ByteBuffer) any(), eq(20L)); - verify(channel, times(1)) - .write((ByteBuffer) any(), eq(23L)); - verify(channel, times(1)) - .write((ByteBuffer) any(), eq(30L)); + verify(channel, times(1)).write((ByteBuffer) any(), eq(20L)); + verify(channel, times(1)).write((ByteBuffer) any(), eq(23L)); + verify(channel, times(1)).write((ByteBuffer) any(), eq(30L)); assertEquals(0, stream.position()); } @@ -127,21 +117,16 @@ public class FileRandomAccessOutputStreamTest extends AbstractTempDirTest { public void testWriteFullyAt_whenPartial_thenFail() throws IOException { final FileChannel channel = mock(FileChannel.class); final FileRandomAccessOutputStream stream = new FileRandomAccessOutputStream(channel); - when(channel.write((ByteBuffer) any(), eq(20L))) - .thenAnswer(answer -> { - ((ByteBuffer) answer.getArgument(0)).position(3); - return 3; - }); - when(channel.write((ByteBuffer) any(), eq(23L))) - .thenAnswer(answer -> 0); + when(channel.write((ByteBuffer) any(), eq(20L))).thenAnswer(answer -> { + ((ByteBuffer) answer.getArgument(0)).position(3); + return 3; + }); + when(channel.write((ByteBuffer) any(), eq(23L))).thenAnswer(answer -> 0); assertThrows(IOException.class, () -> stream.writeFully("hello".getBytes(StandardCharsets.UTF_8), 20)); - verify(channel, times(1)) - .write((ByteBuffer) any(), eq(20L)); - verify(channel, times(1)) - .write((ByteBuffer) any(), eq(23L)); - verify(channel, times(0)) - .write((ByteBuffer) any(), eq(25L)); + verify(channel, times(1)).write((ByteBuffer) any(), eq(20L)); + verify(channel, times(1)).write((ByteBuffer) any(), eq(23L)); + verify(channel, times(0)).write((ByteBuffer) any(), eq(25L)); assertEquals(0, stream.position()); } diff --git a/src/test/java/org/apache/commons/compress/archivers/zip/RandomAccessOutputStreamTest.java b/src/test/java/org/apache/commons/compress/archivers/zip/RandomAccessOutputStreamTest.java index 3a31d1f8d..fdb66169c 100644 --- a/src/test/java/org/apache/commons/compress/archivers/zip/RandomAccessOutputStreamTest.java +++ b/src/test/java/org/apache/commons/compress/archivers/zip/RandomAccessOutputStreamTest.java @@ -27,14 +27,16 @@ import java.io.IOException; import org.apache.commons.compress.AbstractTempDirTest; import org.junit.jupiter.api.Test; - +/** + * Tests {@link RandomAccessOutputStream}. + */ public class RandomAccessOutputStreamTest extends AbstractTempDirTest { @Test public void testWrite() throws IOException { final RandomAccessOutputStream delegate = mock(RandomAccessOutputStream.class); - final RandomAccessOutputStream stream = new RandomAccessOutputStream() { + @Override public long position() throws IOException { return delegate.position(); @@ -50,10 +52,7 @@ public class RandomAccessOutputStreamTest extends AbstractTempDirTest { delegate.writeFully(b, off, len, position); } }; - stream.write('\n'); - - verify(delegate, times(1)) - .write(any(), eq(0), eq(1)); + verify(delegate, times(1)).write(any(), eq(0), eq(1)); } } diff --git a/src/test/java/org/apache/commons/compress/archivers/zip/SeekableChannelRandomAccessOutputStreamTest.java b/src/test/java/org/apache/commons/compress/archivers/zip/SeekableChannelRandomAccessOutputStreamTest.java index b6ed26eb3..ddaf09cda 100644 --- a/src/test/java/org/apache/commons/compress/archivers/zip/SeekableChannelRandomAccessOutputStreamTest.java +++ b/src/test/java/org/apache/commons/compress/archivers/zip/SeekableChannelRandomAccessOutputStreamTest.java @@ -37,15 +37,16 @@ import java.nio.file.StandardOpenOption; import org.apache.commons.compress.AbstractTempDirTest; import org.junit.jupiter.api.Test; - +/** + * Tests {@link SeekableChannelRandomAccessOutputStream}. + */ public class SeekableChannelRandomAccessOutputStreamTest extends AbstractTempDirTest { @Test public void testInitialization() throws IOException { final Path file = newTempPath("testChannel"); try (SeekableChannelRandomAccessOutputStream stream = new SeekableChannelRandomAccessOutputStream( - Files.newByteChannel(file, StandardOpenOption.CREATE, StandardOpenOption.WRITE) - )) { + Files.newByteChannel(file, StandardOpenOption.CREATE, StandardOpenOption.WRITE))) { assertEquals(0, stream.position()); } } @@ -55,23 +56,19 @@ public class SeekableChannelRandomAccessOutputStreamTest extends AbstractTempDir final FileChannel channel = mock(FileChannel.class); final SeekableChannelRandomAccessOutputStream stream = new SeekableChannelRandomAccessOutputStream(channel); - when(channel.position()) - .thenReturn(11L); - when(channel.write((ByteBuffer) any())) - .thenAnswer(answer -> { - ((ByteBuffer) answer.getArgument(0)).position(5); - return 5; - }) - .thenAnswer(answer -> { - ((ByteBuffer) answer.getArgument(0)).position(6); - return 6; - }); + when(channel.position()).thenReturn(11L); + when(channel.write((ByteBuffer) any())).thenAnswer(answer -> { + ((ByteBuffer) answer.getArgument(0)).position(5); + return 5; + }).thenAnswer(answer -> { + ((ByteBuffer) answer.getArgument(0)).position(6); + return 6; + }); stream.write("hello".getBytes(StandardCharsets.UTF_8)); stream.write("world\n".getBytes(StandardCharsets.UTF_8)); - verify(channel, times(2)) - .write((ByteBuffer) any()); + verify(channel, times(2)).write((ByteBuffer) any()); assertEquals(11, stream.position()); } @@ -81,28 +78,21 @@ public class SeekableChannelRandomAccessOutputStreamTest extends AbstractTempDir final SeekableByteChannel channel = mock(SeekableByteChannel.class); final SeekableChannelRandomAccessOutputStream stream = new SeekableChannelRandomAccessOutputStream(channel); - when(channel.position()) - .thenReturn(50L) - .thenReturn(60L); - when(channel.write((ByteBuffer) any())) - .thenAnswer(answer -> { - ((ByteBuffer) answer.getArgument(0)).position(5); - return 5; - }) - .thenAnswer(answer -> { - ((ByteBuffer) answer.getArgument(0)).position(6); - return 6; - }); + when(channel.position()).thenReturn(50L).thenReturn(60L); + when(channel.write((ByteBuffer) any())).thenAnswer(answer -> { + ((ByteBuffer) answer.getArgument(0)).position(5); + return 5; + }).thenAnswer(answer -> { + ((ByteBuffer) answer.getArgument(0)).position(6); + return 6; + }); stream.writeFully("hello".getBytes(StandardCharsets.UTF_8), 20); stream.writeFully("world\n".getBytes(StandardCharsets.UTF_8), 30); - verify(channel, times(2)) - .write((ByteBuffer) any()); - verify(channel, times(1)) - .position(eq(50L)); - verify(channel, times(1)) - .position(eq(60L)); + verify(channel, times(2)).write((ByteBuffer) any()); + verify(channel, times(1)).position(eq(50L)); + verify(channel, times(1)).position(eq(60L)); assertEquals(60L, stream.position()); } @@ -112,32 +102,24 @@ public class SeekableChannelRandomAccessOutputStreamTest extends AbstractTempDir final SeekableByteChannel channel = mock(SeekableByteChannel.class); final SeekableChannelRandomAccessOutputStream stream = new SeekableChannelRandomAccessOutputStream(channel); - when(channel.position()) - .thenReturn(50L) - .thenReturn(60L); - when(channel.write((ByteBuffer) any())) - .thenAnswer(answer -> { - ((ByteBuffer) answer.getArgument(0)).position(3); - return 3; - }) - .thenAnswer(answer -> { - ((ByteBuffer) answer.getArgument(0)).position(5); - return 2; - }) - .thenAnswer(answer -> { - ((ByteBuffer) answer.getArgument(0)).position(6); - return 6; - }); + when(channel.position()).thenReturn(50L).thenReturn(60L); + when(channel.write((ByteBuffer) any())).thenAnswer(answer -> { + ((ByteBuffer) answer.getArgument(0)).position(3); + return 3; + }).thenAnswer(answer -> { + ((ByteBuffer) answer.getArgument(0)).position(5); + return 2; + }).thenAnswer(answer -> { + ((ByteBuffer) answer.getArgument(0)).position(6); + return 6; + }); stream.writeFully("hello".getBytes(StandardCharsets.UTF_8), 20); stream.writeFully("world\n".getBytes(StandardCharsets.UTF_8), 30); - verify(channel, times(3)) - .write((ByteBuffer) any()); - verify(channel, times(1)) - .position(eq(50L)); - verify(channel, times(1)) - .position(eq(60L)); + verify(channel, times(3)).write((ByteBuffer) any()); + verify(channel, times(1)).position(eq(50L)); + verify(channel, times(1)).position(eq(60L)); assertEquals(60L, stream.position()); } @@ -147,19 +129,15 @@ public class SeekableChannelRandomAccessOutputStreamTest extends AbstractTempDir final SeekableByteChannel channel = mock(SeekableByteChannel.class); final SeekableChannelRandomAccessOutputStream stream = new SeekableChannelRandomAccessOutputStream(channel); - when(channel.position()) - .thenReturn(50L); - when(channel.write((ByteBuffer) any())) - .thenAnswer(answer -> { - ((ByteBuffer) answer.getArgument(0)).position(3); - return 3; - }) - .thenAnswer(answer -> 0); + when(channel.position()).thenReturn(50L); + when(channel.write((ByteBuffer) any())).thenAnswer(answer -> { + ((ByteBuffer) answer.getArgument(0)).position(3); + return 3; + }).thenAnswer(answer -> 0); assertThrows(IOException.class, () -> stream.writeFully("hello".getBytes(StandardCharsets.UTF_8), 20)); - verify(channel, times(2)) - .write((ByteBuffer) any()); + verify(channel, times(2)).write((ByteBuffer) any()); assertEquals(50L, stream.position()); } diff --git a/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStreamTest.java b/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStreamTest.java index 0a121b51b..a60e3a8be 100644 --- a/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStreamTest.java +++ b/src/test/java/org/apache/commons/compress/archivers/zip/ZipArchiveOutputStreamTest.java @@ -25,7 +25,9 @@ import java.io.IOException; import org.apache.commons.compress.AbstractTempDirTest; import org.junit.jupiter.api.Test; - +/** + * Tests {@link ZipArchiveOutputStream}. + */ public class ZipArchiveOutputStreamTest extends AbstractTempDirTest { @Test diff --git a/src/test/java/org/apache/commons/compress/archivers/zip/ZipIoUtilTest.java b/src/test/java/org/apache/commons/compress/archivers/zip/ZipIoUtilTest.java index 9b8e90d07..e71f2cd07 100644 --- a/src/test/java/org/apache/commons/compress/archivers/zip/ZipIoUtilTest.java +++ b/src/test/java/org/apache/commons/compress/archivers/zip/ZipIoUtilTest.java @@ -33,148 +33,122 @@ import java.nio.charset.StandardCharsets; import org.apache.commons.compress.AbstractTempDirTest; import org.junit.jupiter.api.Test; - +/** + * Tests {@link ZipIoUtil}. + */ public class ZipIoUtilTest extends AbstractTempDirTest { @Test public void testWriteFully_whenFullAtOnce_thenSucceed() throws IOException { final SeekableByteChannel channel = mock(SeekableByteChannel.class); - when(channel.write((ByteBuffer) any())) - .thenAnswer(answer -> { - ((ByteBuffer) answer.getArgument(0)).position(5); - return 5; - }) - .thenAnswer(answer -> { - ((ByteBuffer) answer.getArgument(0)).position(6); - return 6; - }); + when(channel.write((ByteBuffer) any())).thenAnswer(answer -> { + ((ByteBuffer) answer.getArgument(0)).position(5); + return 5; + }).thenAnswer(answer -> { + ((ByteBuffer) answer.getArgument(0)).position(6); + return 6; + }); ZipIoUtil.writeFully(channel, ByteBuffer.wrap("hello".getBytes(StandardCharsets.UTF_8))); ZipIoUtil.writeFully(channel, ByteBuffer.wrap("world\n".getBytes(StandardCharsets.UTF_8))); - verify(channel, times(2)) - .write((ByteBuffer) any()); + verify(channel, times(2)).write((ByteBuffer) any()); } @Test public void testWriteFully_whenFullButPartial_thenSucceed() throws IOException { final SeekableByteChannel channel = mock(SeekableByteChannel.class); - when(channel.write((ByteBuffer) any())) - .thenAnswer(answer -> { - ((ByteBuffer) answer.getArgument(0)).position(3); - return 3; - }) - .thenAnswer(answer -> { - ((ByteBuffer) answer.getArgument(0)).position(5); - return 2; - }) - .thenAnswer(answer -> { - ((ByteBuffer) answer.getArgument(0)).position(6); - return 6; - }); + when(channel.write((ByteBuffer) any())).thenAnswer(answer -> { + ((ByteBuffer) answer.getArgument(0)).position(3); + return 3; + }).thenAnswer(answer -> { + ((ByteBuffer) answer.getArgument(0)).position(5); + return 2; + }).thenAnswer(answer -> { + ((ByteBuffer) answer.getArgument(0)).position(6); + return 6; + }); ZipIoUtil.writeFully(channel, ByteBuffer.wrap("hello".getBytes(StandardCharsets.UTF_8))); ZipIoUtil.writeFully(channel, ByteBuffer.wrap("world\n".getBytes(StandardCharsets.UTF_8))); - verify(channel, times(3)) - .write((ByteBuffer) any()); + verify(channel, times(3)).write((ByteBuffer) any()); } @Test public void testWriteFully_whenPartial_thenFail() throws IOException { final SeekableByteChannel channel = mock(SeekableByteChannel.class); - when(channel.write((ByteBuffer) any())) - .thenAnswer(answer -> { - ((ByteBuffer) answer.getArgument(0)).position(3); - return 3; - }) - .thenAnswer(answer -> 0); + when(channel.write((ByteBuffer) any())).thenAnswer(answer -> { + ((ByteBuffer) answer.getArgument(0)).position(3); + return 3; + }).thenAnswer(answer -> 0); - assertThrows(IOException.class, () -> - ZipIoUtil.writeFully(channel, ByteBuffer.wrap("hello".getBytes(StandardCharsets.UTF_8))) - ); + assertThrows(IOException.class, () -> ZipIoUtil.writeFully(channel, ByteBuffer.wrap("hello".getBytes(StandardCharsets.UTF_8)))); - verify(channel, times(2)) - .write((ByteBuffer) any()); + verify(channel, times(2)).write((ByteBuffer) any()); } @Test public void testWriteFullyAt_whenFullAtOnce_thenSucceed() throws IOException { final FileChannel channel = mock(FileChannel.class); - when(channel.write((ByteBuffer) any(), eq(20L))) - .thenAnswer(answer -> { - ((ByteBuffer) answer.getArgument(0)).position(5); - return 5; - }); - when(channel.write((ByteBuffer) any(), eq(30L))) - .thenAnswer(answer -> { - ((ByteBuffer) answer.getArgument(0)).position(6); - return 6; - }); + when(channel.write((ByteBuffer) any(), eq(20L))).thenAnswer(answer -> { + ((ByteBuffer) answer.getArgument(0)).position(5); + return 5; + }); + when(channel.write((ByteBuffer) any(), eq(30L))).thenAnswer(answer -> { + ((ByteBuffer) answer.getArgument(0)).position(6); + return 6; + }); ZipIoUtil.writeFullyAt(channel, ByteBuffer.wrap("hello".getBytes(StandardCharsets.UTF_8)), 20); ZipIoUtil.writeFullyAt(channel, ByteBuffer.wrap("world\n".getBytes(StandardCharsets.UTF_8)), 30); - verify(channel, times(1)) - .write((ByteBuffer) any(), eq(20L)); - verify(channel, times(1)) - .write((ByteBuffer) any(), eq(30L)); + verify(channel, times(1)).write((ByteBuffer) any(), eq(20L)); + verify(channel, times(1)).write((ByteBuffer) any(), eq(30L)); } @Test public void testWriteFullyAt_whenFullButPartial_thenSucceed() throws IOException { final FileChannel channel = mock(FileChannel.class); - when(channel.write((ByteBuffer) any(), eq(20L))) - .thenAnswer(answer -> { - ((ByteBuffer) answer.getArgument(0)).position(3); - return 3; - }); - when(channel.write((ByteBuffer) any(), eq(23L))) - .thenAnswer(answer -> { - ((ByteBuffer) answer.getArgument(0)).position(5); - return 2; - }); - when(channel.write((ByteBuffer) any(), eq(30L))) - .thenAnswer(answer -> { - ((ByteBuffer) answer.getArgument(0)).position(6); - return 6; - }); + when(channel.write((ByteBuffer) any(), eq(20L))).thenAnswer(answer -> { + ((ByteBuffer) answer.getArgument(0)).position(3); + return 3; + }); + when(channel.write((ByteBuffer) any(), eq(23L))).thenAnswer(answer -> { + ((ByteBuffer) answer.getArgument(0)).position(5); + return 2; + }); + when(channel.write((ByteBuffer) any(), eq(30L))).thenAnswer(answer -> { + ((ByteBuffer) answer.getArgument(0)).position(6); + return 6; + }); ZipIoUtil.writeFullyAt(channel, ByteBuffer.wrap("hello".getBytes(StandardCharsets.UTF_8)), 20); ZipIoUtil.writeFullyAt(channel, ByteBuffer.wrap("world\n".getBytes(StandardCharsets.UTF_8)), 30); - verify(channel, times(1)) - .write((ByteBuffer) any(), eq(20L)); - verify(channel, times(1)) - .write((ByteBuffer) any(), eq(23L)); - verify(channel, times(1)) - .write((ByteBuffer) any(), eq(30L)); + verify(channel, times(1)).write((ByteBuffer) any(), eq(20L)); + verify(channel, times(1)).write((ByteBuffer) any(), eq(23L)); + verify(channel, times(1)).write((ByteBuffer) any(), eq(30L)); } @Test public void testWriteFullyAt_whenPartial_thenFail() throws IOException { final FileChannel channel = mock(FileChannel.class); - when(channel.write((ByteBuffer) any(), eq(20L))) - .thenAnswer(answer -> { - ((ByteBuffer) answer.getArgument(0)).position(3); - return 3; - }); - when(channel.write((ByteBuffer) any(), eq(23L))) - .thenAnswer(answer -> 0); - assertThrows(IOException.class, () -> - ZipIoUtil.writeFullyAt(channel, ByteBuffer.wrap("hello".getBytes(StandardCharsets.UTF_8)), 20)); - - verify(channel, times(1)) - .write((ByteBuffer) any(), eq(20L)); - verify(channel, times(1)) - .write((ByteBuffer) any(), eq(23L)); - verify(channel, times(0)) - .write((ByteBuffer) any(), eq(25L)); + when(channel.write((ByteBuffer) any(), eq(20L))).thenAnswer(answer -> { + ((ByteBuffer) answer.getArgument(0)).position(3); + return 3; + }); + when(channel.write((ByteBuffer) any(), eq(23L))).thenAnswer(answer -> 0); + assertThrows(IOException.class, () -> ZipIoUtil.writeFullyAt(channel, ByteBuffer.wrap("hello".getBytes(StandardCharsets.UTF_8)), 20)); + + verify(channel, times(1)).write((ByteBuffer) any(), eq(20L)); + verify(channel, times(1)).write((ByteBuffer) any(), eq(23L)); + verify(channel, times(0)).write((ByteBuffer) any(), eq(25L)); } }