KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > terracotta > session > util > DefaultIdGeneratorTest


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

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 JavaDoc;
11 import java.io.DataOutputStream JavaDoc;
12 import java.io.IOException JavaDoc;
13 import java.util.Arrays JavaDoc;
14 import java.util.HashMap JavaDoc;
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     // this is a pretty silly attempt at uniqueness test...
23
HashMap JavaDoc map = new HashMap JavaDoc();
24     final String JavaDoc serverId = "SomeServerId";
25     final DefaultIdGenerator dig = new DefaultIdGenerator(20, serverId, lockType);
26     final int expectedLength = 20;
27     // I tried running this from 0 to Integer.MAX_VALUE but it's taking a LOOOONG time and ran out of heap space :-(
28
for (int i = 0; i < Short.MAX_VALUE; i++) {
29       final String JavaDoc 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 JavaDoc 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 JavaDoc 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     // check loop-back
50
assertEquals(Short.MIN_VALUE, dig.getNextId());
51   }
52
53   public final void testToBytes() throws IOException JavaDoc {
54     for (short s = Short.MIN_VALUE; true; s++) {
55       ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
56       DataOutputStream JavaDoc dos = new DataOutputStream JavaDoc(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     // test all single byte array
69
final byte[] oneByte = new byte[1];
70     for (byte b = Byte.MIN_VALUE; true; b++) {
71       oneByte[0] = b;
72       String JavaDoc out = DefaultIdGenerator.toHex(oneByte, 1);
73       String JavaDoc 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     // test selected multi-byte arrays...
80
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 JavaDoc key = "1234567890";
115     final String JavaDoc serverId = "1234567890";
116     final String JavaDoc newServerId = "someServerId";
117     final String JavaDoc browserId = key + "!" + serverId;
118     final String JavaDoc 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