001    // Copyright 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    
015    package org.apache.tapestry.error;
016    
017    import org.apache.hivemind.ApplicationRuntimeException;
018    import org.apache.hivemind.util.PropertyUtils;
019    import org.apache.tapestry.IPage;
020    import org.apache.tapestry.IRequestCycle;
021    import org.apache.tapestry.html.BasePage;
022    import org.apache.tapestry.services.ResponseRenderer;
023    import org.testng.annotations.Test;
024    
025    /**
026     * Tests for {@link org.apache.tapestry.error.ExceptionPresenterImpl}.
027     * 
028     * @author Howard M. Lewis Ship
029     * @since 4.0
030     */
031    @Test
032    public class TestExceptionPresenter extends BaseErrorTestCase
033    {
034        public abstract static class ExceptionFixture extends BasePage
035        {
036            public abstract void setException(Throwable exception);
037        }
038    
039        public void testSuccess() throws Exception
040        {
041            Throwable cause = new IllegalArgumentException();
042            IPage page = newPage();
043    
044            IRequestCycle cycle = newCycle("Exception", page);
045            ResponseRenderer renderer = newRenderer(cycle, null);
046    
047            cycle.activate(page);
048    
049            replay();
050    
051            ExceptionPresenterImpl ep = new ExceptionPresenterImpl();
052            ep.setExceptionPageName("Exception");
053            ep.setResponseRenderer(renderer);
054    
055            ep.presentException(cycle, cause);
056    
057            verify();
058    
059            assertSame(cause, PropertyUtils.read(page, "exception"));
060        }
061    
062        public void testFailure() throws Exception
063        {
064            Throwable cause = new IllegalArgumentException();
065            Throwable renderCause = new ApplicationRuntimeException("Some failure.");
066    
067            IPage page = newPage();
068    
069            IRequestCycle cycle = newCycle("Exception", page);
070            
071            RequestExceptionReporter reporter = newReporter();
072            
073            cycle.activate(page);
074            
075            reporter.reportRequestException(ErrorMessages.unableToProcessClientRequest(cause), cause);
076            reporter.reportRequestException(ErrorMessages.unableToPresentExceptionPage(renderCause), renderCause);
077            
078            ResponseRenderer renderer = newRenderer(cycle, renderCause);
079            
080            replay();
081    
082            ExceptionPresenterImpl ep = new ExceptionPresenterImpl();
083            ep.setExceptionPageName("Exception");
084            ep.setResponseRenderer(renderer);
085            ep.setRequestExceptionReporter(reporter);
086    
087            try
088            {
089                ep.presentException(cycle, cause);
090                unreachable();
091            }
092            catch (ApplicationRuntimeException ex)
093            {
094                assertSame(renderCause, ex.getRootCause());
095            }
096    
097            verify();
098            
099            assertSame(cause, PropertyUtils.read(page, "exception"));
100        }
101    }