You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by se...@apache.org on 2024/07/27 10:20:41 UTC

(commons-io) branch master updated: Replace assert with JUnit assertions (#648)

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

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


The following commit(s) were added to refs/heads/master by this push:
     new 9ca6fbb5c Replace assert with JUnit assertions (#648)
9ca6fbb5c is described below

commit 9ca6fbb5c3d1220c9b582eeac3b68d1049ead8c6
Author: Sebb <se...@users.noreply.github.com>
AuthorDate: Sat Jul 27 11:20:35 2024 +0100

    Replace assert with JUnit assertions (#648)
    
    * Replace assert with Exception
    
    * Run basic test with experimental Java to reduce false negatives
---
 .github/workflows/maven.yml                                       | 5 +++++
 .../java/org/apache/commons/io/input/ReadAheadInputStream.java    | 8 ++++++--
 .../java/org/apache/commons/io/input/ThrottledInputStream.java    | 8 ++++++--
 3 files changed, 17 insertions(+), 4 deletions(-)

diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml
index 1c698190d..87b00c762 100644
--- a/.github/workflows/maven.yml
+++ b/.github/workflows/maven.yml
@@ -88,7 +88,12 @@ jobs:
         server-username: NEXUS_USER # env variable for username in deploy
         server-password: NEXUS_PW # env variable for token in deploy
     - name: Build with Maven
+      if: ${{ !matrix.experimental }}
       run: mvn --errors --show-version --batch-mode --no-transfer-progress -DtrimStackTrace=false
+    - name: Test only with Maven
+      if: ${{ matrix.experimental }}
+      # Skip PMD etc when using experimental Java
+      run: mvn --errors --show-version --batch-mode --no-transfer-progress -DtrimStackTrace=false clean test
     - name: Deploy SNAPSHOT using minimal build
       if: matrix.deploy && github.repository == 'apache/commons-io' && github.ref_name == 'master' 
       env:
diff --git a/src/main/java/org/apache/commons/io/input/ReadAheadInputStream.java b/src/main/java/org/apache/commons/io/input/ReadAheadInputStream.java
index 3a9fe366e..86dae2d65 100644
--- a/src/main/java/org/apache/commons/io/input/ReadAheadInputStream.java
+++ b/src/main/java/org/apache/commons/io/input/ReadAheadInputStream.java
@@ -485,7 +485,9 @@ public class ReadAheadInputStream extends FilterInputStream {
      * @throws IOException if an I/O error occurs.
      */
     private long skipInternal(final long n) throws IOException {
-        assert stateChangeLock.isLocked();
+        if (!stateChangeLock.isLocked()) {
+            throw new IllegalStateException("Expected stateChangeLock to be locked");
+        }
         waitForAsyncReadComplete();
         if (isEndOfStream()) {
             return 0;
@@ -495,7 +497,9 @@ public class ReadAheadInputStream extends FilterInputStream {
             int toSkip = (int) n;
             // We need to skip from both active buffer and read ahead buffer
             toSkip -= activeBuffer.remaining();
-            assert toSkip > 0; // skipping from activeBuffer already handled.
+            if (toSkip <= 0) { // skipping from activeBuffer already handled.
+                throw new IllegalStateException("Expected toSkip > 0, actual: " + toSkip);
+            }
             activeBuffer.position(0);
             activeBuffer.flip();
             readAheadBuffer.position(toSkip + readAheadBuffer.position());
diff --git a/src/main/java/org/apache/commons/io/input/ThrottledInputStream.java b/src/main/java/org/apache/commons/io/input/ThrottledInputStream.java
index c1826aa9d..55f626978 100644
--- a/src/main/java/org/apache/commons/io/input/ThrottledInputStream.java
+++ b/src/main/java/org/apache/commons/io/input/ThrottledInputStream.java
@@ -127,7 +127,9 @@ public final class ThrottledInputStream extends CountingInputStream {
     }
 
     static long toSleepMillis(final long bytesRead, final long maxBytesPerSec, final long elapsedMillis) {
-        assert elapsedMillis >= 0 : "The elapsed time should be greater or equal to zero";
+        if (elapsedMillis < 0) {
+            throw new IllegalArgumentException("The elapsed time should be greater or equal to zero");
+        }
         if (bytesRead <= 0 || maxBytesPerSec <= 0 || elapsedMillis == 0) {
             return 0;
         }
@@ -147,7 +149,9 @@ public final class ThrottledInputStream extends CountingInputStream {
 
     private ThrottledInputStream(final InputStream proxy, final long maxBytesPerSecond) {
         super(proxy);
-        assert maxBytesPerSecond > 0 : "Bandwidth " + maxBytesPerSecond + " is invalid.";
+        if (maxBytesPerSecond <= 0) {
+            throw new IllegalArgumentException("Bandwidth " + maxBytesPerSecond + " is invalid.");
+        }
         this.maxBytesPerSecond = maxBytesPerSecond;
     }