KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > test > mocks > servlet > MockServletContext


1 /**
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved.
3  * Please look at license.txt in info directory for more license detail.
4  **/

5
6 /**
7  * Created by The eXo Platform SARL .
8  * Author : Mestrallet Benjamin
9  * benjmestrallet@users.sourceforge.net
10  * Date: Jul 25, 2003
11  * Time: 12:26:58 AM
12  */

13 package org.exoplatform.test.mocks.servlet;
14 import javax.servlet.ServletContext JavaDoc;
15 import javax.servlet.RequestDispatcher JavaDoc;
16 import javax.servlet.Servlet JavaDoc;
17 import javax.servlet.ServletException JavaDoc;
18 import java.util.*;
19 import java.net.URL JavaDoc;
20 import java.net.MalformedURLException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23
24 public class MockServletContext implements ServletContext JavaDoc {
25
26   private String JavaDoc name_ ;
27   private HashMap initParams_ ;
28   private HashMap attributes_ ;
29   private String JavaDoc contextPath_ ;
30     private StringBuffer JavaDoc logBuffer = new StringBuffer JavaDoc();
31
32     public MockServletContext() {
33     this("portlet_app_1");
34   }
35
36   public MockServletContext(String JavaDoc name) {
37     name_ = name;
38     initParams_ = new HashMap() ;
39     attributes_ = new HashMap();
40   }
41
42   public MockServletContext(String JavaDoc name, String JavaDoc path) {
43     this(name);
44     contextPath_ = path;
45         attributes_.put("javax.servlet.context.tempdir", path);
46   }
47   
48   public void setName(String JavaDoc name){
49     name_ = name;
50   }
51
52     public String JavaDoc getLogBuffer() {
53         try {
54             return logBuffer.toString();
55         } finally {
56             logBuffer = new StringBuffer JavaDoc();
57         }
58     }
59
60   public ServletContext JavaDoc getContext(String JavaDoc s) {
61     return null;
62   }
63
64   public int getMajorVersion() {
65     return 2;
66   }
67
68   public int getMinorVersion() {
69     return 3;
70   }
71
72   public String JavaDoc getMimeType(String JavaDoc s) {
73     return "text/html";
74   }
75
76   public Set getResourcePaths(String JavaDoc s) {
77         Set set = new HashSet();
78         set.add("/test1");
79         set.add("/WEB-INF");
80         set.add("/test2");
81     return set;
82   }
83
84   public URL JavaDoc getResource(String JavaDoc s) throws MalformedURLException JavaDoc {
85     String JavaDoc path = "file:" + contextPath_ + s ;
86     URL JavaDoc url = new URL JavaDoc(path) ;
87     return url ;
88   }
89
90   public InputStream JavaDoc getResourceAsStream(String JavaDoc s) {
91         try {
92             return getResource(s).openStream();
93         } catch (IOException JavaDoc e) {
94             e.printStackTrace();
95         }
96         return null;
97     }
98
99   public RequestDispatcher JavaDoc getRequestDispatcher(String JavaDoc s) {
100     return null;
101   }
102
103   public RequestDispatcher JavaDoc getNamedDispatcher(String JavaDoc s) {
104     return null;
105   }
106
107   public Servlet JavaDoc getServlet(String JavaDoc s) throws ServletException JavaDoc {
108     return null;
109   }
110
111   public Enumeration getServlets() {
112     return null;
113   }
114
115   public Enumeration getServletNames() {
116     return null;
117   }
118
119   public void log(String JavaDoc s) {
120         logBuffer.append(s);
121   }
122
123   public void log(Exception JavaDoc e, String JavaDoc s) {
124         logBuffer.append(s + e.getMessage());
125   }
126
127   public void log(String JavaDoc s, Throwable JavaDoc throwable) {
128         logBuffer.append(s + throwable.getMessage());
129   }
130
131   public void setContextPath(String JavaDoc s) {
132     contextPath_ = s ;
133   }
134
135   public String JavaDoc getRealPath(String JavaDoc s) {
136     return contextPath_ + s ;
137   }
138
139   public String JavaDoc getServerInfo() {
140     return null;
141   }
142
143   public void setInitParameter(String JavaDoc name, String JavaDoc value) {
144     initParams_.put(name, value) ;
145   }
146
147   public String JavaDoc getInitParameter(String JavaDoc name) {
148     return (String JavaDoc) initParams_.get(name) ;
149   }
150
151   public Enumeration getInitParameterNames() {
152     Vector keys = new Vector(initParams_.keySet()) ;
153     return keys.elements() ;
154   }
155
156   public Object JavaDoc getAttribute(String JavaDoc name) {
157     return attributes_.get(name) ;
158   }
159
160   public Enumeration getAttributeNames() {
161     Vector keys = new Vector(attributes_.keySet()) ;
162     return keys.elements() ;
163   }
164
165   public void setAttribute(String JavaDoc name, Object JavaDoc value) {
166     attributes_.put(name, value) ;
167   }
168
169   public void removeAttribute(String JavaDoc name) {
170     attributes_.remove(name) ;
171   }
172
173   public String JavaDoc getServletContextName() {
174     return name_ ;
175   }
176 }
177
Popular Tags