001: /*
002: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
003: * notice. All rights reserved.
004: */
005: package com.terracotta.session.util;
006:
007: import com.terracotta.session.SessionId;
008:
009: import java.io.ByteArrayOutputStream;
010: import java.io.DataOutputStream;
011: import java.io.IOException;
012: import java.util.Arrays;
013: import java.util.HashMap;
014:
015: import junit.framework.TestCase;
016:
017: public class DefaultIdGeneratorTest extends TestCase {
018:
019: public final void testIdUniqueness() {
020: // this is a pretty silly attempt at uniqueness test...
021: HashMap map = new HashMap();
022: final String serverId = "SomeServerId";
023: final DefaultIdGenerator dig = new DefaultIdGenerator(20,
024: serverId);
025: final int expectedLength = 20;
026: // I tried running this from 0 to Integer.MAX_VALUE but it's taking a LOOOONG time and ran out of heap space :-(
027: for (int i = 0; i < Short.MAX_VALUE; i++) {
028: final String id = dig.generateKey();
029: assertEquals(expectedLength, id.length());
030: assertNull("i = " + i, map.get(id));
031: map.put(id, id);
032: }
033: for (int i = 0; i < 100; i++) {
034: final String id = dig.generateKey();
035: assertEquals(expectedLength, id.length());
036: assertNull("i = " + i, map.get(id));
037: map.put(id, id);
038: }
039: }
040:
041: public final void testNextId() {
042: final String serverId = "SomeServerId";
043: DefaultIdGenerator dig = new DefaultIdGenerator(20, serverId);
044: for (short s = Short.MIN_VALUE; true; s++) {
045: assertEquals(s, dig.getNextId());
046: if (s == Short.MAX_VALUE)
047: break;
048: }
049: // check loop-back
050: assertEquals(Short.MIN_VALUE, dig.getNextId());
051: }
052:
053: public final void testToBytes() throws IOException {
054: for (short s = Short.MIN_VALUE; true; s++) {
055: ByteArrayOutputStream baos = new ByteArrayOutputStream();
056: DataOutputStream dos = new DataOutputStream(baos);
057: dos.writeShort(s);
058: dos.close();
059: byte[] bytes = baos.toByteArray();
060: byte[] actualBytes = new byte[2];
061: DefaultIdGenerator.toBytes(s, actualBytes);
062: assertTrue(Arrays.equals(bytes, actualBytes));
063: if (s == Short.MAX_VALUE)
064: break;
065: }
066: }
067:
068: public final void testToHex() {
069: // test all single byte array
070: final byte[] oneByte = new byte[1];
071: for (byte b = Byte.MIN_VALUE; true; b++) {
072: oneByte[0] = b;
073: String out = DefaultIdGenerator.toHex(oneByte, 1);
074: String expected = Long.toHexString(0x00000000000000ff & b)
075: .toUpperCase();
076: expected = (expected.length() < 2) ? "0" + expected
077: : expected;
078: assertEquals("failed on byte = " + b, expected, out);
079: if (b == Byte.MAX_VALUE)
080: break;
081: }
082:
083: // test selected multi-byte arrays...
084: assertEquals("FFFF", DefaultIdGenerator.toHex(new byte[] {
085: (byte) 0xff, (byte) 0xff }, 2));
086: assertEquals("0F0F", DefaultIdGenerator.toHex(new byte[] {
087: (byte) 0x0f, (byte) 0x0f }, 2));
088: assertEquals("000F", DefaultIdGenerator.toHex(new byte[] {
089: (byte) 0x00, (byte) 0x0f }, 2));
090: assertEquals("01020304050607FF", DefaultIdGenerator.toHex(
091: new byte[] { (byte) 0x01, (byte) 0x02, (byte) 0x03,
092: (byte) 0x04, (byte) 0x05, (byte) 0x06,
093: (byte) 0x07, (byte) 0x0ff }, 8));
094: }
095:
096: public final void testIdLength() {
097: DefaultIdGenerator dig = new DefaultIdGenerator(-1234, null);
098: assertEquals(8, dig.generateKey().length());
099:
100: dig = new DefaultIdGenerator(7, null);
101: assertEquals(8, dig.generateKey().length());
102:
103: dig = new DefaultIdGenerator(8, null);
104: assertEquals(8, dig.generateKey().length());
105:
106: dig = new DefaultIdGenerator(9, null);
107: assertEquals(9, dig.generateKey().length());
108:
109: dig = new DefaultIdGenerator(31, null);
110: assertEquals(31, dig.generateKey().length());
111:
112: dig = new DefaultIdGenerator(52, null);
113: assertEquals(52, dig.generateKey().length());
114:
115: dig = new DefaultIdGenerator(666, null);
116: assertEquals(666, dig.generateKey().length());
117:
118: dig = new DefaultIdGenerator(777, null);
119: assertEquals(777, dig.generateKey().length());
120: }
121:
122: public final void testMakeInstanceFromBrowserId() {
123: final String key = "1234567890";
124: final String serverId = "1234567890";
125: final String newServerId = "someServerId";
126: final String browserId = key + "!" + serverId;
127: final String externalId = key + "!" + newServerId;
128: DefaultIdGenerator dig = new DefaultIdGenerator(10, newServerId);
129: SessionId id = dig.makeInstanceFromBrowserId(browserId);
130: assertEquals(key, id.getKey());
131: assertEquals(browserId, id.getRequestedId());
132: assertEquals(externalId, id.getExternalId());
133: }
134: }
|