KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > terracotta > session > SessionDataTest


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

4 package com.terracotta.session;
5
6 import com.terracotta.session.util.MockContextMgr;
7 import com.terracotta.session.util.MockLifecycleEventMgr;
8
9 import java.util.Arrays JavaDoc;
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(), new MockContextMgr());
25     final String JavaDoc[] attributes = new String JavaDoc[] { "one", "two", "three", "four", "five" };
26     for (int i = 0; i < attributes.length; i++) {
27       String JavaDoc a = attributes[i];
28
29       // test set/get
30
sd.setAttribute(a, a);
31       String JavaDoc v = (String JavaDoc) sd.getAttribute(a);
32       assertSame(a, v);
33
34       // test attribute names
35
String JavaDoc[] namesOut = sd.getValueNames();
36       Arrays.sort(namesOut);
37       assertEquals(i + 1, namesOut.length);
38       for (int j = 0; j < i; j++)
39         assertTrue(Arrays.binarySearch(namesOut, attributes[j]) >= 0);
40
41       // test replace/get
42
final String JavaDoc newVal = new String JavaDoc("SomeNewString");
43       final String JavaDoc oldVal = (String JavaDoc) sd.setAttributeReturnOld(a, newVal);
44       assertSame(a, oldVal);
45       assertSame(newVal, sd.getAttribute(a));
46
47       // test remove
48
final String JavaDoc removedVal = (String JavaDoc) sd.removeAttributeReturnOld(a);
49       assertSame(newVal, removedVal);
50       assertNull(sd.removeAttributeReturnOld(a));
51
52       // put it back for further testing...
53
sd.setAttribute(a, a);
54     }
55   }
56 }
57
Popular Tags