| Nils Diewald | 14b3aa4 | 2015-02-26 22:26:20 +0000 | [diff] [blame] | 1 | package de.ids_mannheim.korap.server; |
| Nils Diewald | 6aa929e | 2014-09-17 13:30:34 +0000 | [diff] [blame] | 2 | |
| Nils Diewald | 50e90e9 | 2015-02-26 21:25:25 +0000 | [diff] [blame] | 3 | import de.ids_mannheim.korap.response.MatchCollector; |
| 4 | import de.ids_mannheim.korap.response.collector.MatchCollectorDB; |
| Nils Diewald | 6aa929e | 2014-09-17 13:30:34 +0000 | [diff] [blame] | 5 | |
| 6 | import java.sql.Connection; |
| 7 | import java.sql.DriverManager; |
| 8 | import java.sql.PreparedStatement; |
| 9 | import java.sql.ResultSet; |
| 10 | import java.sql.Statement; |
| 11 | import java.sql.SQLException; |
| 12 | |
| Nils Diewald | ad3f303 | 2014-09-24 01:42:47 +0000 | [diff] [blame] | 13 | import com.mchange.v2.c3p0.*; |
| Nils Diewald | 6aa929e | 2014-09-17 13:30:34 +0000 | [diff] [blame] | 14 | |
| 15 | import org.junit.After; |
| 16 | import org.junit.Before; |
| 17 | import org.junit.Test; |
| Nils Diewald | f04e100 | 2014-09-24 22:52:59 +0000 | [diff] [blame] | 18 | import org.junit.Ignore; |
| Nils Diewald | 8d8641b | 2014-09-28 17:37:53 +0000 | [diff] [blame] | 19 | import static org.junit.Assert.*; |
| Nils Diewald | 6aa929e | 2014-09-17 13:30:34 +0000 | [diff] [blame] | 20 | |
| Nils Diewald | d723d81 | 2014-09-23 18:50:52 +0000 | [diff] [blame] | 21 | public class TestDatabase { |
| Nils Diewald | 6aa929e | 2014-09-17 13:30:34 +0000 | [diff] [blame] | 22 | |
| 23 | private Connection conn; |
| 24 | private Statement stat; |
| 25 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 26 | |
| Nils Diewald | 6aa929e | 2014-09-17 13:30:34 +0000 | [diff] [blame] | 27 | @Before |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 28 | public void setUp () throws Exception { |
| Nils Diewald | 14b3aa4 | 2015-02-26 22:26:20 +0000 | [diff] [blame] | 29 | Class.forName("org.sqlite.JDBC"); |
| 30 | conn = DriverManager.getConnection("jdbc:sqlite::memory:"); |
| 31 | this.stat = conn.createStatement(); |
| Eliza Margaretha | 6f98920 | 2016-10-14 21:48:29 +0200 | [diff] [blame] | 32 | stat.executeUpdate( |
| 33 | "CREATE TABLE IF NOT EXISTS people (name TEXT, age INTEGER);"); |
| Nils Diewald | 14b3aa4 | 2015-02-26 22:26:20 +0000 | [diff] [blame] | 34 | conn.setAutoCommit(false); |
| Nils Diewald | 6aa929e | 2014-09-17 13:30:34 +0000 | [diff] [blame] | 35 | }; |
| 36 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 37 | |
| Nils Diewald | 6aa929e | 2014-09-17 13:30:34 +0000 | [diff] [blame] | 38 | @Test |
| 39 | public void TestDatabase () throws Exception { |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 40 | PreparedStatement prep = this.conn |
| 41 | .prepareStatement("INSERT INTO people VALUES (?, ?);"); |
| Nils Diewald | 6aa929e | 2014-09-17 13:30:34 +0000 | [diff] [blame] | 42 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 43 | prep.setString(1, "Peter"); |
| 44 | prep.setString(2, "24"); |
| 45 | prep.addBatch(); |
| Nils Diewald | 6aa929e | 2014-09-17 13:30:34 +0000 | [diff] [blame] | 46 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 47 | prep.setString(1, "Klaus"); |
| 48 | prep.setString(2, "31"); |
| 49 | prep.addBatch(); |
| Nils Diewald | 6aa929e | 2014-09-17 13:30:34 +0000 | [diff] [blame] | 50 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 51 | prep.executeBatch(); |
| 52 | conn.setAutoCommit(true); |
| Nils Diewald | 6aa929e | 2014-09-17 13:30:34 +0000 | [diff] [blame] | 53 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 54 | ResultSet rs = stat.executeQuery("SELECT * FROM people;"); |
| Nils Diewald | 6aa929e | 2014-09-17 13:30:34 +0000 | [diff] [blame] | 55 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 56 | rs.next(); |
| Nils Diewald | 6aa929e | 2014-09-17 13:30:34 +0000 | [diff] [blame] | 57 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 58 | assertEquals(rs.getString("name"), "Peter"); |
| 59 | assertEquals(rs.getInt("age"), 24); |
| Nils Diewald | 6aa929e | 2014-09-17 13:30:34 +0000 | [diff] [blame] | 60 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 61 | rs.next(); |
| Nils Diewald | 6aa929e | 2014-09-17 13:30:34 +0000 | [diff] [blame] | 62 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 63 | assertEquals(rs.getString("name"), "Klaus"); |
| 64 | assertEquals(rs.getInt("age"), 31); |
| 65 | |
| 66 | rs.close(); |
| Nils Diewald | 6aa929e | 2014-09-17 13:30:34 +0000 | [diff] [blame] | 67 | }; |
| 68 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 69 | |
| Nils Diewald | f04e100 | 2014-09-24 22:52:59 +0000 | [diff] [blame] | 70 | /* |
| 71 | * The following tests don't work well with in-memory dbs and |
| 72 | * temporary dbs - should be improved |
| 73 | */ |
| 74 | |
| Nils Diewald | 8d8641b | 2014-09-28 17:37:53 +0000 | [diff] [blame] | 75 | @Test |
| Nils Diewald | ad3f303 | 2014-09-24 01:42:47 +0000 | [diff] [blame] | 76 | public void TestDatabasePool () throws Exception { |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 77 | ComboPooledDataSource cpds = new ComboPooledDataSource(); |
| 78 | // Connect to a temporary file instead of a in-memory file |
| 79 | cpds.setDriverClass("org.sqlite.JDBC"); |
| 80 | cpds.setJdbcUrl("jdbc:sqlite:"); |
| 81 | cpds.setMaxStatements(100); |
| Nils Diewald | ad3f303 | 2014-09-24 01:42:47 +0000 | [diff] [blame] | 82 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 83 | // This is part of the match collector |
| 84 | this.conn = cpds.getConnection(); |
| 85 | conn.setAutoCommit(false); |
| 86 | this.stat = conn.createStatement(); |
| Eliza Margaretha | 6f98920 | 2016-10-14 21:48:29 +0200 | [diff] [blame] | 87 | stat.executeUpdate( |
| 88 | "CREATE TABLE IF NOT EXISTS result_a (text_id INTEGER, match_count INTEGER);"); |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 89 | // conn.setAutoCommit(false); |
| 90 | PreparedStatement prep = this.conn |
| 91 | .prepareStatement("INSERT INTO result_a VALUES (?, ?);"); |
| 92 | prep.setInt(1, 5); |
| 93 | prep.setInt(2, 8000); |
| 94 | prep.addBatch(); |
| 95 | prep.executeBatch(); |
| Nils Diewald | ad3f303 | 2014-09-24 01:42:47 +0000 | [diff] [blame] | 96 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 97 | ResultSet rs = stat.executeQuery("SELECT * FROM result_a;"); |
| 98 | rs.next(); |
| 99 | assertEquals(rs.getInt("text_id"), 5); |
| 100 | assertEquals(rs.getInt("match_count"), 8000); |
| 101 | rs.close(); |
| Nils Diewald | ad3f303 | 2014-09-24 01:42:47 +0000 | [diff] [blame] | 102 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 103 | MatchCollectorDB mc = new MatchCollectorDB(2000, "result_a"); |
| 104 | mc.setDBPool("sqlite", cpds, this.conn); |
| Nils Diewald | ad3f303 | 2014-09-24 01:42:47 +0000 | [diff] [blame] | 105 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 106 | mc.add(9, 5000); |
| 107 | mc.add(12, 6785); |
| 108 | mc.add(39, 56576); |
| Nils Diewald | ad3f303 | 2014-09-24 01:42:47 +0000 | [diff] [blame] | 109 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 110 | mc.close(false); |
| Nils Diewald | ad3f303 | 2014-09-24 01:42:47 +0000 | [diff] [blame] | 111 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 112 | rs = stat.executeQuery("SELECT * FROM result_a;"); |
| 113 | assertTrue(rs.next()); |
| 114 | assertEquals(rs.getInt("text_id"), 5); |
| 115 | assertEquals(rs.getInt("match_count"), 8000); |
| 116 | rs.next(); |
| 117 | assertEquals(rs.getInt("text_id"), 9); |
| 118 | assertEquals(rs.getInt("match_count"), 5000); |
| 119 | rs.next(); |
| 120 | assertEquals(rs.getInt("text_id"), 12); |
| 121 | assertEquals(rs.getInt("match_count"), 6785); |
| 122 | rs.next(); |
| 123 | assertEquals(rs.getInt("text_id"), 39); |
| 124 | assertEquals(rs.getInt("match_count"), 56576); |
| 125 | |
| 126 | rs.close(); |
| Nils Diewald | ad3f303 | 2014-09-24 01:42:47 +0000 | [diff] [blame] | 127 | }; |
| 128 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 129 | |
| Nils Diewald | 8d8641b | 2014-09-28 17:37:53 +0000 | [diff] [blame] | 130 | @Test |
| 131 | public void TestDatabasePoolCollector () throws Exception { |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 132 | ComboPooledDataSource cpds = new ComboPooledDataSource(); |
| 133 | // Connect to a temporary file instead of a in-memory file |
| 134 | cpds.setDriverClass("org.sqlite.JDBC"); |
| 135 | cpds.setJdbcUrl("jdbc:sqlite:"); |
| 136 | cpds.setMaxStatements(100); |
| Nils Diewald | f04e100 | 2014-09-24 22:52:59 +0000 | [diff] [blame] | 137 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 138 | // This is part of the match collector |
| 139 | conn = cpds.getConnection(); |
| 140 | conn.setAutoCommit(false); |
| 141 | Statement stat = conn.createStatement(); |
| Eliza Margaretha | 6f98920 | 2016-10-14 21:48:29 +0200 | [diff] [blame] | 142 | stat.executeUpdate( |
| 143 | "CREATE TABLE IF NOT EXISTS matchXYZ (text_id INTEGER, match_count INTEGER);"); |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 144 | conn.commit(); |
| 145 | stat.close(); |
| Nils Diewald | f04e100 | 2014-09-24 22:52:59 +0000 | [diff] [blame] | 146 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 147 | MatchCollectorDB mc = new MatchCollectorDB(3, "matchXYZ"); |
| 148 | mc.setDBPool("sqlite", cpds, conn); |
| Nils Diewald | f04e100 | 2014-09-24 22:52:59 +0000 | [diff] [blame] | 149 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 150 | mc.add(9, 5000); |
| 151 | mc.add(12, 6785); |
| 152 | mc.add(39, 56576); |
| 153 | // First commit |
| Nils Diewald | f04e100 | 2014-09-24 22:52:59 +0000 | [diff] [blame] | 154 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 155 | mc.add(45, 5000); |
| 156 | mc.add(67, 6785); |
| 157 | mc.add(81, 56576); |
| 158 | // Second commit |
| Nils Diewald | f04e100 | 2014-09-24 22:52:59 +0000 | [diff] [blame] | 159 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 160 | mc.add(94, 456); |
| 161 | mc.close(false); |
| 162 | // Final commit |
| Nils Diewald | f04e100 | 2014-09-24 22:52:59 +0000 | [diff] [blame] | 163 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 164 | // conn = cpds.getConnection(); |
| 165 | stat = conn.createStatement(); |
| 166 | ResultSet rs = stat |
| 167 | .executeQuery("SELECT count('*') AS num FROM matchXYZ;"); |
| Nils Diewald | 8d8641b | 2014-09-28 17:37:53 +0000 | [diff] [blame] | 168 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 169 | assertEquals(7, rs.getInt("num")); |
| Nils Diewald | 8d8641b | 2014-09-28 17:37:53 +0000 | [diff] [blame] | 170 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 171 | rs = stat.executeQuery("SELECT text_id, match_count FROM matchXYZ;"); |
| 172 | assertTrue(rs.next()); |
| Nils Diewald | 8d8641b | 2014-09-28 17:37:53 +0000 | [diff] [blame] | 173 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 174 | assertEquals(rs.getInt("text_id"), 9); |
| 175 | assertEquals(rs.getInt("match_count"), 5000); |
| 176 | assertTrue(rs.next()); |
| 177 | assertEquals(rs.getInt("text_id"), 12); |
| 178 | assertEquals(rs.getInt("match_count"), 6785); |
| 179 | assertTrue(rs.next()); |
| 180 | assertEquals(rs.getInt("text_id"), 39); |
| 181 | assertEquals(rs.getInt("match_count"), 56576); |
| 182 | assertTrue(rs.next()); |
| 183 | assertEquals(rs.getInt("text_id"), 45); |
| 184 | assertEquals(rs.getInt("match_count"), 5000); |
| 185 | assertTrue(rs.next()); |
| 186 | assertEquals(rs.getInt("text_id"), 67); |
| 187 | assertEquals(rs.getInt("match_count"), 6785); |
| 188 | assertTrue(rs.next()); |
| 189 | assertEquals(rs.getInt("text_id"), 81); |
| 190 | assertEquals(rs.getInt("match_count"), 56576); |
| 191 | assertTrue(rs.next()); |
| 192 | assertEquals(rs.getInt("text_id"), 94); |
| 193 | assertEquals(rs.getInt("match_count"), 456); |
| 194 | assertFalse(rs.next()); |
| Nils Diewald | 8d8641b | 2014-09-28 17:37:53 +0000 | [diff] [blame] | 195 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 196 | stat.close(); |
| Nils Diewald | f04e100 | 2014-09-24 22:52:59 +0000 | [diff] [blame] | 197 | }; |
| Nils Diewald | ad3f303 | 2014-09-24 01:42:47 +0000 | [diff] [blame] | 198 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 199 | |
| Nils Diewald | ad3f303 | 2014-09-24 01:42:47 +0000 | [diff] [blame] | 200 | @Test |
| Nils Diewald | 6aa929e | 2014-09-17 13:30:34 +0000 | [diff] [blame] | 201 | public void TestMatchCollectorDB () throws Exception { |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 202 | MatchCollector mc = new MatchCollectorDB(2000, "matchXYZ"); |
| 203 | mc.add(5, 7); |
| 204 | mc.add(8, 2); |
| 205 | mc.add(9, 10); |
| 206 | mc.add(16, 90); |
| 207 | mc.commit(); |
| 208 | assertEquals(mc.getTotalResults(), 109); |
| 209 | assertEquals(mc.getTotalResultDocs(), 4); |
| Nils Diewald | 6aa929e | 2014-09-17 13:30:34 +0000 | [diff] [blame] | 210 | }; |
| 211 | |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 212 | |
| Nils Diewald | 6aa929e | 2014-09-17 13:30:34 +0000 | [diff] [blame] | 213 | @After |
| 214 | public void shutDown () throws Exception { |
| Nils Diewald | bb33da2 | 2015-03-04 16:24:25 +0000 | [diff] [blame] | 215 | this.conn.close(); |
| Nils Diewald | 6aa929e | 2014-09-17 13:30:34 +0000 | [diff] [blame] | 216 | }; |
| 217 | }; |