001: /*
002: * Created on Jun 15, 2005 11:57:08 AM
003: */
004: package net.jforum;
005:
006: import junit.framework.TestCase;
007: import net.jforum.cache.DefaultCacheEngine;
008: import net.jforum.context.web.WebRequestContext;
009: import net.jforum.context.web.WebResponseContext;
010: import net.jforum.context.RequestContext;
011: import net.jforum.context.ResponseContext;
012: import net.jforum.context.ForumContext;
013: import net.jforum.context.JForumContext;
014: import net.jforum.entities.UserSession;
015: import net.jforum.http.FakeHttpRequest;
016: import net.jforum.http.FakeHttpResponse;
017: import net.jforum.util.preferences.ConfigKeys;
018: import net.jforum.util.preferences.SystemGlobals;
019:
020: /**
021: * Test {@link net.jforum.SessionFacade} methods for online users
022: *
023: * @author Rafael Steil
024: * @version $Id: OnlineUsersTest.java,v 1.9 2006/08/24 21:03:00 sergemaslyukov Exp $
025: */
026: public class OnlineUsersTest extends TestCase {
027: private static final int ANONYMOUS = 1;
028:
029: protected void setUp() throws Exception {
030: new SessionFacade().setCacheEngine(new DefaultCacheEngine());
031:
032: RequestContext requestContext = new WebRequestContext(
033: new FakeHttpRequest());
034: ResponseContext responseContext = new WebResponseContext(
035: new FakeHttpResponse());
036:
037: ForumContext forumContext = new JForumContext(requestContext
038: .getContextPath(), SystemGlobals
039: .getValue(ConfigKeys.SERVLET_EXTENSION),
040: requestContext, responseContext, false);
041: JForumExecutionContext ex = JForumExecutionContext.get();
042: ex.setForumContext(forumContext);
043:
044: JForumExecutionContext.set(ex);
045:
046: SystemGlobals.setValue(ConfigKeys.ANONYMOUS_USER_ID, Integer
047: .toString(ANONYMOUS));
048: }
049:
050: /**
051: * Check if guest users are being counted correctly
052: */
053: public void testAnonymousCount() {
054: this .createUserSession(ANONYMOUS, ANONYMOUS + 1 + "_"
055: + System.currentTimeMillis());
056: this .createUserSession(ANONYMOUS, ANONYMOUS + 2 + "_"
057: + System.currentTimeMillis());
058: this .createUserSession(ANONYMOUS, ANONYMOUS + 3 + "_"
059: + System.currentTimeMillis());
060:
061: assertEquals(3, SessionFacade.anonymousSize());
062: }
063:
064: /**
065: * Check if counting of both guest and logged users is correct
066: */
067: public void test2Anymous1Logged() {
068: // Anonymous
069: this .createUserSession(ANONYMOUS, ANONYMOUS + "1_"
070: + System.currentTimeMillis());
071: this .createUserSession(ANONYMOUS, ANONYMOUS + "2_"
072: + System.currentTimeMillis());
073:
074: // Logged
075: SessionFacade.setAttribute("logged", "1");
076: this
077: .createUserSession(2, "logged"
078: + System.currentTimeMillis());
079:
080: // Assert
081: assertEquals(2, SessionFacade.anonymousSize());
082: assertEquals(1, SessionFacade.registeredSize());
083: }
084:
085: /**
086: * First register as anonymous, then change to logged, and check counting
087: */
088: public void testAnonymousThenLogged() {
089: // Anonymous
090: String sessionId = ANONYMOUS + "1_"
091: + System.currentTimeMillis();
092:
093: this .createUserSession(ANONYMOUS, sessionId);
094:
095: assertEquals(1, SessionFacade.anonymousSize());
096: assertEquals(0, SessionFacade.registeredSize());
097:
098: // Logged
099: UserSession us = SessionFacade.getUserSession(sessionId);
100: us.setUserId(2);
101:
102: SessionFacade.setAttribute("logged", "1");
103:
104: SessionFacade.remove(sessionId);
105: SessionFacade.add(us);
106:
107: assertEquals(0, SessionFacade.anonymousSize());
108: assertEquals(1, SessionFacade.registeredSize());
109: }
110:
111: public void test3LoggedThen1Logout() {
112: // Logged
113: SessionFacade.setAttribute("logged", "1");
114:
115: this .createUserSession(2, "2_" + System.currentTimeMillis());
116:
117: String sessionId = "3_" + System.currentTimeMillis();
118: this .createUserSession(3, sessionId);
119:
120: this .createUserSession(4, "4_" + System.currentTimeMillis());
121:
122: assertEquals(3, SessionFacade.registeredSize());
123: assertEquals(0, SessionFacade.anonymousSize());
124:
125: // Logout (goes as guest)
126: SessionFacade.removeAttribute("logged");
127: SessionFacade.remove(sessionId);
128:
129: this .createUserSession(ANONYMOUS, sessionId);
130:
131: assertEquals(2, SessionFacade.registeredSize());
132: assertEquals(1, SessionFacade.anonymousSize());
133: }
134:
135: private void createUserSession(int userId, String sessionId) {
136: UserSession us = new UserSession();
137:
138: us.setUserId(userId);
139: us.setSessionId(sessionId);
140: us.setUsername("blah_" + System.currentTimeMillis());
141:
142: SessionFacade.add(us, sessionId);
143: }
144: }
|