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 2024/07/14 23:09:30 UTC
(commons-io) branch master updated (db86ede34 -> 5a68e2081)
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 db86ede34 Sort members
new 62e2df7e2 Javadoc
new 8fa1c0efb Better internal name
new a17cc99de Use Assertions
new 12fe11e58 Javadoc
new ce73e0d90 Add CircularByteBufferTest.testAddByteSmallestBuffer()
new 9dd7f2d99 Add CircularByteBufferTest.testHasSpace()
new 5a68e2081 Add CircularByteBufferTest.testReadByteArray()
The 7 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:
.../io/input/buffer/CircularByteBuffer.java | 17 +++---
.../buffer/CircularBufferInputStreamTest.java | 47 +++++++++--------
.../io/input/buffer/CircularByteBufferTest.java | 60 ++++++++++++++++++++++
3 files changed, 94 insertions(+), 30 deletions(-)
(commons-io) 05/07: Add CircularByteBufferTest.testAddByteSmallestBuffer()
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 ce73e0d90f4b529c40eb0f272839f0d7d61aaf34
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Jul 14 18:36:33 2024 -0400
Add CircularByteBufferTest.testAddByteSmallestBuffer()
---
.../apache/commons/io/input/buffer/CircularByteBufferTest.java | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/src/test/java/org/apache/commons/io/input/buffer/CircularByteBufferTest.java b/src/test/java/org/apache/commons/io/input/buffer/CircularByteBufferTest.java
index d596f3395..dd430ce41 100644
--- a/src/test/java/org/apache/commons/io/input/buffer/CircularByteBufferTest.java
+++ b/src/test/java/org/apache/commons/io/input/buffer/CircularByteBufferTest.java
@@ -28,6 +28,15 @@ import org.junit.jupiter.api.Test;
*/
public class CircularByteBufferTest {
+ @Test
+ public void testAddByteSmallestBuffer() {
+ final CircularByteBuffer cbb = new CircularByteBuffer(1);
+ cbb.add((byte) 1);
+ assertEquals(1, cbb.read());
+ cbb.add((byte) 2);
+ assertEquals(2, cbb.read());
+ }
+
@Test
public void testAddInvalidOffset() {
final CircularByteBuffer cbb = new CircularByteBuffer();
(commons-io) 07/07: Add CircularByteBufferTest.testReadByteArray()
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 5a68e208189ebe1b8a9f2eabea94ae0b8a0d99ca
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Jul 14 18:58:23 2024 -0400
Add CircularByteBufferTest.testReadByteArray()
Add CircularByteBufferTest.testHasSpaceInt()
---
.../io/input/buffer/CircularByteBufferTest.java | 37 ++++++++++++++++++++++
1 file changed, 37 insertions(+)
diff --git a/src/test/java/org/apache/commons/io/input/buffer/CircularByteBufferTest.java b/src/test/java/org/apache/commons/io/input/buffer/CircularByteBufferTest.java
index dacd16fe1..da92e1dee 100644
--- a/src/test/java/org/apache/commons/io/input/buffer/CircularByteBufferTest.java
+++ b/src/test/java/org/apache/commons/io/input/buffer/CircularByteBufferTest.java
@@ -21,6 +21,8 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
+import java.nio.charset.StandardCharsets;
+
import org.junit.jupiter.api.Test;
/**
@@ -101,6 +103,20 @@ public class CircularByteBufferTest {
assertTrue(cbb.hasSpace());
}
+ @Test
+ public void testHasSpaceInt() {
+ final CircularByteBuffer cbb = new CircularByteBuffer(1);
+ assertTrue(cbb.hasSpace(1));
+ cbb.add((byte) 1);
+ assertFalse(cbb.hasSpace(1));
+ assertEquals(1, cbb.read());
+ assertTrue(cbb.hasSpace(1));
+ cbb.add((byte) 2);
+ assertFalse(cbb.hasSpace(1));
+ assertEquals(2, cbb.read());
+ assertTrue(cbb.hasSpace(1));
+ }
+
@Test
public void testPeekWithExcessiveLength() {
assertFalse(new CircularByteBuffer().peek(new byte[] { 1, 3, 5, 7, 9 }, 0, 6));
@@ -125,4 +141,25 @@ public class CircularByteBufferTest {
public void testPeekWithValidArguments() {
assertFalse(new CircularByteBuffer().peek(new byte[] { 5, 10, 15, 20, 25 }, 0, 5));
}
+
+ @Test
+ public void testReadByteArray() {
+ final CircularByteBuffer cbb = new CircularByteBuffer();
+ final String string = "0123456789";
+ final byte[] bytesIn = string.getBytes(StandardCharsets.UTF_8);
+ cbb.add(bytesIn, 0, 10);
+ final byte[] bytesOut = new byte[10];
+ cbb.read(bytesOut, 0, 10);
+ assertEquals(string, new String(bytesOut, StandardCharsets.UTF_8));
+ }
+
+ @Test
+ public void testReadByteArrayIllegalArgumentException() {
+ final CircularByteBuffer cbb = new CircularByteBuffer();
+ final byte[] bytesOut = new byte[10];
+ // targetOffset < 0
+ assertThrows(IllegalArgumentException.class, () -> cbb.read(bytesOut, -1, 10));
+ // targetOffset >= targetBuffer.length
+ assertThrows(IllegalArgumentException.class, () -> cbb.read(bytesOut, 0, bytesOut.length + 1));
+ }
}
(commons-io) 01/07: Javadoc
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 62e2df7e238ae44cda0dc21deb535b20aee05dd7
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Jul 14 17:58:56 2024 -0400
Javadoc
---
.../commons/io/input/buffer/CircularBufferInputStreamTest.java | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)
diff --git a/src/test/java/org/apache/commons/io/input/buffer/CircularBufferInputStreamTest.java b/src/test/java/org/apache/commons/io/input/buffer/CircularBufferInputStreamTest.java
index a08405e40..7fe3e96c9 100644
--- a/src/test/java/org/apache/commons/io/input/buffer/CircularBufferInputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/input/buffer/CircularBufferInputStreamTest.java
@@ -25,9 +25,15 @@ import java.util.Random;
import org.junit.jupiter.api.Test;
+/**
+ * Tests {@link CircularBufferInputStream}.
+ */
public class CircularBufferInputStreamTest {
- private final Random rnd = new Random(1530960934483L); // System.currentTimeMillis(), when this test was written.
- // Always using the same seed should ensure a reproducible test.
+
+ /**
+ * System.currentTimeMillis(), when this test was written. Always using the same seed should ensure a reproducible test.
+ */
+ private final Random rnd = new Random(1530960934483L);
/**
* Create a large, but random input buffer.
(commons-io) 04/07: Javadoc
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 12fe11e5814c17475b791132cd5f6636ccb52c21
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Jul 14 18:21:51 2024 -0400
Javadoc
---
.../commons/io/input/buffer/CircularByteBuffer.java | 17 +++++++++--------
1 file changed, 9 insertions(+), 8 deletions(-)
diff --git a/src/main/java/org/apache/commons/io/input/buffer/CircularByteBuffer.java b/src/main/java/org/apache/commons/io/input/buffer/CircularByteBuffer.java
index 6ad14b399..84cf1e03c 100644
--- a/src/main/java/org/apache/commons/io/input/buffer/CircularByteBuffer.java
+++ b/src/main/java/org/apache/commons/io/input/buffer/CircularByteBuffer.java
@@ -30,6 +30,7 @@ import org.apache.commons.io.IOUtils;
* @since 2.7
*/
public class CircularByteBuffer {
+
private final byte[] buffer;
private int startOffset;
private int endOffset;
@@ -116,7 +117,7 @@ public class CircularByteBuffer {
}
/**
- * Returns the number of bytes, that are currently present in the buffer.
+ * Gets the number of bytes, that are currently present in the buffer.
*
* @return the number of bytes
*/
@@ -125,7 +126,7 @@ public class CircularByteBuffer {
}
/**
- * Returns the number of bytes, that can currently be added to the buffer.
+ * Gets the number of bytes, that can currently be added to the buffer.
*
* @return the number of bytes that can be added
*/
@@ -134,19 +135,19 @@ public class CircularByteBuffer {
}
/**
- * Returns, whether the buffer is currently holding, at least, a single byte.
+ * Tests whether the buffer is currently holding at least a single byte.
*
- * @return true if the buffer is not empty
+ * @return true whether the buffer is currently holding at least a single byte.
*/
public boolean hasBytes() {
return currentNumberOfBytes > 0;
}
/**
- * Returns, whether there is currently room for a single byte in the buffer.
+ * Tests whether there is currently room for a single byte in the buffer.
* Same as {@link #hasSpace(int) hasSpace(1)}.
*
- * @return true if there is space for a byte
+ * @return true whether there is currently room for a single byte in the buffer.
* @see #hasSpace(int)
* @see #getSpace()
*/
@@ -155,10 +156,10 @@ public class CircularByteBuffer {
}
/**
- * Returns, whether there is currently room for the given number of bytes in the buffer.
+ * Tests whether there is currently room for the given number of bytes in the buffer.
*
* @param count the byte count
- * @return true if there is space for the given number of bytes
+ * @return true whether there is currently room for the given number of bytes in the buffer.
* @see #hasSpace()
* @see #getSpace()
*/
(commons-io) 02/07: Better internal name
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 8fa1c0efb7d3268bd73b23089fccba17e72bc24f
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Jul 14 17:59:13 2024 -0400
Better internal name
---
.../commons/io/input/buffer/CircularBufferInputStreamTest.java | 10 +++++-----
1 file changed, 5 insertions(+), 5 deletions(-)
diff --git a/src/test/java/org/apache/commons/io/input/buffer/CircularBufferInputStreamTest.java b/src/test/java/org/apache/commons/io/input/buffer/CircularBufferInputStreamTest.java
index 7fe3e96c9..37e404d26 100644
--- a/src/test/java/org/apache/commons/io/input/buffer/CircularBufferInputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/input/buffer/CircularBufferInputStreamTest.java
@@ -33,14 +33,14 @@ public class CircularBufferInputStreamTest {
/**
* System.currentTimeMillis(), when this test was written. Always using the same seed should ensure a reproducible test.
*/
- private final Random rnd = new Random(1530960934483L);
+ private final Random random = new Random(1530960934483L);
/**
* Create a large, but random input buffer.
*/
private byte[] newInputBuffer() {
- final byte[] buffer = new byte[16 * 512 + rnd.nextInt(512)];
- rnd.nextBytes(buffer);
+ final byte[] buffer = new byte[16 * 512 + random.nextInt(512)];
+ random.nextBytes(buffer);
return buffer;
}
@@ -68,7 +68,7 @@ public class CircularBufferInputStreamTest {
int offset = 0;
final byte[] readBuffer = new byte[256];
while (offset < bufferCopy.length) {
- switch (rnd.nextInt(2)) {
+ switch (random.nextInt(2)) {
case 0: {
final int res = cbis.read();
if (res == -1) {
@@ -81,7 +81,7 @@ public class CircularBufferInputStreamTest {
break;
}
case 1: {
- final int res = cbis.read(readBuffer, 0, rnd.nextInt(readBuffer.length + 1));
+ final int res = cbis.read(readBuffer, 0, random.nextInt(readBuffer.length + 1));
if (res == -1) {
throw new IllegalStateException("Unexpected EOF at offset " + offset);
}
(commons-io) 03/07: Use Assertions
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 a17cc99de2f10874fc8b9b161d75781bca47dfbf
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Jul 14 18:11:55 2024 -0400
Use Assertions
---
.../buffer/CircularBufferInputStreamTest.java | 29 ++++++++++------------
1 file changed, 13 insertions(+), 16 deletions(-)
diff --git a/src/test/java/org/apache/commons/io/input/buffer/CircularBufferInputStreamTest.java b/src/test/java/org/apache/commons/io/input/buffer/CircularBufferInputStreamTest.java
index 37e404d26..55d38f25c 100644
--- a/src/test/java/org/apache/commons/io/input/buffer/CircularBufferInputStreamTest.java
+++ b/src/test/java/org/apache/commons/io/input/buffer/CircularBufferInputStreamTest.java
@@ -17,7 +17,9 @@
package org.apache.commons.io.input.buffer;
import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assertions.fail;
import java.io.ByteArrayInputStream;
import java.io.IOException;
@@ -35,6 +37,10 @@ public class CircularBufferInputStreamTest {
*/
private final Random random = new Random(1530960934483L);
+ void asssertNotEof(int offset, final int res) {
+ assertNotEquals(-1, res, () -> "Unexpected EOF at offset " + offset);
+ }
+
/**
* Create a large, but random input buffer.
*/
@@ -71,33 +77,24 @@ public class CircularBufferInputStreamTest {
switch (random.nextInt(2)) {
case 0: {
final int res = cbis.read();
- if (res == -1) {
- throw new IllegalStateException("Unexpected EOF at offset " + offset);
- }
- if (inputBuffer[offset] != (byte) res) { // compare as bytes
- throw new IllegalStateException("Expected " + inputBuffer[offset] + " at offset " + offset + ", got " + res);
- }
+ asssertNotEof(offset, res);
+ // MUST compare bytes
+ assertEquals(inputBuffer[offset], (byte) res, "Expected " + inputBuffer[offset] + " at offset " + offset + ", got " + res);
++offset;
break;
}
case 1: {
final int res = cbis.read(readBuffer, 0, random.nextInt(readBuffer.length + 1));
- if (res == -1) {
- throw new IllegalStateException("Unexpected EOF at offset " + offset);
- }
- if (res == 0) {
- throw new IllegalStateException("Unexpected zero-byte-result at offset " + offset);
- }
+ asssertNotEof(offset, res);
+ assertNotEquals(0, res, "Unexpected zero-byte-result at offset " + offset);
for (int i = 0; i < res; i++) {
- if (inputBuffer[offset] != readBuffer[i]) {
- throw new IllegalStateException("Expected " + inputBuffer[offset] + " at offset " + offset + ", got " + readBuffer[i]);
- }
+ assertEquals(inputBuffer[offset], readBuffer[i], "Expected " + inputBuffer[offset] + " at offset " + offset + ", got " + readBuffer[i]);
++offset;
}
break;
}
default:
- throw new IllegalStateException("Unexpected random choice value");
+ fail("Unexpected random choice value");
}
}
assertTrue(true, "Test finished OK");
(commons-io) 06/07: Add CircularByteBufferTest.testHasSpace()
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 9dd7f2d99a4af326a9249f9538fc6910cc98e37b
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Jul 14 18:39:25 2024 -0400
Add CircularByteBufferTest.testHasSpace()
---
.../commons/io/input/buffer/CircularByteBufferTest.java | 14 ++++++++++++++
1 file changed, 14 insertions(+)
diff --git a/src/test/java/org/apache/commons/io/input/buffer/CircularByteBufferTest.java b/src/test/java/org/apache/commons/io/input/buffer/CircularByteBufferTest.java
index dd430ce41..dacd16fe1 100644
--- a/src/test/java/org/apache/commons/io/input/buffer/CircularByteBufferTest.java
+++ b/src/test/java/org/apache/commons/io/input/buffer/CircularByteBufferTest.java
@@ -87,6 +87,20 @@ public class CircularByteBufferTest {
assertTrue(buffer.hasSpace());
}
+ @Test
+ public void testHasSpace() {
+ final CircularByteBuffer cbb = new CircularByteBuffer(1);
+ assertTrue(cbb.hasSpace());
+ cbb.add((byte) 1);
+ assertFalse(cbb.hasSpace());
+ assertEquals(1, cbb.read());
+ assertTrue(cbb.hasSpace());
+ cbb.add((byte) 2);
+ assertFalse(cbb.hasSpace());
+ assertEquals(2, cbb.read());
+ assertTrue(cbb.hasSpace());
+ }
+
@Test
public void testPeekWithExcessiveLength() {
assertFalse(new CircularByteBuffer().peek(new byte[] { 1, 3, 5, 7, 9 }, 0, 6));