1/* 2 * Copyright (C) 2014 Square, Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16package okio; 17 18import java.io.ObjectInputStream; 19import java.io.ObjectOutputStream; 20import java.io.Serializable; 21import java.util.Arrays; 22import java.util.Random; 23 24import static org.junit.Assert.assertEquals; 25import static org.junit.Assert.assertFalse; 26import static org.junit.Assert.assertTrue; 27 28final class TestUtil { 29 private TestUtil() { 30 } 31 32 static void assertByteArraysEquals(byte[] a, byte[] b) { 33 assertEquals(Arrays.toString(a), Arrays.toString(b)); 34 } 35 36 static void assertByteArrayEquals(String expectedUtf8, byte[] b) { 37 assertEquals(expectedUtf8, new String(b, Util.UTF_8)); 38 } 39 40 static ByteString randomBytes(int length) { 41 Random random = new Random(0); 42 byte[] randomBytes = new byte[length]; 43 random.nextBytes(randomBytes); 44 return ByteString.of(randomBytes); 45 } 46 47 static String repeat(char c, int count) { 48 char[] array = new char[count]; 49 Arrays.fill(array, c); 50 return new String(array); 51 } 52 53 public static void assertEquivalent(ByteString b1, ByteString b2) { 54 // Equals. 55 assertTrue(b1.equals(b2)); 56 assertTrue(b1.equals(b1)); 57 assertTrue(b2.equals(b1)); 58 59 // Hash code. 60 assertEquals(b1.hashCode(), b2.hashCode()); 61 assertEquals(b1.hashCode(), b1.hashCode()); 62 assertEquals(b1.toString(), b2.toString()); 63 64 // Content. 65 assertEquals(b1.size(), b2.size()); 66 byte[] b2Bytes = b2.toByteArray(); 67 for (int i = 0; i < b2Bytes.length; i++) { 68 byte b = b2Bytes[i]; 69 assertEquals(b, b1.getByte(i)); 70 } 71 assertByteArraysEquals(b1.toByteArray(), b2Bytes); 72 73 // Doesn't equal a different byte string. 74 assertFalse(b1.equals(null)); 75 assertFalse(b1.equals(new Object())); 76 if (b2Bytes.length > 0) { 77 byte[] b3Bytes = b2Bytes.clone(); 78 b3Bytes[b3Bytes.length - 1]++; 79 ByteString b3 = new ByteString(b3Bytes); 80 assertFalse(b1.equals(b3)); 81 assertFalse(b1.hashCode() == b3.hashCode()); 82 } else { 83 ByteString b3 = ByteString.encodeUtf8("a"); 84 assertFalse(b1.equals(b3)); 85 assertFalse(b1.hashCode() == b3.hashCode()); 86 } 87 } 88 89 public static void assertEquivalent(Buffer b1, Buffer b2) { 90 // Equals. 91 assertTrue(b1.equals(b2)); 92 assertTrue(b1.equals(b1)); 93 assertTrue(b2.equals(b1)); 94 95 // Hash code. 96 assertEquals(b1.hashCode(), b2.hashCode()); 97 assertEquals(b1.hashCode(), b1.hashCode()); 98 assertEquals(b1.toString(), b2.toString()); 99 100 // Content. 101 assertEquals(b1.size(), b2.size()); 102 Buffer buffer = new Buffer(); 103 b2.copyTo(buffer, 0, b2.size); 104 byte[] b2Bytes = b2.readByteArray(); 105 for (int i = 0; i < b2Bytes.length; i++) { 106 byte b = b2Bytes[i]; 107 assertEquals(b, b1.getByte(i)); 108 } 109 110 // Doesn't equal a different buffer. 111 assertFalse(b1.equals(null)); 112 assertFalse(b1.equals(new Object())); 113 if (b2Bytes.length > 0) { 114 byte[] b3Bytes = b2Bytes.clone(); 115 b3Bytes[b3Bytes.length - 1]++; 116 Buffer b3 = new Buffer().write(b3Bytes); 117 assertFalse(b1.equals(b3)); 118 assertFalse(b1.hashCode() == b3.hashCode()); 119 } else { 120 Buffer b3 = new Buffer().writeUtf8("a"); 121 assertFalse(b1.equals(b3)); 122 assertFalse(b1.hashCode() == b3.hashCode()); 123 } 124 } 125 126 /** Serializes original to bytes, then deserializes those bytes and returns the result. */ 127 @SuppressWarnings("unchecked") // Assume serialization doesn't change types. 128 public static <T extends Serializable> T reserialize(T original) throws Exception { 129 Buffer buffer = new Buffer(); 130 ObjectOutputStream out = new ObjectOutputStream(buffer.outputStream()); 131 out.writeObject(original); 132 ObjectInputStream in = new ObjectInputStream(buffer.inputStream()); 133 return (T) in.readObject(); 134 } 135} 136