/
TypeSafeBuilderDatabaseTest.java
82 lines (68 loc) · 2.85 KB
/
TypeSafeBuilderDatabaseTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package de.marcphilipp.dbunit.example;
import static de.marcphilipp.dbunit.example.Schema.PersonTable.AGE;
import static de.marcphilipp.dbunit.example.Schema.PersonTable.LAST_NAME;
import static de.marcphilipp.dbunit.example.Schema.PersonTable.NAME;
import static de.marcphilipp.dbunit.example.Schema.Tables.PERSON;
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 javax.sql.DataSource;
import org.dbunit.IDatabaseTester;
import org.dbunit.dataset.DataSetException;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.builder.DataSetBuilder;
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 TypeSafeBuilderDatabaseTest {
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 = buildDataSet();
cleanlyInsertDataset(dataSet);
}
private IDataSet buildDataSet() throws DataSetException {
DataSetBuilder builder = new DataSetBuilder();
builder.newRow(PERSON).with(NAME, "Bob").with(LAST_NAME, "Doe").with(AGE, 18).add();
builder.newRow(PERSON).with(NAME, "Alice").with(LAST_NAME, "Foo").with(AGE, 23).add();
builder.newRow(PERSON).with(NAME, "Charlie").with(LAST_NAME, "Brown").with(AGE, 42).add();
return builder.build();
}
private void cleanlyInsertDataset(IDataSet dataSet) throws ClassNotFoundException, Exception {
IDatabaseTester databaseTester = new H2DatabaseTester(dataSource());
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;
}
}