1 // Protocol Buffers - Google's data interchange format 2 // Copyright 2008 Google Inc. All rights reserved. 3 // https://developers.google.com/protocol-buffers/ 4 // 5 // Redistribution and use in source and binary forms, with or without 6 // modification, are permitted provided that the following conditions are 7 // met: 8 // 9 // * Redistributions of source code must retain the above copyright 10 // notice, this list of conditions and the following disclaimer. 11 // * Redistributions in binary form must reproduce the above 12 // copyright notice, this list of conditions and the following disclaimer 13 // in the documentation and/or other materials provided with the 14 // distribution. 15 // * Neither the name of Google Inc. nor the names of its 16 // contributors may be used to endorse or promote products derived from 17 // this software without specific prior written permission. 18 // 19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 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. 30 31 package com.google.protobuf; 32 33 import java.io.ByteArrayInputStream; 34 import java.io.ByteArrayOutputStream; 35 import java.io.InputStream; 36 import java.io.ObjectInputStream; 37 import java.io.ObjectOutputStream; 38 import java.io.UnsupportedEncodingException; 39 import java.util.Arrays; 40 import java.util.Iterator; 41 42 /** 43 * This class tests {@link RopeByteString} by inheriting the tests from 44 * {@link LiteralByteStringTest}. Only a couple of methods are overridden. 45 * 46 * <p>A full test of the result of {@link RopeByteString#substring(int, int)} is found in the 47 * separate class {@link RopeByteStringSubstringTest}. 48 * 49 * @author carlanton@google.com (Carl Haverl) 50 */ 51 public class RopeByteStringTest extends LiteralByteStringTest { 52 53 @Override setUp()54 protected void setUp() throws Exception { 55 classUnderTest = "RopeByteString"; 56 referenceBytes = ByteStringTest.getTestBytes(22341, 22337766L); 57 Iterator<ByteString> iter = ByteStringTest.makeConcretePieces(referenceBytes).iterator(); 58 stringUnderTest = iter.next(); 59 while (iter.hasNext()) { 60 stringUnderTest = stringUnderTest.concat(iter.next()); 61 } 62 expectedHashCode = -1214197238; 63 } 64 65 @Override testGetTreeDepth()66 public void testGetTreeDepth() { 67 assertEquals(classUnderTest + " must have the expected tree depth", 68 4, stringUnderTest.getTreeDepth()); 69 } 70 testBalance()71 public void testBalance() { 72 int numberOfPieces = 10000; 73 int pieceSize = 64; 74 byte[] testBytes = ByteStringTest.getTestBytes(numberOfPieces * pieceSize, 113377L); 75 76 // Build up a big ByteString from smaller pieces to force a rebalance 77 ByteString concatenated = ByteString.EMPTY; 78 for (int i = 0; i < numberOfPieces; ++i) { 79 concatenated = concatenated.concat(ByteString.copyFrom(testBytes, i * pieceSize, pieceSize)); 80 } 81 82 assertEquals(classUnderTest + " from string must have the expected type", 83 classUnderTest, getActualClassName(concatenated)); 84 assertTrue(classUnderTest + " underlying bytes must match after balancing", 85 Arrays.equals(testBytes, concatenated.toByteArray())); 86 ByteString testString = ByteString.copyFrom(testBytes); 87 assertTrue(classUnderTest + " balanced string must equal flat string", 88 concatenated.equals(testString)); 89 assertTrue(classUnderTest + " flat string must equal balanced string", 90 testString.equals(concatenated)); 91 assertEquals(classUnderTest + " balanced string must have same hash code as flat string", 92 testString.hashCode(), concatenated.hashCode()); 93 } 94 95 @Override testToString()96 public void testToString() throws UnsupportedEncodingException { 97 String sourceString = "I love unicode \u1234\u5678 characters"; 98 ByteString sourceByteString = ByteString.copyFromUtf8(sourceString); 99 int copies = 250; 100 101 // By building the RopeByteString by concatenating, this is actually a fairly strenuous test. 102 StringBuilder builder = new StringBuilder(copies * sourceString.length()); 103 ByteString unicode = ByteString.EMPTY; 104 for (int i = 0; i < copies; ++i) { 105 builder.append(sourceString); 106 unicode = RopeByteString.concatenate(unicode, sourceByteString); 107 } 108 String testString = builder.toString(); 109 110 assertEquals(classUnderTest + " from string must have the expected type", 111 classUnderTest, getActualClassName(unicode)); 112 String roundTripString = unicode.toString(UTF_8); 113 assertEquals(classUnderTest + " unicode bytes must match", 114 testString, roundTripString); 115 ByteString flatString = ByteString.copyFromUtf8(testString); 116 assertEquals(classUnderTest + " string must equal the flat string", flatString, unicode); 117 assertEquals(classUnderTest + " string must must have same hashCode as the flat string", 118 flatString.hashCode(), unicode.hashCode()); 119 } 120 121 @Override testCharsetToString()122 public void testCharsetToString() { 123 String sourceString = "I love unicode \u1234\u5678 characters"; 124 ByteString sourceByteString = ByteString.copyFromUtf8(sourceString); 125 int copies = 250; 126 127 // By building the RopeByteString by concatenating, this is actually a fairly strenuous test. 128 StringBuilder builder = new StringBuilder(copies * sourceString.length()); 129 ByteString unicode = ByteString.EMPTY; 130 for (int i = 0; i < copies; ++i) { 131 builder.append(sourceString); 132 unicode = RopeByteString.concatenate(unicode, sourceByteString); 133 } 134 String testString = builder.toString(); 135 136 assertEquals(classUnderTest + " from string must have the expected type", 137 classUnderTest, getActualClassName(unicode)); 138 String roundTripString = unicode.toString(Internal.UTF_8); 139 assertEquals(classUnderTest + " unicode bytes must match", 140 testString, roundTripString); 141 ByteString flatString = ByteString.copyFromUtf8(testString); 142 assertEquals(classUnderTest + " string must equal the flat string", flatString, unicode); 143 assertEquals(classUnderTest + " string must must have same hashCode as the flat string", 144 flatString.hashCode(), unicode.hashCode()); 145 } 146 147 @Override testToString_returnsCanonicalEmptyString()148 public void testToString_returnsCanonicalEmptyString() { 149 RopeByteString ropeByteString = 150 RopeByteString.newInstanceForTest(ByteString.EMPTY, ByteString.EMPTY); 151 assertSame(classUnderTest + " must be the same string references", 152 ByteString.EMPTY.toString(Internal.UTF_8), ropeByteString.toString(Internal.UTF_8)); 153 } 154 155 @Override testToString_raisesException()156 public void testToString_raisesException() { 157 try { 158 ByteString byteString = 159 RopeByteString.newInstanceForTest(ByteString.EMPTY, ByteString.EMPTY); 160 byteString.toString("invalid"); 161 fail("Should have thrown an exception."); 162 } catch (UnsupportedEncodingException expected) { 163 // This is success 164 } 165 166 try { 167 ByteString byteString = RopeByteString.concatenate(ByteString.copyFromUtf8("foo"), 168 ByteString.copyFromUtf8("bar")); 169 byteString.toString("invalid"); 170 fail("Should have thrown an exception."); 171 } catch (UnsupportedEncodingException expected) { 172 // This is success 173 } 174 } 175 176 @Override testJavaSerialization()177 public void testJavaSerialization() throws Exception { 178 ByteArrayOutputStream out = new ByteArrayOutputStream(); 179 ObjectOutputStream oos = new ObjectOutputStream(out); 180 oos.writeObject(stringUnderTest); 181 oos.close(); 182 byte[] pickled = out.toByteArray(); 183 InputStream in = new ByteArrayInputStream(pickled); 184 ObjectInputStream ois = new ObjectInputStream(in); 185 Object o = ois.readObject(); 186 assertTrue("Didn't get a ByteString back", o instanceof ByteString); 187 assertEquals("Should get an equal ByteString back", stringUnderTest, o); 188 } 189 } 190