Browse Source
Different databases are now supported by means of Maven Profiles and Spring Profiles. There is one Profile for each supported database. The default Profile is equivalent to hql. There is a Maven Profile all-dbs, which runs the integration tests against all databases. The databases need to be started outside the build. The build assumes the default configuration as I found it after `brew install <database>` For now we support the databases mysql, postgres and hsqldb. In order to make the new databases work setting of properties and reading generated ids was improved to do some simple conversions. This might be considered a first step towards DATAJDBC-104. The project root contains some basic scripts for starting and stopping databases, as well as running a build against all supported databases. Integration tests using a database now use Rules instead of JUnit runners. This gives more flexibility when adding fancy stuff to the Tests in the form of other Rules. Related issue: DATAJDBC-104. Original pull request: #5.pull/6/merge
36 changed files with 626 additions and 202 deletions
@ -0,0 +1,3 @@
@@ -0,0 +1,3 @@
|
||||
#!/bin/sh |
||||
|
||||
./start-all-dbs.sh && mvn clean install -Pall-dbs && ./stop-all-dbs.sh |
||||
@ -0,0 +1,59 @@
@@ -0,0 +1,59 @@
|
||||
/* |
||||
* Copyright 2017 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.data.jdbc.testing; |
||||
|
||||
import javax.sql.DataSource; |
||||
|
||||
import org.springframework.beans.factory.FactoryBean; |
||||
|
||||
/** |
||||
* @author Jens Schauder |
||||
*/ |
||||
abstract class DataSourceFactoryBean implements FactoryBean<DataSource> { |
||||
|
||||
private final Class<?> testClass; |
||||
|
||||
DataSourceFactoryBean(Class<?> testClass) { |
||||
this.testClass = testClass; |
||||
} |
||||
|
||||
private DataSource createDataSource() { |
||||
return create(createScriptName(testClass, scriptSuffix())); |
||||
} |
||||
|
||||
abstract String scriptSuffix(); |
||||
|
||||
abstract DataSource create(String scriptName); |
||||
|
||||
private String createScriptName(Class<?> testClass, String databaseType) { |
||||
|
||||
return String.format( //
|
||||
"%s/%s-%s.sql", //
|
||||
testClass.getPackage().getName(), //
|
||||
testClass.getSimpleName(), //
|
||||
databaseType.toLowerCase()); |
||||
} |
||||
|
||||
@Override |
||||
public DataSource getObject() throws Exception { |
||||
return createDataSource(); |
||||
} |
||||
|
||||
@Override |
||||
public Class<?> getObjectType() { |
||||
return DataSource.class; |
||||
} |
||||
} |
||||
@ -0,0 +1,52 @@
@@ -0,0 +1,52 @@
|
||||
/* |
||||
* Copyright 2017 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.data.jdbc.testing; |
||||
|
||||
import javax.sql.DataSource; |
||||
|
||||
import org.springframework.context.annotation.Profile; |
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; |
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType; |
||||
import org.springframework.stereotype.Component; |
||||
|
||||
/** |
||||
* @author Jens Schauder |
||||
*/ |
||||
@Component |
||||
@Profile({ "hsql", "default" }) |
||||
class HsqlDataSourceFactoryBean extends DataSourceFactoryBean { |
||||
|
||||
HsqlDataSourceFactoryBean(Class<?> testClass) { |
||||
super(testClass); |
||||
} |
||||
|
||||
@Override |
||||
String scriptSuffix() { |
||||
return "hsql"; |
||||
} |
||||
|
||||
@Override |
||||
DataSource create(String scriptName) { |
||||
|
||||
return new EmbeddedDatabaseBuilder() //
|
||||
.generateUniqueName(true) //
|
||||
.setType(EmbeddedDatabaseType.HSQL) //
|
||||
.setScriptEncoding("UTF-8") //
|
||||
.ignoreFailedDrops(true) //
|
||||
.addScript(scriptName) //
|
||||
.build(); |
||||
} |
||||
} |
||||
@ -0,0 +1,84 @@
@@ -0,0 +1,84 @@
|
||||
/* |
||||
* Copyright 2017 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.data.jdbc.testing; |
||||
|
||||
import java.sql.Connection; |
||||
import java.sql.SQLException; |
||||
|
||||
import javax.sql.DataSource; |
||||
|
||||
import org.springframework.context.annotation.Profile; |
||||
import org.springframework.core.io.DefaultResourceLoader; |
||||
import org.springframework.jdbc.datasource.init.ScriptUtils; |
||||
import org.springframework.stereotype.Component; |
||||
|
||||
import com.mysql.jdbc.jdbc2.optional.MysqlDataSource; |
||||
|
||||
/** |
||||
* @author Jens Schauder |
||||
*/ |
||||
@Component |
||||
@Profile("mysql") |
||||
class MySqlDataSourceFactoryBean extends DataSourceFactoryBean { |
||||
|
||||
public static final String ROOT_URL = "jdbc:mysql:///?user=root"; |
||||
public static final String COULDN_T_CREATE_DATABASE = //
|
||||
"Couldn't create database. Maybe you don't have a MySql database running, reachable at %s"; |
||||
private static final DefaultResourceLoader resourceLoader = new DefaultResourceLoader(); |
||||
private static final String TEST_URL = "jdbc:mysql:///test?user=root"; |
||||
|
||||
MySqlDataSourceFactoryBean(Class<?> testClass) { |
||||
super(testClass); |
||||
} |
||||
|
||||
@Override |
||||
String scriptSuffix() { |
||||
return "mysql"; |
||||
} |
||||
|
||||
@Override |
||||
DataSource create(String scriptName) { |
||||
|
||||
createDatabase(); |
||||
return setupDatabase(scriptName); |
||||
} |
||||
|
||||
private MysqlDataSource setupDatabase(String scriptName) { |
||||
|
||||
MysqlDataSource ds = new MysqlDataSource(); |
||||
ds.setUrl(TEST_URL); |
||||
|
||||
try (Connection connection = ds.getConnection()) { |
||||
ScriptUtils.executeSqlScript(connection, resourceLoader.getResource(scriptName)); |
||||
} catch (SQLException e) { |
||||
throw new RuntimeException("Couldn't setup database", e); |
||||
} |
||||
|
||||
return ds; |
||||
} |
||||
|
||||
private void createDatabase() { |
||||
|
||||
MysqlDataSource initialDs = new MysqlDataSource(); |
||||
initialDs.setUrl(ROOT_URL); |
||||
|
||||
try (Connection connection = initialDs.getConnection()) { |
||||
ScriptUtils.executeSqlScript(connection, resourceLoader.getResource("create-mysql.sql")); |
||||
} catch (SQLException e) { |
||||
throw new RuntimeException(String.format(COULDN_T_CREATE_DATABASE, ROOT_URL), e); |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,69 @@
@@ -0,0 +1,69 @@
|
||||
/* |
||||
* Copyright 2017 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.data.jdbc.testing; |
||||
|
||||
import java.sql.Connection; |
||||
import java.sql.SQLException; |
||||
|
||||
import javax.sql.DataSource; |
||||
|
||||
import org.postgresql.ds.PGSimpleDataSource; |
||||
import org.springframework.context.annotation.Profile; |
||||
import org.springframework.core.io.DefaultResourceLoader; |
||||
import org.springframework.core.io.support.EncodedResource; |
||||
import org.springframework.jdbc.datasource.init.ScriptUtils; |
||||
import org.springframework.stereotype.Component; |
||||
|
||||
/** |
||||
* @author Jens Schauder |
||||
*/ |
||||
@Component |
||||
@Profile("postgres") |
||||
public class PostgresDataSourceFactoryBean extends DataSourceFactoryBean { |
||||
|
||||
public static final String URL = "jdbc:postgresql:///postgres"; |
||||
private static final DefaultResourceLoader resourceLoader = new DefaultResourceLoader(); |
||||
|
||||
PostgresDataSourceFactoryBean(Class<?> testClass) { |
||||
super(testClass); |
||||
} |
||||
|
||||
@Override |
||||
protected String scriptSuffix() { |
||||
return "postgres"; |
||||
} |
||||
|
||||
@Override |
||||
DataSource create(String scriptName) { |
||||
return setupDatabase(scriptName); |
||||
} |
||||
|
||||
private DataSource setupDatabase(String scriptName) { |
||||
|
||||
PGSimpleDataSource ds = new PGSimpleDataSource(); |
||||
ds.setUrl(URL); |
||||
|
||||
try (Connection connection = ds.getConnection()) { |
||||
|
||||
ScriptUtils.executeSqlScript(connection, new EncodedResource(resourceLoader.getResource(scriptName)), false, true, |
||||
"--", ";", "/*", "*/"); |
||||
} catch (SQLException e) { |
||||
throw new RuntimeException("Couldn't setup database", e); |
||||
} |
||||
|
||||
return ds; |
||||
} |
||||
} |
||||
@ -0,0 +1,2 @@
@@ -0,0 +1,2 @@
|
||||
DROP DATABASE test; |
||||
CREATE DATABASE test; |
||||
@ -1,5 +1,4 @@
@@ -1,5 +1,4 @@
|
||||
-- noinspection SqlNoDataSourceInspectionForFile |
||||
|
||||
CREATE TABLE ReadOnlyIdEntity (ID BIGINT GENERATED BY DEFAULT AS IDENTITY(START WITH 1) PRIMARY KEY, NAME VARCHAR(100)) |
||||
|
||||
CREATE TABLE PrimitiveIdEntity (ID BIGINT GENERATED BY DEFAULT AS IDENTITY(START WITH 1) PRIMARY KEY, NAME VARCHAR(100)) |
||||
@ -0,0 +1,2 @@
@@ -0,0 +1,2 @@
|
||||
CREATE TABLE ReadOnlyIdEntity (ID BIGINT AUTO_INCREMENT PRIMARY KEY, NAME VARCHAR(100)); |
||||
CREATE TABLE PrimitiveIdEntity (ID BIGINT AUTO_INCREMENT PRIMARY KEY, NAME VARCHAR(100)); |
||||
@ -0,0 +1,5 @@
@@ -0,0 +1,5 @@
|
||||
DROP TABLE ReadOnlyIdEntity; |
||||
DROP TABLE PrimitiveIdEntity; |
||||
|
||||
CREATE TABLE ReadOnlyIdEntity (ID SERIAL PRIMARY KEY, NAME VARCHAR(100)); |
||||
CREATE TABLE PrimitiveIdEntity (ID SERIAL PRIMARY KEY, NAME VARCHAR(100)); |
||||
@ -0,0 +1 @@
@@ -0,0 +1 @@
|
||||
CREATE TABLE dummyentity ( idProp BIGINT GENERATED BY DEFAULT AS IDENTITY ( START WITH 1 ) PRIMARY KEY, NAME VARCHAR(100)) |
||||
@ -0,0 +1 @@
@@ -0,0 +1 @@
|
||||
CREATE TABLE dummyentity (idProp BIGINT AUTO_INCREMENT PRIMARY KEY, NAME VARCHAR(100)); |
||||
@ -0,0 +1,2 @@
@@ -0,0 +1,2 @@
|
||||
DROP TABLE dummyentity; |
||||
CREATE TABLE dummyentity (idProp SERIAL PRIMARY KEY, NAME VARCHAR(100)); |
||||
@ -1 +0,0 @@
@@ -1 +0,0 @@
|
||||
CREATE TABLE dummyentity (idProp BIGINT GENERATED BY DEFAULT AS IDENTITY(START WITH 1) PRIMARY KEY, NAME VARCHAR(100)) |
||||
@ -0,0 +1,7 @@
@@ -0,0 +1,7 @@
|
||||
#!/bin/sh |
||||
|
||||
# postgres |
||||
pg_ctl -D /usr/local/var/postgres start |
||||
|
||||
# mysql |
||||
mysql.server start |
||||
Loading…
Reference in new issue