Skip to content

Commit 63667ff

Browse files
committed
Add JUnit for DbState
1 parent 3716e73 commit 63667ff

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

core/build.gradle

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ archivesBaseName = project.projectName
77

88
dependencies {
99
testCompile dependencyJunit
10+
testCompile 'com.h2database:h2:1.4.191'
11+
testCompile 'commons-dbutils:commons-dbutils:1.6'
1012
}
1113

1214
performSigning(signingEnabled, signModule)
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package fj.control.db;
2+
3+
import fj.Unit;
4+
import fj.data.Option;
5+
import org.apache.commons.dbutils.DbUtils;
6+
import org.junit.Test;
7+
8+
import java.sql.*;
9+
10+
import static org.hamcrest.core.Is.is;
11+
import static org.junit.Assert.assertThat;
12+
13+
public class TestDbState {
14+
@Test
15+
public void testWriter() throws SQLException {
16+
final int TEN = 10;
17+
DbState writer = DbState.writer(DbState.driverManager("jdbc:h2:mem:"));
18+
19+
DB<Unit> setup = new DB<Unit>() {
20+
@Override
21+
public Unit run(Connection c) throws SQLException {
22+
Statement s = null;
23+
try {
24+
s = c.createStatement();
25+
assertThat(s.executeUpdate("CREATE TABLE TEST(ID INT PRIMARY KEY, NAME VARCHAR(255))"), is(0));
26+
assertThat(s.executeUpdate("INSERT INTO TEST (ID, NAME) VALUES (" + TEN + ", 'FOO')"), is(1));
27+
} finally {
28+
DbUtils.closeQuietly(s);
29+
}
30+
return Unit.unit();
31+
}
32+
};
33+
DB<Option<Integer>> query = new DB<Option<Integer>>() {
34+
@Override
35+
public Option<Integer> run(Connection c) throws SQLException {
36+
PreparedStatement ps = null;
37+
ResultSet rs = null;
38+
try {
39+
ps = c.prepareStatement("SELECT ID FROM TEST WHERE NAME = ?");
40+
ps.setString(1, "FOO");
41+
rs = ps.executeQuery();
42+
if (rs.next()) {
43+
return Option.some(rs.getInt("ID"));
44+
} else {
45+
return Option.none();
46+
}
47+
} finally {
48+
DbUtils.closeQuietly(rs);
49+
DbUtils.closeQuietly(ps);
50+
}
51+
}
52+
};
53+
assertThat(writer.run(setup.bind(v -> query)).some(), is(TEN));
54+
}
55+
}

0 commit comments

Comments
 (0)