You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2020/09/06 00:32:06 UTC

[commons-io] branch master updated (d531c3b -> c0f4623)

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/commons-io.git.


    from d531c3b  Remove dead comments.
     new e65b52b  Use try-with-resources. Fix some formatting.
     new c0f4623  Fix SpotBugs issues.

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../commons/io/file/CleaningPathVisitor.java       | 44 +++++++++++----
 .../commons/io/file/CopyDirectoryVisitor.java      | 27 ++++++++++
 .../commons/io/file/DeletingPathVisitor.java       | 46 ++++++++++++----
 .../io/input/ObservableInputStreamTest.java        | 62 +++++++++++-----------
 4 files changed, 128 insertions(+), 51 deletions(-)


[commons-io] 01/02: Use try-with-resources. Fix some formatting.

Posted by gg...@apache.org.
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-io.git

commit e65b52b253b2157351ec8b287e7573fa54aceffc
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Sep 5 20:04:00 2020 -0400

    Use try-with-resources. Fix some formatting.
---
 .../io/input/ObservableInputStreamTest.java        | 62 +++++++++++-----------
 1 file changed, 32 insertions(+), 30 deletions(-)

diff --git a/src/test/java/org/apache/commons/io/input/ObservableInputStreamTest.java b/src/test/java/org/apache/commons/io/input/ObservableInputStreamTest.java
index 5fe9019..aa5edea 100644
--- a/src/test/java/org/apache/commons/io/input/ObservableInputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/input/ObservableInputStreamTest.java
@@ -34,34 +34,31 @@ public class ObservableInputStreamTest {
         private boolean closed;
 
         @Override
-		public
-        void data(final int pByte) throws IOException {
+        public void data(final int pByte) throws IOException {
             super.data(pByte);
             lastByteSeen = pByte;
         }
 
         @Override
-		public
-        void finished() throws IOException {
+        public void finished() throws IOException {
             super.finished();
             finished = true;
         }
 
         @Override
-		public
-        void closed() throws IOException {
+        public void closed() throws IOException {
             super.closed();
             closed = true;
         }
     }
+
     private static class LastBytesKeepingObserver extends Observer {
         private byte[] buffer = null;
         private int offset = -1;
         private int length = -1;
 
         @Override
-		public
-        void data(final byte[] pBuffer, final int pOffset, final int pLength) throws IOException {
+        public void data(final byte[] pBuffer, final int pOffset, final int pLength) throws IOException {
             super.data(pBuffer, pOffset, pLength);
             buffer = pBuffer;
             offset = pOffset;
@@ -69,40 +66,45 @@ public class ObservableInputStreamTest {
         }
     }
 
-    /** Tests, that {@link Observer#data(int)} is called.
+    /**
+     * Tests that {@link Observer#data(int)} is called.
      */
     @Test
     public void testDataByteCalled() throws Exception {
-        final byte[] buffer = MessageDigestCalculatingInputStreamTest.generateRandomByteStream(IOUtils.DEFAULT_BUFFER_SIZE);
-        final ObservableInputStream ois = new ObservableInputStream(new ByteArrayInputStream(buffer));
+        final byte[] buffer = MessageDigestCalculatingInputStreamTest
+                .generateRandomByteStream(IOUtils.DEFAULT_BUFFER_SIZE);
         final LastByteKeepingObserver lko = new LastByteKeepingObserver();
-        assertEquals(-1, lko.lastByteSeen);
-        ois.read();
-        assertEquals(-1, lko.lastByteSeen);
-        assertFalse(lko.finished);
-        assertFalse(lko.closed);
-        ois.add(lko);
-        for (int i = 1;  i < buffer.length;  i++) {
-            final int result = ois.read();
-            assertEquals((byte) result, buffer[i]);
-            assertEquals(result, lko.lastByteSeen);
+        try (final ObservableInputStream ois = new ObservableInputStream(new ByteArrayInputStream(buffer))) {
+            assertEquals(-1, lko.lastByteSeen);
+            ois.read();
+            assertEquals(-1, lko.lastByteSeen);
             assertFalse(lko.finished);
             assertFalse(lko.closed);
+            ois.add(lko);
+            for (int i = 1; i < buffer.length; i++) {
+                final int result = ois.read();
+                assertEquals((byte) result, buffer[i]);
+                assertEquals(result, lko.lastByteSeen);
+                assertFalse(lko.finished);
+                assertFalse(lko.closed);
+            }
+            final int result = ois.read();
+            assertEquals(-1, result);
+            assertTrue(lko.finished);
+            assertFalse(lko.closed);
+            ois.close();
+            assertTrue(lko.finished);
+            assertTrue(lko.closed);
         }
-        final int result = ois.read();
-        assertEquals(-1, result);
-        assertTrue(lko.finished);
-        assertFalse(lko.closed);
-        ois.close();
-        assertTrue(lko.finished);
-        assertTrue(lko.closed);
     }
 
-    /** Tests, that {@link Observer#data(byte[],int,int)} is called.
+    /**
+     * Tests that {@link Observer#data(byte[],int,int)} is called.
      */
     @Test
     public void testDataBytesCalled() throws Exception {
-        final byte[] buffer = MessageDigestCalculatingInputStreamTest.generateRandomByteStream(IOUtils.DEFAULT_BUFFER_SIZE);
+        final byte[] buffer = MessageDigestCalculatingInputStreamTest
+                .generateRandomByteStream(IOUtils.DEFAULT_BUFFER_SIZE);
         final ByteArrayInputStream bais = new ByteArrayInputStream(buffer);
         final ObservableInputStream ois = new ObservableInputStream(bais);
         final LastBytesKeepingObserver lko = new LastBytesKeepingObserver();


[commons-io] 02/02: Fix SpotBugs issues.

Posted by gg...@apache.org.
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-io.git

commit c0f4623c95d9e5290c1aa8b16d63eadda6aa7828
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sat Sep 5 20:04:30 2020 -0400

    Fix SpotBugs issues.
    
    - [ERROR] Medium: org.apache.commons.io.file.CleaningPathVisitor doesn't
    override CountingPathVisitor.equals(Object)
    [org.apache.commons.io.file.CleaningPathVisitor] At
    CleaningPathVisitor.java:[line 1] EQ_DOESNT_OVERRIDE_EQUALS
    - [ERROR] Medium: org.apache.commons.io.file.CopyDirectoryVisitor
    doesn't override CountingPathVisitor.equals(Object)
    [org.apache.commons.io.file.CopyDirectoryVisitor] At
    CopyDirectoryVisitor.java:[line 1] EQ_DOESNT_OVERRIDE_EQUALS
    - [ERROR] Medium: org.apache.commons.io.file.DeletingPathVisitor doesn't
    override CountingPathVisitor.equals(Object)
    [org.apache.commons.io.file.DeletingPathVisitor] At
    DeletingPathVisitor.java:[line 1] EQ_DOESNT_OVERRIDE_EQUALS
---
 .../commons/io/file/CleaningPathVisitor.java       | 44 ++++++++++++++++-----
 .../commons/io/file/CopyDirectoryVisitor.java      | 27 +++++++++++++
 .../commons/io/file/DeletingPathVisitor.java       | 46 ++++++++++++++++------
 3 files changed, 96 insertions(+), 21 deletions(-)

diff --git a/src/main/java/org/apache/commons/io/file/CleaningPathVisitor.java b/src/main/java/org/apache/commons/io/file/CleaningPathVisitor.java
index 80546c0..9df929b 100644
--- a/src/main/java/org/apache/commons/io/file/CleaningPathVisitor.java
+++ b/src/main/java/org/apache/commons/io/file/CleaningPathVisitor.java
@@ -60,16 +60,6 @@ public class CleaningPathVisitor extends CountingPathVisitor {
      * Constructs a new visitor that deletes files except for the files and directories explicitly given.
      *
      * @param pathCounter How to count visits.
-     * @param skip The files to skip deleting.
-     */
-    public CleaningPathVisitor(final PathCounters pathCounter, final String... skip) {
-        this(pathCounter, PathUtils.EMPTY_DELETE_OPTION_ARRAY, skip);
-    }
-
-    /**
-     * Constructs a new visitor that deletes files except for the files and directories explicitly given.
-     *
-     * @param pathCounter How to count visits.
      * @param deleteOption options indicating how deletion is handled.
      * @param skip The files to skip deleting.
      * @since 2.8.0
@@ -84,6 +74,16 @@ public class CleaningPathVisitor extends CountingPathVisitor {
     }
 
     /**
+     * Constructs a new visitor that deletes files except for the files and directories explicitly given.
+     *
+     * @param pathCounter How to count visits.
+     * @param skip The files to skip deleting.
+     */
+    public CleaningPathVisitor(final PathCounters pathCounter, final String... skip) {
+        this(pathCounter, PathUtils.EMPTY_DELETE_OPTION_ARRAY, skip);
+    }
+
+    /**
      * Returns true to process the given path, false if not.
      *
      * @param path the path to test.
@@ -94,6 +94,30 @@ public class CleaningPathVisitor extends CountingPathVisitor {
     }
 
     @Override
+    public boolean equals(final Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (!super.equals(obj)) {
+            return false;
+        }
+        if (getClass() != obj.getClass()) {
+            return false;
+        }
+        final CleaningPathVisitor other = (CleaningPathVisitor) obj;
+        return overrideReadOnly == other.overrideReadOnly && Arrays.equals(skip, other.skip);
+    }
+
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = super.hashCode();
+        result = prime * result + Arrays.hashCode(skip);
+        result = prime * result + Objects.hash(overrideReadOnly);
+        return result;
+    }
+
+    @Override
     public FileVisitResult preVisitDirectory(final Path dir, final BasicFileAttributes attributes) throws IOException {
         super.preVisitDirectory(dir, attributes);
         return accept(dir) ? FileVisitResult.CONTINUE : FileVisitResult.SKIP_SUBTREE;
diff --git a/src/main/java/org/apache/commons/io/file/CopyDirectoryVisitor.java b/src/main/java/org/apache/commons/io/file/CopyDirectoryVisitor.java
index 7e46bbb..bb93ba5 100644
--- a/src/main/java/org/apache/commons/io/file/CopyDirectoryVisitor.java
+++ b/src/main/java/org/apache/commons/io/file/CopyDirectoryVisitor.java
@@ -23,6 +23,8 @@ import java.nio.file.FileVisitResult;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.attribute.BasicFileAttributes;
+import java.util.Arrays;
+import java.util.Objects;
 
 import org.apache.commons.io.file.Counters.PathCounters;
 
@@ -67,6 +69,22 @@ public class CopyDirectoryVisitor extends CountingPathVisitor {
         Files.copy(sourceFile, targetFile, copyOptions);
     }
 
+    @Override
+    public boolean equals(final Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (!super.equals(obj)) {
+            return false;
+        }
+        if (getClass() != obj.getClass()) {
+            return false;
+        }
+        final CopyDirectoryVisitor other = (CopyDirectoryVisitor) obj;
+        return Arrays.equals(copyOptions, other.copyOptions) && Objects.equals(sourceDirectory, other.sourceDirectory)
+                && Objects.equals(targetDirectory, other.targetDirectory);
+    }
+
     /**
      * Gets the copy options.
      *
@@ -98,6 +116,15 @@ public class CopyDirectoryVisitor extends CountingPathVisitor {
     }
 
     @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = super.hashCode();
+        result = prime * result + Arrays.hashCode(copyOptions);
+        result = prime * result + Objects.hash(sourceDirectory, targetDirectory);
+        return result;
+    }
+
+    @Override
     public FileVisitResult preVisitDirectory(final Path directory, final BasicFileAttributes attributes)
         throws IOException {
         final Path newTargetDir = targetDirectory.resolve(sourceDirectory.relativize(directory));
diff --git a/src/main/java/org/apache/commons/io/file/DeletingPathVisitor.java b/src/main/java/org/apache/commons/io/file/DeletingPathVisitor.java
index 7437650..d6913a4 100644
--- a/src/main/java/org/apache/commons/io/file/DeletingPathVisitor.java
+++ b/src/main/java/org/apache/commons/io/file/DeletingPathVisitor.java
@@ -61,17 +61,6 @@ public class DeletingPathVisitor extends CountingPathVisitor {
      * Constructs a new visitor that deletes files except for the files and directories explicitly given.
      *
      * @param pathCounter How to count visits.
-     *
-     * @param skip The files to skip deleting.
-     */
-    public DeletingPathVisitor(final PathCounters pathCounter, final String... skip) {
-        this(pathCounter, PathUtils.EMPTY_DELETE_OPTION_ARRAY, skip);
-    }
-
-    /**
-     * Constructs a new visitor that deletes files except for the files and directories explicitly given.
-     *
-     * @param pathCounter How to count visits.
      * @param deleteOption options indicating how deletion is handled.
      * @param skip The files to skip deleting.
      * @since 2.8.0
@@ -85,6 +74,17 @@ public class DeletingPathVisitor extends CountingPathVisitor {
     }
 
     /**
+     * Constructs a new visitor that deletes files except for the files and directories explicitly given.
+     *
+     * @param pathCounter How to count visits.
+     *
+     * @param skip The files to skip deleting.
+     */
+    public DeletingPathVisitor(final PathCounters pathCounter, final String... skip) {
+        this(pathCounter, PathUtils.EMPTY_DELETE_OPTION_ARRAY, skip);
+    }
+
+    /**
      * Returns true to process the given path, false if not.
      *
      * @param path the path to test.
@@ -95,6 +95,30 @@ public class DeletingPathVisitor extends CountingPathVisitor {
     }
 
     @Override
+    public boolean equals(final Object obj) {
+        if (this == obj) {
+            return true;
+        }
+        if (!super.equals(obj)) {
+            return false;
+        }
+        if (getClass() != obj.getClass()) {
+            return false;
+        }
+        final DeletingPathVisitor other = (DeletingPathVisitor) obj;
+        return overrideReadOnly == other.overrideReadOnly && Arrays.equals(skip, other.skip);
+    }
+
+    @Override
+    public int hashCode() {
+        final int prime = 31;
+        int result = super.hashCode();
+        result = prime * result + Arrays.hashCode(skip);
+        result = prime * result + Objects.hash(overrideReadOnly);
+        return result;
+    }
+
+    @Override
     public FileVisitResult postVisitDirectory(final Path dir, final IOException exc) throws IOException {
         if (PathUtils.isEmptyDirectory(dir)) {
             Files.deleteIfExists(dir);