📄 geoservertestsupport.java
字号:
/* Copyright (c) 2001 - 2007 TOPP - www.openplans.org. All rights reserved.
* This code is licensed under the GPL 2.0 license, availible at the root
* application directory.
*/
package org.geoserver.test;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Collection;
import java.util.Iterator;
import java.util.StringTokenizer;
import java.util.logging.Logger;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import junit.framework.TestCase;
import org.geoserver.data.test.MockData;
import org.geoserver.ows.Dispatcher;
import org.geoserver.ows.util.ResponseUtils;
import org.geoserver.platform.GeoServerExtensions;
import org.geoserver.platform.GeoServerResourceLoader;
import org.geotools.data.FeatureSource;
import org.springframework.web.servlet.HandlerInterceptor;
import org.vfny.geoserver.global.Data;
import org.vfny.geoserver.global.GeoServer;
import org.vfny.geoserver.global.GeoserverDataDirectory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import com.mockrunner.mock.web.MockHttpServletRequest;
import com.mockrunner.mock.web.MockHttpServletResponse;
import com.mockrunner.mock.web.MockHttpSession;
import com.mockrunner.mock.web.MockServletContext;
/**
* Base test class for GeoServer unit tests.
* <p>
* Deriving from this test class provides the test case with preconfigured
* geoserver and catalog objects.
* </p>
* <p>
* This test case provides a spring application context which loads the
* application contexts from all modules on the classpath.
* </p>
* @author Justin Deoliveira, The Open Planning Project, jdeolive@openplans.org
*/
public class GeoServerTestSupport extends TestCase {
/**
* Common logger for test cases
*/
protected static final Logger LOGGER = org.geotools.util.logging.Logging.getLogger("org.geoserver.test");
/**
* mock GeoServer data directory
*/
protected MockData dataDirectory;
/**
* Application context
*/
protected GeoServerTestApplicationContext applicationContext;
/**
* If subclasses overide they *must* call super.setUp() first.
*/
protected void setUp() throws Exception {
super.setUp();
//set up the data directory
dataDirectory = new MockData();
dataDirectory.setUp();
//copy the service configuration to the data directory
dataDirectory.copyTo(GeoServerTestSupport.class.getResourceAsStream("services.xml"),
"services.xml");
//set up a mock servlet context
MockServletContext servletContext = new MockServletContext();
servletContext.setInitParameter("GEOSERVER_DATA_DIR",
dataDirectory.getDataDirectoryRoot().getAbsolutePath());
applicationContext = new GeoServerTestApplicationContext(new String[] {
"classpath*:/applicationContext.xml",
"classpath*:/applicationSecurityContext.xml"
}, servletContext);
applicationContext.refresh();
}
/**
* If subclasses overide they *must* call super.tearDown() first.
*/
protected void tearDown() throws Exception {
super.tearDown();
//kill the context
applicationContext.destroy();
applicationContext = null;
//kill the data directory
dataDirectory.tearDown();
GeoserverDataDirectory.destroy();
dataDirectory = null;
}
/**
* Accessor for global catalog instance from the test application context.
*/
protected Data getCatalog() {
return (Data) applicationContext.getBean("data");
}
/**
* Accessor for global geoserver instance from the test application context.
*/
protected GeoServer getGeoServer() {
return (GeoServer) applicationContext.getBean("geoServer");
}
/**
* Accessor for global resource loader instance from the test application context.
*/
protected GeoServerResourceLoader getResourceLoader() {
return (GeoServerResourceLoader) applicationContext.getBean( "resourceLoader" );
}
/**
* Loads a feature source from the catalog.
*
* @param typeName The qualified type name of the feature source.
*/
protected FeatureSource getFeatureSource(QName typeName)
throws IOException {
return getCatalog().getFeatureSource(typeName.getPrefix(), typeName.getLocalPart());
}
/**
* Convenience method for subclasses to create mock http servlet requests.
* <p>
* Examples of using this method are:
* <pre>
* <code>
* createRequest( "wfs?request=GetCapabilities" ); //get
* createRequest( "wfs" ); //post
* </code>
* </pre>
* </p>
* @param path The path for the request and optional the query string.
* @return
*/
protected MockHttpServletRequest createRequest(String path) {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setScheme("http");
request.setServerName("localhost");
request.setContextPath("/geoserver");
request.setRequestURI(ResponseUtils.stripQueryString(ResponseUtils.appendPath(
"/geoserver/", path)));
request.setQueryString(ResponseUtils.stripQueryString(path));
request.setRemoteAddr("127.0.0.1");
request.setServletPath(path);
kvp(request, path);
MockHttpSession session = new MockHttpSession();
session.setupServletContext(new MockServletContext());
request.setSession(session);
request.setUserPrincipal(null);
return request;
}
/**
* Executes an ows request using the GET method.
*
* @param path The porition of the request after hte context,
* example: 'wms?request=GetMap&version=1.1.1&..."
*
* @return An input stream which is the result of the request.
*
* @throws Exception
*/
protected InputStream get( String path ) throws Exception {
MockHttpServletResponse response = getAsServletResponse(path);
return new ByteArrayInputStream( response.getOutputStreamContent().getBytes() );
}
/**
* Executes an ows request using the GET method.
*
* @param path The porition of the request after hte context,
* example: 'wms?request=GetMap&version=1.1.1&..."
*
* @return the mock servlet response
*
* @throws Exception
*/
protected MockHttpServletResponse getAsServletResponse( String path ) throws Exception {
MockHttpServletRequest request = createRequest( path );
request.setMethod( "GET" );
request.setBodyContent(new byte[]{});
return dispatch( request );
}
/**
* Executes an ows request using the POST method with key value pairs
* form encoded.
*
* @param path The porition of the request after hte context,
* example: 'wms?request=GetMap&version=1.1.1&..."
*
* @return An input stream which is the result of the request.
*
* @throws Exception
*/
protected InputStream post( String path ) throws Exception {
MockHttpServletRequest request = createRequest( path );
request.setMethod( "POST" );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -