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.tc.exception.ImplementMe;
008: import com.tc.logging.TCLogger;
009: import com.tc.logging.TCLogging;
010: import com.tc.properties.TCProperties;
011: import com.tc.properties.TCPropertiesImpl;
012: import com.terracotta.session.MockWebAppConfig;
013:
014: import java.util.Properties;
015:
016: import javax.servlet.http.HttpSessionAttributeListener;
017: import javax.servlet.http.HttpSessionBindingEvent;
018: import javax.servlet.http.HttpSessionEvent;
019: import javax.servlet.http.HttpSessionListener;
020:
021: import junit.framework.TestCase;
022:
023: public class ConfigPropertiesTest extends TestCase {
024:
025: private static final TCLogger logger = TCLogging
026: .getTestingLogger(ConfigPropertiesTest.class);
027:
028: private final String wacCookieComment = "wacCookieComment";
029: private final String wacCookieDomain = "wacCookieDomain";
030: private final String wacCookieName = "wacCookieName";
031: private final String wacCookiePath = "wacCookiePath";
032: private final String wacServerId = "wacServerId";
033: private final int wacCookieMaxAge = 111;
034: private final int wacIdLength = 333;
035: private final int wacTimeout = 555;
036: private final int wacInvalidatorSec = 5 * 60;
037: private final boolean wacCookieEnabled = true;
038: private final boolean wacTrackingEnabled = true;
039: private final boolean wacUrlEnabled = true;
040: private final boolean wacCookieSecure = true;
041:
042: private final String spcCookieComment = "spcCookieComment";
043: private final String spcCookieDomain = "spcCookieDomain";
044: private final String spcCookieName = "spcCookieName";
045: private final String spcCookiePath = "spcCookiePath";
046: private final String spcServerId = "spcServerId";
047: private final int spcCookieMaxAge = 222;
048: private final int spcIdLength = 444;
049: private final int spcTimeout = 666;
050: private final int spcInvalidatorSec = 999;
051: private final boolean spcCookieEnabled = false;
052: private final boolean spcTrackingEnabled = false;
053: private final boolean spcUrlEnabled = false;
054: private final boolean spcCookieSecure = false;
055:
056: private MockWebAppConfig wac;
057: private TCProperties tpc;
058:
059: protected void setUp() throws Exception {
060: super .setUp();
061:
062: wac = new MockWebAppConfig();
063: wac.setCookieComment(wacCookieComment);
064: wac.setCookieDomain(wacCookieDomain);
065: wac.setCookieEnabled(wacCookieEnabled);
066: wac.setCookieMaxAge(wacCookieMaxAge);
067: wac.setCookieName(wacCookieName);
068: wac.setCookiePath(wacCookiePath);
069: wac.setCookieSecure(wacCookieSecure);
070: wac.setIdLength(wacIdLength);
071: wac.setServerId(wacServerId);
072: wac.setTimeoutSeconds(wacTimeout);
073: wac.setTrackingEnabled(wacTrackingEnabled);
074: wac.setUrlEnabled(wacUrlEnabled);
075:
076: Properties spc = new Properties();
077: spc.setProperty(ConfigProperties.COOKIE_COMMENT,
078: spcCookieComment);
079: spc
080: .setProperty(ConfigProperties.COOKIE_DOMAIN,
081: spcCookieDomain);
082: spc.setProperty(ConfigProperties.COOKIE_ENABLED, String
083: .valueOf(spcCookieEnabled));
084: spc.setProperty(ConfigProperties.COOKIE_MAX_AGE, String
085: .valueOf(spcCookieMaxAge));
086: spc.setProperty(ConfigProperties.COOKIE_NAME, spcCookieName);
087: spc.setProperty(ConfigProperties.COOKIE_PATH, spcCookiePath);
088: spc.setProperty(ConfigProperties.COOKIE_SECURE, String
089: .valueOf(spcCookieSecure));
090: spc.setProperty(ConfigProperties.ID_LENGTH, String
091: .valueOf(spcIdLength));
092: spc.setProperty(ConfigProperties.INVALIDATOR_SLEEP, String
093: .valueOf(spcInvalidatorSec));
094: spc.setProperty(ConfigProperties.SERVER_ID, spcServerId);
095: spc.setProperty(ConfigProperties.SESSION_TIMEOUT_SECONDS,
096: String.valueOf(spcTimeout));
097: spc.setProperty(ConfigProperties.TRACKING_ENABLED, String
098: .valueOf(spcTrackingEnabled));
099: spc.setProperty(ConfigProperties.TRACKING_ENABLED, String
100: .valueOf(spcUrlEnabled));
101: spc.setProperty(ConfigProperties.URL_REWRITE_ENABLED, String
102: .valueOf(spcUrlEnabled));
103:
104: tpc = new MockTCPropeties(spc);
105:
106: }
107:
108: public void testConfigProperties() {
109: try {
110: new ConfigProperties(null,
111: TCPropertiesImpl.getProperties(), logger);
112: new ConfigProperties(new MockWebAppConfig(),
113: TCPropertiesImpl.getProperties(), logger);
114: } catch (Exception e) {
115: fail("unexpected exception");
116: }
117: }
118:
119: public void testSystemPropertyOverrides() {
120: ConfigProperties cp = new ConfigProperties(wac, tpc, logger);
121: assertEquals(spcCookieEnabled, cp.getCookiesEnabled());
122: assertEquals(spcTrackingEnabled, cp.getSessionTrackingEnabled());
123: assertEquals(spcUrlEnabled, cp.getUrlRewritingEnabled());
124: assertEquals(spcCookieComment, cp.getCookieCoomment());
125: assertEquals(spcCookieDomain, cp.getCookieDomain());
126: assertEquals(spcCookieMaxAge, cp.getCookieMaxAgeSeconds());
127: assertEquals(spcInvalidatorSec, cp.getInvalidatorSleepSeconds());
128: assertEquals(spcCookieName, cp.getCookieName());
129: assertEquals(spcCookiePath, cp.getCookiePath());
130: assertEquals(spcCookieSecure, cp.getCookieSecure());
131: assertEquals(spcIdLength, cp.getSessionIdLength());
132: assertEquals(spcServerId, cp.getServerId());
133: assertEquals(spcTimeout, cp.getSessionTimeoutSeconds());
134: }
135:
136: public void testEmptyProperties() {
137: ConfigProperties cp = new ConfigProperties(wac,
138: TCPropertiesImpl.getProperties(), logger);
139: assertEquals(wacCookieEnabled, cp.getCookiesEnabled());
140: assertEquals(wacTrackingEnabled, cp.getSessionTrackingEnabled());
141: assertEquals(wacUrlEnabled, cp.getUrlRewritingEnabled());
142: assertEquals(wacCookieComment, cp.getCookieCoomment());
143: assertEquals(wacCookieDomain, cp.getCookieDomain());
144: assertEquals(wacCookieMaxAge, cp.getCookieMaxAgeSeconds());
145: assertEquals(wacInvalidatorSec, cp.getInvalidatorSleepSeconds());
146: assertEquals(wacCookieName, cp.getCookieName());
147: assertEquals(wacCookiePath, cp.getCookiePath());
148: assertEquals(wacCookieSecure, cp.getCookieSecure());
149: assertEquals(wacIdLength, cp.getSessionIdLength());
150: assertEquals(wacServerId, cp.getServerId());
151: assertEquals(wacTimeout, cp.getSessionTimeoutSeconds());
152: }
153:
154: public void testDefaults() {
155: ConfigProperties cp = new ConfigProperties(null,
156: TCPropertiesImpl.getProperties(), logger);
157: assertEquals(ConfigProperties.defaultCookiesEnabled, cp
158: .getCookiesEnabled());
159: assertEquals(ConfigProperties.defaultTrackingEnabled, cp
160: .getSessionTrackingEnabled());
161: assertEquals(ConfigProperties.defaultUrlEnabled, cp
162: .getUrlRewritingEnabled());
163: assertEquals(ConfigProperties.defaultCookieComment, cp
164: .getCookieCoomment());
165: assertEquals(ConfigProperties.defaultCookieDomain, cp
166: .getCookieDomain());
167: assertEquals(ConfigProperties.defaultCookieMaxAge, cp
168: .getCookieMaxAgeSeconds());
169: assertEquals(ConfigProperties.defaultCookieName, cp
170: .getCookieName());
171: assertEquals(null, cp.getCookiePath());
172: assertEquals(ConfigProperties.defaultCookieSecure, cp
173: .getCookieSecure());
174: assertEquals(ConfigProperties.defaultIdLength, cp
175: .getSessionIdLength());
176: assertEquals(ConfigProperties.defaultServerId, cp.getServerId());
177: assertEquals(ConfigProperties.defaultSessionTimeout, cp
178: .getSessionTimeoutSeconds());
179: }
180:
181: public void testGetSessionListeners() {
182: final String propVal = SessionListener.class.getName() + ","
183: + AttributeListener.class.getName();
184: final Properties props = new Properties();
185: props.setProperty(ConfigProperties.SESSION_LISTENERS, propVal);
186: final ConfigProperties cp = new ConfigProperties(null,
187: new MockTCPropeties(props), logger);
188: HttpSessionListener[] listeners = cp.getSessionListeners();
189: assertNotNull(listeners);
190: assertEquals(1, listeners.length);
191: assertTrue(listeners[0] instanceof SessionListener);
192: }
193:
194: public void testGetAttibuteListeners() {
195: final String propVal = SessionListener.class.getName() + ","
196: + AttributeListener.class.getName();
197: final Properties props = new Properties();
198: props
199: .setProperty(ConfigProperties.ATTRIBUTE_LISTENERS,
200: propVal);
201: final ConfigProperties cp = new ConfigProperties(null,
202: new MockTCPropeties(props), logger);
203: HttpSessionAttributeListener[] listeners = cp
204: .getSessionAttributeListeners();
205: assertNotNull(listeners);
206: assertEquals(1, listeners.length);
207: assertTrue(listeners[0] instanceof AttributeListener);
208: }
209:
210: public void testMakeSessionListener() {
211: ConfigProperties cp = new ConfigProperties(wac, tpc, logger);
212:
213: HttpSessionListener listener = cp
214: .makeSessionListener("com.some.Bogus.Type");
215: assertNull(listener);
216:
217: listener = cp
218: .makeSessionListener(ConfigPropertiesTest.SessionListener.class
219: .getName());
220: assertTrue(listener instanceof ConfigPropertiesTest.SessionListener);
221:
222: listener = cp
223: .makeSessionListener(ConfigPropertiesTest.AttributeListener.class
224: .getName());
225: assertNull(listener);
226: }
227:
228: public void testMakeAttributeListener() {
229: ConfigProperties cp = new ConfigProperties(wac, tpc, logger);
230:
231: HttpSessionAttributeListener listener = cp
232: .makeAttributeListener("com.some.Bogus.Type");
233: assertNull(listener);
234:
235: listener = cp
236: .makeAttributeListener(ConfigPropertiesTest.AttributeListener.class
237: .getName());
238: assertTrue(listener instanceof ConfigPropertiesTest.AttributeListener);
239:
240: listener = cp
241: .makeAttributeListener(ConfigPropertiesTest.SessionListener.class
242: .getName());
243: assertNull(listener);
244: }
245:
246: static class AttributeListener implements
247: HttpSessionAttributeListener {
248:
249: public void attributeAdded(HttpSessionBindingEvent arg0) {
250: // n/a
251: }
252:
253: public void attributeRemoved(HttpSessionBindingEvent arg0) {
254: // n/a
255: }
256:
257: public void attributeReplaced(HttpSessionBindingEvent arg0) {
258: // n/a
259: }
260: }
261:
262: static class SessionListener implements HttpSessionListener {
263:
264: public void sessionCreated(HttpSessionEvent arg0) {
265: // n/a
266: }
267:
268: public void sessionDestroyed(HttpSessionEvent arg0) {
269: // n/a
270: }
271:
272: }
273:
274: private static class MockTCPropeties implements TCProperties {
275:
276: private final Properties props;
277:
278: MockTCPropeties(Properties props) {
279: this .props = props;
280: }
281:
282: public boolean getBoolean(String key) {
283: return Boolean.valueOf(getProperty(key)).booleanValue();
284: }
285:
286: public int getInt(String key) {
287: return Integer.valueOf(getProperty(key)).intValue();
288: }
289:
290: public long getLong(String key) {
291: return Long.valueOf(getProperty(key)).longValue();
292: }
293:
294: public TCProperties getPropertiesFor(String key) {
295: throw new ImplementMe();
296: }
297:
298: public String getProperty(String key) {
299: return getProperty(key, false);
300: }
301:
302: public String getProperty(String key, boolean missingOkay) {
303: String s = props.getProperty(key);
304: if (s == null && !missingOkay) {
305: throw new RuntimeException("missing value for " + key);
306: }
307: return s;
308: }
309:
310: public float getFloat(String key) {
311: return Float.valueOf(getProperty(key)).floatValue();
312: }
313:
314: public Properties addAllPropertiesTo(Properties properties) {
315: properties.putAll(props);
316: return properties;
317: }
318:
319: public int getInt(String key, int defaultValue) {
320: return 0;
321: }
322:
323: public boolean getBoolean(String key, boolean defaultValue) {
324: return getBoolean(key);
325: }
326: }
327:
328: }
|