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