Skip to content

Commit 09d8cc8

Browse files
authored
closed test resource after use (#681)
1 parent dce66ee commit 09d8cc8

1 file changed

Lines changed: 96 additions & 75 deletions

File tree

src/test/java/com/teragrep/pth_10/steps/teragrep/bloomfilter/TeragrepBloomFilterTest.java

Lines changed: 96 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ void setEnv() {
8686
);
8787
Config config = ConfigFactory.parseProperties(properties);
8888
Assertions.assertDoesNotThrow(() -> {
89-
conn.prepareStatement("DROP ALL OBJECTS").execute(); // h2 clear database
89+
try (PreparedStatement statement = conn.prepareStatement("DROP ALL OBJECTS")) {
90+
statement.execute(); // h2 clear database
91+
}
9092
});
9193
Assertions.assertDoesNotThrow(() -> {
9294
Class.forName("org.h2.Driver");
@@ -104,20 +106,25 @@ void setEnv() {
104106
+ " `partition_id` BIGINT(20) UNSIGNED NOT NULL,"
105107
+ " `filter_type_id` BIGINT(20) UNSIGNED NOT NULL," + " `filter` LONGBLOB NOT NULL" + ");";
106108
Assertions.assertDoesNotThrow(() -> {
107-
conn.prepareStatement(createFilterType).execute();
108-
conn.prepareStatement(createTable).execute();
109+
try (PreparedStatement statement = conn.prepareStatement(createFilterType)) {
110+
statement.execute();
111+
}
112+
try (PreparedStatement statement = conn.prepareStatement(createTable)) {
113+
statement.execute();
114+
}
109115
});
110116
int loops = 0;
111117
for (Map.Entry<Long, Double> entry : sizeMap.entrySet()) {
112118
loops++;
113119
Assertions.assertDoesNotThrow(() -> {
114-
PreparedStatement stmt = conn.prepareStatement(insertSql);
115-
stmt.setInt(1, entry.getKey().intValue()); // filtertype.expectedElements
116-
stmt.setDouble(2, entry.getValue()); // filtertype.targetFpp
117-
stmt.setString(3, pattern);
118-
stmt.executeUpdate();
119-
stmt.clearParameters();
120-
conn.commit();
120+
try (PreparedStatement stmt = conn.prepareStatement(insertSql)) {
121+
stmt.setInt(1, entry.getKey().intValue()); // filtertype.expectedElements
122+
stmt.setDouble(2, entry.getValue()); // filtertype.targetFpp
123+
stmt.setString(3, pattern);
124+
stmt.executeUpdate();
125+
stmt.clearParameters();
126+
conn.commit();
127+
}
121128
});
122129
}
123130
Assertions.assertEquals(3, loops);
@@ -126,9 +133,11 @@ void setEnv() {
126133
@AfterAll
127134
public void tearDown() {
128135
Assertions.assertDoesNotThrow(() -> {
129-
conn.prepareStatement("DROP ALL OBJECTS").execute(); // h2 clear database
136+
try (PreparedStatement statement = conn.prepareStatement("DROP ALL OBJECTS")) {
137+
statement.execute();// h2 clear database
138+
}
130139
});
131-
Assertions.assertDoesNotThrow(conn::close);
140+
132141
}
133142

134143
// -- Tests --
@@ -153,23 +162,26 @@ void testSavingToDatabase() {
153162
Map.Entry<Long, Double> entry = sizeMap.entrySet().iterator().next();
154163
String sql = "SELECT `filter` FROM `" + tableName + "`";
155164
Assertions.assertDoesNotThrow(() -> {
156-
ResultSet rs = conn.prepareStatement(sql).executeQuery();
157-
int cols = rs.getMetaData().getColumnCount();
158-
BloomFilter resultFilter = emptyFilter;
159-
int loops = 0;
160-
while (rs.next()) {
161-
byte[] bytes = rs.getBytes(1);
162-
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
163-
resultFilter = BloomFilter.readFrom(bais);
164-
loops++;
165+
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
166+
ResultSet rs = stmt.executeQuery();
167+
int cols = rs.getMetaData().getColumnCount();
168+
BloomFilter resultFilter = emptyFilter;
169+
int loops = 0;
170+
171+
while (rs.next()) {
172+
byte[] bytes = rs.getBytes(1);
173+
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
174+
resultFilter = BloomFilter.readFrom(bais);
175+
loops++;
176+
}
177+
Assertions.assertEquals(4, loops);
178+
Assertions.assertNotNull(resultFilter);
179+
Assertions.assertEquals(1, cols);
180+
Assertions.assertTrue(resultFilter.mightContain("one"));
181+
Assertions.assertFalse(resultFilter.mightContain("neo"));
182+
Assertions.assertTrue(resultFilter.expectedFpp() <= entry.getValue());
183+
rs.close();
165184
}
166-
Assertions.assertEquals(4, loops);
167-
Assertions.assertNotNull(resultFilter);
168-
Assertions.assertEquals(1, cols);
169-
Assertions.assertTrue(resultFilter.mightContain("one"));
170-
Assertions.assertFalse(resultFilter.mightContain("neo"));
171-
Assertions.assertTrue(resultFilter.expectedFpp() <= entry.getValue());
172-
rs.close();
173185
});
174186
}
175187

@@ -193,19 +205,22 @@ void testSavingToDatabaseWithOverwrite() {
193205
String sql = "SELECT `filter` FROM `" + tableName + "`";
194206
Assertions.assertDoesNotThrow(() -> {
195207
BloomFilter resultFilter = emptyFilter;
196-
ResultSet rs = conn.prepareStatement(sql).executeQuery();
197-
while (rs.next()) {
198-
byte[] bytes = rs.getBytes(1);
199-
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
200-
resultFilter = BloomFilter.readFrom(bais);
208+
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
209+
ResultSet rs = stmt.executeQuery();
210+
while (rs.next()) {
211+
byte[] bytes = rs.getBytes(1);
212+
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
213+
resultFilter = BloomFilter.readFrom(bais);
214+
}
215+
216+
int cols = rs.getMetaData().getColumnCount();
217+
Assertions.assertNotNull(resultFilter);
218+
Assertions.assertEquals(1, cols);
219+
Assertions.assertTrue(resultFilter.mightContain("one"));
220+
Assertions.assertFalse(resultFilter.mightContain("neo"));
221+
Assertions.assertTrue(resultFilter.expectedFpp() <= 0.01D);
222+
Assertions.assertDoesNotThrow(rs::close);
201223
}
202-
int cols = rs.getMetaData().getColumnCount();
203-
Assertions.assertNotNull(resultFilter);
204-
Assertions.assertEquals(1, cols);
205-
Assertions.assertTrue(resultFilter.mightContain("one"));
206-
Assertions.assertFalse(resultFilter.mightContain("neo"));
207-
Assertions.assertTrue(resultFilter.expectedFpp() <= 0.01D);
208-
Assertions.assertDoesNotThrow(rs::close);
209224
});
210225
// Create second filter that will overwrite first one
211226
List<String> secondTokens = new ArrayList<>(Collections.singletonList("neo"));
@@ -225,21 +240,23 @@ void testSavingToDatabaseWithOverwrite() {
225240
secondFilter.saveFilter(true);
226241
String secondSql = "SELECT `filter` FROM `" + tableName + "`";
227242
Assertions.assertDoesNotThrow(() -> {
228-
ResultSet secondRs = conn.prepareStatement(secondSql).executeQuery();
229-
BloomFilter secondResultFilter = emptyFilter;
230-
while (secondRs.next()) {
231-
byte[] bytes = secondRs.getBytes(1);
232-
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
233-
secondResultFilter = BloomFilter.readFrom(bais);
234-
}
235-
int secondCols = secondRs.getMetaData().getColumnCount();
243+
try (PreparedStatement stmt = conn.prepareStatement(secondSql)) {
244+
ResultSet secondRs = stmt.executeQuery();
245+
BloomFilter secondResultFilter = emptyFilter;
246+
while (secondRs.next()) {
247+
byte[] bytes = secondRs.getBytes(1);
248+
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
249+
secondResultFilter = BloomFilter.readFrom(bais);
250+
}
251+
int secondCols = secondRs.getMetaData().getColumnCount();
236252

237-
Assertions.assertNotNull(secondResultFilter);
238-
Assertions.assertEquals(1, secondCols);
239-
Assertions.assertFalse(secondResultFilter.mightContain("one"));
240-
Assertions.assertTrue(secondResultFilter.mightContain("neo"));
241-
Assertions.assertTrue(secondResultFilter.expectedFpp() <= 0.01D);
242-
Assertions.assertDoesNotThrow(secondRs::close);
253+
Assertions.assertNotNull(secondResultFilter);
254+
Assertions.assertEquals(1, secondCols);
255+
Assertions.assertFalse(secondResultFilter.mightContain("one"));
256+
Assertions.assertTrue(secondResultFilter.mightContain("neo"));
257+
Assertions.assertTrue(secondResultFilter.expectedFpp() <= 0.01D);
258+
Assertions.assertDoesNotThrow(secondRs::close);
259+
}
243260
});
244261
}
245262

@@ -273,36 +290,40 @@ void testCorrectFilterSizeSelection() {
273290
Double fpp = sizeMap.get(size);
274291
String sql = "SELECT `filter` FROM `" + tableName + "`";
275292
Assertions.assertDoesNotThrow(() -> {
276-
ResultSet rs = conn.prepareStatement(sql).executeQuery();
277-
int cols = rs.getMetaData().getColumnCount();
278-
BloomFilter resultFilter = emptyFilter;
279-
int loops = 0;
280-
while (rs.next()) {
281-
loops++;
282-
byte[] bytes = rs.getBytes(1);
283-
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
284-
resultFilter = BloomFilter.readFrom(bais);
293+
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
294+
ResultSet rs = stmt.executeQuery();
295+
int cols = rs.getMetaData().getColumnCount();
296+
BloomFilter resultFilter = emptyFilter;
297+
int loops = 0;
298+
while (rs.next()) {
299+
loops++;
300+
byte[] bytes = rs.getBytes(1);
301+
ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
302+
resultFilter = BloomFilter.readFrom(bais);
303+
}
304+
Assertions.assertEquals(1, loops);
305+
Assertions.assertNotNull(resultFilter);
306+
Assertions.assertEquals(1, cols);
307+
Assertions.assertTrue(resultFilter.mightContain("one"));
308+
Assertions.assertFalse(resultFilter.mightContain("neo"));
309+
Assertions.assertTrue(resultFilter.expectedFpp() <= fpp);
310+
Assertions.assertDoesNotThrow(rs::close);
285311
}
286-
Assertions.assertEquals(1, loops);
287-
Assertions.assertNotNull(resultFilter);
288-
Assertions.assertEquals(1, cols);
289-
Assertions.assertTrue(resultFilter.mightContain("one"));
290-
Assertions.assertFalse(resultFilter.mightContain("neo"));
291-
Assertions.assertTrue(resultFilter.expectedFpp() <= fpp);
292-
Assertions.assertDoesNotThrow(rs::close);
293312
});
294313
}
295314

296315
@Test
297316
public void testPatternSavedToDatabase() {
298317
String sql = "SELECT `pattern` FROM `filtertype` GROUP BY `pattern`";
299318
Assertions.assertDoesNotThrow(() -> {
300-
ResultSet rs = conn.prepareStatement(sql).executeQuery();
301-
String pattern = "";
302-
while (rs.next()) {
303-
pattern = rs.getString(1);
319+
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
320+
ResultSet rs = stmt.executeQuery();
321+
String pattern = "";
322+
while (rs.next()) {
323+
pattern = rs.getString(1);
324+
}
325+
Assertions.assertEquals(this.pattern, pattern);
304326
}
305-
Assertions.assertEquals(this.pattern, pattern);
306327
});
307328
}
308329

0 commit comments

Comments
 (0)