GRAILS-1019: Allowing expressions to be used with the 'disabled' attribute for g...
[grails.git] / test / commons / org / codehaus / groovy / grails / commons / test / AbstractGrailsResourceTests.java
blob8e74264e5690f6c116fa6a55b455d97388bec052
1 /*
2 * Copyright 2004-2005 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.codehaus.groovy.grails.commons.test;
18 import java.beans.IntrospectionException;
20 import javax.servlet.http.HttpServletRequest;
21 import javax.servlet.http.HttpServletResponse;
23 import groovy.lang.GroovyClassLoader;
24 import groovy.lang.GroovyObject;
26 import org.codehaus.groovy.grails.commons.DefaultGrailsApplication;
27 import org.codehaus.groovy.grails.commons.GrailsApplication;
28 import org.codehaus.groovy.grails.commons.GrailsControllerClass;
29 import org.codehaus.groovy.grails.commons.ControllerArtefactHandler;
30 import org.codehaus.groovy.grails.commons.spring.GrailsRuntimeConfigurator;
31 import org.codehaus.groovy.grails.orm.hibernate.cfg.GrailsHibernateUtil;
32 import org.codehaus.groovy.grails.support.MockApplicationContext;
33 import org.codehaus.groovy.grails.web.servlet.GrailsApplicationAttributes;
34 import org.codehaus.groovy.grails.web.servlet.mvc.GrailsControllerHelper;
35 import org.codehaus.groovy.grails.web.servlet.mvc.SimpleGrailsControllerHelper;
36 import org.hibernate.SessionFactory;
37 import org.hibernate.classic.Session;
38 import org.springframework.context.support.StaticMessageSource;
39 import org.springframework.core.io.Resource;
40 import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
41 import org.springframework.mock.web.MockHttpServletRequest;
42 import org.springframework.mock.web.MockHttpServletResponse;
43 import org.springframework.mock.web.MockServletContext;
44 import org.springframework.orm.hibernate3.SessionFactoryUtils;
45 import org.springframework.orm.hibernate3.SessionHolder;
46 import org.springframework.transaction.support.TransactionSynchronizationManager;
47 import org.springframework.web.context.WebApplicationContext;
48 import org.springframework.web.servlet.DispatcherServlet;
49 import org.springframework.web.servlet.i18n.AcceptHeaderLocaleResolver;
51 import junit.framework.TestCase;
52 /**
53 * <p>Abstract test harness that takes some classes and bootstraps the whole Grails environment.
54 * <p>
55 * This class can be extended by an child that wants to bootstrap the whole Grails environment
56 * and perform integration type tests against a real data source, and mocked servlet container.
58 * <p>
59 * You can modify the resourcePattern the test case uses to load Grails classes using the onSetUp method
61 * <pre>
62 * void onSetUp() {
63 * resourcePattern = "my/path/grails-app/**.groovy"
64 * }
65 * </pre>
67 * The above code loads all of the groovy files in the specified path into a Grails environment
70 * @author Graeme Rocher
73 public abstract class AbstractGrailsResourceTests extends TestCase {
74 /**
75 * A GroovyClassLoader instance
77 public GroovyClassLoader gcl = new GroovyClassLoader();
78 /**
79 * The GrailsApplication instance created during setup
81 public GrailsApplication ga;
82 protected StaticMessageSource messageSource;
83 protected MockApplicationContext parentContext;
84 protected WebApplicationContext applicationContext;
85 protected MockServletContext servletContext;
86 protected SessionFactory sessionFactory;
87 protected Session session;
88 protected String resourcePattern = "**/grails-app/*/*.groovy";
89 private PathMatchingResourcePatternResolver resolver;
90 private Resource[] resources;
94 public void setResourcePattern(String resourcePattern) {
95 this.resourcePattern = resourcePattern;
100 protected final void setUp() throws Exception {
101 super.setUp();
103 onSetUp();
104 resolver = new PathMatchingResourcePatternResolver();
105 resources = resolver.getResources(resourcePattern);
106 ga = new DefaultGrailsApplication(resources);
107 parentContext = new MockApplicationContext();
108 parentContext.registerMockBean("grailsApplication", ga);
109 GrailsRuntimeConfigurator configurator = new GrailsRuntimeConfigurator(ga, parentContext);
110 servletContext = new MockServletContext();
111 applicationContext = configurator.configure(servletContext);
113 if(this.applicationContext.containsBean(GrailsRuntimeConfigurator.SESSION_FACTORY_BEAN)) {
114 this.sessionFactory = (SessionFactory)this.applicationContext.getBean(GrailsRuntimeConfigurator.SESSION_FACTORY_BEAN);
115 GrailsHibernateUtil.configureDynamicMethods(applicationContext,ga);
117 if(!TransactionSynchronizationManager.hasResource(this.sessionFactory)) {
118 this.session = this.sessionFactory.openSession();
119 TransactionSynchronizationManager.bindResource(this.sessionFactory, new SessionHolder(session));
126 protected GroovyObject getMockController(String name) throws IntrospectionException {
128 GrailsControllerClass controllerClass = (GrailsControllerClass) ga.getArtefact(
129 ControllerArtefactHandler.TYPE, name);
130 if(controllerClass == null)
131 throw new IllegalArgumentException("Controller not found for name " + name);
133 GroovyObject mockController = (GroovyObject)controllerClass.newInstance();
134 this.messageSource = new StaticMessageSource();
135 servletContext.setAttribute(GrailsApplicationAttributes.APPLICATION_CONTEXT,parentContext);
137 GrailsControllerHelper helper = new SimpleGrailsControllerHelper(ga,parentContext,servletContext);
138 HttpServletRequest request = createMockRequest();
139 request.setAttribute(GrailsApplicationAttributes.CONTROLLER, mockController);
142 HttpServletResponse response = new MockHttpServletResponse();
144 mockController.setProperty("controllerUri", "/"+controllerClass.getLogicalPropertyName());
145 return mockController;
151 * @return
153 protected HttpServletRequest createMockRequest() {
154 HttpServletRequest request = new MockHttpServletRequest(servletContext);
155 request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
156 return request;
159 protected HttpServletRequest createMockRequest(String uri) {
160 MockHttpServletRequest request = new MockHttpServletRequest(servletContext);
161 request.setRequestURI(uri);
162 request.setAttribute(DispatcherServlet.LOCALE_RESOLVER_ATTRIBUTE, new AcceptHeaderLocaleResolver());
163 return request;
166 protected abstract void onSetUp();
168 protected final void tearDown() throws Exception {
169 super.tearDown();
170 if(TransactionSynchronizationManager.hasResource(this.sessionFactory)) {
171 SessionHolder holder = (SessionHolder) TransactionSynchronizationManager.getResource(this.sessionFactory);
172 org.hibernate.Session s = holder.getSession();
173 s.flush();
174 TransactionSynchronizationManager.unbindResource(this.sessionFactory);
175 SessionFactoryUtils.releaseSession(s, this.sessionFactory);
177 onTearDown();
179 gcl = null;
180 ga = null;
181 messageSource = null;
182 parentContext = null;
183 applicationContext = null;
184 servletContext = null;
185 sessionFactory = null;
186 session = null;
187 resourcePattern = null;
188 resolver = null;
189 resources = null;
193 * Called directly before destruction of the TestCase in the junit.framework.TestCase#tearDown() method
195 protected void onTearDown() throws Exception {
201 public Resource[] getResources() {
202 return resources;