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    
015    package org.apache.tapestry.test.mock;
016    
017    import java.io.File;
018    import java.io.IOException;
019    import java.io.InputStream;
020    import java.net.MalformedURLException;
021    import java.net.URL;
022    import java.util.Collections;
023    import java.util.Enumeration;
024    import java.util.HashMap;
025    import java.util.Map;
026    import java.util.Set;
027    
028    import javax.servlet.RequestDispatcher;
029    import javax.servlet.Servlet;
030    import javax.servlet.ServletContext;
031    import javax.servlet.ServletException;
032    
033    /**
034     * Mock implementation of {@link javax.servlet.ServletContext}.
035     *
036     * @author Howard Lewis Ship
037     * @since 4.0
038     */
039    
040    public class MockContext extends AttributeHolder implements ServletContext, InitParameterHolder
041    {
042        private MockSession _session;
043    
044        private static final Map _suffixToContentType = new HashMap();
045    
046        static {
047            _suffixToContentType.put("html", "text/html");
048            _suffixToContentType.put("gif", "image/gif");
049            _suffixToContentType.put("png", "image/png");
050        }
051    
052        private String _rootDirectory;
053        private String _servletContextName = "test";
054        private Map _initParameters = new HashMap();
055    
056        public MockContext()
057        {
058        }
059    
060        public MockContext(String testDirectory)
061        {
062            _rootDirectory = testDirectory + "/context";
063        }
064    
065        public ServletContext getContext(String name)
066        {
067            return null;
068        }
069    
070        public int getMajorVersion()
071        {
072            return 2;
073        }
074    
075        public int getMinorVersion()
076        {
077            return 1;
078        }
079    
080        public String getMimeType(String path)
081        {
082            int lastx = path.lastIndexOf('.');
083            String suffix = path.substring(lastx + 1);
084    
085            return (String) _suffixToContentType.get(suffix);
086        }
087    
088        public Set getResourcePaths(String arg0)
089        {
090            return null;
091        }
092    
093        public URL getResource(String path) throws MalformedURLException
094        {
095            if (path == null || !path.startsWith("/"))
096                throw new MalformedURLException("Not a valid context path.");
097    
098            String fullPath = _rootDirectory + path;
099    
100            File file = new File(fullPath);
101    
102            if (file.exists())
103                return file.toURL();
104    
105            return null;
106        }
107    
108        public InputStream getResourceAsStream(String path)
109        {
110            try
111            {
112                URL url = getResource(path);
113    
114                if (url == null)
115                    return null;
116    
117                return url.openStream();
118            }
119            catch (MalformedURLException ex)
120            {
121                return null;
122            }
123            catch (IOException ex)
124            {
125                return null;
126            }
127        }
128    
129        /**
130         *  Gets a dispatcher for the given path.  Path should be a relative path (relative
131         *  to the context).  A special case:  "NULL" returns null (i.e., when a 
132         *  dispatcher can't be found).
133         * 
134         **/
135    
136        public RequestDispatcher getRequestDispatcher(String path)
137        {
138            if (path.endsWith("/NULL"))
139                return null;
140    
141            StringBuffer buffer = new StringBuffer(_rootDirectory);
142            buffer.append(path);
143    
144            // Simulate the handling of directories by serving the index.html
145            // in the directory.
146    
147            if (path.endsWith("/"))
148                buffer.append("index.html");
149    
150            return new MockRequestDispatcher(buffer.toString());
151        }
152    
153        public RequestDispatcher getNamedDispatcher(String name)
154        {
155            return null;
156        }
157    
158        public Servlet getServlet(String name) throws ServletException
159        {
160            return null;
161        }
162    
163        public Enumeration getServlets()
164        {
165            return null;
166        }
167    
168        public Enumeration getServletNames()
169        {
170            return null;
171        }
172    
173        public void log(String message)
174        {
175            log(message, null);
176        }
177    
178        public void log(Exception exception, String message)
179        {
180            log(message, exception);
181        }
182    
183        public void log(String message, Throwable exception)
184        {
185        }
186    
187        public String getRealPath(String arg0)
188        {
189            return null;
190        }
191    
192        public String getContextPath()
193        {
194            return null;
195        }
196        
197        public String getServerInfo()
198        {
199            return "Tapestry Mock Objects";
200        }
201    
202        public String getInitParameter(String name)
203        {
204            return (String) _initParameters.get(name);
205        }
206    
207        public Enumeration getInitParameterNames()
208        {
209            return Collections.enumeration(_initParameters.keySet());
210        }
211    
212        public void setInitParameter(String name, String value)
213        {
214            _initParameters.put(name, value);
215        }
216    
217        public String getServletContextName()
218        {
219            return _servletContextName;
220        }
221    
222        public MockSession createSession()
223        {
224            if (_session == null)
225            {
226                String id = Long.toHexString(System.currentTimeMillis());
227    
228                _session = new MockSession(this, id);
229            }
230    
231            return _session;
232        }
233    
234        public MockSession getSession()
235        {
236            return _session;
237        }
238    
239        public void setServletContextName(String servletContextName)
240        {
241            _servletContextName = servletContextName;
242        }
243    
244        public String getRootDirectory()
245        {
246            return _rootDirectory;
247        }
248    
249        public void setRootDirectory(String rootDirectory)
250        {
251            _rootDirectory = rootDirectory;
252        }
253    
254    }