1 package org
.codehaus
.groovy
.grails
.commons
;
3 import java
.io
.IOException
;
5 import org
.codehaus
.groovy
.grails
.plugins
.*
6 import org
.codehaus
.groovy
.grails
.commons
.test
.AbstractGrailsMockTests
;
7 import org
.codehaus
.groovy
.grails
.plugins
.exceptions
.PluginException
8 import org
.springframework
.beans
.factory
.support
.RootBeanDefinition
;
9 import org
.springframework
.context
.support
.GenericApplicationContext
;
10 import org
.springframework
.web
.servlet
.i18n
.CookieLocaleResolver
11 import org
.springframework
.core
.io
.Resource
;
14 public class GrailsPluginManagerTests
extends AbstractGrailsMockTests
{
16 private static final String RESOURCE_PATH
= "classpath:org/codehaus/groovy/grails/plugins/ClassEditorGrailsPlugin.groovy";
18 protected void onSetUp() {
24 driverClassName = "org.hsqldb.jdbcDriver"
27 dbCreate = "create-drop" // one of 'create
', 'create
-drop
','update
'
28 url = "jdbc:hsqldb:mem:devDB"
32 cache.use_second_level_cache=true
33 cache.use_query_cache=true
34 cache.provider_class='org
.hibernate
.cache
.OSCacheProvider
'
41 public void testObservablePlugin() {
42 def manager
= new DefaultGrailsPluginManager([MyGrailsPlugin
,AnotherGrailsPlugin
, ObservingGrailsPlugin
] as Class
[], ga
)
46 assertTrue manager
.hasGrailsPlugin("another")
48 // Get the "another" plugin and all the plugins that are
50 def plugin
= manager
.getGrailsPlugin("another")
51 def observers
= manager
.getPluginObservers(plugin
)
53 // Check that the observers are what we expect (note that the
54 // core plugin "logging" should be one of them).
55 def expectedObservers
= ["logging", "observing"]
56 assertTrue observers
*.name
.containsAll(expectedObservers
)
57 assertEquals expectedObservers
.size(), observers
.size()
59 // Also check that the "logging" plugin is observing the "my"
60 // plugin (which is not observed by any other plugins).
61 observers
= manager
.getPluginObservers(manager
.getGrailsPlugin("my"))
62 expectedObservers
= ["logging"]
64 assertTrue observers
*.name
.containsAll(expectedObservers
)
65 assertEquals expectedObservers
.size(), observers
.size()
67 // Make sure the observers are being notified of changes to
68 // the observed plugin.
69 def event
= [source
:"foo"]
70 manager
.informObservers("another", event
)
72 assertEquals
"bar", event
.source
75 public void testNoSelfObserving() {
76 def manager
= new DefaultGrailsPluginManager([AnotherGrailsPlugin
,ObservingAllGrailsPlugin
] as Class
[], ga
)
80 // Get the "another" plugin and all the plugins that are
82 def plugin
= manager
.getGrailsPlugin("another")
83 def observers
= manager
.getPluginObservers(plugin
)
85 // Check that the observers are what we expect (note that the
86 // core plugin "logging" should be one of them).
87 def expectedObservers
= ["logging", "observingAll"]
88 assertTrue observers
*.name
.containsAll(expectedObservers
)
89 assertEquals expectedObservers
.size(), observers
.size()
91 // Now check that the "observingAll" plugin is *not* observing
93 observers
= manager
.getPluginObservers(manager
.getGrailsPlugin("observingAll"))
94 expectedObservers
= ["logging"]
96 assertTrue observers
*.name
.containsAll(expectedObservers
)
97 assertEquals expectedObservers
.size(), observers
.size()
100 public void testDisabledPlugin() {
101 def manager
= new DefaultGrailsPluginManager([MyGrailsPlugin
,AnotherGrailsPlugin
,DisabledGrailsPlugin
] as Class
[], ga
)
103 manager
.loadPlugins()
105 assertTrue manager
.hasGrailsPlugin("my")
106 assertNotNull manager
.getGrailsPlugin("my").instance
107 assertFalse manager
.hasGrailsPlugin("disabled")
110 public void testDefaultGrailsPluginManager() throws Exception
{
111 DefaultGrailsPluginManager manager
= new DefaultGrailsPluginManager(RESOURCE_PATH
,ga
);
112 assertEquals(1, manager
.getPluginResources().length
);
115 public void testLoadPlugins() throws Exception
{
116 println
"LOAD PLUGINS"
117 GrailsPluginManager manager
= new DefaultGrailsPluginManager(RESOURCE_PATH
,ga
);
118 manager
.loadPlugins();
121 GrailsPlugin plugin
= manager
.getGrailsPlugin("classEditor");
122 assertNotNull(plugin
);
123 assertEquals("classEditor",plugin
.getName());
124 assertEquals("1.1", plugin
.getVersion());
126 plugin
= manager
.getGrailsPlugin("classEditor", "1.1");
127 assertNotNull(plugin
);
129 plugin
= manager
.getGrailsPlugin("classEditor", "1.2");
133 public void testWithLoadLastPlugin() throws Exception
{
134 def manager
= new DefaultGrailsPluginManager([MyGrailsPlugin
,AnotherGrailsPlugin
,ShouldLoadLastGrailsPlugin
] as Class
[], ga
)
136 manager
.loadPlugins()
140 public void testDependencyResolutionFailure() throws Exception
{
141 def manager
= new DefaultGrailsPluginManager([MyGrailsPlugin
] as Class
[], ga
)
144 manager
.loadPlugins()
145 assert !manager
.hasGrailsPlugin("my")
147 catch(PluginException pe
) {
152 public void testDependencyResolutionSucces() throws Exception
{
153 def manager
= new DefaultGrailsPluginManager([MyGrailsPlugin
,AnotherGrailsPlugin
, SomeOtherGrailsPlugin
] as Class
[], ga
)
155 manager
.loadPlugins()
158 public void testDoRuntimeConfiguration() {
159 def manager
= new DefaultGrailsPluginManager([MyGrailsPlugin
,AnotherGrailsPlugin
] as Class
[], ga
)
161 manager
.loadPlugins()
163 def parent
= createMockApplicationContext()
164 parent
.registerMockBean("grailsApplication", ga
)
165 parent
.registerMockBean(PluginMetaManager
.BEAN_ID
, new DefaultPluginMetaManager(new Resource
[0]));
167 def springConfig
= new org
.codehaus
.groovy
.grails
.commons
.spring
.WebRuntimeSpringConfiguration(parent
)
168 springConfig
.servletContext
= createMockServletContext()
169 manager
.doRuntimeConfiguration(springConfig
)
171 def ctx
= springConfig
.getApplicationContext()
173 assert ctx
.containsBean("classEditor")
176 public void testDoPostProcessing() {
177 def manager
= new DefaultGrailsPluginManager([MyGrailsPlugin
,AnotherGrailsPlugin
] as Class
[], ga
)
179 manager
.loadPlugins()
181 def parent
= createMockApplicationContext()
182 parent
.registerMockBean("grailsApplication", ga
)
183 parent
.registerMockBean(PluginMetaManager
.BEAN_ID
, new DefaultPluginMetaManager(new Resource
[0]));
184 def springConfig
= new org
.codehaus
.groovy
.grails
.commons
.spring
.WebRuntimeSpringConfiguration(parent
)
185 springConfig
.servletContext
= createMockServletContext()
187 manager
.doRuntimeConfiguration(springConfig
)
189 def ctx
= springConfig
.getApplicationContext()
191 assert ctx
.containsBean("classEditor")
193 manager
.doPostProcessing(ctx
)
195 assert ctx
.containsBean("localeResolver")
199 public void testEviction() {
200 def manager
= new DefaultGrailsPluginManager([MyGrailsPlugin
,AnotherGrailsPlugin
,SomeOtherGrailsPlugin
,ShouldEvictSomeOtherGrailsPlugin
] as Class
[], ga
)
202 manager
.loadPlugins()
204 assertFalse manager
.hasGrailsPlugin("someOther")
205 assertTrue manager
.hasGrailsPlugin("my")
206 assertTrue manager
.hasGrailsPlugin("another")
207 assertTrue manager
.hasGrailsPlugin("shouldEvictSomeOther")
210 void testShutdownCalled() {
211 def manager
= new DefaultGrailsPluginManager([MyGrailsPlugin
,AnotherGrailsPlugin
] as Class
[], ga
)
213 manager
.loadPlugins()
215 assertEquals
"nullme",MyGrailsPlugin
.SHUTDOWN_FIELD
217 assertNull MyGrailsPlugin
.SHUTDOWN_FIELD
221 class MyGrailsPlugin
{
222 static SHUTDOWN_FIELD
= "nullme"
223 def dependsOn
= [another
:1.2]
226 classEditor(org
.springframework
.beans
.propertyeditors
.ClassEditor
,application
.classLoader
)
229 SHUTDOWN_FIELD
= null
232 class AnotherGrailsPlugin
{
234 def watchedResources
= ['classpath:org/codehaus/groovy/grails/plugins/*.xml']
235 def doWithApplicationContext
= { ctx
->
236 RootBeanDefinition bd
= new RootBeanDefinition(CookieLocaleResolver
.class);
237 ctx
.registerBeanDefinition("localeResolver", bd
);
240 class SomeOtherGrailsPlugin
{
242 def dependsOn
= [my
:1.1, another
:1.2]
244 class ShouldLoadLastGrailsPlugin
{
245 def loadAfter
= ["my", "someOther"]
248 class ShouldEvictSomeOtherGrailsPlugin
{
249 def evict
= ['someOther']
252 class DisabledGrailsPlugin
{
254 def status
= "disabled"
256 class ObservingGrailsPlugin
{
257 def version
= "1.0-RC1"
258 def observe
= ['another']
260 def onChange
= { event
->
261 assert event
.source
!= null
265 class ObservingAllGrailsPlugin
{