| Index: third_party/protobuf/java/compatibility_tests/v2.5.0/tests/src/main/java/com/google/protobuf/test/LiteralByteStringTest.java
|
| diff --git a/third_party/protobuf/java/core/src/test/java/com/google/protobuf/LiteralByteStringTest.java b/third_party/protobuf/java/compatibility_tests/v2.5.0/tests/src/main/java/com/google/protobuf/test/LiteralByteStringTest.java
|
| similarity index 60%
|
| copy from third_party/protobuf/java/core/src/test/java/com/google/protobuf/LiteralByteStringTest.java
|
| copy to third_party/protobuf/java/compatibility_tests/v2.5.0/tests/src/main/java/com/google/protobuf/test/LiteralByteStringTest.java
|
| index 2e7792a880d58f52dd8e8e2df9b973cbc872dcc3..b2dcc7e88238d62976b4f34303d46bbbc513dbc4 100644
|
| --- a/third_party/protobuf/java/core/src/test/java/com/google/protobuf/LiteralByteStringTest.java
|
| +++ b/third_party/protobuf/java/compatibility_tests/v2.5.0/tests/src/main/java/com/google/protobuf/test/LiteralByteStringTest.java
|
| @@ -1,6 +1,6 @@
|
| // Protocol Buffers - Google's data interchange format
|
| // Copyright 2008 Google Inc. All rights reserved.
|
| -// https://developers.google.com/protocol-buffers/
|
| +// http://code.google.com/p/protobuf/
|
| //
|
| // Redistribution and use in source and binary forms, with or without
|
| // modification, are permitted provided that the following conditions are
|
| @@ -28,17 +28,14 @@
|
| // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
| // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
| -package com.google.protobuf;
|
| +package com.google.protobuf.test;
|
| +import com.google.protobuf.*;
|
|
|
| import junit.framework.TestCase;
|
|
|
| -import java.io.ByteArrayInputStream;
|
| import java.io.ByteArrayOutputStream;
|
| -import java.io.EOFException;
|
| import java.io.IOException;
|
| import java.io.InputStream;
|
| -import java.io.ObjectInputStream;
|
| -import java.io.ObjectOutputStream;
|
| import java.io.OutputStream;
|
| import java.io.UnsupportedEncodingException;
|
| import java.nio.ByteBuffer;
|
| @@ -47,9 +44,9 @@ import java.util.List;
|
| import java.util.NoSuchElementException;
|
|
|
| /**
|
| - * Test {@code LiteralByteString} by setting up a reference string in {@link #setUp()}.
|
| - * This class is designed to be extended for testing extensions of {@code LiteralByteString}
|
| - * such as {@code BoundedByteString}, see {@link BoundedByteStringTest}.
|
| + * Test {@link LiteralByteString} by setting up a reference string in {@link #setUp()}.
|
| + * This class is designed to be extended for testing extensions of {@link LiteralByteString}
|
| + * such as {@link BoundedByteString}, see {@link BoundedByteStringTest}.
|
| *
|
| * @author carlanton@google.com (Carl Haverl)
|
| */
|
| @@ -69,13 +66,10 @@ public class LiteralByteStringTest extends TestCase {
|
| expectedHashCode = 331161852;
|
| }
|
|
|
| - public void testExpectedType() {
|
| - String actualClassName = getActualClassName(stringUnderTest);
|
| - assertEquals(classUnderTest + " should match type exactly", classUnderTest, actualClassName);
|
| - }
|
| -
|
| protected String getActualClassName(Object object) {
|
| - return object.getClass().getSimpleName();
|
| + String actualClassName = object.getClass().getName();
|
| + actualClassName = actualClassName.substring(actualClassName.lastIndexOf('.') + 1);
|
| + return actualClassName;
|
| }
|
|
|
| public void testByteAt() {
|
| @@ -119,14 +113,6 @@ public class LiteralByteStringTest extends TestCase {
|
| stringUnderTest.size());
|
| }
|
|
|
| - public void testGetTreeDepth() {
|
| - assertEquals(classUnderTest + " must have depth 0", 0, stringUnderTest.getTreeDepth());
|
| - }
|
| -
|
| - public void testIsBalanced() {
|
| - assertTrue(classUnderTest + " is technically balanced", stringUnderTest.isBalanced());
|
| - }
|
| -
|
| public void testCopyTo_ByteArrayOffsetLength() {
|
| int destinationOffset = 50;
|
| int length = 100;
|
| @@ -208,62 +194,6 @@ public class LiteralByteStringTest extends TestCase {
|
| Arrays.equals(referenceBytes, myBuffer.array()));
|
| }
|
|
|
| - public void testMarkSupported() {
|
| - InputStream stream = stringUnderTest.newInput();
|
| - assertTrue(classUnderTest + ".newInput() must support marking", stream.markSupported());
|
| - }
|
| -
|
| - public void testMarkAndReset() throws IOException {
|
| - int fraction = stringUnderTest.size() / 3;
|
| -
|
| - InputStream stream = stringUnderTest.newInput();
|
| - stream.mark(stringUnderTest.size()); // First, mark() the end.
|
| -
|
| - skipFully(stream, fraction); // Skip a large fraction, but not all.
|
| - int available = stream.available();
|
| - assertTrue(
|
| - classUnderTest + ": after skipping to the 'middle', half the bytes are available",
|
| - (stringUnderTest.size() - fraction) == available);
|
| - stream.reset();
|
| -
|
| - skipFully(stream, stringUnderTest.size()); // Skip to the end.
|
| - available = stream.available();
|
| - assertTrue(
|
| - classUnderTest + ": after skipping to the end, no more bytes are available",
|
| - 0 == 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.
|
| - * <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
|
| - */
|
| - static void skipFully(InputStream in, long n) throws IOException {
|
| - long toSkip = n;
|
| - while (n > 0) {
|
| - long amt = in.skip(n);
|
| - if (amt == 0) {
|
| - // 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");
|
| - }
|
| - n--;
|
| - } else {
|
| - n -= amt;
|
| - }
|
| - }
|
| - }
|
| -
|
| public void testAsReadOnlyByteBuffer() {
|
| ByteBuffer byteBuffer = stringUnderTest.asReadOnlyByteBuffer();
|
| byte[] roundTripBytes = new byte[referenceBytes.length];
|
| @@ -303,76 +233,26 @@ public class LiteralByteStringTest extends TestCase {
|
| assertTrue(classUnderTest + ".writeTo() must give back the same bytes",
|
| Arrays.equals(referenceBytes, roundTripBytes));
|
| }
|
| -
|
| - public void testWriteToShouldNotExposeInternalBufferToOutputStream() throws IOException {
|
| +
|
| + public void testWriteTo_mutating() throws IOException {
|
| OutputStream os = new OutputStream() {
|
| @Override
|
| public void write(byte[] b, int off, int len) {
|
| - Arrays.fill(b, off, off + len, (byte) 0);
|
| + for (int x = 0; x < len; ++x) {
|
| + b[off + x] = (byte) 0;
|
| + }
|
| }
|
|
|
| @Override
|
| public void write(int b) {
|
| - throw new UnsupportedOperationException();
|
| + // Purposefully left blank.
|
| }
|
| };
|
|
|
| stringUnderTest.writeTo(os);
|
| + byte[] newBytes = stringUnderTest.toByteArray();
|
| assertTrue(classUnderTest + ".writeTo() must not grant access to underlying array",
|
| - Arrays.equals(referenceBytes, stringUnderTest.toByteArray()));
|
| - }
|
| -
|
| - 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();
|
| - }
|
| - };
|
| -
|
| - stringUnderTest.writeToInternal(os, 0, stringUnderTest.size());
|
| - byte[] allZeros = new byte[stringUnderTest.size()];
|
| - assertTrue(classUnderTest + ".writeToInternal() must grant access to underlying array",
|
| - Arrays.equals(allZeros, stringUnderTest.toByteArray()));
|
| - }
|
| -
|
| - 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 {
|
| - Arrays.fill(value, offset, offset + length, (byte) 0);
|
| - }
|
| -
|
| - @Override
|
| - public void write(ByteBuffer value) throws IOException {
|
| - throw new UnsupportedOperationException();
|
| - }
|
| -
|
| - @Override
|
| - public void writeLazy(ByteBuffer value) throws IOException {
|
| - throw new UnsupportedOperationException();
|
| - }
|
| - };
|
| -
|
| - stringUnderTest.writeTo(out);
|
| - byte[] allZeros = new byte[stringUnderTest.size()];
|
| - assertTrue(classUnderTest + ".writeToInternal() must grant access to underlying array",
|
| - Arrays.equals(allZeros, stringUnderTest.toByteArray()));
|
| + Arrays.equals(referenceBytes, newBytes));
|
| }
|
|
|
| public void testNewOutput() throws IOException {
|
| @@ -394,59 +274,7 @@ public class LiteralByteStringTest extends TestCase {
|
| assertEquals("Output.reset() resets the output", 0, output.size());
|
| assertEquals("Output.reset() resets the output",
|
| ByteString.EMPTY, output.toByteString());
|
| - }
|
| -
|
| - public void testToString() throws UnsupportedEncodingException {
|
| - String testString = "I love unicode \u1234\u5678 characters";
|
| - ByteString unicode = ByteString.wrap(testString.getBytes(Internal.UTF_8));
|
| - String roundTripString = unicode.toString(UTF_8);
|
| - assertEquals(classUnderTest + " unicode must match", testString, roundTripString);
|
| - }
|
| -
|
| - public void testCharsetToString() {
|
| - String testString = "I love unicode \u1234\u5678 characters";
|
| - ByteString unicode = ByteString.wrap(testString.getBytes(Internal.UTF_8));
|
| - String roundTripString = unicode.toString(Internal.UTF_8);
|
| - assertEquals(classUnderTest + " unicode must match", testString, roundTripString);
|
| - }
|
| -
|
| - public void testToString_returnsCanonicalEmptyString() {
|
| - assertSame(classUnderTest + " must be the same string references",
|
| - ByteString.EMPTY.toString(Internal.UTF_8),
|
| - ByteString.wrap(new byte[]{}).toString(Internal.UTF_8));
|
| - }
|
| -
|
| - public void testToString_raisesException() {
|
| - try {
|
| - ByteString.EMPTY.toString("invalid");
|
| - fail("Should have thrown an exception.");
|
| - } catch (UnsupportedEncodingException expected) {
|
| - // This is success
|
| - }
|
| -
|
| - try {
|
| - ByteString.wrap(referenceBytes).toString("invalid");
|
| - fail("Should have thrown an exception.");
|
| - } catch (UnsupportedEncodingException expected) {
|
| - // This is success
|
| - }
|
| - }
|
| -
|
| - public void testEquals() {
|
| - assertEquals(classUnderTest + " must not equal null", false, stringUnderTest.equals(null));
|
| - assertEquals(classUnderTest + " must equal self", stringUnderTest, stringUnderTest);
|
| - assertFalse(classUnderTest + " must not equal the empty string",
|
| - stringUnderTest.equals(ByteString.EMPTY));
|
| - assertEquals(classUnderTest + " empty strings must be equal",
|
| - ByteString.wrap(new byte[]{}), stringUnderTest.substring(55, 55));
|
| - assertEquals(classUnderTest + " must equal another string with the same value",
|
| - stringUnderTest, ByteString.wrap(referenceBytes));
|
| -
|
| - byte[] mungedBytes = new byte[referenceBytes.length];
|
| - System.arraycopy(referenceBytes, 0, mungedBytes, 0, referenceBytes.length);
|
| - mungedBytes[mungedBytes.length - 5] = (byte) (mungedBytes[mungedBytes.length - 5] ^ 0xFF);
|
| - assertFalse(classUnderTest + " must not equal every string with the same length",
|
| - stringUnderTest.equals(ByteString.wrap(mungedBytes)));
|
| +
|
| }
|
|
|
| public void testHashCode() {
|
| @@ -454,22 +282,6 @@ public class LiteralByteStringTest extends TestCase {
|
| assertEquals(classUnderTest + " must have expected hashCode", expectedHashCode, hash);
|
| }
|
|
|
| - public void testPeekCachedHashCode() {
|
| - assertEquals(classUnderTest + ".peekCachedHashCode() should return zero at first", 0,
|
| - stringUnderTest.peekCachedHashCode());
|
| - stringUnderTest.hashCode();
|
| - assertEquals(classUnderTest + ".peekCachedHashCode should return zero at first",
|
| - expectedHashCode, stringUnderTest.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 = stringUnderTest.partialHash(stringUnderTest.size(), 0, stringUnderTest.size());
|
| - assertEquals(classUnderTest + ".partialHash() must yield expected hashCode",
|
| - expectedHashCode, hash);
|
| - }
|
| -
|
| public void testNewInput() throws IOException {
|
| InputStream input = stringUnderTest.newInput();
|
| assertEquals("InputStream.available() returns correct value",
|
| @@ -529,17 +341,4 @@ public class LiteralByteStringTest extends TestCase {
|
| assertSame("empty concatenated with " + classUnderTest + " must give " + classUnderTest,
|
| ByteString.EMPTY.concat(stringUnderTest), stringUnderTest);
|
| }
|
| -
|
| - public void testJavaSerialization() throws Exception {
|
| - ByteArrayOutputStream out = new ByteArrayOutputStream();
|
| - ObjectOutputStream oos = new ObjectOutputStream(out);
|
| - oos.writeObject(stringUnderTest);
|
| - oos.close();
|
| - byte[] pickled = out.toByteArray();
|
| - InputStream in = new ByteArrayInputStream(pickled);
|
| - ObjectInputStream ois = new ObjectInputStream(in);
|
| - Object o = ois.readObject();
|
| - assertTrue("Didn't get a ByteString back", o instanceof ByteString);
|
| - assertEquals("Should get an equal ByteString back", stringUnderTest, o);
|
| - }
|
| }
|
|
|