Index: third_party/protobuf/java/core/src/test/java/com/google/protobuf/LiteralByteStringTest.java |
diff --git a/third_party/protobuf/java/src/test/java/com/google/protobuf/LiteralByteStringTest.java b/third_party/protobuf/java/core/src/test/java/com/google/protobuf/LiteralByteStringTest.java |
similarity index 87% |
rename from third_party/protobuf/java/src/test/java/com/google/protobuf/LiteralByteStringTest.java |
rename to third_party/protobuf/java/core/src/test/java/com/google/protobuf/LiteralByteStringTest.java |
index c84956335e4c6be461a56be3d32960c6893af6f5..2e7792a880d58f52dd8e8e2df9b973cbc872dcc3 100644 |
--- a/third_party/protobuf/java/src/test/java/com/google/protobuf/LiteralByteStringTest.java |
+++ b/third_party/protobuf/java/core/src/test/java/com/google/protobuf/LiteralByteStringTest.java |
@@ -47,9 +47,9 @@ import java.util.List; |
import java.util.NoSuchElementException; |
/** |
- * 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}. |
+ * 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}. |
* |
* @author carlanton@google.com (Carl Haverl) |
*/ |
@@ -75,9 +75,7 @@ public class LiteralByteStringTest extends TestCase { |
} |
protected String getActualClassName(Object object) { |
- String actualClassName = object.getClass().getName(); |
- actualClassName = actualClassName.substring(actualClassName.lastIndexOf('.') + 1); |
- return actualClassName; |
+ return object.getClass().getSimpleName(); |
} |
public void testByteAt() { |
@@ -306,25 +304,75 @@ public class LiteralByteStringTest extends TestCase { |
Arrays.equals(referenceBytes, roundTripBytes)); |
} |
- public void testWriteTo_mutating() throws IOException { |
+ public void testWriteToShouldNotExposeInternalBufferToOutputStream() throws IOException { |
OutputStream os = new OutputStream() { |
@Override |
public void write(byte[] b, int off, int len) { |
- for (int x = 0; x < len; ++x) { |
- b[off + x] = (byte) 0; |
- } |
+ Arrays.fill(b, off, off + len, (byte) 0); |
} |
@Override |
public void write(int b) { |
- // Purposefully left blank. |
+ throw new UnsupportedOperationException(); |
} |
}; |
stringUnderTest.writeTo(os); |
- byte[] newBytes = stringUnderTest.toByteArray(); |
assertTrue(classUnderTest + ".writeTo() must not grant access to underlying array", |
- Arrays.equals(referenceBytes, newBytes)); |
+ 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())); |
} |
public void testNewOutput() throws IOException { |
@@ -350,14 +398,14 @@ public class LiteralByteStringTest extends TestCase { |
public void testToString() throws UnsupportedEncodingException { |
String testString = "I love unicode \u1234\u5678 characters"; |
- LiteralByteString unicode = new LiteralByteString(testString.getBytes(Internal.UTF_8)); |
+ 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"; |
- LiteralByteString unicode = new LiteralByteString(testString.getBytes(Internal.UTF_8)); |
+ ByteString unicode = ByteString.wrap(testString.getBytes(Internal.UTF_8)); |
String roundTripString = unicode.toString(Internal.UTF_8); |
assertEquals(classUnderTest + " unicode must match", testString, roundTripString); |
} |
@@ -365,7 +413,7 @@ public class LiteralByteStringTest extends TestCase { |
public void testToString_returnsCanonicalEmptyString() { |
assertSame(classUnderTest + " must be the same string references", |
ByteString.EMPTY.toString(Internal.UTF_8), |
- new LiteralByteString(new byte[]{}).toString(Internal.UTF_8)); |
+ ByteString.wrap(new byte[]{}).toString(Internal.UTF_8)); |
} |
public void testToString_raisesException() { |
@@ -377,7 +425,7 @@ public class LiteralByteStringTest extends TestCase { |
} |
try { |
- new LiteralByteString(referenceBytes).toString("invalid"); |
+ ByteString.wrap(referenceBytes).toString("invalid"); |
fail("Should have thrown an exception."); |
} catch (UnsupportedEncodingException expected) { |
// This is success |
@@ -390,15 +438,15 @@ public class LiteralByteStringTest extends TestCase { |
assertFalse(classUnderTest + " must not equal the empty string", |
stringUnderTest.equals(ByteString.EMPTY)); |
assertEquals(classUnderTest + " empty strings must be equal", |
- new LiteralByteString(new byte[]{}), stringUnderTest.substring(55, 55)); |
+ ByteString.wrap(new byte[]{}), stringUnderTest.substring(55, 55)); |
assertEquals(classUnderTest + " must equal another string with the same value", |
- stringUnderTest, new LiteralByteString(referenceBytes)); |
+ 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(new LiteralByteString(mungedBytes))); |
+ stringUnderTest.equals(ByteString.wrap(mungedBytes))); |
} |
public void testHashCode() { |