📄 mockservletcontext.java
字号:
/*
* Copyright 2002-2004 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.mock;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.Set;
import java.util.Vector;
import javax.servlet.ServletContext;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
/**
* This class is a test implementation of the ServletContext class.
* Based on ServletUnit, by Rusell Gold
* <p>It only requires a pretty simple implementation to bootstrap the Spring framework.
* getResourceAsStream() and getInitParameter() must be implemented.
**/
public class MockServletContext implements ServletContext {
/** Root of web app */
private String warRoot;
private String displayName;
private java.util.Properties initParams = new java.util.Properties();
private final static Vector EMPTY_VECTOR = new Vector();
private Hashtable _attributes = new Hashtable();
/**
* Param warRoot should not end with a /.
*/
public MockServletContext(String warRoot) throws Exception {
this.warRoot = warRoot;
}
/**
* Param warRoot should not end with a /.
*/
public MockServletContext(String warRoot, String webXml) throws Exception {
this.warRoot = warRoot;
InputStream is = getClass().getResourceAsStream(warRoot + webXml);
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = db.parse(is);
parseWebXml(doc);
}
/** Won't be able to load resources */
public MockServletContext() {
this.warRoot = null;
//System.out.println("------> MockServletContext: no WAR root!!");
}
private void parseWebXml(Document webxml) {
NodeList nl = webxml.getElementsByTagName("display-name");
if (nl.getLength() > 0)
displayName = getText(nl.item(0));
nl = webxml.getElementsByTagName("context-param");
for (int i = 0; i < nl.getLength(); i++) {
//System.out.println(nl.item(i));
parseContextParamNode((Element) nl.item(i));
}
}
private void parseContextParamNode(Element n) {
//<context-param>
//<param-name>configPath</param-name>
//<param-value>/WEB-INF/applicationContext.xml</param-value>
//</context-param>
// CRIMSON HACK!?
Node name = n.getElementsByTagName("param-name").item(0);
Node value = n.getElementsByTagName("param-value").item(0);
// System.out.println("Added context param '" + name);
addInitParameter(getText(name), getText(value));
}
private String getText(Node e) {
if (e instanceof Text)
return ((Text) e).getData();
NodeList nl2 = e.getChildNodes();
if (nl2.getLength() != 1 || !(nl2.item(0) instanceof Text))
throw new RuntimeException("Unexpected element or type mismatch: " + nl2.item(0) + "; tag name was <" + ((Element) e).getTagName() + ">" );
Text t = (Text) nl2.item(0);
return t.getData();
}
/**
* Returns a ServletContext object that corresponds to a specified URL on the server.
* <p>
* This method allows servlets to gain access to the context for various parts of the server,
* and as needed obtain RequestDispatcher objects from the context. The given path must be
* absolute (beginning with "/") and is interpreted based on the server's document root.
* <p>
* In a security conscious environment, the servlet container may return null for a given URL.
**/
public javax.servlet.ServletContext getContext(java.lang.String A) {
throw new UnsupportedOperationException("getContext not implemented");
}
/**
* Returns the major version of the Java Servlet API that this servlet container supports.
* All implementations that comply with Version 2.3 must have this method return the integer 2.
**/
public int getMajorVersion() {
return 2;
}
/**
* Returns the minor version of the Servlet API that this servlet container supports.
* All implementations that comply with Version 2.3 must have this method return the integer 3.
**/
public int getMinorVersion() {
return 3;
}
/**
* Returns the MIME type of the specified file, or null if the MIME type is not known.
* The MIME type is determined by the configuration of the servlet container, and
* may be specified in a web application deployment descriptor. Common MIME types are
* "text/html" and "image/gif".
**/
public java.lang.String getMimeType(String filePath) {
return null; // XXX not implemented
}
/**
* Returns a URL to the resource that is mapped to a specified path. The path must begin
* with a "/" and is interpreted as relative to the current context root.
* <p>
* This method allows the servlet container to make a resource available to servlets from any source.
* Resources can be located on a local or remote file system, in a database, or in a .war file.
* <p>
* The servlet container must implement the URL handlers and URLConnection objects that are necessary to access the resource.
* <p>
* This method returns null if no resource is mapped to the pathname.
*
* Some containers may allow writing to the URL returned by this method using the methods of the URL class.
*
* The resource content is returned directly, so be aware that requesting a .jsp page returns the JSP source code. Use a
* RequestDispatcher instead to include results of an execution.
*
* This method has a different purpose than java.lang.Class.getResource, which looks up resources based on a class loader. This
* method does not use class loaders.
**/
public java.net.URL getResource(String path) throws java.net.MalformedURLException {
if (warRoot == null)
//return null;
throw new UnsupportedOperationException("No war root: getResource fails");
return new java.net.URL(warRoot + path);
}
/**
* Returns the resource located at the named path as an InputStream object.
*
* The data in the InputStream can be of any type or length. The path must be specified according to the rules given in getResource.
* This method returns null if no resource exists at the specified path.
* Meta-information such as content length and content type that is available via getResource method is lost when using this method.
* The servlet container must implement the URL handlers and URLConnection objects necessary to access the resource.
* This method is different from java.lang.Class.getResourceAsStream, which uses a class loader. This method allows servlet
* containers to make a resource available to a servlet from any location, without using a class loader.
**/
public java.io.InputStream getResourceAsStream(String path) {
if (warRoot == null)
//return null;
throw new UnsupportedOperationException("No war root: getResourceAsStream fails");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -