OLD | NEW |
1 // Protocol Buffers - Google's data interchange format | 1 // Protocol Buffers - Google's data interchange format |
2 // Copyright 2008 Google Inc. All rights reserved. | 2 // Copyright 2008 Google Inc. All rights reserved. |
3 // https://developers.google.com/protocol-buffers/ | 3 // http://code.google.com/p/protobuf/ |
4 // | 4 // |
5 // Redistribution and use in source and binary forms, with or without | 5 // Redistribution and use in source and binary forms, with or without |
6 // modification, are permitted provided that the following conditions are | 6 // modification, are permitted provided that the following conditions are |
7 // met: | 7 // met: |
8 // | 8 // |
9 // * Redistributions of source code must retain the above copyright | 9 // * Redistributions of source code must retain the above copyright |
10 // notice, this list of conditions and the following disclaimer. | 10 // notice, this list of conditions and the following disclaimer. |
11 // * Redistributions in binary form must reproduce the above | 11 // * Redistributions in binary form must reproduce the above |
12 // copyright notice, this list of conditions and the following disclaimer | 12 // copyright notice, this list of conditions and the following disclaimer |
13 // in the documentation and/or other materials provided with the | 13 // in the documentation and/or other materials provided with the |
14 // distribution. | 14 // distribution. |
15 // * Neither the name of Google Inc. nor the names of its | 15 // * Neither the name of Google Inc. nor the names of its |
16 // contributors may be used to endorse or promote products derived from | 16 // contributors may be used to endorse or promote products derived from |
17 // this software without specific prior written permission. | 17 // this software without specific prior written permission. |
18 // | 18 // |
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | 25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | 29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
30 | 30 |
31 package com.google.protobuf; | 31 package com.google.protobuf.test; |
| 32 import com.google.protobuf.*; |
32 | 33 |
33 import junit.framework.TestCase; | 34 import junit.framework.TestCase; |
34 | 35 |
35 import java.io.ByteArrayInputStream; | |
36 import java.io.ByteArrayOutputStream; | 36 import java.io.ByteArrayOutputStream; |
37 import java.io.EOFException; | |
38 import java.io.IOException; | 37 import java.io.IOException; |
39 import java.io.InputStream; | 38 import java.io.InputStream; |
40 import java.io.ObjectInputStream; | |
41 import java.io.ObjectOutputStream; | |
42 import java.io.OutputStream; | 39 import java.io.OutputStream; |
43 import java.io.UnsupportedEncodingException; | 40 import java.io.UnsupportedEncodingException; |
44 import java.nio.ByteBuffer; | 41 import java.nio.ByteBuffer; |
45 import java.util.Arrays; | 42 import java.util.Arrays; |
46 import java.util.List; | 43 import java.util.List; |
47 import java.util.NoSuchElementException; | 44 import java.util.NoSuchElementException; |
48 | 45 |
49 /** | 46 /** |
50 * Test {@code LiteralByteString} by setting up a reference string in {@link #se
tUp()}. | 47 * Test {@link LiteralByteString} by setting up a reference string in {@link #se
tUp()}. |
51 * This class is designed to be extended for testing extensions of {@code Litera
lByteString} | 48 * This class is designed to be extended for testing extensions of {@link Litera
lByteString} |
52 * such as {@code BoundedByteString}, see {@link BoundedByteStringTest}. | 49 * such as {@link BoundedByteString}, see {@link BoundedByteStringTest}. |
53 * | 50 * |
54 * @author carlanton@google.com (Carl Haverl) | 51 * @author carlanton@google.com (Carl Haverl) |
55 */ | 52 */ |
56 public class LiteralByteStringTest extends TestCase { | 53 public class LiteralByteStringTest extends TestCase { |
57 protected static final String UTF_8 = "UTF-8"; | 54 protected static final String UTF_8 = "UTF-8"; |
58 | 55 |
59 protected String classUnderTest; | 56 protected String classUnderTest; |
60 protected byte[] referenceBytes; | 57 protected byte[] referenceBytes; |
61 protected ByteString stringUnderTest; | 58 protected ByteString stringUnderTest; |
62 protected int expectedHashCode; | 59 protected int expectedHashCode; |
63 | 60 |
64 @Override | 61 @Override |
65 protected void setUp() throws Exception { | 62 protected void setUp() throws Exception { |
66 classUnderTest = "LiteralByteString"; | 63 classUnderTest = "LiteralByteString"; |
67 referenceBytes = ByteStringTest.getTestBytes(1234, 11337766L); | 64 referenceBytes = ByteStringTest.getTestBytes(1234, 11337766L); |
68 stringUnderTest = ByteString.copyFrom(referenceBytes); | 65 stringUnderTest = ByteString.copyFrom(referenceBytes); |
69 expectedHashCode = 331161852; | 66 expectedHashCode = 331161852; |
70 } | 67 } |
71 | 68 |
72 public void testExpectedType() { | |
73 String actualClassName = getActualClassName(stringUnderTest); | |
74 assertEquals(classUnderTest + " should match type exactly", classUnderTest,
actualClassName); | |
75 } | |
76 | |
77 protected String getActualClassName(Object object) { | 69 protected String getActualClassName(Object object) { |
78 return object.getClass().getSimpleName(); | 70 String actualClassName = object.getClass().getName(); |
| 71 actualClassName = actualClassName.substring(actualClassName.lastIndexOf('.')
+ 1); |
| 72 return actualClassName; |
79 } | 73 } |
80 | 74 |
81 public void testByteAt() { | 75 public void testByteAt() { |
82 boolean stillEqual = true; | 76 boolean stillEqual = true; |
83 for (int i = 0; stillEqual && i < referenceBytes.length; ++i) { | 77 for (int i = 0; stillEqual && i < referenceBytes.length; ++i) { |
84 stillEqual = (referenceBytes[i] == stringUnderTest.byteAt(i)); | 78 stillEqual = (referenceBytes[i] == stringUnderTest.byteAt(i)); |
85 } | 79 } |
86 assertTrue(classUnderTest + " must capture the right bytes", stillEqual); | 80 assertTrue(classUnderTest + " must capture the right bytes", stillEqual); |
87 } | 81 } |
88 | 82 |
(...skipping 23 matching lines...) Expand all Loading... |
112 } | 106 } |
113 assertTrue(classUnderTest + " must capture the right bytes as Bytes", stillE
qual); | 107 assertTrue(classUnderTest + " must capture the right bytes as Bytes", stillE
qual); |
114 assertEquals(classUnderTest + " iterable character count", referenceBytes.le
ngth, j); | 108 assertEquals(classUnderTest + " iterable character count", referenceBytes.le
ngth, j); |
115 } | 109 } |
116 | 110 |
117 public void testSize() { | 111 public void testSize() { |
118 assertEquals(classUnderTest + " must have the expected size", referenceBytes
.length, | 112 assertEquals(classUnderTest + " must have the expected size", referenceBytes
.length, |
119 stringUnderTest.size()); | 113 stringUnderTest.size()); |
120 } | 114 } |
121 | 115 |
122 public void testGetTreeDepth() { | |
123 assertEquals(classUnderTest + " must have depth 0", 0, stringUnderTest.getTr
eeDepth()); | |
124 } | |
125 | |
126 public void testIsBalanced() { | |
127 assertTrue(classUnderTest + " is technically balanced", stringUnderTest.isBa
lanced()); | |
128 } | |
129 | |
130 public void testCopyTo_ByteArrayOffsetLength() { | 116 public void testCopyTo_ByteArrayOffsetLength() { |
131 int destinationOffset = 50; | 117 int destinationOffset = 50; |
132 int length = 100; | 118 int length = 100; |
133 byte[] destination = new byte[destinationOffset + length]; | 119 byte[] destination = new byte[destinationOffset + length]; |
134 int sourceOffset = 213; | 120 int sourceOffset = 213; |
135 stringUnderTest.copyTo(destination, sourceOffset, destinationOffset, length)
; | 121 stringUnderTest.copyTo(destination, sourceOffset, destinationOffset, length)
; |
136 boolean stillEqual = true; | 122 boolean stillEqual = true; |
137 for (int i = 0; stillEqual && i < length; ++i) { | 123 for (int i = 0; stillEqual && i < length; ++i) { |
138 stillEqual = referenceBytes[i + sourceOffset] == destination[i + destinati
onOffset]; | 124 stillEqual = referenceBytes[i + sourceOffset] == destination[i + destinati
onOffset]; |
139 } | 125 } |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 } | 187 } |
202 } | 188 } |
203 | 189 |
204 public void testCopyTo_ByteBuffer() { | 190 public void testCopyTo_ByteBuffer() { |
205 ByteBuffer myBuffer = ByteBuffer.allocate(referenceBytes.length); | 191 ByteBuffer myBuffer = ByteBuffer.allocate(referenceBytes.length); |
206 stringUnderTest.copyTo(myBuffer); | 192 stringUnderTest.copyTo(myBuffer); |
207 assertTrue(classUnderTest + ".copyTo(ByteBuffer) must give back the same byt
es", | 193 assertTrue(classUnderTest + ".copyTo(ByteBuffer) must give back the same byt
es", |
208 Arrays.equals(referenceBytes, myBuffer.array())); | 194 Arrays.equals(referenceBytes, myBuffer.array())); |
209 } | 195 } |
210 | 196 |
211 public void testMarkSupported() { | |
212 InputStream stream = stringUnderTest.newInput(); | |
213 assertTrue(classUnderTest + ".newInput() must support marking", stream.markS
upported()); | |
214 } | |
215 | |
216 public void testMarkAndReset() throws IOException { | |
217 int fraction = stringUnderTest.size() / 3; | |
218 | |
219 InputStream stream = stringUnderTest.newInput(); | |
220 stream.mark(stringUnderTest.size()); // First, mark() the end. | |
221 | |
222 skipFully(stream, fraction); // Skip a large fraction, but not all. | |
223 int available = stream.available(); | |
224 assertTrue( | |
225 classUnderTest + ": after skipping to the 'middle', half the bytes are a
vailable", | |
226 (stringUnderTest.size() - fraction) == available); | |
227 stream.reset(); | |
228 | |
229 skipFully(stream, stringUnderTest.size()); // Skip to the end. | |
230 available = stream.available(); | |
231 assertTrue( | |
232 classUnderTest + ": after skipping to the end, no more bytes are availab
le", | |
233 0 == available); | |
234 } | |
235 | |
236 /** | |
237 * Discards {@code n} bytes of data from the input stream. This method | |
238 * will block until the full amount has been skipped. Does not close the | |
239 * stream. | |
240 * <p>Copied from com.google.common.io.ByteStreams to avoid adding dependency. | |
241 * | |
242 * @param in the input stream to read from | |
243 * @param n the number of bytes to skip | |
244 * @throws EOFException if this stream reaches the end before skipping all | |
245 * the bytes | |
246 * @throws IOException if an I/O error occurs, or the stream does not | |
247 * support skipping | |
248 */ | |
249 static void skipFully(InputStream in, long n) throws IOException { | |
250 long toSkip = n; | |
251 while (n > 0) { | |
252 long amt = in.skip(n); | |
253 if (amt == 0) { | |
254 // Force a blocking read to avoid infinite loop | |
255 if (in.read() == -1) { | |
256 long skipped = toSkip - n; | |
257 throw new EOFException("reached end of stream after skipping " | |
258 + skipped + " bytes; " + toSkip + " bytes expected"); | |
259 } | |
260 n--; | |
261 } else { | |
262 n -= amt; | |
263 } | |
264 } | |
265 } | |
266 | |
267 public void testAsReadOnlyByteBuffer() { | 197 public void testAsReadOnlyByteBuffer() { |
268 ByteBuffer byteBuffer = stringUnderTest.asReadOnlyByteBuffer(); | 198 ByteBuffer byteBuffer = stringUnderTest.asReadOnlyByteBuffer(); |
269 byte[] roundTripBytes = new byte[referenceBytes.length]; | 199 byte[] roundTripBytes = new byte[referenceBytes.length]; |
270 assertTrue(byteBuffer.remaining() == referenceBytes.length); | 200 assertTrue(byteBuffer.remaining() == referenceBytes.length); |
271 assertTrue(byteBuffer.isReadOnly()); | 201 assertTrue(byteBuffer.isReadOnly()); |
272 byteBuffer.get(roundTripBytes); | 202 byteBuffer.get(roundTripBytes); |
273 assertTrue(classUnderTest + ".asReadOnlyByteBuffer() must give back the same
bytes", | 203 assertTrue(classUnderTest + ".asReadOnlyByteBuffer() must give back the same
bytes", |
274 Arrays.equals(referenceBytes, roundTripBytes)); | 204 Arrays.equals(referenceBytes, roundTripBytes)); |
275 } | 205 } |
276 | 206 |
(...skipping 19 matching lines...) Expand all Loading... |
296 Arrays.equals(referenceBytes, roundTripBytes)); | 226 Arrays.equals(referenceBytes, roundTripBytes)); |
297 } | 227 } |
298 | 228 |
299 public void testWriteTo() throws IOException { | 229 public void testWriteTo() throws IOException { |
300 ByteArrayOutputStream bos = new ByteArrayOutputStream(); | 230 ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
301 stringUnderTest.writeTo(bos); | 231 stringUnderTest.writeTo(bos); |
302 byte[] roundTripBytes = bos.toByteArray(); | 232 byte[] roundTripBytes = bos.toByteArray(); |
303 assertTrue(classUnderTest + ".writeTo() must give back the same bytes", | 233 assertTrue(classUnderTest + ".writeTo() must give back the same bytes", |
304 Arrays.equals(referenceBytes, roundTripBytes)); | 234 Arrays.equals(referenceBytes, roundTripBytes)); |
305 } | 235 } |
306 | 236 |
307 public void testWriteToShouldNotExposeInternalBufferToOutputStream() throws IO
Exception { | 237 public void testWriteTo_mutating() throws IOException { |
308 OutputStream os = new OutputStream() { | 238 OutputStream os = new OutputStream() { |
309 @Override | 239 @Override |
310 public void write(byte[] b, int off, int len) { | 240 public void write(byte[] b, int off, int len) { |
311 Arrays.fill(b, off, off + len, (byte) 0); | 241 for (int x = 0; x < len; ++x) { |
| 242 b[off + x] = (byte) 0; |
| 243 } |
312 } | 244 } |
313 | 245 |
314 @Override | 246 @Override |
315 public void write(int b) { | 247 public void write(int b) { |
316 throw new UnsupportedOperationException(); | 248 // Purposefully left blank. |
317 } | 249 } |
318 }; | 250 }; |
319 | 251 |
320 stringUnderTest.writeTo(os); | 252 stringUnderTest.writeTo(os); |
| 253 byte[] newBytes = stringUnderTest.toByteArray(); |
321 assertTrue(classUnderTest + ".writeTo() must not grant access to underlying
array", | 254 assertTrue(classUnderTest + ".writeTo() must not grant access to underlying
array", |
322 Arrays.equals(referenceBytes, stringUnderTest.toByteArray())); | 255 Arrays.equals(referenceBytes, newBytes)); |
323 } | |
324 | |
325 public void testWriteToInternalShouldExposeInternalBufferToOutputStream() thro
ws IOException { | |
326 OutputStream os = new OutputStream() { | |
327 @Override | |
328 public void write(byte[] b, int off, int len) { | |
329 Arrays.fill(b, off, off + len, (byte) 0); | |
330 } | |
331 | |
332 @Override | |
333 public void write(int b) { | |
334 throw new UnsupportedOperationException(); | |
335 } | |
336 }; | |
337 | |
338 stringUnderTest.writeToInternal(os, 0, stringUnderTest.size()); | |
339 byte[] allZeros = new byte[stringUnderTest.size()]; | |
340 assertTrue(classUnderTest + ".writeToInternal() must grant access to underly
ing array", | |
341 Arrays.equals(allZeros, stringUnderTest.toByteArray())); | |
342 } | |
343 | |
344 public void testWriteToShouldExposeInternalBufferToByteOutput() throws IOExcep
tion { | |
345 ByteOutput out = new ByteOutput() { | |
346 @Override | |
347 public void write(byte value) throws IOException { | |
348 throw new UnsupportedOperationException(); | |
349 } | |
350 | |
351 @Override | |
352 public void write(byte[] value, int offset, int length) throws IOException
{ | |
353 throw new UnsupportedOperationException(); | |
354 } | |
355 | |
356 @Override | |
357 public void writeLazy(byte[] value, int offset, int length) throws IOExcep
tion { | |
358 Arrays.fill(value, offset, offset + length, (byte) 0); | |
359 } | |
360 | |
361 @Override | |
362 public void write(ByteBuffer value) throws IOException { | |
363 throw new UnsupportedOperationException(); | |
364 } | |
365 | |
366 @Override | |
367 public void writeLazy(ByteBuffer value) throws IOException { | |
368 throw new UnsupportedOperationException(); | |
369 } | |
370 }; | |
371 | |
372 stringUnderTest.writeTo(out); | |
373 byte[] allZeros = new byte[stringUnderTest.size()]; | |
374 assertTrue(classUnderTest + ".writeToInternal() must grant access to underly
ing array", | |
375 Arrays.equals(allZeros, stringUnderTest.toByteArray())); | |
376 } | 256 } |
377 | 257 |
378 public void testNewOutput() throws IOException { | 258 public void testNewOutput() throws IOException { |
379 ByteArrayOutputStream bos = new ByteArrayOutputStream(); | 259 ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
380 ByteString.Output output = ByteString.newOutput(); | 260 ByteString.Output output = ByteString.newOutput(); |
381 stringUnderTest.writeTo(output); | 261 stringUnderTest.writeTo(output); |
382 assertEquals("Output Size returns correct result", | 262 assertEquals("Output Size returns correct result", |
383 output.size(), stringUnderTest.size()); | 263 output.size(), stringUnderTest.size()); |
384 output.writeTo(bos); | 264 output.writeTo(bos); |
385 assertTrue("Output.writeTo() must give back the same bytes", | 265 assertTrue("Output.writeTo() must give back the same bytes", |
386 Arrays.equals(referenceBytes, bos.toByteArray())); | 266 Arrays.equals(referenceBytes, bos.toByteArray())); |
387 | 267 |
388 // write the output stream to itself! This should cause it to double | 268 // write the output stream to itself! This should cause it to double |
389 output.writeTo(output); | 269 output.writeTo(output); |
390 assertEquals("Writing an output stream to itself is successful", | 270 assertEquals("Writing an output stream to itself is successful", |
391 stringUnderTest.concat(stringUnderTest), output.toByteString()); | 271 stringUnderTest.concat(stringUnderTest), output.toByteString()); |
392 | 272 |
393 output.reset(); | 273 output.reset(); |
394 assertEquals("Output.reset() resets the output", 0, output.size()); | 274 assertEquals("Output.reset() resets the output", 0, output.size()); |
395 assertEquals("Output.reset() resets the output", | 275 assertEquals("Output.reset() resets the output", |
396 ByteString.EMPTY, output.toByteString()); | 276 ByteString.EMPTY, output.toByteString()); |
397 } | 277 |
398 | |
399 public void testToString() throws UnsupportedEncodingException { | |
400 String testString = "I love unicode \u1234\u5678 characters"; | |
401 ByteString unicode = ByteString.wrap(testString.getBytes(Internal.UTF_8)); | |
402 String roundTripString = unicode.toString(UTF_8); | |
403 assertEquals(classUnderTest + " unicode must match", testString, roundTripSt
ring); | |
404 } | |
405 | |
406 public void testCharsetToString() { | |
407 String testString = "I love unicode \u1234\u5678 characters"; | |
408 ByteString unicode = ByteString.wrap(testString.getBytes(Internal.UTF_8)); | |
409 String roundTripString = unicode.toString(Internal.UTF_8); | |
410 assertEquals(classUnderTest + " unicode must match", testString, roundTripSt
ring); | |
411 } | |
412 | |
413 public void testToString_returnsCanonicalEmptyString() { | |
414 assertSame(classUnderTest + " must be the same string references", | |
415 ByteString.EMPTY.toString(Internal.UTF_8), | |
416 ByteString.wrap(new byte[]{}).toString(Internal.UTF_8)); | |
417 } | |
418 | |
419 public void testToString_raisesException() { | |
420 try { | |
421 ByteString.EMPTY.toString("invalid"); | |
422 fail("Should have thrown an exception."); | |
423 } catch (UnsupportedEncodingException expected) { | |
424 // This is success | |
425 } | |
426 | |
427 try { | |
428 ByteString.wrap(referenceBytes).toString("invalid"); | |
429 fail("Should have thrown an exception."); | |
430 } catch (UnsupportedEncodingException expected) { | |
431 // This is success | |
432 } | |
433 } | |
434 | |
435 public void testEquals() { | |
436 assertEquals(classUnderTest + " must not equal null", false, stringUnderTest
.equals(null)); | |
437 assertEquals(classUnderTest + " must equal self", stringUnderTest, stringUnd
erTest); | |
438 assertFalse(classUnderTest + " must not equal the empty string", | |
439 stringUnderTest.equals(ByteString.EMPTY)); | |
440 assertEquals(classUnderTest + " empty strings must be equal", | |
441 ByteString.wrap(new byte[]{}), stringUnderTest.substring(55, 55)); | |
442 assertEquals(classUnderTest + " must equal another string with the same valu
e", | |
443 stringUnderTest, ByteString.wrap(referenceBytes)); | |
444 | |
445 byte[] mungedBytes = new byte[referenceBytes.length]; | |
446 System.arraycopy(referenceBytes, 0, mungedBytes, 0, referenceBytes.length); | |
447 mungedBytes[mungedBytes.length - 5] = (byte) (mungedBytes[mungedBytes.length
- 5] ^ 0xFF); | |
448 assertFalse(classUnderTest + " must not equal every string with the same len
gth", | |
449 stringUnderTest.equals(ByteString.wrap(mungedBytes))); | |
450 } | 278 } |
451 | 279 |
452 public void testHashCode() { | 280 public void testHashCode() { |
453 int hash = stringUnderTest.hashCode(); | 281 int hash = stringUnderTest.hashCode(); |
454 assertEquals(classUnderTest + " must have expected hashCode", expectedHashCo
de, hash); | 282 assertEquals(classUnderTest + " must have expected hashCode", expectedHashCo
de, hash); |
455 } | 283 } |
456 | 284 |
457 public void testPeekCachedHashCode() { | |
458 assertEquals(classUnderTest + ".peekCachedHashCode() should return zero at f
irst", 0, | |
459 stringUnderTest.peekCachedHashCode()); | |
460 stringUnderTest.hashCode(); | |
461 assertEquals(classUnderTest + ".peekCachedHashCode should return zero at fir
st", | |
462 expectedHashCode, stringUnderTest.peekCachedHashCode()); | |
463 } | |
464 | |
465 public void testPartialHash() { | |
466 // partialHash() is more strenuously tested elsewhere by testing hashes of s
ubstrings. | |
467 // This test would fail if the expected hash were 1. It's not. | |
468 int hash = stringUnderTest.partialHash(stringUnderTest.size(), 0, stringUnde
rTest.size()); | |
469 assertEquals(classUnderTest + ".partialHash() must yield expected hashCode", | |
470 expectedHashCode, hash); | |
471 } | |
472 | |
473 public void testNewInput() throws IOException { | 285 public void testNewInput() throws IOException { |
474 InputStream input = stringUnderTest.newInput(); | 286 InputStream input = stringUnderTest.newInput(); |
475 assertEquals("InputStream.available() returns correct value", | 287 assertEquals("InputStream.available() returns correct value", |
476 stringUnderTest.size(), input.available()); | 288 stringUnderTest.size(), input.available()); |
477 boolean stillEqual = true; | 289 boolean stillEqual = true; |
478 for (byte referenceByte : referenceBytes) { | 290 for (byte referenceByte : referenceBytes) { |
479 int expectedInt = (referenceByte & 0xFF); | 291 int expectedInt = (referenceByte & 0xFF); |
480 stillEqual = (expectedInt == input.read()); | 292 stillEqual = (expectedInt == input.read()); |
481 } | 293 } |
482 assertEquals("InputStream.available() returns correct value", | 294 assertEquals("InputStream.available() returns correct value", |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
522 /** | 334 /** |
523 * Make sure we keep things simple when concatenating with empty. See also | 335 * Make sure we keep things simple when concatenating with empty. See also |
524 * {@link ByteStringTest#testConcat_empty()}. | 336 * {@link ByteStringTest#testConcat_empty()}. |
525 */ | 337 */ |
526 public void testConcat_empty() { | 338 public void testConcat_empty() { |
527 assertSame(classUnderTest + " concatenated with empty must give " + classUnd
erTest, | 339 assertSame(classUnderTest + " concatenated with empty must give " + classUnd
erTest, |
528 stringUnderTest.concat(ByteString.EMPTY), stringUnderTest); | 340 stringUnderTest.concat(ByteString.EMPTY), stringUnderTest); |
529 assertSame("empty concatenated with " + classUnderTest + " must give " + cla
ssUnderTest, | 341 assertSame("empty concatenated with " + classUnderTest + " must give " + cla
ssUnderTest, |
530 ByteString.EMPTY.concat(stringUnderTest), stringUnderTest); | 342 ByteString.EMPTY.concat(stringUnderTest), stringUnderTest); |
531 } | 343 } |
532 | |
533 public void testJavaSerialization() throws Exception { | |
534 ByteArrayOutputStream out = new ByteArrayOutputStream(); | |
535 ObjectOutputStream oos = new ObjectOutputStream(out); | |
536 oos.writeObject(stringUnderTest); | |
537 oos.close(); | |
538 byte[] pickled = out.toByteArray(); | |
539 InputStream in = new ByteArrayInputStream(pickled); | |
540 ObjectInputStream ois = new ObjectInputStream(in); | |
541 Object o = ois.readObject(); | |
542 assertTrue("Didn't get a ByteString back", o instanceof ByteString); | |
543 assertEquals("Should get an equal ByteString back", stringUnderTest, o); | |
544 } | |
545 } | 344 } |
OLD | NEW |