KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > terracotta > session > util > ConfigPropertiesTest


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

5 package com.terracotta.session.util;
6
7 import com.tc.exception.ImplementMe;
8 import com.tc.properties.TCProperties;
9 import com.tc.properties.TCPropertiesImpl;
10 import com.terracotta.session.MockWebAppConfig;
11
12 import java.util.Properties JavaDoc;
13
14 import javax.servlet.http.HttpSessionAttributeListener JavaDoc;
15 import javax.servlet.http.HttpSessionBindingEvent JavaDoc;
16 import javax.servlet.http.HttpSessionEvent JavaDoc;
17 import javax.servlet.http.HttpSessionListener JavaDoc;
18
19 import junit.framework.TestCase;
20
21 public class ConfigPropertiesTest extends TestCase {
22
23   private final String JavaDoc wacCookieComment = "wacCookieComment";
24   private final String JavaDoc wacCookieDomain = "wacCookieDomain";
25   private final String JavaDoc wacCookieName = "wacCookieName";
26   private final String JavaDoc wacCookiePath = "wacCookiePath";
27   private final String JavaDoc wacServerId = "wacServerId";
28   private final int wacCookieMaxAge = 111;
29   private final int wacIdLength = 333;
30   private final int wacTimeout = 555;
31   private final int wacInvalidatorSec = 5 * 60;
32   private final boolean wacCookieEnabled = true;
33   private final boolean wacTrackingEnabled = true;
34   private final boolean wacUrlEnabled = true;
35   private final boolean wacCookieSecure = true;
36
37   private final String JavaDoc spcCookieComment = "spcCookieComment";
38   private final String JavaDoc spcCookieDomain = "spcCookieDomain";
39   private final String JavaDoc spcCookieName = "spcCookieName";
40   private final String JavaDoc spcCookiePath = "spcCookiePath";
41   private final String JavaDoc spcServerId = "spcServerId";
42   private final int spcCookieMaxAge = 222;
43   private final int spcIdLength = 444;
44   private final int spcTimeout = 666;
45   private final int spcInvalidatorSec = 999;
46   private final boolean spcCookieEnabled = false;
47   private final boolean spcTrackingEnabled = false;
48   private final boolean spcUrlEnabled = false;
49   private final boolean spcCookieSecure = false;
50
51   private MockWebAppConfig wac;
52   private TCProperties tpc;
53
54   protected void setUp() throws Exception JavaDoc {
55     super.setUp();
56
57     wac = new MockWebAppConfig();
58     wac.setCookieComment(wacCookieComment);
59     wac.setCookieDomain(wacCookieDomain);
60     wac.setCookieEnabled(wacCookieEnabled);
61     wac.setCookieMaxAge(wacCookieMaxAge);
62     wac.setCookieName(wacCookieName);
63     wac.setCookiePath(wacCookiePath);
64     wac.setCookieSecure(wacCookieSecure);
65     wac.setIdLength(wacIdLength);
66     wac.setServerId(wacServerId);
67     wac.setTimeoutSeconds(wacTimeout);
68     wac.setTrackingEnabled(wacTrackingEnabled);
69     wac.setUrlEnabled(wacUrlEnabled);
70
71     Properties spc = new Properties();
72     spc.setProperty(ConfigProperties.COOKIE_COMMENT, spcCookieComment);
73     spc.setProperty(ConfigProperties.COOKIE_DOMAIN, spcCookieDomain);
74     spc.setProperty(ConfigProperties.COOKIE_ENABLED, String.valueOf(spcCookieEnabled));
75     spc.setProperty(ConfigProperties.COOKIE_MAX_AGE, String.valueOf(spcCookieMaxAge));
76     spc.setProperty(ConfigProperties.COOKIE_NAME, spcCookieName);
77     spc.setProperty(ConfigProperties.COOKIE_PATH, spcCookiePath);
78     spc.setProperty(ConfigProperties.COOKIE_SECURE, String.valueOf(spcCookieSecure));
79     spc.setProperty(ConfigProperties.ID_LENGTH, String.valueOf(spcIdLength));
80     spc.setProperty(ConfigProperties.INVALIDATOR_SLEEP, String.valueOf(spcInvalidatorSec));
81     spc.setProperty(ConfigProperties.SERVER_ID, spcServerId);
82     spc.setProperty(ConfigProperties.SESSION_TIMEOUT_SECONDS, String.valueOf(spcTimeout));
83     spc.setProperty(ConfigProperties.TRACKING_ENABLED, String.valueOf(spcTrackingEnabled));
84     spc.setProperty(ConfigProperties.TRACKING_ENABLED, String.valueOf(spcUrlEnabled));
85     spc.setProperty(ConfigProperties.URL_REWRITE_ENABLED, String.valueOf(spcUrlEnabled));
86
87     tpc = new MockTCPropeties(spc);
88
89   }
90
91   public void testConfigProperties() {
92     try {
93       new ConfigProperties(null, TCPropertiesImpl.getProperties());
94       new ConfigProperties(new MockWebAppConfig(), TCPropertiesImpl.getProperties());
95     } catch (Exception JavaDoc e) {
96       fail("unexpected exception");
97     }
98   }
99
100   public void testSystemPropertyOverrides() {
101     ConfigProperties cp = new ConfigProperties(wac, tpc);
102     assertEquals(spcCookieEnabled, cp.getCookiesEnabled());
103     assertEquals(spcTrackingEnabled, cp.getSessionTrackingEnabled());
104     assertEquals(spcUrlEnabled, cp.getUrlRewritingEnabled());
105     assertEquals(spcCookieComment, cp.getCookieCoomment());
106     assertEquals(spcCookieDomain, cp.getCookieDomain());
107     assertEquals(spcCookieMaxAge, cp.getCookieMaxAgeSeconds());
108     assertEquals(spcInvalidatorSec, cp.getInvalidatorSleepSeconds());
109     assertEquals(spcCookieName, cp.getCookieName());
110     assertEquals(spcCookiePath, cp.getCookiePath());
111     assertEquals(spcCookieSecure, cp.getCookieSecure());
112     assertEquals(spcIdLength, cp.getSessionIdLength());
113     assertEquals(spcServerId, cp.getServerId());
114     assertEquals(spcTimeout, cp.getSessionTimeoutSeconds());
115   }
116
117   public void testEmptyProperties() {
118     ConfigProperties cp = new ConfigProperties(wac, TCPropertiesImpl.getProperties());
119     assertEquals(wacCookieEnabled, cp.getCookiesEnabled());
120     assertEquals(wacTrackingEnabled, cp.getSessionTrackingEnabled());
121     assertEquals(wacUrlEnabled, cp.getUrlRewritingEnabled());
122     assertEquals(wacCookieComment, cp.getCookieCoomment());
123     assertEquals(wacCookieDomain, cp.getCookieDomain());
124     assertEquals(wacCookieMaxAge, cp.getCookieMaxAgeSeconds());
125     assertEquals(wacInvalidatorSec, cp.getInvalidatorSleepSeconds());
126     assertEquals(wacCookieName, cp.getCookieName());
127     assertEquals(wacCookiePath, cp.getCookiePath());
128     assertEquals(wacCookieSecure, cp.getCookieSecure());
129     assertEquals(wacIdLength, cp.getSessionIdLength());
130     assertEquals(wacServerId, cp.getServerId());
131     assertEquals(wacTimeout, cp.getSessionTimeoutSeconds());
132   }
133
134   public void testDefaults() {
135     ConfigProperties cp = new ConfigProperties(null, TCPropertiesImpl.getProperties());
136     assertEquals(ConfigProperties.defaultCookiesEnabled, cp.getCookiesEnabled());
137     assertEquals(ConfigProperties.defaultTrackingEnabled, cp.getSessionTrackingEnabled());
138     assertEquals(ConfigProperties.defaultUrlEnabled, cp.getUrlRewritingEnabled());
139     assertEquals(ConfigProperties.defaultCookieComment, cp.getCookieCoomment());
140     assertEquals(ConfigProperties.defaultCookieDomain, cp.getCookieDomain());
141     assertEquals(ConfigProperties.defaultCookieMaxAge, cp.getCookieMaxAgeSeconds());
142     assertEquals(ConfigProperties.defaultCookieName, cp.getCookieName());
143     assertEquals(ConfigProperties.defaultCookiePath, cp.getCookiePath());
144     assertEquals(ConfigProperties.defaultCookieSecure, cp.getCookieSecure());
145     assertEquals(ConfigProperties.defaultIdLength, cp.getSessionIdLength());
146     assertEquals(ConfigProperties.defaultServerId, cp.getServerId());
147     assertEquals(ConfigProperties.defaultSessionTimeout, cp.getSessionTimeoutSeconds());
148   }
149
150   public void testGetSessionListeners() {
151     final String JavaDoc propVal = SessionListener JavaDoc.class.getName() + "," + AttributeListener JavaDoc.class.getName();
152     final Properties props = new Properties();
153     props.setProperty(ConfigProperties.SESSION_LISTENERS, propVal);
154     final ConfigProperties cp = new ConfigProperties(null, new MockTCPropeties(props));
155     HttpSessionListener JavaDoc[] listeners = cp.getSessionListeners();
156     assertNotNull(listeners);
157     assertEquals(1, listeners.length);
158     assertTrue(listeners[0] instanceof SessionListener JavaDoc);
159   }
160
161   public void testGetAttibuteListeners() {
162     final String JavaDoc propVal = SessionListener JavaDoc.class.getName() + "," + AttributeListener JavaDoc.class.getName();
163     final Properties props = new Properties();
164     props.setProperty(ConfigProperties.ATTRIBUTE_LISTENERS, propVal);
165     final ConfigProperties cp = new ConfigProperties(null, new MockTCPropeties(props));
166     HttpSessionAttributeListener JavaDoc[] listeners = cp.getSessionAttributeListeners();
167     assertNotNull(listeners);
168     assertEquals(1, listeners.length);
169     assertTrue(listeners[0] instanceof AttributeListener JavaDoc);
170   }
171
172   public void testMakeSessionListener() {
173     HttpSessionListener JavaDoc listener = ConfigProperties.makeSessionListener("com.some.Bogus.Type");
174     assertNull(listener);
175
176     listener = ConfigProperties.makeSessionListener(ConfigPropertiesTest.SessionListener.class.getName());
177     assertTrue(listener instanceof ConfigPropertiesTest.SessionListener);
178
179     listener = ConfigProperties.makeSessionListener(ConfigPropertiesTest.AttributeListener.class.getName());
180     assertNull(listener);
181   }
182
183   public void testMakeAttributeListener() {
184     HttpSessionAttributeListener JavaDoc listener = ConfigProperties.makeAttributeListener("com.some.Bogus.Type");
185     assertNull(listener);
186
187     listener = ConfigProperties.makeAttributeListener(ConfigPropertiesTest.AttributeListener.class.getName());
188     assertTrue(listener instanceof ConfigPropertiesTest.AttributeListener);
189
190     listener = ConfigProperties.makeAttributeListener(ConfigPropertiesTest.SessionListener.class.getName());
191     assertNull(listener);
192   }
193
194   static class AttributeListener implements HttpSessionAttributeListener JavaDoc {
195
196     public void attributeAdded(HttpSessionBindingEvent JavaDoc arg0) {
197       // n/a
198
}
199
200     public void attributeRemoved(HttpSessionBindingEvent JavaDoc arg0) {
201       // n/a
202
}
203
204     public void attributeReplaced(HttpSessionBindingEvent JavaDoc arg0) {
205       // n/a
206
}
207   }
208
209   static class SessionListener implements HttpSessionListener JavaDoc {
210
211     public void sessionCreated(HttpSessionEvent JavaDoc arg0) {
212       // n/a
213
}
214
215     public void sessionDestroyed(HttpSessionEvent JavaDoc arg0) {
216       // n/a
217
}
218
219   }
220
221   private static class MockTCPropeties implements TCProperties {
222
223     private final Properties props;
224
225     MockTCPropeties(Properties props) {
226       this.props = props;
227     }
228
229     public boolean getBoolean(String JavaDoc key) {
230       return Boolean.valueOf(getProperty(key)).booleanValue();
231     }
232
233     public int getInt(String JavaDoc key) {
234       return Integer.valueOf(getProperty(key)).intValue();
235     }
236
237     public long getLong(String JavaDoc key) {
238       return Long.valueOf(getProperty(key)).longValue();
239     }
240
241     public TCProperties getPropertiesFor(String JavaDoc key) {
242       throw new ImplementMe();
243     }
244
245     public String JavaDoc getProperty(String JavaDoc key) {
246       return getProperty(key, false);
247     }
248
249     public String JavaDoc getProperty(String JavaDoc key, boolean missingOkay) {
250       String JavaDoc s = props.getProperty(key);
251       if (s == null && !missingOkay) { throw new RuntimeException JavaDoc("missing value for " + key); }
252       return s;
253     }
254
255     public float getFloat(String JavaDoc key) {
256       return Float.valueOf(getProperty(key)).floatValue();
257     }
258
259     public Properties addAllPropertiesTo(Properties properties) {
260       properties.putAll(props);
261       return properties;
262     }
263   }
264
265 }
266
Popular Tags