31 package com.google.protobuf;
35 import java.io.ByteArrayInputStream;
36 import java.io.ByteArrayOutputStream;
37 import java.io.EOFException;
38 import java.io.IOException;
39 import java.io.InputStream;
40 import java.io.ObjectInputStream;
41 import java.io.ObjectOutputStream;
42 import java.io.OutputStream;
43 import java.io.UnsupportedEncodingException;
44 import java.nio.BufferOverflowException;
45 import java.nio.ByteBuffer;
46 import java.util.Arrays;
47 import java.util.List;
48 import java.util.NoSuchElementException;
49 import junit.framework.TestCase;
53 private static final ByteString EMPTY =
new NioByteString(ByteBuffer.wrap(
new byte[0]));
54 private static final String
CLASSNAME = NioByteString.class.getSimpleName();
67 String actualClassName =
object.getClass().getName();
68 actualClassName = actualClassName.substring(actualClassName.lastIndexOf(
'.') + 1);
69 return actualClassName;
73 boolean stillEqual =
true;
74 for (
int i = 0; stillEqual &&
i <
BYTES.length; ++
i) {
77 assertTrue(
CLASSNAME +
" must capture the right bytes", stillEqual);
81 boolean stillEqual =
true;
83 for (
int i = 0; stillEqual &&
i <
BYTES.length; ++
i) {
84 stillEqual = (iter.hasNext() &&
BYTES[
i] == iter.nextByte());
86 assertTrue(
CLASSNAME +
" must capture the right bytes", stillEqual);
87 assertFalse(
CLASSNAME +
" must have exhausted the itertor", iter.hasNext());
91 fail(
"Should have thrown an exception.");
92 }
catch (NoSuchElementException e) {
98 boolean stillEqual =
true;
101 stillEqual = (
BYTES[j] == quantum);
104 assertTrue(
CLASSNAME +
" must capture the right bytes as Bytes", stillEqual);
105 assertEquals(
CLASSNAME +
" iterable character count",
BYTES.length, j);
121 int destinationOffset = 50;
123 byte[] destination =
new byte[destinationOffset +
length];
124 int sourceOffset = 213;
126 boolean stillEqual =
true;
127 for (
int i = 0; stillEqual &&
i <
length; ++
i) {
128 stillEqual =
BYTES[
i + sourceOffset] == destination[
i + destinationOffset];
130 assertTrue(
CLASSNAME +
".copyTo(4 arg) must give the expected bytes", stillEqual);
134 int destinationOffset = 50;
136 byte[] destination =
new byte[destinationOffset +
length];
141 fail(
"Should have thrown an exception when copying too many bytes of a " +
CLASSNAME);
142 }
catch (IndexOutOfBoundsException expected) {
149 fail(
"Should have thrown an exception when given a negative sourceOffset in " +
CLASSNAME);
150 }
catch (IndexOutOfBoundsException expected) {
158 "Should have thrown an exception when given a negative destinationOffset in "
160 }
catch (IndexOutOfBoundsException expected) {
167 fail(
"Should have thrown an exception when given a negative size in " +
CLASSNAME);
168 }
catch (IndexOutOfBoundsException expected) {
176 "Should have thrown an exception when the destinationOffset is too large in "
178 }
catch (IndexOutOfBoundsException expected) {
186 "Should have thrown an exception when the destinationOffset is too large in "
188 }
catch (IndexOutOfBoundsException expected) {
195 ByteBuffer myBuffer = ByteBuffer.allocate(
BYTES.length);
208 myBuffer = ByteBuffer.allocate(0);
211 fail(
"Should have thrown an exception when target ByteBuffer has insufficient capacity");
212 }
catch (BufferOverflowException e) {
217 myBuffer = ByteBuffer.allocate(1);
220 fail(
"Should have thrown an exception when target ByteBuffer has insufficient capacity");
221 }
catch (BufferOverflowException e) {
228 assertTrue(
CLASSNAME +
".newInput() must support marking",
stream.markSupported());
237 skipFully(
stream, fraction);
239 CLASSNAME +
": after skipping to the 'middle', half the bytes are available",
244 CLASSNAME +
": after resetting, all bytes are available",
250 CLASSNAME +
": after skipping to the end, no more bytes are available",
266 static void skipFully(InputStream in,
long n)
throws IOException {
269 long amt = in.skip(
n);
272 if (in.read() == -1) {
273 long skipped = toSkip -
n;
274 throw new EOFException(
275 "reached end of stream after skipping "
279 +
" bytes expected");
289 ByteBuffer byteBuffer =
testString.asReadOnlyByteBuffer();
290 byte[] roundTripBytes =
new byte[
BYTES.length];
291 assertTrue(byteBuffer.remaining() ==
BYTES.length);
292 assertTrue(byteBuffer.isReadOnly());
293 byteBuffer.get(roundTripBytes);
295 CLASSNAME +
".asReadOnlyByteBuffer() must give back the same bytes",
296 Arrays.equals(
BYTES, roundTripBytes));
300 List<ByteBuffer> byteBuffers =
testString.asReadOnlyByteBufferList();
302 byte[] roundTripBytes =
new byte[
BYTES.length];
303 for (ByteBuffer byteBuffer : byteBuffers) {
304 int thisLength = byteBuffer.remaining();
305 assertTrue(byteBuffer.isReadOnly());
306 assertTrue(bytesSeen + thisLength <=
BYTES.length);
307 byteBuffer.get(roundTripBytes, bytesSeen, thisLength);
308 bytesSeen += thisLength;
310 assertTrue(bytesSeen ==
BYTES.length);
312 CLASSNAME +
".asReadOnlyByteBufferTest() must give back the same bytes",
313 Arrays.equals(
BYTES, roundTripBytes));
317 byte[] roundTripBytes =
testString.toByteArray();
319 CLASSNAME +
".toByteArray() must give back the same bytes",
320 Arrays.equals(
BYTES, roundTripBytes));
324 ByteArrayOutputStream bos =
new ByteArrayOutputStream();
326 byte[] roundTripBytes = bos.toByteArray();
328 CLASSNAME +
".writeTo() must give back the same bytes",
329 Arrays.equals(
BYTES, roundTripBytes));
336 public void write(
byte[]
b,
int off,
int len) {
337 Arrays.fill(
b, off, off +
len, (
byte) 0);
341 public void write(
int b) {
342 throw new UnsupportedOperationException();
346 byte[] original = Arrays.copyOf(
BYTES,
BYTES.length);
349 CLASSNAME +
".writeTo() must NOT grant access to underlying buffer",
350 Arrays.equals(original,
BYTES));
357 public void write(
byte[]
b,
int off,
int len) {
358 Arrays.fill(
b, off, off +
len, (
byte) 0);
362 public void write(
int b) {
363 throw new UnsupportedOperationException();
370 CLASSNAME +
".writeToInternal() must grant access to underlying buffer",
378 public void write(
byte value)
throws IOException {
379 throw new UnsupportedOperationException();
384 throw new UnsupportedOperationException();
388 public void write(ByteBuffer
value)
throws IOException {
389 throw new UnsupportedOperationException();
394 throw new UnsupportedOperationException();
398 public void writeLazy(ByteBuffer
value)
throws IOException {
407 CLASSNAME +
".writeTo() must grant access to underlying buffer",
412 ByteArrayOutputStream bos =
new ByteArrayOutputStream();
418 "Output.writeTo() must give back the same bytes", Arrays.equals(
BYTES, bos.toByteArray()));
423 "Writing an output stream to itself is successful",
428 assertEquals(
"Output.reset() resets the output", 0,
output.size());
429 assertEquals(
"Output.reset() resets the output",
EMPTY,
output.toByteString());
433 String
testString =
"I love unicode \u1234\u5678 characters";
435 String roundTripString = unicode.toString(UTF_8);
440 String
testString =
"I love unicode \u1234\u5678 characters";
442 String roundTripString = unicode.toString(UTF_8);
448 CLASSNAME +
" must be the same string references",
449 EMPTY.toString(UTF_8),
450 new NioByteString(ByteBuffer.wrap(
new byte[0])).toString(UTF_8));
455 EMPTY.toString(
"invalid");
456 fail(
"Should have thrown an exception.");
457 }
catch (UnsupportedEncodingException expected) {
463 fail(
"Should have thrown an exception.");
464 }
catch (UnsupportedEncodingException expected) {
475 CLASSNAME +
" must equal another string with the same value",
481 CLASSNAME +
" must not equal every string with the same length",
487 assertEquals(
CLASSNAME +
" must equal LiteralByteString with same value", literal,
testString);
488 assertEquals(
CLASSNAME +
" must equal LiteralByteString with same value",
testString, literal);
496 CLASSNAME +
" must not equal every LiteralByteString with the same length",
499 CLASSNAME +
" must not equal every LiteralByteString with the same length",
511 CLASSNAME +
" must not equal the empty string",
514 CLASSNAME +
" empty strings must be equal",
521 rope = p1.concat(p2);
523 CLASSNAME +
" must not equal every RopeByteString with the same length",
526 CLASSNAME +
" must not equal every RopeByteString with the same length",
545 CLASSNAME +
".peekCachedHashCode() should return zero at first",
547 newString.peekCachedHashCode());
548 newString.hashCode();
550 CLASSNAME +
".peekCachedHashCode should return zero at first",
552 newString.peekCachedHashCode());
566 boolean stillEqual =
true;
567 for (
byte referenceByte :
BYTES) {
568 int expectedInt = (referenceByte & 0xFF);
569 stillEqual = (expectedInt ==
input.read());
571 assertEquals(
"InputStream.available() returns correct value", 0,
input.available());
572 assertTrue(
CLASSNAME +
" must give the same bytes from the InputStream", stillEqual);
573 assertEquals(
CLASSNAME +
" InputStream must now be exhausted", -1,
input.read());
579 int nearEndIndex = stringSize * 2 / 3;
580 long skipped1 =
input.skip(nearEndIndex);
581 assertEquals(
"InputStream.skip()", skipped1, nearEndIndex);
582 assertEquals(
"InputStream.available()", stringSize - skipped1,
input.available());
583 assertTrue(
"InputStream.mark() is available",
input.markSupported());
587 assertEquals(
"InputStream.available()", stringSize - skipped1 - 1,
input.available());
588 long skipped2 =
input.skip(stringSize);
589 assertEquals(
"InputStream.skip() incomplete", skipped2, stringSize - skipped1 - 1);
590 assertEquals(
"InputStream.skip(), no more input", 0,
input.available());
591 assertEquals(
"InputStream.skip(), no more input", -1,
input.read());
593 assertEquals(
"InputStream.reset() succeded", stringSize - skipped1,
input.available());
602 CLASSNAME +
" must give the same bytes back from the CodedInputStream",
603 Arrays.equals(
BYTES, roundTripBytes));
604 assertTrue(
CLASSNAME +
" CodedInputStream must now be exhausted", cis.
isAtEnd());
623 ByteArrayOutputStream out =
new ByteArrayOutputStream();
624 ObjectOutputStream oos =
new ObjectOutputStream(out);
627 byte[] pickled = out.toByteArray();
628 InputStream in =
new ByteArrayInputStream(pickled);
629 ObjectInputStream ois =
new ObjectInputStream(in);
630 Object o = ois.readObject();
631 assertTrue(
"Didn't get a ByteString back", o instanceof
ByteString);
632 assertEquals(
"Should get an equal ByteString back",
testString, o);
636 return new NioByteString(ByteBuffer.wrap(
str.getBytes(UTF_8)));