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.web;
016    
017    import static org.easymock.EasyMock.expect;
018    import org.testng.annotations.Test;
019    
020    import javax.servlet.ServletContext;
021    import java.net.MalformedURLException;
022    import java.net.URL;
023    import java.util.List;
024    
025    /**
026     * Tests for {@link org.apache.tapestry.web.ServletWebContext}.
027     * 
028     */
029    @Test(sequential = true)
030    public class TestServletWebContext extends BaseWebTestCase
031    {
032    
033        public void testGetInitParameterNames()
034        {
035            ServletContext context = newMock(ServletContext.class);
036    
037            expect(context.getInitParameterNames()).andReturn(newEnumeration());
038    
039            replay();
040    
041            WebContext wc = new ServletWebContext(context);
042    
043            List l = wc.getInitParameterNames();
044    
045            checkList(l);
046    
047            verify();
048        }
049    
050        public void testGetInitParameterValue()
051        {
052            String value = "William Orbit";
053    
054            ServletContext context = newMock(ServletContext.class);
055    
056            expect(context.getInitParameter("artist")).andReturn(value);
057    
058            replay();
059    
060            WebContext wc = new ServletWebContext(context);
061    
062            assertSame(value, wc.getInitParameterValue("artist"));
063    
064            verify();
065        }
066    
067        public void testGetAttributeNames()
068        {
069            ServletContext context = newMock(ServletContext.class);
070    
071            expect(context.getAttributeNames()).andReturn(newEnumeration());
072    
073            replay();
074    
075            WebContext wc = new ServletWebContext(context);
076    
077            List l = wc.getAttributeNames();
078    
079            checkList(l);
080    
081            verify();
082        }
083    
084        public void testGetAttribute()
085        {
086            Object attribute = new Object();
087    
088            ServletContext context = newMock(ServletContext.class);
089    
090            expect(context.getAttribute("attr")).andReturn(attribute);
091    
092            replay();
093    
094            WebContext wc = new ServletWebContext(context);
095    
096            assertSame(attribute, wc.getAttribute("attr"));
097    
098            verify();
099        }
100    
101        public void testSetAttribute()
102        {
103            Object attribute = new Object();
104    
105            ServletContext context = newMock(ServletContext.class);
106    
107            context.setAttribute("name", attribute);
108    
109            replay();
110    
111            WebContext wc = new ServletWebContext(context);
112    
113            wc.setAttribute("name", attribute);
114    
115            verify();
116        }
117    
118        public void testSetAttributeToNull()
119        {
120            ServletContext context = newMock(ServletContext.class);
121    
122            context.removeAttribute("tonull");
123    
124            replay();
125    
126            WebContext wc = new ServletWebContext(context);
127    
128            wc.setAttribute("tonull", null);
129    
130            verify();
131        }
132    
133        public void testGetResource() throws Exception
134        {
135            URL url = new URL("http://jakarta.apache.org/tapestry");
136    
137            ServletContext context = newMock(ServletContext.class);
138    
139            expect(context.getResource("/tapestry")).andReturn(url);
140    
141            replay();
142    
143            WebContext wc = new ServletWebContext(context);
144    
145            assertSame(url, wc.getResource("/tapestry"));
146    
147            verify();
148        }
149    
150        public void testGetResourceFailure() throws Exception
151        {
152            Throwable t = new MalformedURLException("Like this ever happens.");
153    
154            ServletContext context = newMock(ServletContext.class);
155    
156            expect(context.getResource("/tapestry")).andThrow(t);
157    
158            replay();
159    
160            // interceptLogging(ServletWebContext.class.getName());
161    
162            WebContext wc = new ServletWebContext(context);
163    
164            assertNull(wc.getResource("/tapestry"));
165    
166            verify();
167            
168            // assertLoggedMessage("Error getting context resource '/tapestry': Like this ever happens.");
169        }
170    }