diff --git a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/config/EmbeddedDatabaseBeanDefinitionParser.java b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/config/EmbeddedDatabaseBeanDefinitionParser.java index 9e6e9d3f5e8..2c896f04786 100644 --- a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/config/EmbeddedDatabaseBeanDefinitionParser.java +++ b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/config/EmbeddedDatabaseBeanDefinitionParser.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2011 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,6 +19,8 @@ package org.springframework.jdbc.config; import java.util.ArrayList; import java.util.List; +import org.w3c.dom.Element; + import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.support.AbstractBeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionBuilder; @@ -28,34 +30,31 @@ import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseFactoryBean; import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator; import org.springframework.util.StringUtils; import org.springframework.util.xml.DomUtils; -import org.w3c.dom.Element; /** - * {@link org.springframework.beans.factory.xml.BeanDefinitionParser} that parses {@code embedded-database} element and - * creates a {@link BeanDefinition} for {@link EmbeddedDatabaseFactoryBean}. Picks up nested {@code script} elements and - * configures a {@link ResourceDatabasePopulator} for them. - * + * {@link org.springframework.beans.factory.xml.BeanDefinitionParser} that parses an {@code embedded-database} + * element and creates a {@link BeanDefinition} for {@link EmbeddedDatabaseFactoryBean}. Picks up nested + * {@code script} elements and configures a {@link ResourceDatabasePopulator} for them. + * * @author Oliver Gierke * @since 3.0 */ class EmbeddedDatabaseBeanDefinitionParser extends AbstractBeanDefinitionParser { - private static final String NAME_PROPERTY = "databaseName"; - @Override - protected AbstractBeanDefinition parseInternal(Element element, ParserContext context) { + protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) { BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(EmbeddedDatabaseFactoryBean.class); setDatabaseType(element, builder); - setDatabasePopulator(element, context, builder); + setDatabasePopulator(element, builder); useIdAsDatabaseNameIfGiven(element, builder); - return getSourcedBeanDefinition(builder, element, context); + builder.getRawBeanDefinition().setSource(parserContext.extractSource(element)); + return builder.getBeanDefinition(); } private void useIdAsDatabaseNameIfGiven(Element element, BeanDefinitionBuilder builder) { - String id = element.getAttribute(ID_ATTRIBUTE); if (StringUtils.hasText(id)) { - builder.addPropertyValue(NAME_PROPERTY, id); + builder.addPropertyValue("databaseName", id); } } @@ -66,34 +65,24 @@ class EmbeddedDatabaseBeanDefinitionParser extends AbstractBeanDefinitionParser } } - private void setDatabasePopulator(Element element, ParserContext context, BeanDefinitionBuilder builder) { + private void setDatabasePopulator(Element element, BeanDefinitionBuilder builder) { List scripts = DomUtils.getChildElementsByTagName(element, "script"); if (scripts.size() > 0) { - builder.addPropertyValue("databasePopulator", createDatabasePopulator(scripts, context)); + builder.addPropertyValue("databasePopulator", createDatabasePopulator(scripts)); } } - private BeanDefinition createDatabasePopulator(List scripts, ParserContext context) { + private BeanDefinition createDatabasePopulator(List scripts) { BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(ResourceDatabasePopulator.class); List locations = new ArrayList(); for (Element scriptElement : scripts) { locations.add(scriptElement.getAttribute("location")); } // Use a factory bean for the resources so they can be given an order if a pattern is used - BeanDefinitionBuilder resourcesFactory = BeanDefinitionBuilder - .genericBeanDefinition(SortedResourcesFactoryBean.class); + BeanDefinitionBuilder resourcesFactory = BeanDefinitionBuilder.genericBeanDefinition(SortedResourcesFactoryBean.class); resourcesFactory.addConstructorArgValue(locations); builder.addPropertyValue("scripts", resourcesFactory.getBeanDefinition()); - return builder.getBeanDefinition(); } - private AbstractBeanDefinition getSourcedBeanDefinition( - BeanDefinitionBuilder builder, Element source, ParserContext context) { - - AbstractBeanDefinition definition = builder.getBeanDefinition(); - definition.setSource(context.extractSource(source)); - return definition; - } - } diff --git a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/config/InitializeDatabaseBeanDefinitionParser.java b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/config/InitializeDatabaseBeanDefinitionParser.java index 766b2d08636..f724399c3b6 100644 --- a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/config/InitializeDatabaseBeanDefinitionParser.java +++ b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/config/InitializeDatabaseBeanDefinitionParser.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2011 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,6 +19,8 @@ package org.springframework.jdbc.config; import java.util.ArrayList; import java.util.List; +import org.w3c.dom.Element; + import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.support.AbstractBeanDefinition; import org.springframework.beans.factory.support.BeanDefinitionBuilder; @@ -27,24 +29,25 @@ import org.springframework.beans.factory.xml.ParserContext; import org.springframework.jdbc.datasource.init.DataSourceInitializer; import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator; import org.springframework.util.xml.DomUtils; -import org.w3c.dom.Element; /** - * {@link org.springframework.beans.factory.xml.BeanDefinitionParser} that parses an {@code initialize-database} element and - * creates a {@link BeanDefinition} of type {@link DataSourceInitializer}. Picks up nested {@code script} elements and - * configures a {@link ResourceDatabasePopulator} for them. + * {@link org.springframework.beans.factory.xml.BeanDefinitionParser} that parses an {@code initialize-database} + * element and creates a {@link BeanDefinition} of type {@link DataSourceInitializer}. Picks up nested + * {@code script} elements and configures a {@link ResourceDatabasePopulator} for them. + * * @author Dave Syer * @since 3.0 */ public class InitializeDatabaseBeanDefinitionParser extends AbstractBeanDefinitionParser { @Override - protected AbstractBeanDefinition parseInternal(Element element, ParserContext context) { + protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) { BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(DataSourceInitializer.class); builder.addPropertyReference("dataSource", element.getAttribute("data-source")); builder.addPropertyValue("enabled", element.getAttribute("enabled")); - setDatabasePopulator(element, context, builder); - return getSourcedBeanDefinition(builder, element, context); + setDatabasePopulator(element, builder); + builder.getRawBeanDefinition().setSource(parserContext.extractSource(element)); + return builder.getBeanDefinition(); } @Override @@ -52,14 +55,14 @@ public class InitializeDatabaseBeanDefinitionParser extends AbstractBeanDefiniti return true; } - private void setDatabasePopulator(Element element, ParserContext context, BeanDefinitionBuilder builder) { + private void setDatabasePopulator(Element element, BeanDefinitionBuilder builder) { List scripts = DomUtils.getChildElementsByTagName(element, "script"); if (scripts.size() > 0) { - builder.addPropertyValue("databasePopulator", createDatabasePopulator(element, scripts, context)); + builder.addPropertyValue("databasePopulator", createDatabasePopulator(element, scripts)); } } - private BeanDefinition createDatabasePopulator(Element element, List scripts, ParserContext context) { + private BeanDefinition createDatabasePopulator(Element element, List scripts) { BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(ResourceDatabasePopulator.class); builder.addPropertyValue("ignoreFailedDrops", element.getAttribute("ignore-failures").equals("DROPS")); builder.addPropertyValue("continueOnError", element.getAttribute("ignore-failures").equals("ALL")); @@ -71,19 +74,11 @@ public class InitializeDatabaseBeanDefinitionParser extends AbstractBeanDefiniti } // Use a factory bean for the resources so they can be given an order if a pattern is used - BeanDefinitionBuilder resourcesFactory = BeanDefinitionBuilder - .genericBeanDefinition(SortedResourcesFactoryBean.class); + BeanDefinitionBuilder resourcesFactory = BeanDefinitionBuilder.genericBeanDefinition(SortedResourcesFactoryBean.class); resourcesFactory.addConstructorArgValue(locations); builder.addPropertyValue("scripts", resourcesFactory.getBeanDefinition()); return builder.getBeanDefinition(); } - private AbstractBeanDefinition getSourcedBeanDefinition(BeanDefinitionBuilder builder, Element source, - ParserContext context) { - AbstractBeanDefinition definition = builder.getBeanDefinition(); - definition.setSource(context.extractSource(source)); - return definition; - } - } diff --git a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseFactory.java b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseFactory.java index 2898becf10f..3d85af76de7 100644 --- a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseFactory.java +++ b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2011 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -68,6 +68,16 @@ public class EmbeddedDatabaseFactory { this.databaseName = databaseName; } + /** + * Set the factory to use to create the DataSource instance that connects to the embedded database. + * Defaults to {@link SimpleDriverDataSourceFactory}. + * @param dataSourceFactory the data source factory + */ + public void setDataSourceFactory(DataSourceFactory dataSourceFactory) { + Assert.notNull(dataSourceFactory, "DataSourceFactory is required"); + this.dataSourceFactory = dataSourceFactory; + } + /** * Set the type of embedded database to use. Call this when you wish to configure * one of the pre-supported types. Defaults to HSQL. @@ -83,7 +93,6 @@ public class EmbeddedDatabaseFactory { * @param configurer the embedded database configurer */ public void setDatabaseConfigurer(EmbeddedDatabaseConfigurer configurer) { - Assert.notNull(configurer, "EmbeddedDatabaseConfigurer is required"); this.databaseConfigurer = configurer; } @@ -92,20 +101,9 @@ public class EmbeddedDatabaseFactory { * @param populator the database populator */ public void setDatabasePopulator(DatabasePopulator populator) { - Assert.notNull(populator, "DatabasePopulator is required"); this.databasePopulator = populator; } - /** - * Set the factory to use to create the DataSource instance that connects to the embedded database. - * Defaults to {@link SimpleDriverDataSourceFactory}. - * @param dataSourceFactory the data source factory - */ - public void setDataSourceFactory(DataSourceFactory dataSourceFactory) { - Assert.notNull(dataSourceFactory, "DataSourceFactory is required"); - this.dataSourceFactory = dataSourceFactory; - } - /** * Factory method that returns the embedded database instance. */ diff --git a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/package-info.java b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/package-info.java index 734176c994b..4349f9d217f 100644 --- a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/package-info.java +++ b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/package-info.java @@ -2,7 +2,6 @@ /** * * Provides extensible support for creating embedded database instances. - * HSQL in-memory support provided natively * */ package org.springframework.jdbc.datasource.embedded; diff --git a/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/init/package-info.java b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/init/package-info.java new file mode 100644 index 00000000000..80d4d0724dd --- /dev/null +++ b/org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/init/package-info.java @@ -0,0 +1,8 @@ + +/** + * + * Provides extensible support for initializing databases through scripts. + * + */ +package org.springframework.jdbc.datasource.init; + diff --git a/org.springframework.jdbc/src/main/resources/org/springframework/jdbc/config/spring-jdbc-3.0.xsd b/org.springframework.jdbc/src/main/resources/org/springframework/jdbc/config/spring-jdbc-3.0.xsd index c7e57ae8e2e..9c25333a08e 100644 --- a/org.springframework.jdbc/src/main/resources/org/springframework/jdbc/config/spring-jdbc-3.0.xsd +++ b/org.springframework.jdbc/src/main/resources/org/springframework/jdbc/config/spring-jdbc-3.0.xsd @@ -18,7 +18,7 @@ ]]> - + @@ -26,8 +26,7 @@ - + - + - elements. ]]> - + + ]]> - + + A reference to a data source that should be initialized. Defaults to "dataSource". + ]]> - - + + - + Is this bean "enabled", meaning the scripts will be executed? @@ -87,8 +81,7 @@ - + Should failed SQL statements be ignored during initialization? @@ -159,4 +152,4 @@ - \ No newline at end of file + diff --git a/org.springframework.jdbc/src/test/java/org/springframework/jdbc/config/InitializeDatabaseIntegrationTest.java b/org.springframework.jdbc/src/test/java/org/springframework/jdbc/config/InitializeDatabaseIntegrationTests.java similarity index 82% rename from org.springframework.jdbc/src/test/java/org/springframework/jdbc/config/InitializeDatabaseIntegrationTest.java rename to org.springframework.jdbc/src/test/java/org/springframework/jdbc/config/InitializeDatabaseIntegrationTests.java index f932ca42d83..eb46775db94 100644 --- a/org.springframework.jdbc/src/test/java/org/springframework/jdbc/config/InitializeDatabaseIntegrationTest.java +++ b/org.springframework.jdbc/src/test/java/org/springframework/jdbc/config/InitializeDatabaseIntegrationTests.java @@ -1,21 +1,40 @@ -package org.springframework.jdbc.config; +/* + * Copyright 2002-2011 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ -import static org.junit.Assert.assertEquals; +package org.springframework.jdbc.config; import java.util.List; import java.util.Map; - import javax.sql.DataSource; import org.junit.After; import org.junit.Before; import org.junit.Test; + import org.springframework.beans.factory.InitializingBean; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.jdbc.BadSqlGrammarException; import org.springframework.jdbc.core.JdbcTemplate; -public class InitializeDatabaseIntegrationTest { +import static org.junit.Assert.*; + +/** + * @author Dave Syer + */ +public class InitializeDatabaseIntegrationTests { private String enabled; private ClassPathXmlApplicationContext context; diff --git a/org.springframework.jdbc/src/test/java/org/springframework/jdbc/config/JdbcNamespaceIntegrationTest.java b/org.springframework.jdbc/src/test/java/org/springframework/jdbc/config/JdbcNamespaceIntegrationTests.java similarity index 79% rename from org.springframework.jdbc/src/test/java/org/springframework/jdbc/config/JdbcNamespaceIntegrationTest.java rename to org.springframework.jdbc/src/test/java/org/springframework/jdbc/config/JdbcNamespaceIntegrationTests.java index 7ef895d8f72..38d7f073fde 100644 --- a/org.springframework.jdbc/src/test/java/org/springframework/jdbc/config/JdbcNamespaceIntegrationTest.java +++ b/org.springframework.jdbc/src/test/java/org/springframework/jdbc/config/JdbcNamespaceIntegrationTests.java @@ -1,3 +1,19 @@ +/* + * Copyright 2002-2011 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + package org.springframework.jdbc.config; import static org.hamcrest.CoreMatchers.*; @@ -15,7 +31,10 @@ import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.core.io.ClassPathResource; import org.springframework.jdbc.core.JdbcTemplate; -public class JdbcNamespaceIntegrationTest { +/** + * @author Dave Syer + */ +public class JdbcNamespaceIntegrationTests { @Test public void testCreateEmbeddedDatabase() throws Exception { diff --git a/org.springframework.jdbc/src/test/resources/org/springframework/jdbc/config/jdbc-initialize-cache-config.xml b/org.springframework.jdbc/src/test/resources/org/springframework/jdbc/config/jdbc-initialize-cache-config.xml index 01897f217b5..24135d9fd5e 100644 --- a/org.springframework.jdbc/src/test/resources/org/springframework/jdbc/config/jdbc-initialize-cache-config.xml +++ b/org.springframework.jdbc/src/test/resources/org/springframework/jdbc/config/jdbc-initialize-cache-config.xml @@ -14,7 +14,7 @@ - +