Skip to content

Commit 9ca6fbb

Browse files
authored
Replace assert with JUnit assertions (#648)
* Replace assert with Exception * Run basic test with experimental Java to reduce false negatives
1 parent 7d5fb89 commit 9ca6fbb

3 files changed

Lines changed: 17 additions & 4 deletions

File tree

.github/workflows/maven.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,12 @@ jobs:
8888
server-username: NEXUS_USER # env variable for username in deploy
8989
server-password: NEXUS_PW # env variable for token in deploy
9090
- name: Build with Maven
91+
if: ${{ !matrix.experimental }}
9192
run: mvn --errors --show-version --batch-mode --no-transfer-progress -DtrimStackTrace=false
93+
- name: Test only with Maven
94+
if: ${{ matrix.experimental }}
95+
# Skip PMD etc when using experimental Java
96+
run: mvn --errors --show-version --batch-mode --no-transfer-progress -DtrimStackTrace=false clean test
9297
- name: Deploy SNAPSHOT using minimal build
9398
if: matrix.deploy && github.repository == 'apache/commons-io' && github.ref_name == 'master'
9499
env:

src/main/java/org/apache/commons/io/input/ReadAheadInputStream.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,9 @@ public long skip(final long n) throws IOException {
485485
* @throws IOException if an I/O error occurs.
486486
*/
487487
private long skipInternal(final long n) throws IOException {
488-
assert stateChangeLock.isLocked();
488+
if (!stateChangeLock.isLocked()) {
489+
throw new IllegalStateException("Expected stateChangeLock to be locked");
490+
}
489491
waitForAsyncReadComplete();
490492
if (isEndOfStream()) {
491493
return 0;
@@ -495,7 +497,9 @@ private long skipInternal(final long n) throws IOException {
495497
int toSkip = (int) n;
496498
// We need to skip from both active buffer and read ahead buffer
497499
toSkip -= activeBuffer.remaining();
498-
assert toSkip > 0; // skipping from activeBuffer already handled.
500+
if (toSkip <= 0) { // skipping from activeBuffer already handled.
501+
throw new IllegalStateException("Expected toSkip > 0, actual: " + toSkip);
502+
}
499503
activeBuffer.position(0);
500504
activeBuffer.flip();
501505
readAheadBuffer.position(toSkip + readAheadBuffer.position());

src/main/java/org/apache/commons/io/input/ThrottledInputStream.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,9 @@ public static Builder builder() {
127127
}
128128

129129
static long toSleepMillis(final long bytesRead, final long maxBytesPerSec, final long elapsedMillis) {
130-
assert elapsedMillis >= 0 : "The elapsed time should be greater or equal to zero";
130+
if (elapsedMillis < 0) {
131+
throw new IllegalArgumentException("The elapsed time should be greater or equal to zero");
132+
}
131133
if (bytesRead <= 0 || maxBytesPerSec <= 0 || elapsedMillis == 0) {
132134
return 0;
133135
}
@@ -147,7 +149,9 @@ static long toSleepMillis(final long bytesRead, final long maxBytesPerSec, final
147149

148150
private ThrottledInputStream(final InputStream proxy, final long maxBytesPerSecond) {
149151
super(proxy);
150-
assert maxBytesPerSecond > 0 : "Bandwidth " + maxBytesPerSecond + " is invalid.";
152+
if (maxBytesPerSecond <= 0) {
153+
throw new IllegalArgumentException("Bandwidth " + maxBytesPerSecond + " is invalid.");
154+
}
151155
this.maxBytesPerSecond = maxBytesPerSecond;
152156
}
153157

0 commit comments

Comments
 (0)