01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.terracotta.session;
05:
06: import com.terracotta.session.util.MockContextMgr;
07: import com.terracotta.session.util.MockLifecycleEventMgr;
08:
09: import java.util.Arrays;
10:
11: import junit.framework.TestCase;
12:
13: public class SessionDataTest extends TestCase {
14:
15: public final void testConstructor() {
16: final int maxIdleSeconds = 123;
17: SessionData sd = new SessionData(maxIdleSeconds);
18: assertEquals(maxIdleSeconds, sd.getMaxInactiveMillis() / 1000);
19: }
20:
21: public final void testCollection() {
22: final int maxIdleSeconds = 123;
23: SessionData sd = new SessionData(maxIdleSeconds);
24: sd.associate(new MockSessionId(), new MockLifecycleEventMgr(),
25: new MockContextMgr(), new MockSessionManager());
26: final String[] attributes = new String[] { "one", "two",
27: "three", "four", "five" };
28: for (int i = 0; i < attributes.length; i++) {
29: String a = attributes[i];
30:
31: // test set/get
32: sd.setAttribute(a, a);
33: String v = (String) sd.getAttribute(a);
34: assertSame(a, v);
35:
36: // test attribute names
37: String[] namesOut = sd.getValueNames();
38: Arrays.sort(namesOut);
39: assertEquals(i + 1, namesOut.length);
40: for (int j = 0; j < i; j++)
41: assertTrue(Arrays.binarySearch(namesOut, attributes[j]) >= 0);
42:
43: // test replace/get
44: final String newVal = new String("SomeNewString");
45: final String oldVal = (String) sd.setAttributeReturnOld(a,
46: newVal);
47: assertSame(a, oldVal);
48: assertSame(newVal, sd.getAttribute(a));
49:
50: // test remove
51: final String removedVal = (String) sd
52: .removeAttributeReturnOld(a);
53: assertSame(newVal, removedVal);
54: assertNull(sd.removeAttributeReturnOld(a));
55:
56: // put it back for further testing...
57: sd.setAttribute(a, a);
58: }
59: }
60: }
|