001: /*
002: License $Id: TestListenerSupport.java,v 1.1 2004/04/16 01:30:05 hendriks73 Exp $
003:
004: Copyright (c) 2001 tagtraum industries.
005:
006: LGPL
007: ====
008:
009: jo! is free software; you can redistribute it and/or
010: modify it under the terms of the GNU Lesser General Public
011: License as published by the Free Software Foundation; either
012: version 2.1 of the License, or (at your option) any later version.
013:
014: jo! is distributed in the hope that it will be useful,
015: but WITHOUT ANY WARRANTY; without even the implied warranty of
016: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017: Lesser General Public License for more details.
018:
019: You should have received a copy of the GNU Lesser General Public
020: License along with this library; if not, write to the Free Software
021: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
022:
023: For LGPL see <http://www.fsf.org/copyleft/lesser.txt>
024:
025:
026: Sun license
027: ===========
028:
029: This release contains software by Sun Microsystems. Therefore
030: the following conditions have to be met, too. They apply to the
031: files
032:
033: - lib/mail.jar
034: - lib/activation.jar
035: - lib/jsse.jar
036: - lib/jcert.jar
037: - lib/jaxp.jar
038: - lib/crimson.jar
039: - lib/servlet.jar
040: - lib/jnet.jar
041: - lib/jaas.jar
042: - lib/jaasmod.jar
043:
044: contained in this release.
045:
046: a. Licensee may not modify the Java Platform
047: Interface (JPI, identified as classes contained within the javax
048: package or any subpackages of the javax package), by creating additional
049: classes within the JPI or otherwise causing the addition to or modification
050: of the classes in the JPI. In the event that Licensee creates any
051: Java-related API and distribute such API to others for applet or
052: application development, you must promptly publish broadly, an accurate
053: specification for such API for free use by all developers of Java-based
054: software.
055:
056: b. Software is confidential copyrighted information of Sun and
057: title to all copies is retained by Sun and/or its licensors. Licensee
058: shall not modify, decompile, disassemble, decrypt, extract, or otherwise
059: reverse engineer Software. Software may not be leased, assigned, or
060: sublicensed, in whole or in part. Software is not designed or intended
061: for use in on-line control of aircraft, air traffic, aircraft navigation
062: or aircraft communications; or in the design, construction, operation or
063: maintenance of any nuclear facility. Licensee warrants that it will not
064: use or redistribute the Software for such purposes.
065:
066: c. Software is provided "AS IS," without a warranty
067: of any kind. ALL EXPRESS OR IMPLIED REPRESENTATIONS AND WARRANTIES,
068: INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A
069: PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED.
070:
071: d. This License is effective until terminated. Licensee may
072: terminate this License at any time by destroying all copies of Software.
073: This License will terminate immediately without notice from Sun if Licensee
074: fails to comply with any provision of this License. Upon such termination,
075: Licensee must destroy all copies of Software.
076:
077: e. Software, including technical data, is subject to U.S.
078: export control laws, including the U.S. Export Administration Act and its
079: associated regulations, and may be subject to export or import regulations
080: in other countries. Licensee agrees to comply strictly with all such
081: regulations and acknowledges that it has the responsibility to obtain
082: licenses to export, re-export, or import Software. Software may not be
083: downloaded, or otherwise exported or re-exported (i) into, or to a national
084: or resident of, Cuba, Iraq, Iran, North Korea, Libya, Sudan, Syria or any
085: country to which the U.S. has embargoed goods; or (ii) to anyone on the
086: U.S. Treasury Department's list of Specially Designated Nations or the U.S.
087: Commerce Department's Table of Denial Orders.
088:
089:
090: Feedback
091: ========
092:
093: We encourage your feedback and suggestions and want to use your feedback to
094: improve the Software. Send all such feedback to:
095: <feedback@tagtraum.com>
096:
097: For more information on tagtraum industries and jo!
098: please see <http://www.tagtraum.com/>.
099:
100:
101: */
102: package com.tagtraum.jo;
103:
104: import junit.framework.TestCase;
105: import junit.framework.TestSuite;
106:
107: import javax.servlet.http.HttpSessionAttributeListener;
108: import javax.servlet.http.HttpSessionBindingEvent;
109: import javax.servlet.http.HttpSession;
110: import javax.servlet.http.HttpSessionContext;
111: import javax.servlet.http.HttpSessionListener;
112: import javax.servlet.http.HttpSessionEvent;
113: import javax.servlet.*;
114: import java.util.Enumeration;
115: import java.util.Set;
116: import java.util.EventListener;
117: import java.net.URL;
118: import java.net.MalformedURLException;
119: import java.io.InputStream;
120:
121: /**
122: * @author <a href="mailto:hs@tagtraum.com">Hendrik Schreiber</a>
123: * @version $Id: TestListenerSupport.java,v 1.1 2004/04/16 01:30:05 hendriks73 Exp $
124: */
125: public class TestListenerSupport extends TestCase {
126:
127: private ListenerSupport listenerSupport;
128:
129: public TestListenerSupport(String name) {
130: super (name);
131: }
132:
133: public void setUp() throws Exception {
134: listenerSupport = new ListenerSupport();
135: }
136:
137: public void testHttpSessionAttributeListener() {
138: final MockHttpSessionAttributeListener eventListener = new MockHttpSessionAttributeListener();
139: listenerSupport.addListener(eventListener);
140: listenerSupport.fireSessionAttributeAdded(
141: new MockHttpSession(), "Name", "Value");
142: assertEquals("attributeAdded", eventListener.method);
143: listenerSupport.fireSessionAttributeRemoved(
144: new MockHttpSession(), "Name", "Value");
145: assertEquals("attributeRemoved", eventListener.method);
146: listenerSupport.fireSessionAttributeReplaced(
147: new MockHttpSession(), "Name", "Value");
148: assertEquals("attributeReplaced", eventListener.method);
149: }
150:
151: public void testHttpSessionListener() {
152: final MockHttpSessionListener eventListener = new MockHttpSessionListener();
153: listenerSupport.addListener(eventListener);
154: listenerSupport.fireSessionCreated(new MockHttpSession());
155: assertEquals("sessionCreated", eventListener.method);
156: listenerSupport.fireSessionDestroyed(new MockHttpSession());
157: assertEquals("sessionDestroyed", eventListener.method);
158: }
159:
160: public void testHttpSessionListenerOrder() {
161: final MockHttpSessionListener eventListener1 = new MockHttpSessionListener();
162: final MockHttpSessionListener eventListener2 = new MockHttpSessionListener();
163: listenerSupport.addListener(eventListener1);
164: listenerSupport.addListener(eventListener2);
165: listenerSupport.fireSessionCreated(new MockHttpSession());
166: assertEquals(0, eventListener1.touch);
167: assertEquals(1, eventListener2.touch);
168: listenerSupport.fireSessionDestroyed(new MockHttpSession());
169: assertEquals(0, eventListener2.touch);
170: assertEquals(1, eventListener1.touch);
171: }
172:
173: public void testServletContextListener() {
174: final MockServletContextListener eventListener = new MockServletContextListener();
175: listenerSupport.addListener(eventListener);
176: listenerSupport
177: .fireServletContextDestroyed(new MockServletContext());
178: assertEquals("contextDestroyed", eventListener.method);
179: listenerSupport
180: .fireServletContextInitialized(new MockServletContext());
181: assertEquals("contextInitialized", eventListener.method);
182: }
183:
184: public void testServletContextListenerOrder() {
185: final MockServletContextListener eventListener1 = new MockServletContextListener();
186: final MockServletContextListener eventListener2 = new MockServletContextListener();
187: listenerSupport.addListener(eventListener1);
188: listenerSupport.addListener(eventListener2);
189: listenerSupport
190: .fireServletContextInitialized(new MockServletContext());
191: assertEquals(0, eventListener1.touch);
192: assertEquals(1, eventListener2.touch);
193: listenerSupport
194: .fireServletContextDestroyed(new MockServletContext());
195: assertEquals(0, eventListener2.touch);
196: assertEquals(1, eventListener1.touch);
197: }
198:
199: public void testServletContextAttributeListener() {
200: final MockServletContextAttributeListener eventListener = new MockServletContextAttributeListener();
201: listenerSupport.addListener(eventListener);
202: listenerSupport.fireServletContextAttributeAdded(
203: new MockServletContext(), "Name", "Value");
204: assertEquals("attributeAdded", eventListener.method);
205: listenerSupport.fireServletContextAttributeRemoved(
206: new MockServletContext(), "Name", "Value");
207: assertEquals("attributeRemoved", eventListener.method);
208: listenerSupport.fireServletContextAttributeReplaced(
209: new MockServletContext(), "Name", "Value");
210: assertEquals("attributeReplaced", eventListener.method);
211: }
212:
213: public void testUnknownListener() {
214: try {
215: listenerSupport.addListener(new EventListener() {
216: });
217: fail("Expected IllegalArgumentException");
218: } catch (IllegalArgumentException iae) {
219: // we expected this
220: }
221: }
222:
223: /**
224: * Returns the TestSuite for TestJspDirective.
225: */
226: public static TestSuite suite() {
227: return new TestSuite(TestListenerSupport.class);
228: }
229:
230: /**
231: * Runs the textui JUnit-TestRunner for TestJspDirective.
232: */
233: public static void main(String[] args) {
234: junit.textui.TestRunner.run(suite());
235: }
236:
237: private static class MockHttpSessionAttributeListener implements
238: HttpSessionAttributeListener {
239:
240: private String method;
241:
242: public void attributeAdded(HttpSessionBindingEvent event) {
243: method = "attributeAdded";
244: }
245:
246: public void attributeRemoved(HttpSessionBindingEvent event) {
247: method = "attributeRemoved";
248: }
249:
250: public void attributeReplaced(HttpSessionBindingEvent event) {
251: method = "attributeReplaced";
252: }
253: }
254:
255: private static class MockHttpSessionListener implements
256: HttpSessionListener {
257:
258: private String method;
259: private int touch;
260:
261: public void sessionCreated(HttpSessionEvent event) {
262: touch = ((MockHttpSession) event.getSession()).touch();
263: method = "sessionCreated";
264: }
265:
266: public void sessionDestroyed(HttpSessionEvent event) {
267: touch = ((MockHttpSession) event.getSession()).touch();
268: method = "sessionDestroyed";
269: }
270: }
271:
272: private static class MockServletContextListener implements
273: ServletContextListener {
274:
275: private String method;
276: private int touch;
277:
278: public void contextInitialized(ServletContextEvent event) {
279: touch = ((MockServletContext) event.getServletContext())
280: .touch();
281: method = "contextInitialized";
282: }
283:
284: public void contextDestroyed(ServletContextEvent event) {
285: touch = ((MockServletContext) event.getServletContext())
286: .touch();
287: method = "contextDestroyed";
288: }
289: }
290:
291: private static class MockHttpSession implements HttpSession {
292:
293: private int touch;
294:
295: public long getCreationTime() {
296: return 0;
297: }
298:
299: public String getId() {
300: return null;
301: }
302:
303: public long getLastAccessedTime() {
304: return 0;
305: }
306:
307: public ServletContext getServletContext() {
308: return null;
309: }
310:
311: public void setMaxInactiveInterval(int i) {
312: }
313:
314: public int getMaxInactiveInterval() {
315: return 0;
316: }
317:
318: public HttpSessionContext getSessionContext() {
319: return null;
320: }
321:
322: public Object getAttribute(String s) {
323: return null;
324: }
325:
326: public Object getValue(String s) {
327: return null;
328: }
329:
330: public Enumeration getAttributeNames() {
331: return null;
332: }
333:
334: public String[] getValueNames() {
335: return new String[0];
336: }
337:
338: public void setAttribute(String s, Object o) {
339: }
340:
341: public void putValue(String s, Object o) {
342: }
343:
344: public void removeAttribute(String s) {
345: }
346:
347: public void removeValue(String s) {
348: }
349:
350: public void invalidate() {
351: }
352:
353: public boolean isNew() {
354: return false;
355: }
356:
357: public int touch() {
358: return touch++;
359: }
360: }
361:
362: private static class MockServletContext implements ServletContext {
363:
364: private int touch;
365:
366: public int touch() {
367: return touch++;
368: }
369:
370: public ServletContext getContext(String s) {
371: return null;
372: }
373:
374: public int getMajorVersion() {
375: return 0;
376: }
377:
378: public int getMinorVersion() {
379: return 0;
380: }
381:
382: public String getMimeType(String s) {
383: return null;
384: }
385:
386: public Set getResourcePaths(String s) {
387: return null;
388: }
389:
390: public URL getResource(String s) throws MalformedURLException {
391: return null;
392: }
393:
394: public InputStream getResourceAsStream(String s) {
395: return null;
396: }
397:
398: public RequestDispatcher getRequestDispatcher(String s) {
399: return null;
400: }
401:
402: public RequestDispatcher getNamedDispatcher(String s) {
403: return null;
404: }
405:
406: public Servlet getServlet(String s) throws ServletException {
407: return null;
408: }
409:
410: public Enumeration getServlets() {
411: return null;
412: }
413:
414: public Enumeration getServletNames() {
415: return null;
416: }
417:
418: public void log(String s) {
419: }
420:
421: public void log(Exception e, String s) {
422: }
423:
424: public void log(String s, Throwable throwable) {
425: }
426:
427: public String getRealPath(String s) {
428: return null;
429: }
430:
431: public String getServerInfo() {
432: return null;
433: }
434:
435: public String getInitParameter(String s) {
436: return null;
437: }
438:
439: public Enumeration getInitParameterNames() {
440: return null;
441: }
442:
443: public Object getAttribute(String s) {
444: return null;
445: }
446:
447: public Enumeration getAttributeNames() {
448: return null;
449: }
450:
451: public void setAttribute(String s, Object o) {
452: }
453:
454: public void removeAttribute(String s) {
455: }
456:
457: public String getServletContextName() {
458: return null;
459: }
460: }
461:
462: private static class MockServletContextAttributeListener implements
463: ServletContextAttributeListener {
464:
465: private String method;
466:
467: public void attributeAdded(ServletContextAttributeEvent event) {
468: method = "attributeAdded";
469: }
470:
471: public void attributeRemoved(ServletContextAttributeEvent event) {
472: method = "attributeRemoved";
473: }
474:
475: public void attributeReplaced(ServletContextAttributeEvent event) {
476: method = "attributeReplaced";
477: }
478: }
479: }
|