|
| 1 | +package org.grails.plugins.services |
| 2 | + |
| 3 | +import org.grails.commons.test.AbstractGrailsMockTests |
| 4 | +import org.grails.plugins.DefaultGrailsPlugin |
| 5 | +import org.grails.web.servlet.context.support.WebRuntimeSpringConfiguration |
| 6 | +import org.springframework.context.ApplicationContext |
| 7 | + |
| 8 | +class DataSourcesGrailsPluginTests extends AbstractGrailsMockTests { |
| 9 | + |
| 10 | + void testSingleDataSource() { |
| 11 | + def appCtx = initializeContext([ |
| 12 | + dataSource: [ |
| 13 | + pooled :true, |
| 14 | + driverClassName:"org.h2.Driver", |
| 15 | + username :"sa", |
| 16 | + password :"", |
| 17 | + dbCreate :"create-drop" |
| 18 | + ] |
| 19 | + ]) |
| 20 | + assertTrue appCtx.containsBean("dataSource") |
| 21 | + } |
| 22 | + void testMultipleDataSources() { |
| 23 | + def appCtx = initializeContext([ |
| 24 | + dataSource: [ |
| 25 | + pooled :true, |
| 26 | + driverClassName:"org.h2.Driver", |
| 27 | + username :"sa", |
| 28 | + password :"", |
| 29 | + dbCreate :"create-drop" |
| 30 | + ], |
| 31 | + dataSources: [ |
| 32 | + second:[ |
| 33 | + pooled :true, |
| 34 | + driverClassName:"org.h2.Driver", |
| 35 | + username :"sa", |
| 36 | + password :"", |
| 37 | + dbCreate :"create-drop" |
| 38 | + ] |
| 39 | + ] |
| 40 | + ]) |
| 41 | + assertTrue appCtx.containsBean("dataSource") |
| 42 | + assertTrue appCtx.containsBean("dataSource_second") |
| 43 | + } |
| 44 | + |
| 45 | + |
| 46 | + private ApplicationContext initializeContext(Map dataSources) { |
| 47 | + ga.getConfig().putAll(dataSources) |
| 48 | + def corePluginClass = gcl.loadClass("org.grails.plugins.CoreGrailsPlugin") |
| 49 | + def corePlugin = new DefaultGrailsPlugin(corePluginClass, ga) |
| 50 | + def dataSourcePluginClass = gcl.loadClass("org.grails.plugins.datasource.DataSourceGrailsPlugin") |
| 51 | + |
| 52 | + def dataSourcePlugin = new DefaultGrailsPlugin(dataSourcePluginClass, ga) |
| 53 | + def springConfig = new WebRuntimeSpringConfiguration(ctx) |
| 54 | + springConfig.servletContext = createMockServletContext() |
| 55 | + |
| 56 | + corePlugin.doWithRuntimeConfiguration(springConfig) |
| 57 | + dataSourcePlugin.doWithRuntimeConfiguration(springConfig) |
| 58 | + |
| 59 | + springConfig.getApplicationContext() |
| 60 | + } |
| 61 | +} |
0 commit comments