Skip to content

Commit ac1ffff

Browse files
committed
general polishing (backported from 3.1)
1 parent c0257ab commit ac1ffff

File tree

9 files changed

+108
-88
lines changed

9 files changed

+108
-88
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2010 the original author or authors.
2+
* Copyright 2002-2011 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,8 @@
1919
import java.util.ArrayList;
2020
import java.util.List;
2121

22+
import org.w3c.dom.Element;
23+
2224
import org.springframework.beans.factory.config.BeanDefinition;
2325
import org.springframework.beans.factory.support.AbstractBeanDefinition;
2426
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
@@ -28,34 +30,31 @@
2830
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
2931
import org.springframework.util.StringUtils;
3032
import org.springframework.util.xml.DomUtils;
31-
import org.w3c.dom.Element;
3233

3334
/**
34-
* {@link org.springframework.beans.factory.xml.BeanDefinitionParser} that parses {@code embedded-database} element and
35-
* creates a {@link BeanDefinition} for {@link EmbeddedDatabaseFactoryBean}. Picks up nested {@code script} elements and
36-
* configures a {@link ResourceDatabasePopulator} for them.
37-
*
35+
* {@link org.springframework.beans.factory.xml.BeanDefinitionParser} that parses an {@code embedded-database}
36+
* element and creates a {@link BeanDefinition} for {@link EmbeddedDatabaseFactoryBean}. Picks up nested
37+
* {@code script} elements and configures a {@link ResourceDatabasePopulator} for them.
38+
*
3839
* @author Oliver Gierke
3940
* @since 3.0
4041
*/
4142
class EmbeddedDatabaseBeanDefinitionParser extends AbstractBeanDefinitionParser {
4243

43-
private static final String NAME_PROPERTY = "databaseName";
44-
4544
@Override
46-
protected AbstractBeanDefinition parseInternal(Element element, ParserContext context) {
45+
protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
4746
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(EmbeddedDatabaseFactoryBean.class);
4847
setDatabaseType(element, builder);
49-
setDatabasePopulator(element, context, builder);
48+
setDatabasePopulator(element, builder);
5049
useIdAsDatabaseNameIfGiven(element, builder);
51-
return getSourcedBeanDefinition(builder, element, context);
50+
builder.getRawBeanDefinition().setSource(parserContext.extractSource(element));
51+
return builder.getBeanDefinition();
5252
}
5353

5454
private void useIdAsDatabaseNameIfGiven(Element element, BeanDefinitionBuilder builder) {
55-
5655
String id = element.getAttribute(ID_ATTRIBUTE);
5756
if (StringUtils.hasText(id)) {
58-
builder.addPropertyValue(NAME_PROPERTY, id);
57+
builder.addPropertyValue("databaseName", id);
5958
}
6059
}
6160

@@ -66,34 +65,24 @@ private void setDatabaseType(Element element, BeanDefinitionBuilder builder) {
6665
}
6766
}
6867

69-
private void setDatabasePopulator(Element element, ParserContext context, BeanDefinitionBuilder builder) {
68+
private void setDatabasePopulator(Element element, BeanDefinitionBuilder builder) {
7069
List<Element> scripts = DomUtils.getChildElementsByTagName(element, "script");
7170
if (scripts.size() > 0) {
72-
builder.addPropertyValue("databasePopulator", createDatabasePopulator(scripts, context));
71+
builder.addPropertyValue("databasePopulator", createDatabasePopulator(scripts));
7372
}
7473
}
7574

76-
private BeanDefinition createDatabasePopulator(List<Element> scripts, ParserContext context) {
75+
private BeanDefinition createDatabasePopulator(List<Element> scripts) {
7776
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(ResourceDatabasePopulator.class);
7877
List<String> locations = new ArrayList<String>();
7978
for (Element scriptElement : scripts) {
8079
locations.add(scriptElement.getAttribute("location"));
8180
}
8281
// Use a factory bean for the resources so they can be given an order if a pattern is used
83-
BeanDefinitionBuilder resourcesFactory = BeanDefinitionBuilder
84-
.genericBeanDefinition(SortedResourcesFactoryBean.class);
82+
BeanDefinitionBuilder resourcesFactory = BeanDefinitionBuilder.genericBeanDefinition(SortedResourcesFactoryBean.class);
8583
resourcesFactory.addConstructorArgValue(locations);
8684
builder.addPropertyValue("scripts", resourcesFactory.getBeanDefinition());
87-
8885
return builder.getBeanDefinition();
8986
}
9087

91-
private AbstractBeanDefinition getSourcedBeanDefinition(
92-
BeanDefinitionBuilder builder, Element source, ParserContext context) {
93-
94-
AbstractBeanDefinition definition = builder.getBeanDefinition();
95-
definition.setSource(context.extractSource(source));
96-
return definition;
97-
}
98-
9988
}
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2010 the original author or authors.
2+
* Copyright 2002-2011 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,8 @@
1919
import java.util.ArrayList;
2020
import java.util.List;
2121

22+
import org.w3c.dom.Element;
23+
2224
import org.springframework.beans.factory.config.BeanDefinition;
2325
import org.springframework.beans.factory.support.AbstractBeanDefinition;
2426
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
@@ -27,39 +29,40 @@
2729
import org.springframework.jdbc.datasource.init.DataSourceInitializer;
2830
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
2931
import org.springframework.util.xml.DomUtils;
30-
import org.w3c.dom.Element;
3132

3233
/**
33-
* {@link org.springframework.beans.factory.xml.BeanDefinitionParser} that parses an {@code initialize-database} element and
34-
* creates a {@link BeanDefinition} of type {@link DataSourceInitializer}. Picks up nested {@code script} elements and
35-
* configures a {@link ResourceDatabasePopulator} for them.
34+
* {@link org.springframework.beans.factory.xml.BeanDefinitionParser} that parses an {@code initialize-database}
35+
* element and creates a {@link BeanDefinition} of type {@link DataSourceInitializer}. Picks up nested
36+
* {@code script} elements and configures a {@link ResourceDatabasePopulator} for them.
37+
*
3638
* @author Dave Syer
3739
* @since 3.0
3840
*/
3941
public class InitializeDatabaseBeanDefinitionParser extends AbstractBeanDefinitionParser {
4042

4143
@Override
42-
protected AbstractBeanDefinition parseInternal(Element element, ParserContext context) {
44+
protected AbstractBeanDefinition parseInternal(Element element, ParserContext parserContext) {
4345
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(DataSourceInitializer.class);
4446
builder.addPropertyReference("dataSource", element.getAttribute("data-source"));
4547
builder.addPropertyValue("enabled", element.getAttribute("enabled"));
46-
setDatabasePopulator(element, context, builder);
47-
return getSourcedBeanDefinition(builder, element, context);
48+
setDatabasePopulator(element, builder);
49+
builder.getRawBeanDefinition().setSource(parserContext.extractSource(element));
50+
return builder.getBeanDefinition();
4851
}
4952

5053
@Override
5154
protected boolean shouldGenerateId() {
5255
return true;
5356
}
5457

55-
private void setDatabasePopulator(Element element, ParserContext context, BeanDefinitionBuilder builder) {
58+
private void setDatabasePopulator(Element element, BeanDefinitionBuilder builder) {
5659
List<Element> scripts = DomUtils.getChildElementsByTagName(element, "script");
5760
if (scripts.size() > 0) {
58-
builder.addPropertyValue("databasePopulator", createDatabasePopulator(element, scripts, context));
61+
builder.addPropertyValue("databasePopulator", createDatabasePopulator(element, scripts));
5962
}
6063
}
6164

62-
private BeanDefinition createDatabasePopulator(Element element, List<Element> scripts, ParserContext context) {
65+
private BeanDefinition createDatabasePopulator(Element element, List<Element> scripts) {
6366
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(ResourceDatabasePopulator.class);
6467
builder.addPropertyValue("ignoreFailedDrops", element.getAttribute("ignore-failures").equals("DROPS"));
6568
builder.addPropertyValue("continueOnError", element.getAttribute("ignore-failures").equals("ALL"));
@@ -71,19 +74,11 @@ private BeanDefinition createDatabasePopulator(Element element, List<Element> sc
7174
}
7275

7376
// Use a factory bean for the resources so they can be given an order if a pattern is used
74-
BeanDefinitionBuilder resourcesFactory = BeanDefinitionBuilder
75-
.genericBeanDefinition(SortedResourcesFactoryBean.class);
77+
BeanDefinitionBuilder resourcesFactory = BeanDefinitionBuilder.genericBeanDefinition(SortedResourcesFactoryBean.class);
7678
resourcesFactory.addConstructorArgValue(locations);
7779
builder.addPropertyValue("scripts", resourcesFactory.getBeanDefinition());
7880

7981
return builder.getBeanDefinition();
8082
}
8183

82-
private AbstractBeanDefinition getSourcedBeanDefinition(BeanDefinitionBuilder builder, Element source,
83-
ParserContext context) {
84-
AbstractBeanDefinition definition = builder.getBeanDefinition();
85-
definition.setSource(context.extractSource(source));
86-
return definition;
87-
}
88-
8984
}

org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/EmbeddedDatabaseFactory.java

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2010 the original author or authors.
2+
* Copyright 2002-2011 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -68,6 +68,16 @@ public void setDatabaseName(String databaseName) {
6868
this.databaseName = databaseName;
6969
}
7070

71+
/**
72+
* Set the factory to use to create the DataSource instance that connects to the embedded database.
73+
* Defaults to {@link SimpleDriverDataSourceFactory}.
74+
* @param dataSourceFactory the data source factory
75+
*/
76+
public void setDataSourceFactory(DataSourceFactory dataSourceFactory) {
77+
Assert.notNull(dataSourceFactory, "DataSourceFactory is required");
78+
this.dataSourceFactory = dataSourceFactory;
79+
}
80+
7181
/**
7282
* Set the type of embedded database to use. Call this when you wish to configure
7383
* one of the pre-supported types. Defaults to HSQL.
@@ -83,7 +93,6 @@ public void setDatabaseType(EmbeddedDatabaseType type) {
8393
* @param configurer the embedded database configurer
8494
*/
8595
public void setDatabaseConfigurer(EmbeddedDatabaseConfigurer configurer) {
86-
Assert.notNull(configurer, "EmbeddedDatabaseConfigurer is required");
8796
this.databaseConfigurer = configurer;
8897
}
8998

@@ -92,20 +101,9 @@ public void setDatabaseConfigurer(EmbeddedDatabaseConfigurer configurer) {
92101
* @param populator the database populator
93102
*/
94103
public void setDatabasePopulator(DatabasePopulator populator) {
95-
Assert.notNull(populator, "DatabasePopulator is required");
96104
this.databasePopulator = populator;
97105
}
98106

99-
/**
100-
* Set the factory to use to create the DataSource instance that connects to the embedded database.
101-
* Defaults to {@link SimpleDriverDataSourceFactory}.
102-
* @param dataSourceFactory the data source factory
103-
*/
104-
public void setDataSourceFactory(DataSourceFactory dataSourceFactory) {
105-
Assert.notNull(dataSourceFactory, "DataSourceFactory is required");
106-
this.dataSourceFactory = dataSourceFactory;
107-
}
108-
109107
/**
110108
* Factory method that returns the embedded database instance.
111109
*/

org.springframework.jdbc/src/main/java/org/springframework/jdbc/datasource/embedded/package-info.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
/**
33
*
44
* Provides extensible support for creating embedded database instances.
5-
* HSQL in-memory support provided natively
65
*
76
*/
87
package org.springframework.jdbc.datasource.embedded;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
/**
3+
*
4+
* Provides extensible support for initializing databases through scripts.
5+
*
6+
*/
7+
package org.springframework.jdbc.datasource.init;
8+

org.springframework.jdbc/src/main/resources/org/springframework/jdbc/config/spring-jdbc-3.0.xsd

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,23 @@
1818
]]></xsd:documentation>
1919
<xsd:appinfo>
2020
<tool:annotation>
21-
<tool:exports type="javax.sql.DataSource" />
21+
<tool:exports type="javax.sql.DataSource"/>
2222
</tool:annotation>
2323
</xsd:appinfo>
2424
</xsd:annotation>
2525
<xsd:complexType>
2626
<xsd:complexContent>
2727
<xsd:extension base="beans:identifiedType">
2828
<xsd:sequence>
29-
<xsd:element name="script" type="scriptType"
30-
minOccurs="0" maxOccurs="unbounded">
29+
<xsd:element name="script" type="scriptType" minOccurs="0" maxOccurs="unbounded">
3130
<xsd:annotation>
3231
<xsd:documentation><![CDATA[
3332
A SQL script to execute during embedded database initialization.
3433
]]></xsd:documentation>
3534
</xsd:annotation>
3635
</xsd:element>
3736
</xsd:sequence>
38-
<xsd:attribute name="type" type="databaseType"
39-
default="HSQL">
37+
<xsd:attribute name="type" type="databaseType" default="HSQL">
4038
<xsd:annotation>
4139
<xsd:documentation><![CDATA[
4240
The type of embedded database to create, such as HSQL, H2 or Derby. Defaults to HSQL.
@@ -50,45 +48,40 @@
5048

5149
<xsd:element name="initialize-database">
5250
<xsd:annotation>
53-
<xsd:documentation
54-
source="java:org.springframework.jdbc.embedded.DataSourceInitializer"><![CDATA[
51+
<xsd:documentation source="java:org.springframework.jdbc.embedded.DataSourceInitializer"><![CDATA[
5552
Initializes a database instance with SQL scripts provided in nested <script/> elements.
5653
]]></xsd:documentation>
5754
</xsd:annotation>
5855
<xsd:complexType>
5956
<xsd:sequence>
60-
<xsd:element name="script" type="scriptType" minOccurs="1"
61-
maxOccurs="unbounded">
57+
<xsd:element name="script" type="scriptType" minOccurs="1" maxOccurs="unbounded">
6258
<xsd:annotation>
6359
<xsd:documentation><![CDATA[
6460
A SQL script to execute during database initialization.
65-
]]></xsd:documentation>
61+
]]></xsd:documentation>
6662
</xsd:annotation>
6763
</xsd:element>
6864
</xsd:sequence>
69-
<xsd:attribute name="data-source" type="xsd:string"
70-
default="dataSource">
65+
<xsd:attribute name="data-source" type="xsd:string" default="dataSource">
7166
<xsd:annotation>
7267
<xsd:documentation><![CDATA[
73-
A reference to a data source that should be initialized. Defaults to "dataSource".
74-
]]></xsd:documentation>
68+
A reference to a data source that should be initialized. Defaults to "dataSource".
69+
]]></xsd:documentation>
7570
<xsd:appinfo>
76-
<tool:annotation kind="ref" />
77-
<tool:expected-type type="javax.sql.DataSource" />
71+
<tool:annotation kind="ref"/>
72+
<tool:expected-type type="javax.sql.DataSource"/>
7873
</xsd:appinfo>
7974
</xsd:annotation>
8075
</xsd:attribute>
81-
<xsd:attribute name="enabled" type="xsd:string" use="optional"
82-
default="true">
76+
<xsd:attribute name="enabled" type="xsd:string" use="optional" default="true">
8377
<xsd:annotation>
8478
<xsd:documentation>
8579
Is this bean "enabled", meaning the scripts will be executed?
8680
Defaults to true, but can be used to switch on and off the initialization depending on the environment.
8781
</xsd:documentation>
8882
</xsd:annotation>
8983
</xsd:attribute>
90-
<xsd:attribute name="ignore-failures" use="optional"
91-
default="NONE">
84+
<xsd:attribute name="ignore-failures" use="optional" default="NONE">
9285
<xsd:annotation>
9386
<xsd:documentation>
9487
Should failed SQL statements be ignored during initialization?
@@ -159,4 +152,4 @@
159152
</xsd:restriction>
160153
</xsd:simpleType>
161154

162-
</xsd:schema>
155+
</xsd:schema>
Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,40 @@
1-
package org.springframework.jdbc.config;
1+
/*
2+
* Copyright 2002-2011 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
216

3-
import static org.junit.Assert.assertEquals;
17+
package org.springframework.jdbc.config;
418

519
import java.util.List;
620
import java.util.Map;
7-
821
import javax.sql.DataSource;
922

1023
import org.junit.After;
1124
import org.junit.Before;
1225
import org.junit.Test;
26+
1327
import org.springframework.beans.factory.InitializingBean;
1428
import org.springframework.context.support.ClassPathXmlApplicationContext;
1529
import org.springframework.jdbc.BadSqlGrammarException;
1630
import org.springframework.jdbc.core.JdbcTemplate;
1731

18-
public class InitializeDatabaseIntegrationTest {
32+
import static org.junit.Assert.*;
33+
34+
/**
35+
* @author Dave Syer
36+
*/
37+
public class InitializeDatabaseIntegrationTests {
1938

2039
private String enabled;
2140
private ClassPathXmlApplicationContext context;

0 commit comments

Comments
 (0)