|
Groovy example source code file (AbstractHttpServletTest.groovy)
The Groovy AbstractHttpServletTest.groovy source code
/*
* Copyright 2003-2010 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 groovy.servlet
import javax.servlet.http.HttpServletRequest
import javax.servlet.ServletContext
import javax.servlet.ServletConfig
/**
* This test case tests the AbstractHttpServlet class. It uses a test
* specific subclass called ConcreteHttpServlet to test the abstract
* class in isolation from any implementations.
*
* @author Hamlet D'Arcy
*/
class AbstractHttpServletTest extends GroovyTestCase {
def servlet
void setUp() {
super.setUp()
servlet = new ConcreteHttpServlet()
}
/**
* getScriptUri() concatenates the servlet path and path info
* attributes if attributes exist on the http request.
*/
void testGetScriptUri_AllAttributesExist() {
//just return whatever attributes were requested
def request = {attribute -> attribute}
assert servlet.getScriptUri(request as HttpServletRequest) ==
AbstractHttpServlet.INC_SERVLET_PATH + AbstractHttpServlet.INC_PATH_INFO
}
/**
* getScriptUri() returns the servlet path if the http request
* contains path but no path info attribute.
*/
void testGetScriptUri_NoPathInfoAttribute() {
//just return whatever attributes were requested, except for path info attribute
def request = {attribute ->
if (attribute == AbstractHttpServlet.INC_PATH_INFO) {
return null
}
attribute
}
assert servlet.getScriptUri(request as HttpServletRequest) ==
AbstractHttpServlet.INC_SERVLET_PATH
}
/**
* Tests getScriptUri when no attributes exist, but servletPath and
* pathInfo methods return data.
*/
void testGetScriptUri_NoAttributesPathInfoExists() {
def request = [
getAttribute: {null},
getServletPath: {"servletPath"},
getPathInfo: {"pathInfo"}] as HttpServletRequest
def servlet = new ConcreteHttpServlet()
assert servlet.getScriptUri(request) == "servletPathpathInfo"
}
/**
* Tests getScriptUri when no attributes exist, no path info exists,
* but servletPath returns data.
*/
void testGetScriptUri_NoAttributesPathInfoMissing() {
def request = [
getAttribute: {null},
getServletPath: {"servletPath"},
getPathInfo: {null}] as HttpServletRequest
def servlet = new ConcreteHttpServlet()
assert servlet.getScriptUri(request) == "servletPath"
}
/**
* Tests getting URIs as files.
*/
void testGetScriptURIasFile() {
def request = [
getAttribute: {null},
getServletPath: {"servletPath"},
getPathInfo: {"pathInfo"}] as HttpServletRequest
def servletContext = [
getRealPath: { arg -> "realPath" + arg}] as ServletContext
def servletConfig = [
getServletContext: {servletContext},
getInitParameter: {null}] as ServletConfig
servlet.init(servletConfig)
def file = servlet.getScriptUriAsFile(request)
assert file.getName() == "realPathservletPathpathInfo"
}
/**
* Tests that exception is thrown when resource is not found.
*/
void testGetResourceConnection_MissingResource() {
def servletContext = [
getRealPath: {arg -> "realPath" + arg},
getResource: {arg -> null}] as ServletContext
def servletConfig = [
getServletContext: {servletContext},
getInitParameter: {null}] as ServletConfig
//servlet config is used to find resources
servlet.init(servletConfig)
shouldFail(groovy.util.ResourceException) {
servlet.getResourceConnection("someresource")
}
}
/**
* Tests finding resource.
*/
public void testGetResourceConnection_FoundInCurrentDir() {
def urlStub = new java.net.URL("file:realPath/someresource")
def servletContext = [
getRealPath: { arg -> "realPath" + arg},
getResource: {arg ->
if (arg == "/someresource") return urlStub
else return null
}] as ServletContext
def servletConfig = [
getServletContext: {servletContext},
getInitParameter: {null}] as ServletConfig
//servlet config is used to find resources
servlet.init(servletConfig)
def connection = servlet.getResourceConnection("someresource")
assert connection.getURL() == urlStub
}
/**
* Tests finding resource in web-inf directory.
*/
public void testGetResourceConnection_FoundInWebInf() {
def urlStub = new java.net.URL("file:realPath/WEB-INF/groovy/someresource")
def servletContext = [
getRealPath: { arg -> "realPath" + arg},
getResource: {arg ->
if (arg == "/WEB-INF/groovy/someresource") return urlStub
else return null
}] as ServletContext
def servletConfig = [
getServletContext: {servletContext},
getInitParameter: {null}] as ServletConfig
//servlet config is used to find resources
servlet.init(servletConfig)
def connection = servlet.getResourceConnection("someresource")
assert connection.getURL() == urlStub
}
/**
* Tests regex style resource replacement for first occurrence.
*/
public void testGetResourceConnection_Replace1stFooWithBar() {
def servletContext = [
getRealPath: {arg -> "realPath" + arg},
getResource: {arg ->
if (arg.startsWith("//")) arg = arg.substring(2)
new URL("http://" + (arg == "/" ? "" : arg))
}
] as ServletContext
def servletConfig = [
getServletContext: {servletContext},
getInitParameter: {arg ->
//replace first occurrence of foo resources with bar resources
if (arg == "resource.name.regex") return "foo"
else if (arg == "resource.name.replacement") return "bar"
else if (arg == "resource.name.replace.all") return "false"
else return null
}] as ServletConfig
def request = [
getAttribute: {null},
getServletPath: {"/somefoo/foo"},
getPathInfo: {null}] as HttpServletRequest
//servlet config is used to find resources
servlet.init(servletConfig)
//replace first foo with bar in resources
def connection = servlet.getResourceConnection(servlet.getScriptUri(request))
//expecting http://somebar/foo
def actual = connection.getURL().toExternalForm()
def expected = new URL("file:realPath/somebar/foo").toExternalForm()
assert actual == expected
}
/**
* Tests regex style resource replacement for all occurrences.
*/
public void testGetResourceConnection_ReplaceAllFooWithBar() {
def servletContext = [
getRealPath: {arg -> "realPath" + arg},
getResource: {arg ->
if (arg.startsWith("//")) arg = arg.substring(2)
new URL("http://" + (arg == "/" ? "" : arg))
}] as ServletContext
def servletConfig = [
getServletContext: {servletContext},
getInitParameter: {arg ->
//replace all occurrences of foo resources with bar resources
if (arg == "resource.name.regex") return "foo"
else if (arg == "resource.name.replacement") return "bar"
else if (arg == "resource.name.replace.all") return "true"
else return null
}] as ServletConfig
def request = [
getAttribute: {null},
getServletPath: {"/somefoo/foo"},
getPathInfo: {null}] as HttpServletRequest
//servlet config is used to find resources
servlet.init(servletConfig)
//replace all foo(s) with bar in resources
def connection = servlet.getResourceConnection(servlet.getScriptUri(request))
//expecting http://somebar/foo
def actual = connection.getURL().toExternalForm()
def expected = new URL("file:realPath/somebar/bar").toExternalForm()
assert actual == expected
}
}
//test specific subclass
class ConcreteHttpServlet extends AbstractHttpServlet {}
Other Groovy examples (source code examples)Here is a short list of links related to this Groovy AbstractHttpServletTest.groovy source code file: |
Other websites by Alvin Alexander:
Life/living in Alaska (OneMansAlaska.com)
How I Sold My Business (HowISoldMyBusiness.com)
Copyright 1998-2011 Alvin Alexander, devdaily.com
All Rights Reserved.