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