1 5 package com.terracotta.session.util; 6 7 import com.tc.object.bytecode.Manager; 8 import com.terracotta.session.SessionId; 9 10 import java.io.ByteArrayOutputStream ; 11 import java.io.DataOutputStream ; 12 import java.io.IOException ; 13 import java.util.Arrays ; 14 import java.util.HashMap ; 15 16 import junit.framework.TestCase; 17 18 public class DefaultIdGeneratorTest extends TestCase { 19 private final static int lockType = Manager.LOCK_TYPE_WRITE; 20 21 public final void testIdUniqueness() { 22 HashMap map = new HashMap (); 24 final String serverId = "SomeServerId"; 25 final DefaultIdGenerator dig = new DefaultIdGenerator(20, serverId, lockType); 26 final int expectedLength = 20; 27 for (int i = 0; i < Short.MAX_VALUE; i++) { 29 final String id = dig.generateKey(); 30 assertEquals(expectedLength, id.length()); 31 assertNull("i = " + i, map.get(id)); 32 map.put(id, id); 33 } 34 for (int i = 0; i < 100; i++) { 35 final String id = dig.generateKey(); 36 assertEquals(expectedLength, id.length()); 37 assertNull("i = " + i, map.get(id)); 38 map.put(id, id); 39 } 40 } 41 42 public final void testNextId() { 43 final String serverId = "SomeServerId"; 44 DefaultIdGenerator dig = new DefaultIdGenerator(20, serverId, lockType); 45 for (short s = Short.MIN_VALUE; true; s++) { 46 assertEquals(s, dig.getNextId()); 47 if (s == Short.MAX_VALUE) break; 48 } 49 assertEquals(Short.MIN_VALUE, dig.getNextId()); 51 } 52 53 public final void testToBytes() throws IOException { 54 for (short s = Short.MIN_VALUE; true; s++) { 55 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 56 DataOutputStream dos = new DataOutputStream (baos); 57 dos.writeShort(s); 58 dos.close(); 59 byte[] bytes = baos.toByteArray(); 60 byte[] actualBytes = new byte[2]; 61 DefaultIdGenerator.toBytes(s, actualBytes); 62 assertTrue(Arrays.equals(bytes, actualBytes)); 63 if (s == Short.MAX_VALUE) break; 64 } 65 } 66 67 public final void testToHex() { 68 final byte[] oneByte = new byte[1]; 70 for (byte b = Byte.MIN_VALUE; true; b++) { 71 oneByte[0] = b; 72 String out = DefaultIdGenerator.toHex(oneByte, 1); 73 String expected = Long.toHexString(0x00000000000000ff & b).toUpperCase(); 74 expected = (expected.length() < 2) ? "0" + expected : expected; 75 assertEquals("failed on byte = " + b, expected, out); 76 if (b == Byte.MAX_VALUE) break; 77 } 78 79 assertEquals("FFFF", DefaultIdGenerator.toHex(new byte[] { (byte) 0xff, (byte) 0xff }, 2)); 81 assertEquals("0F0F", DefaultIdGenerator.toHex(new byte[] { (byte) 0x0f, (byte) 0x0f }, 2)); 82 assertEquals("000F", DefaultIdGenerator.toHex(new byte[] { (byte) 0x00, (byte) 0x0f }, 2)); 83 assertEquals("01020304050607FF", DefaultIdGenerator.toHex(new byte[] { (byte) 0x01, (byte) 0x02, (byte) 0x03, 84 (byte) 0x04, (byte) 0x05, (byte) 0x06, (byte) 0x07, (byte) 0x0ff }, 8)); 85 } 86 87 public final void testIdLength() { 88 DefaultIdGenerator dig = new DefaultIdGenerator(-1234, null, lockType); 89 assertEquals(8, dig.generateKey().length()); 90 91 dig = new DefaultIdGenerator(7, null, lockType); 92 assertEquals(8, dig.generateKey().length()); 93 94 dig = new DefaultIdGenerator(8, null, lockType); 95 assertEquals(8, dig.generateKey().length()); 96 97 dig = new DefaultIdGenerator(9, null, lockType); 98 assertEquals(9, dig.generateKey().length()); 99 100 dig = new DefaultIdGenerator(31, null, lockType); 101 assertEquals(31, dig.generateKey().length()); 102 103 dig = new DefaultIdGenerator(52, null, lockType); 104 assertEquals(52, dig.generateKey().length()); 105 106 dig = new DefaultIdGenerator(666, null, lockType); 107 assertEquals(666, dig.generateKey().length()); 108 109 dig = new DefaultIdGenerator(777, null, lockType); 110 assertEquals(777, dig.generateKey().length()); 111 } 112 113 public final void testMakeInstanceFromBrowserId() { 114 final String key = "1234567890"; 115 final String serverId = "1234567890"; 116 final String newServerId = "someServerId"; 117 final String browserId = key + "!" + serverId; 118 final String externalId = key + "!" + newServerId; 119 DefaultIdGenerator dig = new DefaultIdGenerator(10, newServerId, lockType); 120 SessionId id = dig.makeInstanceFromBrowserId(browserId); 121 assertEquals(key, id.getKey()); 122 assertEquals(browserId, id.getRequestedId()); 123 assertEquals(externalId, id.getExternalId()); 124 } 125 } 126 | Popular Tags |