001    // Copyright 2004, 2005 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    package org.apache.tapestry.services.impl;
015    
016    import java.io.IOException;
017    
018    import org.apache.tapestry.BaseComponentTestCase;
019    import org.apache.tapestry.event.ResetEventListener;
020    import org.apache.tapestry.services.ResetEventHub;
021    import org.apache.tapestry.services.WebRequestServicer;
022    import org.apache.tapestry.web.WebRequest;
023    import org.apache.tapestry.web.WebResponse;
024    import org.testng.annotations.AfterSuite;
025    import org.testng.annotations.BeforeClass;
026    import org.testng.annotations.Test;
027    
028    
029    /**
030     * Tests threaded calls on {@link DisableCachingFilter}.
031     *
032     * @author jkuhnert
033     */
034    @Test(threadPoolSize=4)
035    public class TestDisableCachingFilterThreaded extends BaseComponentTestCase
036    {
037        class MockServicer implements WebRequestServicer 
038        {
039            CountingResetListener _listener;
040            
041            public MockServicer(CountingResetListener listener)
042            {
043                _listener = listener;
044            }
045            
046            /**
047             * {@inheritDoc}
048             */
049            public void service(WebRequest request, WebResponse response)
050                throws IOException
051            {
052                _listener.performOperation();
053            }
054            
055        }
056        
057        class CountingResetListener implements ResetEventListener
058        {
059            private int _counter=0;
060            
061            public void performOperation()
062            {
063                _counter++;
064    
065                if (_counter != 1)
066                    throw new AssertionError("Counter should be 1 but is " + _counter);
067            }
068            
069            public void resetEventDidOccur()
070            {
071                _counter--;
072            }
073            
074            public int getCount()
075            {
076                return _counter;
077            }
078        }
079        
080        private ResetEventHub _resetHub;
081        private CountingResetListener _listener;
082        
083        DisableCachingFilter _filter;
084        WebRequestServicer _servicer;
085        
086        @BeforeClass
087        public void setup_Event_Hub()
088        {
089            _resetHub = new ResetEventHubImpl();
090            
091            _filter = new DisableCachingFilter();
092            _filter.setResetEventHub(_resetHub);
093            
094            _listener = new CountingResetListener();
095            
096            _resetHub.addResetEventListener(_listener);
097            
098            _servicer = new MockServicer(_listener);
099        }
100        
101        @Test(invocationCount = 100, threadPoolSize=4)
102        public void invoke_Listener() throws Exception
103        {
104            _filter.service(null, null, _servicer);
105        }
106        
107        @AfterSuite(alwaysRun=true)
108        public void verify_Reset_Counters()
109        {
110            assertEquals(_listener.getCount(), 0);
111        }
112    }