001: // Copyright 2006, 2007 The Apache Software Foundation
002: //
003: // Licensed under the Apache License, Version 2.0 (the "License");
004: // you may not use this file except in compliance with the License.
005: // You may obtain a copy of the License at
006: //
007: // http://www.apache.org/licenses/LICENSE-2.0
008: //
009: // Unless required by applicable law or agreed to in writing, software
010: // distributed under the License is distributed on an "AS IS" BASIS,
011: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: // See the License for the specific language governing permissions and
013: // limitations under the License.
014:
015: package org.apache.tapestry.ioc.internal.services;
016:
017: import org.apache.commons.logging.Log;
018: import org.apache.tapestry.ioc.services.ThreadCleanupListener;
019: import org.apache.tapestry.ioc.test.IOCTestCase;
020: import org.testng.annotations.Test;
021:
022: public class ThreadCleanupHubImplTest extends IOCTestCase {
023: @Test
024: public void no_listeners() {
025: Log log = mockLog();
026:
027: replay();
028:
029: new ThreadCleanupHubImpl(log).cleanup();
030:
031: verify();
032: }
033:
034: @Test
035: public void listeners_are_one_shot() {
036: Log log = mockLog();
037: ThreadCleanupListener listener = mockThreadCleanupListener();
038:
039: listener.threadDidCleanup();
040:
041: replay();
042:
043: ThreadCleanupHubImpl hub = new ThreadCleanupHubImpl(log);
044:
045: hub.addThreadCleanupListener(listener);
046:
047: hub.cleanup();
048:
049: verify();
050:
051: // No more training.
052:
053: replay();
054:
055: // Listener not invoked.
056:
057: hub.cleanup();
058:
059: verify();
060: }
061:
062: private ThreadCleanupListener mockThreadCleanupListener() {
063: return newMock(ThreadCleanupListener.class);
064: }
065:
066: @Test
067: public void listener_cleanup_failure() {
068: final RuntimeException t = new RuntimeException("Boom!");
069:
070: Log log = mockLog();
071:
072: ThreadCleanupListener listener = new ThreadCleanupListener() {
073:
074: public void threadDidCleanup() {
075: throw t;
076: }
077:
078: };
079:
080: log.warn(ServiceMessages.threadCleanupError(listener, t), t);
081:
082: replay();
083:
084: ThreadCleanupHubImpl hub = new ThreadCleanupHubImpl(log);
085:
086: hub.addThreadCleanupListener(listener);
087:
088: hub.cleanup();
089:
090: verify();
091: }
092:
093: // @Test
094: // public void listener_list_is_per_thread()
095: // {
096: // ThreadCleanupListener l1 = newThreadCleanupListener();
097: // final ThreadCleanupListener l2 = newThreadCleanupListener();
098: //
099: // Thread thread = new Thread();
100: //
101: // l1.threadDidCleanup();
102: //
103: // replay();
104: //
105: // final ThreadCleanupHub hub = new ThreadCleanupHubImpl(log);
106: //
107: // hub.addThreadCleanupListener(l1);
108: //
109: // hub.cleanup();
110: //
111: // verify();
112: // }
113: }
|