Permalink
Browse files

SimpleDatabaseTest

- H2 in-memory database
- create schema before the tests using H2
- DbUnit to import a dataset from an XML file
- Plain JDBC to read from the database in PersonRepository
  • Loading branch information...
0 parents commit 022a3124cf2a874b7e8e9060531f56d073e38f1b @marcphilipp committed Feb 19, 2012
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<classpath>
+ <classpathentry kind="src" path="src"/>
+ <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
+ <classpathentry kind="lib" path="lib/h2-1.3.164.jar" sourcepath="lib/h2-1.3.164-src.jar"/>
+ <classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
+ <classpathentry combineaccessrules="false" kind="src" path="/DataSetBuilder"/>
+ <classpathentry kind="output" path="bin"/>
+</classpath>
@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>DatabaseTests</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ <buildCommand>
+ <name>org.eclipse.jdt.core.javabuilder</name>
+ <arguments>
+ </arguments>
+ </buildCommand>
+ </buildSpec>
+ <natures>
+ <nature>org.eclipse.jdt.core.javanature</nature>
+ </natures>
+</projectDescription>
@@ -0,0 +1,12 @@
+#Thu Feb 16 21:21:18 CET 2012
+eclipse.preferences.version=1
+org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
+org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
+org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
+org.eclipse.jdt.core.compiler.compliance=1.6
+org.eclipse.jdt.core.compiler.debug.lineNumber=generate
+org.eclipse.jdt.core.compiler.debug.localVariable=generate
+org.eclipse.jdt.core.compiler.debug.sourceFile=generate
+org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
+org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
+org.eclipse.jdt.core.compiler.source=1.6
@@ -0,0 +1,5 @@
+<dataset>
+ <PERSON NAME="Bob" LAST_NAME="Doe" AGE="18"/>
+ <PERSON NAME="Alice" LAST_NAME="Foo" AGE="23"/>
+ <PERSON NAME="Charlie" LAST_NAME="Brown" AGE="42"/>
+</dataset>
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,6 @@
+create table if not exists PERSON (
+ ID int identity primary key,
+ NAME varchar,
+ LAST_NAME varchar,
+ AGE smallint,
+)
@@ -0,0 +1,25 @@
+public class Person {
+
+ private final String firstName;
+ private final String lastName;
+ private final int age;
+
+ public Person(String firstName, String lastName, int age) {
+ this.firstName = firstName;
+ this.lastName = lastName;
+ this.age = age;
+ }
+
+ public String getFirstName() {
+ return firstName;
+ }
+
+ public String getLastName() {
+ return lastName;
+ }
+
+ public int getAge() {
+ return age;
+ }
+
+}
@@ -0,0 +1,56 @@
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+
+import javax.sql.DataSource;
+
+public class PersonRepository {
+
+ private final DataSource dataSource;
+
+ public PersonRepository(DataSource dataSource) {
+ this.dataSource = dataSource;
+ }
+
+ public Person findPersonByFirstName(String name) throws SQLException {
+ Person person = null;
+ PreparedStatement statement = dataSource.getConnection()
+ .prepareStatement("select * from PERSON where NAME = ?");
+ statement.setString(1, name);
+ ResultSet resultSet = null;
+ try {
+ resultSet = statement.executeQuery();
+ if (resultSet.next()) {
+ person = convertSingleRow(resultSet);
+ }
+ } finally {
+ closeQuietly(resultSet);
+ closeQuietly(statement);
+ }
+ return person;
+ }
+
+ private Person convertSingleRow(ResultSet resultSet) throws SQLException {
+ String firstName = resultSet.getString("NAME");
+ String lastName = resultSet.getString("LAST_NAME");
+ int age = resultSet.getInt("AGE");
+ Person person2 = new Person(firstName, lastName, age);
+ return person2;
+ }
+
+ private void closeQuietly(ResultSet resultSet) {
+ try {
+ resultSet.close();
+ } catch (SQLException exception) {
+ }
+ }
+
+ private void closeQuietly(Statement statement) {
+ try {
+ statement.close();
+ } catch (SQLException exception) {
+ }
+ }
+
+}
@@ -0,0 +1,69 @@
+import static org.h2.engine.Constants.UTF8;
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.nullValue;
+import static org.junit.Assert.assertThat;
+
+import java.io.FileInputStream;
+
+import javax.sql.DataSource;
+
+import org.dbunit.IDatabaseTester;
+import org.dbunit.JdbcDatabaseTester;
+import org.dbunit.dataset.IDataSet;
+import org.dbunit.dataset.xml.FlatXmlDataSetBuilder;
+import org.dbunit.operation.DatabaseOperation;
+import org.h2.jdbcx.JdbcDataSource;
+import org.h2.tools.RunScript;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+public class SimpleDatabaseTest {
+
+ private static final String JDBC_DRIVER = org.h2.Driver.class.getName();
+ private static final String JDBC_URL = "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1";
+ private static final String USER = "sa";
+ private static final String PASSWORD = "";
+
+ @BeforeClass
+ public static void createSchema() throws Exception {
+ RunScript.execute(JDBC_URL, USER, PASSWORD, "schema.sql", UTF8, false);
+ }
+
+ @Before
+ public void importDataSet() throws Exception {
+ IDataSet dataSet = new FlatXmlDataSetBuilder().build(new FileInputStream("dataset.xml"));
+ IDatabaseTester databaseTester = new JdbcDatabaseTester(JDBC_DRIVER, JDBC_URL, USER, PASSWORD);
+ databaseTester.setSetUpOperation(DatabaseOperation.CLEAN_INSERT);
+ databaseTester.setDataSet(dataSet);
+ databaseTester.onSetup();
+ }
+
+ @Test
+ public void findsAndReadsExistingPersonByFirstName() throws Exception {
+ PersonRepository repository = new PersonRepository(dataSource());
+
+ Person charlie = repository.findPersonByFirstName("Charlie");
+
+ assertThat(charlie.getFirstName(), is("Charlie"));
+ assertThat(charlie.getLastName(), is("Brown"));
+ assertThat(charlie.getAge(), is(42));
+ }
+
+ @Test
+ public void returnsNullWhenPersonCannotBeFoundByFirstName() throws Exception {
+ PersonRepository repository = new PersonRepository(dataSource());
+
+ Person person = repository.findPersonByFirstName("iDoNotExist");
+
+ assertThat(person, is(nullValue()));
+ }
+
+ private DataSource dataSource() {
+ JdbcDataSource dataSource = new JdbcDataSource();
+ dataSource.setURL(JDBC_URL);
+ dataSource.setUser(USER);
+ dataSource.setPassword(PASSWORD);
+ return dataSource;
+ }
+}

0 comments on commit 022a312

Please sign in to comment.