Browse Source
git-svn-id: https://src.springframework.org/svn/spring-framework/trunk@4814 50f2f4bb-b051-0410-bef5-90022cba6387pull/2/head
19 changed files with 409 additions and 300 deletions
@ -0,0 +1,84 @@
@@ -0,0 +1,84 @@
|
||||
/* |
||||
* 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 java.util.Arrays; |
||||
import java.util.List; |
||||
|
||||
import org.w3c.dom.Element; |
||||
|
||||
import org.springframework.beans.BeanMetadataElement; |
||||
import org.springframework.beans.factory.config.BeanDefinition; |
||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder; |
||||
import org.springframework.beans.factory.support.ManagedList; |
||||
import org.springframework.jdbc.datasource.init.CompositeDatabasePopulator; |
||||
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator; |
||||
import org.springframework.util.StringUtils; |
||||
import org.springframework.util.xml.DomUtils; |
||||
|
||||
/** |
||||
* @author Juergen Hoeller |
||||
* @since 3.1 |
||||
*/ |
||||
class DatabasePopulatorConfigUtils { |
||||
|
||||
public static void setDatabasePopulator(Element element, BeanDefinitionBuilder builder) { |
||||
List<Element> scripts = DomUtils.getChildElementsByTagName(element, "script"); |
||||
if (scripts.size() > 0) { |
||||
builder.addPropertyValue("databasePopulator", createDatabasePopulator(element, scripts, "INIT")); |
||||
builder.addPropertyValue("databaseCleaner", createDatabasePopulator(element, scripts, "DESTROY")); |
||||
} |
||||
} |
||||
|
||||
static private BeanDefinition createDatabasePopulator(Element element, List<Element> scripts, String execution) { |
||||
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(CompositeDatabasePopulator.class); |
||||
|
||||
boolean ignoreFailedDrops = element.getAttribute("ignore-failures").equals("DROPS"); |
||||
boolean continueOnError = element.getAttribute("ignore-failures").equals("ALL"); |
||||
|
||||
ManagedList<BeanMetadataElement> delegates = new ManagedList<BeanMetadataElement>(); |
||||
for (Element scriptElement : scripts) { |
||||
String executionAttr = scriptElement.getAttribute("execution"); |
||||
if (!StringUtils.hasText(executionAttr)) { |
||||
executionAttr = "INIT"; |
||||
} |
||||
if (!execution.equals(executionAttr)) { |
||||
continue; |
||||
} |
||||
BeanDefinitionBuilder delegate = BeanDefinitionBuilder.genericBeanDefinition(ResourceDatabasePopulator.class); |
||||
delegate.addPropertyValue("ignoreFailedDrops", ignoreFailedDrops); |
||||
delegate.addPropertyValue("continueOnError", continueOnError); |
||||
|
||||
List<String> locations = Arrays.asList(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); |
||||
resourcesFactory.addConstructorArgValue(locations); |
||||
delegate.addPropertyValue("scripts", resourcesFactory.getBeanDefinition()); |
||||
if (StringUtils.hasLength(scriptElement.getAttribute("encoding"))) { |
||||
delegate.addPropertyValue("sqlScriptEncoding", scriptElement.getAttribute("encoding")); |
||||
} |
||||
if (StringUtils.hasLength(scriptElement.getAttribute("separator"))) { |
||||
delegate.addPropertyValue("separator", scriptElement.getAttribute("separator")); |
||||
} |
||||
delegates.add(delegate.getBeanDefinition()); |
||||
} |
||||
builder.addPropertyValue("populators", delegates); |
||||
|
||||
return builder.getBeanDefinition(); |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,61 @@
@@ -0,0 +1,61 @@
|
||||
/* |
||||
* 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.datasource.init; |
||||
|
||||
import java.sql.Connection; |
||||
import java.sql.SQLException; |
||||
import javax.sql.DataSource; |
||||
|
||||
import org.springframework.dao.DataAccessResourceFailureException; |
||||
import org.springframework.util.Assert; |
||||
|
||||
/** |
||||
* Utility methods for executing a DatabasePopulator. |
||||
* |
||||
* @author Juergen Hoeller |
||||
* @since 3.1 |
||||
*/ |
||||
public abstract class DatabasePopulatorUtils { |
||||
|
||||
/** |
||||
* Execute the given DatabasePopulator against the given DataSource. |
||||
* @param populator the DatabasePopulator to execute |
||||
* @param dataSource the DataSource to execute against |
||||
*/ |
||||
public static void execute(DatabasePopulator populator, DataSource dataSource) { |
||||
Assert.notNull(populator, "DatabasePopulator must be provided"); |
||||
Assert.notNull(dataSource, "DataSource must be provided"); |
||||
try { |
||||
Connection connection = dataSource.getConnection(); |
||||
try { |
||||
populator.populate(connection); |
||||
} |
||||
finally { |
||||
try { |
||||
connection.close(); |
||||
} |
||||
catch (SQLException ex) { |
||||
// ignore
|
||||
} |
||||
} |
||||
} |
||||
catch (Exception ex) { |
||||
throw new DataAccessResourceFailureException("Failed to execute database script", ex); |
||||
} |
||||
} |
||||
|
||||
} |
||||
@ -0,0 +1,8 @@
@@ -0,0 +1,8 @@
|
||||
|
||||
/** |
||||
* |
||||
* Provides extensible support for initializing databases through scripts. |
||||
* |
||||
*/ |
||||
package org.springframework.jdbc.datasource.init; |
||||
|
||||
@ -1,21 +1,40 @@
@@ -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; |
||||
@ -0,0 +1,14 @@
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<beans xmlns="http://www.springframework.org/schema/beans" |
||||
xmlns:jdbc="http://www.springframework.org/schema/jdbc" |
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd |
||||
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd"> |
||||
|
||||
<jdbc:embedded-database id="dataSource" type="HSQL"> |
||||
<jdbc:script location="classpath:org/springframework/jdbc/config/db-schema.sql" execution="INIT"/> |
||||
<jdbc:script location="classpath:org/springframework/jdbc/config/db-test-data.sql" execution="INIT"/> |
||||
<jdbc:script location="classpath:org/springframework/jdbc/config/db-drops.sql" execution="DESTROY"/> |
||||
</jdbc:embedded-database> |
||||
|
||||
</beans> |
||||
@ -0,0 +1,13 @@
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?> |
||||
<beans xmlns="http://www.springframework.org/schema/beans" |
||||
xmlns:jdbc="http://www.springframework.org/schema/jdbc" |
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd |
||||
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd"> |
||||
|
||||
<jdbc:embedded-database id="dataSource" type="HSQL"> |
||||
<jdbc:script location="classpath:org/springframework/jdbc/config/db-schema.sql" encoding="ISO-8859-1"/> |
||||
<jdbc:script location="classpath:org/springframework/jdbc/config/db-test-data-endings.sql" separator="@@"/> |
||||
</jdbc:embedded-database> |
||||
|
||||
</beans> |
||||
Loading…
Reference in new issue