aboutsummaryrefslogtreecommitdiff
path: root/java/core/src/test/java/com/google/protobuf/NioByteStringTest.java
diff options
context:
space:
mode:
Diffstat (limited to 'java/core/src/test/java/com/google/protobuf/NioByteStringTest.java')
-rw-r--r--java/core/src/test/java/com/google/protobuf/NioByteStringTest.java323
1 files changed, 171 insertions, 152 deletions
diff --git a/java/core/src/test/java/com/google/protobuf/NioByteStringTest.java b/java/core/src/test/java/com/google/protobuf/NioByteStringTest.java
index c388bd05..4a68c8b8 100644
--- a/java/core/src/test/java/com/google/protobuf/NioByteStringTest.java
+++ b/java/core/src/test/java/com/google/protobuf/NioByteStringTest.java
@@ -48,9 +48,7 @@ import java.util.List;
import java.util.NoSuchElementException;
import junit.framework.TestCase;
-/**
- * Tests for {@link NioByteString}.
- */
+/** Tests for {@link NioByteString}. */
public class NioByteStringTest extends TestCase {
private static final ByteString EMPTY = new NioByteString(ByteBuffer.wrap(new byte[0]));
private static final String CLASSNAME = NioByteString.class.getSimpleName();
@@ -108,8 +106,7 @@ public class NioByteStringTest extends TestCase {
}
public void testSize() {
- assertEquals(CLASSNAME + " must have the expected size", BYTES.length,
- testString.size());
+ assertEquals(CLASSNAME + " must have the expected size", BYTES.length, testString.size());
}
public void testGetTreeDepth() {
@@ -140,10 +137,8 @@ public class NioByteStringTest extends TestCase {
try {
// Copy one too many bytes
- testString.copyTo(destination, testString.size() + 1 - length,
- destinationOffset, length);
- fail("Should have thrown an exception when copying too many bytes of a "
- + CLASSNAME);
+ testString.copyTo(destination, testString.size() + 1 - length, destinationOffset, length);
+ fail("Should have thrown an exception when copying too many bytes of a " + CLASSNAME);
} catch (IndexOutOfBoundsException expected) {
// This is success
}
@@ -151,8 +146,7 @@ public class NioByteStringTest extends TestCase {
try {
// Copy with illegal negative sourceOffset
testString.copyTo(destination, -1, destinationOffset, length);
- fail("Should have thrown an exception when given a negative sourceOffset in "
- + CLASSNAME);
+ fail("Should have thrown an exception when given a negative sourceOffset in " + CLASSNAME);
} catch (IndexOutOfBoundsException expected) {
// This is success
}
@@ -160,8 +154,9 @@ public class NioByteStringTest extends TestCase {
try {
// Copy with illegal negative destinationOffset
testString.copyTo(destination, 0, -1, length);
- fail("Should have thrown an exception when given a negative destinationOffset in "
- + CLASSNAME);
+ fail(
+ "Should have thrown an exception when given a negative destinationOffset in "
+ + CLASSNAME);
} catch (IndexOutOfBoundsException expected) {
// This is success
}
@@ -169,8 +164,7 @@ public class NioByteStringTest extends TestCase {
try {
// Copy with illegal negative size
testString.copyTo(destination, 0, 0, -1);
- fail("Should have thrown an exception when given a negative size in "
- + CLASSNAME);
+ fail("Should have thrown an exception when given a negative size in " + CLASSNAME);
} catch (IndexOutOfBoundsException expected) {
// This is success
}
@@ -178,8 +172,9 @@ public class NioByteStringTest extends TestCase {
try {
// Copy with illegal too-large sourceOffset
testString.copyTo(destination, 2 * testString.size(), 0, length);
- fail("Should have thrown an exception when the destinationOffset is too large in "
- + CLASSNAME);
+ fail(
+ "Should have thrown an exception when the destinationOffset is too large in "
+ + CLASSNAME);
} catch (IndexOutOfBoundsException expected) {
// This is success
}
@@ -187,8 +182,9 @@ public class NioByteStringTest extends TestCase {
try {
// Copy with illegal too-large destinationOffset
testString.copyTo(destination, 0, 2 * destination.length, length);
- fail("Should have thrown an exception when the destinationOffset is too large in "
- + CLASSNAME);
+ fail(
+ "Should have thrown an exception when the destinationOffset is too large in "
+ + CLASSNAME);
} catch (IndexOutOfBoundsException expected) {
// This is success
}
@@ -199,8 +195,8 @@ public class NioByteStringTest extends TestCase {
ByteBuffer myBuffer = ByteBuffer.allocate(BYTES.length);
testString.copyTo(myBuffer);
myBuffer.flip();
- assertEquals(CLASSNAME + ".copyTo(ByteBuffer) must give back the same bytes",
- backingBuffer, myBuffer);
+ assertEquals(
+ CLASSNAME + ".copyTo(ByteBuffer) must give back the same bytes", backingBuffer, myBuffer);
// Target buffer bigger than required.
myBuffer = ByteBuffer.allocate(testString.size() + 1);
@@ -241,30 +237,31 @@ public class NioByteStringTest extends TestCase {
skipFully(stream, fraction); // Skip a large fraction, but not all.
assertEquals(
CLASSNAME + ": after skipping to the 'middle', half the bytes are available",
- (testString.size() - fraction), stream.available());
+ (testString.size() - fraction),
+ stream.available());
stream.reset();
assertEquals(
CLASSNAME + ": after resetting, all bytes are available",
- testString.size(), stream.available());
+ testString.size(),
+ stream.available());
skipFully(stream, testString.size()); // Skip to the end.
assertEquals(
CLASSNAME + ": after skipping to the end, no more bytes are available",
- 0, stream.available());
+ 0,
+ stream.available());
}
/**
- * Discards {@code n} bytes of data from the input stream. This method
- * will block until the full amount has been skipped. Does not close the
- * stream.
+ * Discards {@code n} bytes of data from the input stream. This method will block until the full
+ * amount has been skipped. Does not close the stream.
+ *
* <p>Copied from com.google.common.io.ByteStreams to avoid adding dependency.
*
* @param in the input stream to read from
* @param n the number of bytes to skip
- * @throws EOFException if this stream reaches the end before skipping all
- * the bytes
- * @throws IOException if an I/O error occurs, or the stream does not
- * support skipping
+ * @throws EOFException if this stream reaches the end before skipping all the bytes
+ * @throws IOException if an I/O error occurs, or the stream does not support skipping
*/
static void skipFully(InputStream in, long n) throws IOException {
long toSkip = n;
@@ -274,8 +271,12 @@ public class NioByteStringTest extends TestCase {
// Force a blocking read to avoid infinite loop
if (in.read() == -1) {
long skipped = toSkip - n;
- throw new EOFException("reached end of stream after skipping "
- + skipped + " bytes; " + toSkip + " bytes expected");
+ throw new EOFException(
+ "reached end of stream after skipping "
+ + skipped
+ + " bytes; "
+ + toSkip
+ + " bytes expected");
}
n--;
} else {
@@ -290,7 +291,8 @@ public class NioByteStringTest extends TestCase {
assertTrue(byteBuffer.remaining() == BYTES.length);
assertTrue(byteBuffer.isReadOnly());
byteBuffer.get(roundTripBytes);
- assertTrue(CLASSNAME + ".asReadOnlyByteBuffer() must give back the same bytes",
+ assertTrue(
+ CLASSNAME + ".asReadOnlyByteBuffer() must give back the same bytes",
Arrays.equals(BYTES, roundTripBytes));
}
@@ -306,13 +308,15 @@ public class NioByteStringTest extends TestCase {
bytesSeen += thisLength;
}
assertTrue(bytesSeen == BYTES.length);
- assertTrue(CLASSNAME + ".asReadOnlyByteBufferTest() must give back the same bytes",
+ assertTrue(
+ CLASSNAME + ".asReadOnlyByteBufferTest() must give back the same bytes",
Arrays.equals(BYTES, roundTripBytes));
}
public void testToByteArray() {
byte[] roundTripBytes = testString.toByteArray();
- assertTrue(CLASSNAME + ".toByteArray() must give back the same bytes",
+ assertTrue(
+ CLASSNAME + ".toByteArray() must give back the same bytes",
Arrays.equals(BYTES, roundTripBytes));
}
@@ -320,80 +324,87 @@ public class NioByteStringTest extends TestCase {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
testString.writeTo(bos);
byte[] roundTripBytes = bos.toByteArray();
- assertTrue(CLASSNAME + ".writeTo() must give back the same bytes",
+ assertTrue(
+ CLASSNAME + ".writeTo() must give back the same bytes",
Arrays.equals(BYTES, roundTripBytes));
}
public void testWriteToShouldNotExposeInternalBufferToOutputStream() throws IOException {
- OutputStream os = new OutputStream() {
- @Override
- public void write(byte[] b, int off, int len) {
- Arrays.fill(b, off, off + len, (byte) 0);
- }
-
- @Override
- public void write(int b) {
- throw new UnsupportedOperationException();
- }
- };
+ OutputStream os =
+ new OutputStream() {
+ @Override
+ public void write(byte[] b, int off, int len) {
+ Arrays.fill(b, off, off + len, (byte) 0);
+ }
+
+ @Override
+ public void write(int b) {
+ throw new UnsupportedOperationException();
+ }
+ };
byte[] original = Arrays.copyOf(BYTES, BYTES.length);
testString.writeTo(os);
- assertTrue(CLASSNAME + ".writeTo() must NOT grant access to underlying buffer",
+ assertTrue(
+ CLASSNAME + ".writeTo() must NOT grant access to underlying buffer",
Arrays.equals(original, BYTES));
}
public void testWriteToInternalShouldExposeInternalBufferToOutputStream() throws IOException {
- OutputStream os = new OutputStream() {
- @Override
- public void write(byte[] b, int off, int len) {
- Arrays.fill(b, off, off + len, (byte) 0);
- }
-
- @Override
- public void write(int b) {
- throw new UnsupportedOperationException();
- }
- };
+ OutputStream os =
+ new OutputStream() {
+ @Override
+ public void write(byte[] b, int off, int len) {
+ Arrays.fill(b, off, off + len, (byte) 0);
+ }
+
+ @Override
+ public void write(int b) {
+ throw new UnsupportedOperationException();
+ }
+ };
testString.writeToInternal(os, 0, testString.size());
byte[] allZeros = new byte[testString.size()];
- assertTrue(CLASSNAME + ".writeToInternal() must grant access to underlying buffer",
+ assertTrue(
+ CLASSNAME + ".writeToInternal() must grant access to underlying buffer",
Arrays.equals(allZeros, backingBuffer.array()));
}
public void testWriteToShouldExposeInternalBufferToByteOutput() throws IOException {
- ByteOutput out = new ByteOutput() {
- @Override
- public void write(byte value) throws IOException {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public void write(byte[] value, int offset, int length) throws IOException {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public void writeLazy(byte[] value, int offset, int length) throws IOException {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public void write(ByteBuffer value) throws IOException {
- throw new UnsupportedOperationException();
- }
-
- @Override
- public void writeLazy(ByteBuffer value) throws IOException {
- Arrays.fill(value.array(), value.arrayOffset(), value.arrayOffset() + value.limit(),
- (byte) 0);
- }
- };
+ ByteOutput out =
+ new ByteOutput() {
+ @Override
+ public void write(byte value) throws IOException {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void write(byte[] value, int offset, int length) throws IOException {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void writeLazy(byte[] value, int offset, int length) throws IOException {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void write(ByteBuffer value) throws IOException {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public void writeLazy(ByteBuffer value) throws IOException {
+ Arrays.fill(
+ value.array(), value.arrayOffset(), value.arrayOffset() + value.limit(), (byte) 0);
+ }
+ };
testString.writeTo(out);
byte[] allZeros = new byte[testString.size()];
- assertTrue(CLASSNAME + ".writeTo() must grant access to underlying buffer",
+ assertTrue(
+ CLASSNAME + ".writeTo() must grant access to underlying buffer",
Arrays.equals(allZeros, backingBuffer.array()));
}
@@ -401,21 +412,21 @@ public class NioByteStringTest extends TestCase {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ByteString.Output output = ByteString.newOutput();
testString.writeTo(output);
- assertEquals("Output Size returns correct result",
- output.size(), testString.size());
+ assertEquals("Output Size returns correct result", output.size(), testString.size());
output.writeTo(bos);
- assertTrue("Output.writeTo() must give back the same bytes",
- Arrays.equals(BYTES, bos.toByteArray()));
+ assertTrue(
+ "Output.writeTo() must give back the same bytes", Arrays.equals(BYTES, bos.toByteArray()));
// write the output stream to itself! This should cause it to double
output.writeTo(output);
- assertEquals("Writing an output stream to itself is successful",
- testString.concat(testString), output.toByteString());
+ assertEquals(
+ "Writing an output stream to itself is successful",
+ testString.concat(testString),
+ output.toByteString());
output.reset();
assertEquals("Output.reset() resets the output", 0, output.size());
- assertEquals("Output.reset() resets the output",
- EMPTY, output.toByteString());
+ assertEquals("Output.reset() resets the output", EMPTY, output.toByteString());
}
public void testToString() {
@@ -433,7 +444,8 @@ public class NioByteStringTest extends TestCase {
}
public void testToString_returnsCanonicalEmptyString() {
- assertSame(CLASSNAME + " must be the same string references",
+ assertSame(
+ CLASSNAME + " must be the same string references",
EMPTY.toString(UTF_8),
new NioByteString(ByteBuffer.wrap(new byte[0])).toString(UTF_8));
}
@@ -457,33 +469,34 @@ public class NioByteStringTest extends TestCase {
public void testEquals() {
assertEquals(CLASSNAME + " must not equal null", false, testString.equals(null));
assertEquals(CLASSNAME + " must equal self", testString, testString);
- assertFalse(CLASSNAME + " must not equal the empty string",
- testString.equals(EMPTY));
- assertEquals(CLASSNAME + " empty strings must be equal",
- EMPTY, testString.substring(55, 55));
- assertEquals(CLASSNAME + " must equal another string with the same value",
- testString, new NioByteString(backingBuffer));
+ assertFalse(CLASSNAME + " must not equal the empty string", testString.equals(EMPTY));
+ assertEquals(CLASSNAME + " empty strings must be equal", EMPTY, testString.substring(55, 55));
+ assertEquals(
+ CLASSNAME + " must equal another string with the same value",
+ testString,
+ new NioByteString(backingBuffer));
byte[] mungedBytes = mungedBytes();
- assertFalse(CLASSNAME + " must not equal every string with the same length",
+ assertFalse(
+ CLASSNAME + " must not equal every string with the same length",
testString.equals(new NioByteString(ByteBuffer.wrap(mungedBytes))));
}
public void testEqualsLiteralByteString() {
ByteString literal = ByteString.copyFrom(BYTES);
- assertEquals(CLASSNAME + " must equal LiteralByteString with same value", literal,
- testString);
- assertEquals(CLASSNAME + " must equal LiteralByteString with same value", testString,
- literal);
- assertFalse(CLASSNAME + " must not equal the empty string",
- testString.equals(ByteString.EMPTY));
- assertEquals(CLASSNAME + " empty strings must be equal",
- ByteString.EMPTY, testString.substring(55, 55));
+ assertEquals(CLASSNAME + " must equal LiteralByteString with same value", literal, testString);
+ assertEquals(CLASSNAME + " must equal LiteralByteString with same value", testString, literal);
+ assertFalse(
+ CLASSNAME + " must not equal the empty string", testString.equals(ByteString.EMPTY));
+ assertEquals(
+ CLASSNAME + " empty strings must be equal", ByteString.EMPTY, testString.substring(55, 55));
literal = ByteString.copyFrom(mungedBytes());
- assertFalse(CLASSNAME + " must not equal every LiteralByteString with the same length",
+ assertFalse(
+ CLASSNAME + " must not equal every LiteralByteString with the same length",
testString.equals(literal));
- assertFalse(CLASSNAME + " must not equal every LiteralByteString with the same length",
+ assertFalse(
+ CLASSNAME + " must not equal every LiteralByteString with the same length",
literal.equals(testString));
}
@@ -492,22 +505,25 @@ public class NioByteStringTest extends TestCase {
ByteString p2 = ByteString.copyFrom(BYTES, 5, BYTES.length - 5);
ByteString rope = p1.concat(p2);
- assertEquals(CLASSNAME + " must equal RopeByteString with same value", rope,
- testString);
- assertEquals(CLASSNAME + " must equal RopeByteString with same value", testString,
- rope);
- assertFalse(CLASSNAME + " must not equal the empty string",
+ assertEquals(CLASSNAME + " must equal RopeByteString with same value", rope, testString);
+ assertEquals(CLASSNAME + " must equal RopeByteString with same value", testString, rope);
+ assertFalse(
+ CLASSNAME + " must not equal the empty string",
testString.equals(ByteString.EMPTY.concat(ByteString.EMPTY)));
- assertEquals(CLASSNAME + " empty strings must be equal",
- ByteString.EMPTY.concat(ByteString.EMPTY), testString.substring(55, 55));
+ assertEquals(
+ CLASSNAME + " empty strings must be equal",
+ ByteString.EMPTY.concat(ByteString.EMPTY),
+ testString.substring(55, 55));
byte[] mungedBytes = mungedBytes();
p1 = ByteString.copyFrom(mungedBytes, 0, 5);
p2 = ByteString.copyFrom(mungedBytes, 5, mungedBytes.length - 5);
rope = p1.concat(p2);
- assertFalse(CLASSNAME + " must not equal every RopeByteString with the same length",
+ assertFalse(
+ CLASSNAME + " must not equal every RopeByteString with the same length",
testString.equals(rope));
- assertFalse(CLASSNAME + " must not equal every RopeByteString with the same length",
+ assertFalse(
+ CLASSNAME + " must not equal every RopeByteString with the same length",
rope.equals(testString));
}
@@ -525,32 +541,34 @@ public class NioByteStringTest extends TestCase {
public void testPeekCachedHashCode() {
ByteString newString = new NioByteString(backingBuffer);
- assertEquals(CLASSNAME + ".peekCachedHashCode() should return zero at first", 0,
+ assertEquals(
+ CLASSNAME + ".peekCachedHashCode() should return zero at first",
+ 0,
newString.peekCachedHashCode());
newString.hashCode();
- assertEquals(CLASSNAME + ".peekCachedHashCode should return zero at first",
- EXPECTED_HASH, newString.peekCachedHashCode());
+ assertEquals(
+ CLASSNAME + ".peekCachedHashCode should return zero at first",
+ EXPECTED_HASH,
+ newString.peekCachedHashCode());
}
public void testPartialHash() {
// partialHash() is more strenuously tested elsewhere by testing hashes of substrings.
// This test would fail if the expected hash were 1. It's not.
int hash = testString.partialHash(testString.size(), 0, testString.size());
- assertEquals(CLASSNAME + ".partialHash() must yield expected hashCode",
- EXPECTED_HASH, hash);
+ assertEquals(CLASSNAME + ".partialHash() must yield expected hashCode", EXPECTED_HASH, hash);
}
public void testNewInput() throws IOException {
InputStream input = testString.newInput();
- assertEquals("InputStream.available() returns correct value",
- testString.size(), input.available());
+ assertEquals(
+ "InputStream.available() returns correct value", testString.size(), input.available());
boolean stillEqual = true;
for (byte referenceByte : BYTES) {
int expectedInt = (referenceByte & 0xFF);
stillEqual = (expectedInt == input.read());
}
- assertEquals("InputStream.available() returns correct value",
- 0, input.available());
+ assertEquals("InputStream.available() returns correct value", 0, input.available());
assertTrue(CLASSNAME + " must give the same bytes from the InputStream", stillEqual);
assertEquals(CLASSNAME + " InputStream must now be exhausted", -1, input.read());
}
@@ -561,43 +579,44 @@ public class NioByteStringTest extends TestCase {
int nearEndIndex = stringSize * 2 / 3;
long skipped1 = input.skip(nearEndIndex);
assertEquals("InputStream.skip()", skipped1, nearEndIndex);
- assertEquals("InputStream.available()",
- stringSize - skipped1, input.available());
+ assertEquals("InputStream.available()", stringSize - skipped1, input.available());
assertTrue("InputStream.mark() is available", input.markSupported());
input.mark(0);
- assertEquals("InputStream.skip(), read()",
- testString.byteAt(nearEndIndex) & 0xFF, input.read());
- assertEquals("InputStream.available()",
- stringSize - skipped1 - 1, input.available());
+ assertEquals(
+ "InputStream.skip(), read()", testString.byteAt(nearEndIndex) & 0xFF, input.read());
+ assertEquals("InputStream.available()", stringSize - skipped1 - 1, input.available());
long skipped2 = input.skip(stringSize);
- assertEquals("InputStream.skip() incomplete",
- skipped2, stringSize - skipped1 - 1);
+ assertEquals("InputStream.skip() incomplete", skipped2, stringSize - skipped1 - 1);
assertEquals("InputStream.skip(), no more input", 0, input.available());
assertEquals("InputStream.skip(), no more input", -1, input.read());
input.reset();
- assertEquals("InputStream.reset() succeded",
- stringSize - skipped1, input.available());
- assertEquals("InputStream.reset(), read()",
- testString.byteAt(nearEndIndex) & 0xFF, input.read());
+ assertEquals("InputStream.reset() succeded", stringSize - skipped1, input.available());
+ assertEquals(
+ "InputStream.reset(), read()", testString.byteAt(nearEndIndex) & 0xFF, input.read());
}
public void testNewCodedInput() throws IOException {
CodedInputStream cis = testString.newCodedInput();
byte[] roundTripBytes = cis.readRawBytes(BYTES.length);
- assertTrue(CLASSNAME + " must give the same bytes back from the CodedInputStream",
+ assertTrue(
+ CLASSNAME + " must give the same bytes back from the CodedInputStream",
Arrays.equals(BYTES, roundTripBytes));
assertTrue(CLASSNAME + " CodedInputStream must now be exhausted", cis.isAtEnd());
}
/**
- * Make sure we keep things simple when concatenating with empty. See also
- * {@link ByteStringTest#testConcat_empty()}.
+ * Make sure we keep things simple when concatenating with empty. See also {@link
+ * ByteStringTest#testConcat_empty()}.
*/
public void testConcat_empty() {
- assertSame(CLASSNAME + " concatenated with empty must give " + CLASSNAME,
- testString.concat(EMPTY), testString);
- assertSame("empty concatenated with " + CLASSNAME + " must give " + CLASSNAME,
- EMPTY.concat(testString), testString);
+ assertSame(
+ CLASSNAME + " concatenated with empty must give " + CLASSNAME,
+ testString.concat(EMPTY),
+ testString);
+ assertSame(
+ "empty concatenated with " + CLASSNAME + " must give " + CLASSNAME,
+ EMPTY.concat(testString),
+ testString);
}
public void testJavaSerialization() throws Exception {