embeddedd databases convenience factory; builder polishing

This commit is contained in:
Keith Donald
2009-11-03 23:05:41 +00:00
parent 6c80dbaa9e
commit f1d012bffa
4 changed files with 91 additions and 40 deletions

View File

@@ -16,7 +16,6 @@
package org.springframework.jdbc.datasource.embedded;
import org.springframework.core.io.ClassRelativeResourceLoader;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.ResourceLoader;
import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
@@ -27,7 +26,7 @@ import org.springframework.jdbc.datasource.init.ResourceDatabasePopulator;
* <p>Usage example:
* <pre>
* EmbeddedDatabaseBuilder builder = new EmbeddedDatabaseBuilder();
* EmbeddedDatabase db = builder.script("schema.sql").script("data.sql").build();
* EmbeddedDatabase db = builder.setType(H2).addScript("schema.sql").addScript("data.sql").build();
* db.shutdown();
* </pre>
*
@@ -69,7 +68,7 @@ public class EmbeddedDatabaseBuilder {
* @param databaseName the database name
* @return this, for fluent call chaining
*/
public EmbeddedDatabaseBuilder name(String databaseName) {
public EmbeddedDatabaseBuilder setName(String databaseName) {
this.databaseFactory.setDatabaseName(databaseName);
return this;
}
@@ -80,7 +79,7 @@ public class EmbeddedDatabaseBuilder {
* @param databaseType the database type
* @return this, for fluent call chaining
*/
public EmbeddedDatabaseBuilder type(EmbeddedDatabaseType databaseType) {
public EmbeddedDatabaseBuilder setType(EmbeddedDatabaseType databaseType) {
this.databaseFactory.setDatabaseType(databaseType);
return this;
}
@@ -90,7 +89,7 @@ public class EmbeddedDatabaseBuilder {
* @param sqlResource the sql resource location
* @return this, for fluent call chaining
*/
public EmbeddedDatabaseBuilder script(String sqlResource) {
public EmbeddedDatabaseBuilder addScript(String sqlResource) {
this.databasePopulator.addScript(this.resourceLoader.getResource(sqlResource));
return this;
}
@@ -102,26 +101,5 @@ public class EmbeddedDatabaseBuilder {
public EmbeddedDatabase build() {
return this.databaseFactory.getDatabase();
}
/**
* Factory method that builds a default EmbeddedDatabase instance.
* <p>The default instance is HSQL with a schema created from "classpath:schema.sql"
* and data loaded from "classpath:data.sql".
* @return an embedded database
*/
public static EmbeddedDatabase buildDefault() {
return new EmbeddedDatabaseBuilder().script("schema.sql").script("data.sql").build();
}
/**
* Factory method that creates a EmbeddedDatabaseBuilder that loads SQL resources
* relative to the provided class.
* @param clazz the class to load relative to
* @return the embedded database builder
*/
public static EmbeddedDatabaseBuilder relativeTo(Class<?> clazz) {
return new EmbeddedDatabaseBuilder(new ClassRelativeResourceLoader(clazz));
}
}

View File

@@ -0,0 +1,56 @@
/*
* 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.jdbc.datasource.embedded;
import org.springframework.core.io.ClassRelativeResourceLoader;
/**
* Convenience factory for constructing commonly used EmbeddedDatabase configurations.
* @author Keith Donald
*/
public final class EmbeddedDatabases {
private EmbeddedDatabases() {
}
/**
* Factory method that creates a default EmbeddedDatabase instance.
* <p>The default instance is HQL populated with a schema loaded from <code>classpath:schema.sql</code> and data loaded from <code>classpath:data.sql</code>.
* @return an embedded database
*/
public static EmbeddedDatabase createDefault() {
return buildDefault(new EmbeddedDatabaseBuilder());
}
/**
* Factory method that creates a default HSQL EmbeddedDatabase instance.
* <p>The default instance is HQL populated with a schema loaded from <code>schema.sql</code> and data loaded from <code>data.sql</code>, where
* each .sql file location is relative to the specified class.
* @param clazz the class to load .sql resources relative to
* @return an embedded database
*/
public static EmbeddedDatabase createDefault(Class<?> clazz) {
return buildDefault(new EmbeddedDatabaseBuilder(new ClassRelativeResourceLoader(clazz)));
}
// internal helpers
private static EmbeddedDatabase buildDefault(EmbeddedDatabaseBuilder builder) {
return builder.addScript("schema.sql").addScript("data.sql").build();
}
}