com.mtvnet.boxspring.testing.AbstractBoxSpringWebIntegrationTest Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of boxspring-test-support Show documentation
Show all versions of boxspring-test-support Show documentation
Classes for assisting in testing with BoxSpring.
The newest version!
/**
* Copyright (c) 2009, MTV Networks. All rights reserved.
*
* This program is licensed to you under the Apache License Version 2.0,
* and you may not use this file except in compliance with the Apache License Version 2.0.
* You may obtain a copy of the Apache License Version 2.0 at http://www.apache.org/licenses/LICENSE-2.0.
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the Apache License Version 2.0 is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Apache License Version 2.0 for the specific language governing permissions and limitations there under.
*/
package com.mtvnet.boxspring.testing;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockServletConfig;
import org.springframework.mock.web.MockServletContext;
import com.mtvnet.boxspring.web.LayeredPathedXmlWebApplicationContext;
/**
* Abstract base class for writing integration tests that use a BoxSpring
* container in a web context.
*
* @author edelsonj
*
*/
public abstract class AbstractBoxSpringWebIntegrationTest {
/**
* The ApplicationContext instance that tests should use.
*/
protected LayeredPathedXmlWebApplicationContext context;
/**
* The ApplicationContext instance that tests should use.
*/
protected LayeredPathedXmlWebApplicationContext dispatcherContext;
protected MockHttpServletRequest httpServletRequest;
protected MockHttpServletResponse httpServletResponse;
protected MockServletConfig servletConfig;
protected MockServletContext servletContext;
private ResourceLoader loader = new ResourceLoader() {
public ClassLoader getClassLoader() {
return this.getClassLoader();
}
public Resource getResource(String s) {
Resource r = new FileSystemResource("src/main/webapp/").createRelative(s);
return r;
}
};
/**
* Close the context to avoid any residual JMX issues.
*/
@After
public final void closeBoxSpringContext() {
dispatcherContext.close();
context.close();
}
/**
* Create a BoxSpring application context using the classpath and any
* additional resource roots specified.
*
* @throws Exception if something goes wrong
*/
@Before
public final void setupBoxSpringContext() throws Exception {
String servletName = getDispatcherServletName();
servletContext = new MockServletContext(loader);
servletConfig = new MockServletConfig(servletContext, servletName);
httpServletRequest = new MockHttpServletRequest(servletContext);
httpServletResponse = new MockHttpServletResponse();
context = new LayeredPathedXmlWebApplicationContext();
context.setServletContext(servletContext);
List configLocations = new ArrayList();
configLocations.add("classpath*:META-INF/spring/");
for (String stage : getStages()) {
configLocations.add("classpath*:META-INF/spring-" + stage + "/");
}
configLocations.add("/WEB-INF/");
String[] additionalConfigLocations = getAdditionalConfigLocations();
if (additionalConfigLocations != null) {
configLocations.addAll(Arrays.asList(additionalConfigLocations));
}
context.setConfigLocations(configLocations.toArray(new String[0]));
context.refresh();
dispatcherContext = new LayeredPathedXmlWebApplicationContext();
dispatcherContext.setParent(context);
dispatcherContext.setNamespace(servletName + "-servlet");
dispatcherContext.setServletContext(servletContext);
dispatcherContext.setServletConfig(servletConfig);
dispatcherContext.setConfigLocations(configLocations.toArray(new String[0]));
dispatcherContext.refresh();
}
/**
* Hook method for subclasses to specify an additional set of config
* locations.
*
* @return the additional config locations for testing.
*/
protected String[] getAdditionalConfigLocations() {
return null;
}
/**
* Hook method for subclasses to specify an additional set of resource
* roots.
*
* @return the additional resource roots for testing.
*/
protected String[] getAdditionalResourceRoots() {
return null;
}
/**
* Hook method for subclasses to specify an alternate name for the
* DispatcherServlet. This will impact which files are loaded by the
* servlet-specific Spring context.
*
* @return the dispatcher servlet name
*/
protected String getDispatcherServletName() {
return "dispatcher";
}
/**
* Hook method for subclasses to specify stages to use when testing. These
* will be automatically prepended with 'classpath*:META-INF/spring-' and
* added to the resource root list. These are added before those
* provided in getAdditionalResourceRoots().
*
* @return the stages to include in the configuration layout.
*/
protected String[] getStages() {
return new String[] { "test" };
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy