Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
爱吃血肠
spring-framework
提交
eb4d1bf2
S
spring-framework
项目概览
爱吃血肠
/
spring-framework
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
spring-framework
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
eb4d1bf2
编写于
4月 22, 2009
作者:
K
Keith Donald
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
deleted old test data source factory
上级
d896a53c
变更
9
隐藏空白更改
内联
并排
Showing
9 changed file
with
1 addition
and
435 deletion
+1
-435
org.springframework.test/ivy.xml
org.springframework.test/ivy.xml
+1
-1
org.springframework.test/src/main/java/org/springframework/test/jdbc/HsqlTestDataSourceConfigurer.java
...ringframework/test/jdbc/HsqlTestDataSourceConfigurer.java
+0
-47
org.springframework.test/src/main/java/org/springframework/test/jdbc/ResourceTestDatabasePopulator.java
...ingframework/test/jdbc/ResourceTestDatabasePopulator.java
+0
-81
org.springframework.test/src/main/java/org/springframework/test/jdbc/TestDataSourceConfigurer.java
...g/springframework/test/jdbc/TestDataSourceConfigurer.java
+0
-43
org.springframework.test/src/main/java/org/springframework/test/jdbc/TestDataSourceConfigurerFactory.java
...gframework/test/jdbc/TestDataSourceConfigurerFactory.java
+0
-32
org.springframework.test/src/main/java/org/springframework/test/jdbc/TestDataSourceFactory.java
.../org/springframework/test/jdbc/TestDataSourceFactory.java
+0
-143
org.springframework.test/src/main/java/org/springframework/test/jdbc/TestDatabasePopulator.java
.../org/springframework/test/jdbc/TestDatabasePopulator.java
+0
-34
org.springframework.test/src/main/java/org/springframework/test/jdbc/TestDatabaseType.java
.../java/org/springframework/test/jdbc/TestDatabaseType.java
+0
-24
org.springframework.test/src/test/java/org/springframework/test/jdbc/TestDataSourceFactoryTests.java
...springframework/test/jdbc/TestDataSourceFactoryTests.java
+0
-30
未找到文件。
org.springframework.test/ivy.xml
浏览文件 @
eb4d1bf2
...
@@ -41,7 +41,7 @@
...
@@ -41,7 +41,7 @@
<dependency
org=
"org.springframework"
name=
"org.springframework.web"
rev=
"latest.integration"
conf=
"optional->compile"
/>
<dependency
org=
"org.springframework"
name=
"org.springframework.web"
rev=
"latest.integration"
conf=
"optional->compile"
/>
<dependency
org=
"org.springframework"
name=
"org.springframework.web.portlet"
rev=
"latest.integration"
conf=
"optional->compile"
/>
<dependency
org=
"org.springframework"
name=
"org.springframework.web.portlet"
rev=
"latest.integration"
conf=
"optional->compile"
/>
<dependency
org=
"org.springframework"
name=
"org.springframework.web.servlet"
rev=
"latest.integration"
conf=
"optional->compile"
/>
<dependency
org=
"org.springframework"
name=
"org.springframework.web.servlet"
rev=
"latest.integration"
conf=
"optional->compile"
/>
<dependency
org=
"org.hsqldb"
name=
"com.springsource.org.hsqldb"
rev=
"1.8.0.9"
conf=
"
optional
->compile"
/>
<dependency
org=
"org.hsqldb"
name=
"com.springsource.org.hsqldb"
rev=
"1.8.0.9"
conf=
"
test
->compile"
/>
</dependencies>
</dependencies>
</ivy-module>
</ivy-module>
org.springframework.test/src/main/java/org/springframework/test/jdbc/HsqlTestDataSourceConfigurer.java
已删除
100644 → 0
浏览文件 @
d896a53c
/*
* Copyright 2002-2009 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.test.jdbc
;
import
javax.sql.DataSource
;
import
org.springframework.jdbc.core.JdbcTemplate
;
import
org.springframework.jdbc.datasource.SimpleDriverDataSource
;
import
org.springframework.util.ClassUtils
;
class
HsqlTestDataSourceConfigurer
extends
TestDataSourceConfigurer
{
private
static
HsqlTestDataSourceConfigurer
INSTANCE
;
public
static
synchronized
HsqlTestDataSourceConfigurer
getInstance
()
throws
ClassNotFoundException
{
if
(
INSTANCE
==
null
)
{
ClassUtils
.
forName
(
"org.hsqldb.jdbcDriver"
,
HsqlTestDataSourceConfigurer
.
class
.
getClassLoader
());
INSTANCE
=
new
HsqlTestDataSourceConfigurer
();
}
return
INSTANCE
;
}
public
void
configureConnectionProperties
(
SimpleDriverDataSource
dataSource
,
String
databaseName
)
{
dataSource
.
setDriverClass
(
org
.
hsqldb
.
jdbcDriver
.
class
);
dataSource
.
setUrl
(
"jdbc:hsqldb:mem:"
+
databaseName
);
dataSource
.
setUsername
(
"sa"
);
dataSource
.
setPassword
(
""
);
}
public
void
shutdown
(
DataSource
dataSource
)
{
new
JdbcTemplate
(
dataSource
).
execute
(
"SHUTDOWN"
);
}
}
org.springframework.test/src/main/java/org/springframework/test/jdbc/ResourceTestDatabasePopulator.java
已删除
100644 → 0
浏览文件 @
d896a53c
/*
* Copyright 2002-2009 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.test.jdbc
;
import
org.springframework.core.io.ClassPathResource
;
import
org.springframework.core.io.Resource
;
import
org.springframework.core.io.support.EncodedResource
;
import
org.springframework.jdbc.core.JdbcTemplate
;
import
org.springframework.jdbc.core.simple.SimpleJdbcTemplate
;
/**
* Populates a test DataSource from schema and test-data SQL defined in external resources.
* By default, looks for a schema.sql file and test-data.sql resource in the root of the classpath.
*
* May be configured.
* Call {@link #setSchemaLocation(Resource)} to configure the location of the database schema file.
* Call {@link #setTestDataLocation(Resource)} to configure the location of the test data file.
* Call {@link #setSqlScriptEncoding(String)} to set the encoding for the schema and test data SQL.
*/
public
class
ResourceTestDatabasePopulator
implements
TestDatabasePopulator
{
private
Resource
schemaLocation
=
new
ClassPathResource
(
"schema.sql"
);
private
Resource
testDataLocation
=
new
ClassPathResource
(
"test-data.sql"
);
private
String
sqlScriptEncoding
;
/**
* Sets the location of .sql file containing the database schema to create.
* @param schemaLocation the path to the db schema definition
*/
public
void
setSchemaLocation
(
Resource
schemaLocation
)
{
this
.
schemaLocation
=
schemaLocation
;
}
/**
* Sets the location of the .sql file containing the test data to load.
* @param testDataLocation the path to the db test data file
*/
public
void
setTestDataLocation
(
Resource
testDataLocation
)
{
this
.
testDataLocation
=
testDataLocation
;
}
/**
* Specify the encoding for SQL scripts, if different from the platform encoding.
*/
public
void
setSqlScriptEncoding
(
String
sqlScriptEncoding
)
{
this
.
sqlScriptEncoding
=
sqlScriptEncoding
;
}
public
void
populate
(
JdbcTemplate
template
)
{
createDatabaseSchema
(
template
);
insertTestData
(
template
);
}
// create the application's database schema (tables, indexes, etc.)
private
void
createDatabaseSchema
(
JdbcTemplate
template
)
{
// TODO SimpleJdbcTemplate is unnecessary now with Java5+ - make similar method available on JdbcTestUtils?
SimpleJdbcTestUtils
.
executeSqlScript
(
new
SimpleJdbcTemplate
(
template
),
new
EncodedResource
(
schemaLocation
,
sqlScriptEncoding
),
false
);
}
// populate the tables with test data
private
void
insertTestData
(
JdbcTemplate
template
)
{
// TODO SimpleJdbcTemplate is unnecessary now with Java5+ - make similar method available on JdbcTestUtils?
SimpleJdbcTestUtils
.
executeSqlScript
(
new
SimpleJdbcTemplate
(
template
),
new
EncodedResource
(
testDataLocation
,
sqlScriptEncoding
),
false
);
}
}
\ No newline at end of file
org.springframework.test/src/main/java/org/springframework/test/jdbc/TestDataSourceConfigurer.java
已删除
100644 → 0
浏览文件 @
d896a53c
/*
* Copyright 2002-2009 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.test.jdbc
;
import
javax.sql.DataSource
;
import
org.springframework.jdbc.datasource.SimpleDriverDataSource
;
/**
* Encapsulates the configuration required to connect to a specific type of test database such as HSQLdb or H2.
* Create a subclass for each database type we wish to support.
*
* @see TestDataSourceConfigurerFactory
*/
abstract
class
TestDataSourceConfigurer
{
/**
* Configure the properties required to connect to databaseName.
* @param dataSource the data source to configure
* @param databaseName the name of the test database
*/
public
abstract
void
configureConnectionProperties
(
SimpleDriverDataSource
dataSource
,
String
databaseName
);
/**
* Shutdown the test database backed by dataSource.
* @param dataSource the data source
*/
public
abstract
void
shutdown
(
DataSource
dataSource
);
}
org.springframework.test/src/main/java/org/springframework/test/jdbc/TestDataSourceConfigurerFactory.java
已删除
100644 → 0
浏览文件 @
d896a53c
/*
* Copyright 2002-2009 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.test.jdbc
;
class
TestDataSourceConfigurerFactory
{
public
static
TestDataSourceConfigurer
getConfigurer
(
TestDatabaseType
type
)
throws
IllegalStateException
{
try
{
if
(
type
==
TestDatabaseType
.
HSQL
)
{
return
HsqlTestDataSourceConfigurer
.
getInstance
();
}
else
{
throw
new
UnsupportedOperationException
(
"Other types not yet supported"
);
}
}
catch
(
ClassNotFoundException
e
)
{
throw
new
IllegalStateException
(
"Drivers for test database type ["
+
type
+
"] are not available in the classpath"
,
e
);
}
}
}
org.springframework.test/src/main/java/org/springframework/test/jdbc/TestDataSourceFactory.java
已删除
100644 → 0
浏览文件 @
d896a53c
/*
* Copyright 2002-2009 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.test.jdbc
;
import
javax.sql.DataSource
;
import
org.apache.commons.logging.Log
;
import
org.apache.commons.logging.LogFactory
;
import
org.springframework.jdbc.core.JdbcTemplate
;
import
org.springframework.jdbc.datasource.DataSourceTransactionManager
;
import
org.springframework.jdbc.datasource.SimpleDriverDataSource
;
import
org.springframework.transaction.TransactionStatus
;
import
org.springframework.transaction.support.TransactionCallbackWithoutResult
;
import
org.springframework.transaction.support.TransactionTemplate
;
import
org.springframework.util.Assert
;
/**
* Returns a {@link DataSource} that connects to an in-memory test database pre-populated with test data.
* When the DataSource is returned, callers are guaranteed that the database schema and test data will have already been loaded.
*
* Can be configured.
* Call {@link #setDatabaseName(String)} to change the name of the test database.
* Call {@link #setDatabaseType(TestDatabaseType)} to set the test database type.
* Call {@link #setDatabasePopulator(TestDatabasePopulator)} to change the algorithm used to populate the test database.
*/
public
class
TestDataSourceFactory
{
private
static
Log
logger
=
LogFactory
.
getLog
(
TestDataSourceFactory
.
class
);
private
String
databaseName
;
private
TestDataSourceConfigurer
dataSourceConfigurer
;
private
TestDatabasePopulator
databasePopulator
;
private
DataSource
dataSource
;
/**
* Creates a new TestDataSourceFactory that uses the default {@link ResourceTestDatabasePopulator}.
*/
public
TestDataSourceFactory
()
{
setDatabaseName
(
"testdb"
);
setDatabaseType
(
TestDatabaseType
.
HSQL
);
setDatabasePopulator
(
new
ResourceTestDatabasePopulator
());
}
/**
* Sets the name of the test database.
* Defaults to 'testdb'.
* @param name of the test database
*/
public
void
setDatabaseName
(
String
name
)
{
Assert
.
notNull
(
name
,
"The testDatabaseName is required"
);
databaseName
=
name
;
}
/**
* Sets the type of test database to use.
* Defaults to HSQL.
* @param type the test database type
*/
public
void
setDatabaseType
(
TestDatabaseType
type
)
{
Assert
.
notNull
(
type
,
"The TestDatabaseType is required"
);
dataSourceConfigurer
=
TestDataSourceConfigurerFactory
.
getConfigurer
(
type
);
}
/**
* Sets the helper that will be invoked to populate the test database with data.
* Defaults a {@link ResourceTestDatabasePopulator}.
* @param populator
*/
public
void
setDatabasePopulator
(
TestDatabasePopulator
populator
)
{
Assert
.
notNull
(
populator
,
"The TestDatabasePopulator is required"
);
databasePopulator
=
populator
;
}
// other public methods
/**
* The factory method that returns the {@link DataSource} that connects to the test database.
*/
public
DataSource
getDataSource
()
{
if
(
dataSource
==
null
)
{
initDataSource
();
}
return
dataSource
;
}
/**
* Destroy the test database.
* Does nothing if the database has not been initialized.
*/
public
void
destroyDataSource
()
{
if
(
dataSource
!=
null
)
{
dataSourceConfigurer
.
shutdown
(
dataSource
);
dataSource
=
null
;
}
}
// internal helper methods
// encapsulates the steps involved in initializing the data source: creating it, and populating it
private
void
initDataSource
()
{
// create the in-memory database source first
dataSource
=
createDataSource
();
if
(
logger
.
isInfoEnabled
())
{
logger
.
info
(
"Created in-memory test database '"
+
databaseName
+
"'"
);
}
if
(
databasePopulator
!=
null
)
{
// now populate the database
populateDatabase
();
}
}
protected
DataSource
createDataSource
()
{
SimpleDriverDataSource
dataSource
=
new
SimpleDriverDataSource
();
dataSourceConfigurer
.
configureConnectionProperties
(
dataSource
,
databaseName
);
return
dataSource
;
}
private
void
populateDatabase
()
{
TransactionTemplate
template
=
new
TransactionTemplate
(
new
DataSourceTransactionManager
(
dataSource
));
template
.
execute
(
new
TransactionCallbackWithoutResult
()
{
@Override
protected
void
doInTransactionWithoutResult
(
TransactionStatus
status
)
{
databasePopulator
.
populate
(
new
JdbcTemplate
(
dataSource
));
}
});
}
}
\ No newline at end of file
org.springframework.test/src/main/java/org/springframework/test/jdbc/TestDatabasePopulator.java
已删除
100644 → 0
浏览文件 @
d896a53c
/*
* Copyright 2002-2009 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.test.jdbc
;
import
org.springframework.dao.DataAccessException
;
import
org.springframework.jdbc.core.JdbcTemplate
;
/**
* Strategy for populating a test database with test data.
*
* @see ResourceTestDatabasePopulator
*/
public
interface
TestDatabasePopulator
{
/**
* Populate the test database using the JDBC-based data access template provided.
* @param template the data access template to use to populate the db; already configured and ready to use
* @throws DataAccessException if an unrecoverable data access exception occurs during database population
*/
void
populate
(
JdbcTemplate
template
);
}
\ No newline at end of file
org.springframework.test/src/main/java/org/springframework/test/jdbc/TestDatabaseType.java
已删除
100644 → 0
浏览文件 @
d896a53c
/*
* Copyright 2002-2009 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.test.jdbc
;
/**
* A supported test database type.
* @author Keith Donald
*/
public
enum
TestDatabaseType
{
HSQL
;
}
org.springframework.test/src/test/java/org/springframework/test/jdbc/TestDataSourceFactoryTests.java
已删除
100644 → 0
浏览文件 @
d896a53c
package
org.springframework.test.jdbc
;
import
static
org
.
junit
.
Assert
.
assertTrue
;
import
org.junit.Test
;
import
org.springframework.dao.DataAccessException
;
import
org.springframework.jdbc.core.JdbcTemplate
;
public
class
TestDataSourceFactoryTests
{
TestDataSourceFactory
factory
=
new
TestDataSourceFactory
();
@Test
public
void
testGetDataSource
()
{
StubTestDataSourcePopulator
populator
=
new
StubTestDataSourcePopulator
();
factory
.
setDatabasePopulator
(
populator
);
factory
.
getDataSource
();
assertTrue
(
populator
.
populateCalled
);
factory
.
destroyDataSource
();
}
private
static
class
StubTestDataSourcePopulator
implements
TestDatabasePopulator
{
private
boolean
populateCalled
;
public
void
populate
(
JdbcTemplate
template
)
throws
DataAccessException
{
this
.
populateCalled
=
true
;
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录