1 5 package com.tc.object.dna.impl; 6 7 import org.apache.commons.io.IOUtils; 8 9 import com.tc.bytes.TCByteBuffer; 10 import com.tc.io.TCByteBufferInputStream; 11 import com.tc.io.TCByteBufferOutputStream; 12 import com.tc.io.serializer.TCObjectInputStream; 13 import com.tc.object.ObjectID; 14 import com.tc.object.bytecode.MockClassProvider; 15 import com.tc.object.loaders.ClassProvider; 16 17 import java.io.ByteArrayInputStream ; 18 import java.io.ByteArrayOutputStream ; 19 import java.math.BigDecimal ; 20 import java.math.BigInteger ; 21 import java.util.ArrayList ; 22 import java.util.Arrays ; 23 import java.util.Iterator ; 24 import java.util.List ; 25 import java.util.Random ; 26 27 import junit.framework.TestCase; 28 29 public class DNAEncodingTest extends TestCase { 30 31 Random rnd = new Random (); 32 ClassProvider classProvider = new MockClassProvider(); 33 34 public void testZeroLengthByteArray() throws Exception { 35 TCByteBufferOutputStream output = new TCByteBufferOutputStream(); 36 37 byte[] b = new byte[] {}; 38 39 DNAEncoding encoding = getApplicatorEncoding(); 40 encoding.encode(b, output); 41 42 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 45 IOUtils.copy(new TCByteBufferInputStream(output.toArray()), baos); 46 TCObjectInputStream input = new TCObjectInputStream(new ByteArrayInputStream (baos.toByteArray())); 47 48 assertTrue(Arrays.equals(b, (byte[]) encoding.decode(input))); 49 50 assertEquals(0, input.available()); 51 } 52 53 public void testNonPrimitiveArrays2() throws Exception { 54 TCByteBufferOutputStream output = new TCByteBufferOutputStream(); 55 56 Integer [] array = new Integer [] { new Integer (43), new Integer (-1) }; 57 ObjectID[] array2 = new ObjectID[] {}; 58 59 DNAEncoding encoding = getApplicatorEncoding(); 60 encoding.encodeArray(array, output); 61 encoding.encodeArray(array2, output); 62 63 TCByteBufferInputStream input = new TCByteBufferInputStream(output.toArray()); 64 65 assertTrue(Arrays.equals(array, (Object []) encoding.decode(input))); 66 assertTrue(Arrays.equals(array2, (Object []) encoding.decode(input))); 67 68 assertEquals(0, input.available()); 69 } 70 71 private DNAEncoding getApplicatorEncoding() { 72 return new DNAEncoding(classProvider); 73 } 74 75 public void testNonPrimitiveArrays() throws Exception { 76 TCByteBufferOutputStream output = new TCByteBufferOutputStream(); 77 78 Object [] array = new Object [] { new ObjectID(12), new Integer (34), new Double (Math.PI), ObjectID.NULL_ID, 79 new Long (Long.MIN_VALUE + 34), "timmy" }; 80 81 DNAEncoding encoding = getApplicatorEncoding(); 82 encoding.encodeArray(array, output); 83 84 TCByteBufferInputStream input = new TCByteBufferInputStream(output.toArray()); 85 86 assertTrue(Arrays.equals(array, (Object []) encoding.decode(input))); 87 88 assertEquals(0, input.available()); 89 } 90 91 public void testNullArray() throws Exception { 92 TCByteBufferOutputStream output = new TCByteBufferOutputStream(); 93 94 DNAEncoding encoding = getApplicatorEncoding(); 95 encoding.encodeArray(null, output); 96 TCByteBufferInputStream input = new TCByteBufferInputStream(output.toArray()); 97 98 assertNull(encoding.decode(input)); 99 100 assertEquals(0, input.available()); 101 } 102 103 public void testPrimitiveArrays() throws Exception { 104 105 DNAEncoding encoding = getApplicatorEncoding(); 106 for (int iter = 0; iter < 250; iter++) { 107 TCByteBufferOutputStream output = new TCByteBufferOutputStream(); 108 109 byte[] b = makeByteArray(); 110 char[] c = makeCharArray(); 111 double[] d = makeDoubleArray(); 112 float[] f = makeFloatArray(); 113 int[] i = makeIntArray(); 114 long[] j = makeLongArray(); 115 short[] s = makeShortArray(); 116 boolean[] z = makeBooleanArray(); 117 118 encoding.encodeArray(b, output); 119 encoding.encodeArray(c, output); 120 encoding.encodeArray(d, output); 121 encoding.encodeArray(f, output); 122 encoding.encodeArray(i, output); 123 encoding.encodeArray(j, output); 124 encoding.encodeArray(s, output); 125 encoding.encodeArray(z, output); 126 127 TCByteBufferInputStream input = new TCByteBufferInputStream(output.toArray()); 128 129 assertTrue(Arrays.equals(b, (byte[]) encoding.decode(input))); 130 assertTrue(Arrays.equals(c, (char[]) encoding.decode(input))); 131 assertTrue(Arrays.equals(d, (double[]) encoding.decode(input))); 132 assertTrue(Arrays.equals(f, (float[]) encoding.decode(input))); 133 assertTrue(Arrays.equals(i, (int[]) encoding.decode(input))); 134 assertTrue(Arrays.equals(j, (long[]) encoding.decode(input))); 135 assertTrue(Arrays.equals(s, (short[]) encoding.decode(input))); 136 assertTrue(Arrays.equals(z, (boolean[]) encoding.decode(input))); 137 138 assertEquals(0, input.available()); 139 } 140 } 141 142 private short[] makeShortArray() { 143 short[] rv = new short[rnd.nextInt(10)]; 144 for (int i = 0; i < rv.length; i++) { 145 rv[i] = (short) rnd.nextInt(); 146 } 147 return rv; 148 } 149 150 private long[] makeLongArray() { 151 long[] rv = new long[rnd.nextInt(10)]; 152 for (int i = 0; i < rv.length; i++) { 153 rv[i] = rnd.nextLong(); 154 } 155 return rv; 156 } 157 158 private int[] makeIntArray() { 159 int[] rv = new int[rnd.nextInt(10)]; 160 for (int i = 0; i < rv.length; i++) { 161 rv[i] = rnd.nextInt(); 162 } 163 return rv; 164 } 165 166 private float[] makeFloatArray() { 167 float[] rv = new float[rnd.nextInt(10)]; 168 for (int i = 0; i < rv.length; i++) { 169 rv[i] = rnd.nextFloat(); 170 } 171 172 return rv; 173 } 174 175 private double[] makeDoubleArray() { 176 double[] rv = new double[rnd.nextInt(10)]; 177 for (int i = 0; i < rv.length; i++) { 178 rv[i] = rnd.nextDouble(); 179 } 180 181 return rv; 182 } 183 184 private char[] makeCharArray() { 185 char[] rv = new char[rnd.nextInt(10)]; 186 for (int i = 0; i < rv.length; i++) { 187 rv[i] = new Character ((char) rnd.nextInt(Character.MAX_VALUE)).charValue(); 188 } 189 return rv; 190 } 191 192 private byte[] makeByteArray() { 193 byte[] rv = new byte[rnd.nextInt(10)]; 194 for (int i = 0; i < rv.length; i++) { 195 rv[i] = (byte) rnd.nextInt(); 196 } 197 return rv; 198 } 199 200 private boolean[] makeBooleanArray() { 201 boolean[] rv = new boolean[rnd.nextInt(10)]; 202 for (int i = 0; i < rv.length; i++) { 203 rv[i] = rnd.nextBoolean(); 204 } 205 return rv; 206 } 207 208 public void testStringDecode() throws Exception { 209 TCByteBufferOutputStream output = new TCByteBufferOutputStream(); 210 211 DNAEncoding encoding = getApplicatorEncoding(); 212 encoding.encode("timmy", output); 213 UTF8ByteDataHolder orgUTF; 214 encoding.encode((orgUTF = new UTF8ByteDataHolder("teck".getBytes("UTF-8"))), output); 215 216 TCByteBuffer[] data = output.toArray(); 217 218 encoding = getStorageEncoder(); 219 TCByteBufferInputStream input = new TCByteBufferInputStream(data); 220 UTF8ByteDataHolder decoded = (UTF8ByteDataHolder) encoding.decode(input); 221 assertTrue(Arrays.equals("timmy".getBytes("UTF-8"), decoded.getBytes())); 222 decoded = (UTF8ByteDataHolder) encoding.decode(input); 223 assertTrue(Arrays.equals("teck".getBytes("UTF-8"), decoded.getBytes())); 224 assertEquals(0, input.available()); 225 226 encoding = getApplicatorEncoding(); 227 input = new TCByteBufferInputStream(data); 228 String str = (String ) encoding.decode(input); 229 assertEquals("timmy", str); 230 str = (String ) encoding.decode(input); 231 assertEquals("teck", str); 232 assertEquals(0, input.available()); 233 234 encoding = getSerializerEncoder(); 235 input = new TCByteBufferInputStream(data); 236 str = (String ) encoding.decode(input); 237 assertEquals("timmy", str); 238 decoded = (UTF8ByteDataHolder) encoding.decode(input); 239 assertEquals(orgUTF, decoded); 240 assertEquals(0, input.available()); 241 } 242 243 private DNAEncoding getStorageEncoder() { 244 return new DNAEncoding(DNAEncoding.STORAGE); 245 } 246 247 private DNAEncoding getSerializerEncoder() { 248 return new DNAEncoding(DNAEncoding.SERIALIZER); 249 } 250 251 public void testClassExpand() throws Exception { 252 TCByteBufferOutputStream output = new TCByteBufferOutputStream(); 253 254 DNAEncoding encoding = getApplicatorEncoding(); 255 encoding.encode(getClass(), output); 256 Class c = Object .class; 257 UTF8ByteDataHolder name = new UTF8ByteDataHolder(c.getName()); 258 UTF8ByteDataHolder def = new UTF8ByteDataHolder(classProvider.getLoaderDescriptionFor(c)); 259 ClassInstance ci = new ClassInstance(name, def); 260 encoding.encode(ci, output); 261 262 TCByteBuffer[] data = output.toArray(); 263 264 encoding = getStorageEncoder(); 265 TCByteBufferInputStream input = new TCByteBufferInputStream(data); 266 ClassInstance holder = (ClassInstance) encoding.decode(input); 267 assertEquals(getClass().getName(), holder.getName().asString()); 268 assertEquals(classProvider.getLoaderDescriptionFor(getClass()), holder.getLoaderDef().asString()); 269 270 holder = (ClassInstance) encoding.decode(input); 271 assertEquals(name, holder.getName()); 272 assertEquals(def, holder.getLoaderDef()); 273 274 assertEquals(0, input.available()); 275 276 encoding = getApplicatorEncoding(); 277 input = new TCByteBufferInputStream(data); 278 c = (Class ) encoding.decode(input); 279 assertEquals(getClass(), c); 280 c = (Class ) encoding.decode(input); 281 assertEquals(Object .class, c); 282 assertEquals(0, input.available()); 283 284 } 285 286 public void testClassSerialize() throws Exception { 287 TCByteBufferOutputStream output = new TCByteBufferOutputStream(); 288 289 DNAEncoding encoding = getSerializerEncoder(); 290 encoding.encode(getClass(), output); 291 Class c = Object .class; 292 UTF8ByteDataHolder name = new UTF8ByteDataHolder(c.getName()); 293 UTF8ByteDataHolder def = new UTF8ByteDataHolder(classProvider.getLoaderDescriptionFor(c)); 294 ClassInstance ci = new ClassInstance(name, def); 295 encoding.encode(ci, output); 296 297 TCByteBuffer[] data = output.toArray(); 298 299 encoding = getSerializerEncoder(); 300 TCByteBufferInputStream input = new TCByteBufferInputStream(data); 301 c = (Class ) encoding.decode(input); 302 assertEquals(getClass(), c); 303 ClassInstance holder = (ClassInstance) encoding.decode(input); 304 assertEquals(ci, holder); 305 assertEquals(0, input.available()); 306 } 307 308 public void testBasic() throws Exception { 309 TCByteBufferOutputStream output = new TCByteBufferOutputStream(); 310 311 List data = new ArrayList (); 312 data.add(new ObjectID(1)); 313 data.add("one"); 314 data.add(new Boolean (true)); 315 data.add("two"); 316 data.add(new Byte ((byte) 42)); 317 data.add("three"); 318 data.add(new Character ('\t')); 319 data.add("four"); 320 data.add(new Double (Math.PI)); 321 data.add("five"); 322 data.add(new Float (Math.E)); 323 data.add("six"); 324 data.add(new Integer (Integer.MAX_VALUE)); 325 data.add("seven"); 326 data.add(new Long (System.currentTimeMillis() % 17)); 327 data.add("eight"); 328 data.add(new Short ((short) -1)); 329 data.add("nine"); 330 data.add(new BigInteger (512, new Random ())); 331 data.add("ten"); 332 data.add(new BigDecimal (84564547.45465478d)); 333 334 DNAEncoding encoding = getApplicatorEncoding(); 335 for (Iterator i = data.iterator(); i.hasNext();) { 336 encoding.encode(i.next(), output); 337 } 338 339 TCByteBufferInputStream input = new TCByteBufferInputStream(output.toArray()); 340 for (Iterator i = data.iterator(); i.hasNext();) { 341 Object orig = i.next(); 342 Object decoded = encoding.decode(input); 343 344 assertEquals(orig, decoded); 345 } 346 347 assertEquals(0, input.available()); 348 } 349 350 } 351 | Popular Tags |