-
Notifications
You must be signed in to change notification settings - Fork 951
/
EmbeddedContainerWithGrailsSpec.groovy
140 lines (115 loc) · 5.12 KB
/
EmbeddedContainerWithGrailsSpec.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package grails.boot
import grails.artefact.Artefact
import org.codehaus.groovy.grails.commons.DefaultGrailsApplication
import org.codehaus.groovy.grails.commons.GrailsApplication
import org.codehaus.groovy.grails.commons.spring.DefaultRuntimeSpringConfiguration
import org.codehaus.groovy.grails.plugins.DefaultGrailsPluginManager
import org.codehaus.groovy.grails.plugins.GrailsPluginManager
import org.codehaus.groovy.grails.web.servlet.mvc.GrailsDispatcherServlet
import org.springframework.beans.BeansException
import org.springframework.beans.factory.ListableBeanFactory
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory
import org.springframework.beans.factory.groovy.GroovyBeanDefinitionReader
import org.springframework.beans.factory.support.BeanDefinitionRegistry
import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor
import org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory
import org.springframework.boot.context.embedded.ServletRegistrationBean
import org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory
import org.springframework.context.ApplicationContext
import org.springframework.context.ApplicationContextAware
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.context.annotation.Import
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar
import org.springframework.core.type.AnnotationMetadata
import org.springframework.http.HttpMethod
import org.springframework.http.client.ClientHttpRequest
import org.springframework.http.client.ClientHttpResponse
import org.springframework.http.client.SimpleClientHttpRequestFactory
import org.springframework.util.StreamUtils
import org.springframework.web.servlet.DispatcherServlet
import org.springframework.web.servlet.config.annotation.EnableWebMvc
import spock.lang.Specification
import java.nio.charset.Charset
import static org.junit.Assert.assertThat
/**
* Created by graemerocher on 28/05/14.
*/
class EmbeddedContainerWithGrailsSpec extends Specification {
AnnotationConfigEmbeddedWebApplicationContext context
void cleanup() {
context.close()
}
void "Test that you can load Grails in an embedded server config"() {
when:"An embedded server config is created"
this.context = new AnnotationConfigEmbeddedWebApplicationContext(
TomcatConfig.class);
then:"The context is valid"
context != null
new URL("http://localhost:${context.embeddedServletContainer.port}/foo/bar").text == 'hello world'
}
@Configuration
@Import([GrailsConfig.class, ApplicationConfig.class])
static class TomcatConfig {
@Bean
public EmbeddedServletContainerFactory containerFactory() {
return new TomcatEmbeddedServletContainerFactory(0);
}
}
@Configuration
static class GrailsConfig implements ImportBeanDefinitionRegistrar{
@Override
void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
def reader = new GroovyBeanDefinitionReader(registry)
reader.beans {
grailsApplication(DefaultGrailsApplication, [FooController, UrlMappings] as Class[]) { bean ->
bean.initMethod = 'initialise'
}
pluginManager(DefaultGrailsPluginManager, ref('grailsApplication')) { bean ->
bean.initMethod = 'loadPlugins'
}
pluginManagerProcessor(PluginManagerPostProcessor)
}
}
}
@Configuration
@EnableWebMvc
static class ApplicationConfig {
@Bean
public GrailsDispatcherServlet dispatcherServlet() {
new GrailsDispatcherServlet()
}
@Bean
public ServletRegistrationBean dispatcherServletRegistration() {
def servletBean = new ServletRegistrationBean(dispatcherServlet())
servletBean.loadOnStartup = 2
return servletBean;
}
}
}
@Artefact("Controller")
class FooController {
def bar() {
render "hello world"
}
}
class UrlMappings {
static mappings = {
"/$controller/$action?/$id?(.$format)?"()
}
}
class PluginManagerPostProcessor implements BeanDefinitionRegistryPostProcessor, ApplicationContextAware {
ApplicationContext applicationContext
@Override
void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {
GrailsPluginManager pluginManager = applicationContext.getBean(GrailsPluginManager)
def springConfig = new DefaultRuntimeSpringConfiguration()
springConfig.setBeanFactory((ListableBeanFactory) registry)
pluginManager.doRuntimeConfiguration(springConfig)
springConfig.registerBeansWithRegistry(registry)
}
@Override
void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
}
}