提交 c23bb0ed authored 作者: Noel Grandin's avatar Noel Grandin

close a couple of resource leaks

上级 c1087334
...@@ -11,7 +11,6 @@ import java.io.LineNumberReader; ...@@ -11,7 +11,6 @@ import java.io.LineNumberReader;
import java.sql.Connection; import java.sql.Connection;
import java.sql.ResultSet; import java.sql.ResultSet;
import java.sql.SQLException; import java.sql.SQLException;
import org.h2.test.TestBase; import org.h2.test.TestBase;
import org.h2.util.ScriptReader; import org.h2.util.ScriptReader;
...@@ -43,34 +42,34 @@ public class TestScriptSimple extends TestBase { ...@@ -43,34 +42,34 @@ public class TestScriptSimple extends TestBase {
InputStream is = getClass().getClassLoader().getResourceAsStream(inFile); InputStream is = getClass().getClassLoader().getResourceAsStream(inFile);
LineNumberReader lineReader = new LineNumberReader( LineNumberReader lineReader = new LineNumberReader(
new InputStreamReader(is, "Cp1252")); new InputStreamReader(is, "Cp1252"));
ScriptReader reader = new ScriptReader(lineReader); try (ScriptReader reader = new ScriptReader(lineReader)) {
while (true) { while (true) {
String sql = reader.readStatement(); String sql = reader.readStatement();
if (sql == null) { if (sql == null) {
break; break;
} }
sql = sql.trim(); sql = sql.trim();
try { try {
if ("@reconnect".equals(sql.toLowerCase())) { if ("@reconnect".equals(sql.toLowerCase())) {
reconnect(); reconnect();
} else if (sql.length() == 0) { } else if (sql.length() == 0) {
// ignore // ignore
} else if (sql.toLowerCase().startsWith("select")) { } else if (sql.toLowerCase().startsWith("select")) {
ResultSet rs = conn.createStatement().executeQuery(sql); ResultSet rs = conn.createStatement().executeQuery(sql);
while (rs.next()) { while (rs.next()) {
String expected = reader.readStatement().trim(); String expected = reader.readStatement().trim();
String got = "> " + rs.getString(1); String got = "> " + rs.getString(1);
assertEquals(sql, expected, got); assertEquals(sql, expected, got);
}
} else {
conn.createStatement().execute(sql);
} }
} else { } catch (SQLException e) {
conn.createStatement().execute(sql); System.out.println(sql);
throw e;
} }
} catch (SQLException e) {
System.out.println(sql);
throw e;
} }
} }
is.close();
conn.close(); conn.close();
deleteDb("scriptSimple"); deleteDb("scriptSimple");
} }
......
...@@ -46,16 +46,17 @@ public class TestScriptReader extends TestBase { ...@@ -46,16 +46,17 @@ public class TestScriptReader extends TestBase {
} }
String s = buff.toString(); String s = buff.toString();
StringReader reader = new StringReader(s); StringReader reader = new StringReader(s);
ScriptReader source = new ScriptReader(reader); try (ScriptReader source = new ScriptReader(reader)) {
for (int j = 0; j < l; j++) { for (int j = 0; j < l; j++) {
String e = source.readStatement(); String e = source.readStatement();
String c = sql[j]; String c = sql[j];
if (c.length() == 0 && j == l - 1) { if (c.length() == 0 && j == l - 1) {
c = null; c = null;
}
assertEquals(c, e);
} }
assertEquals(c, e); assertEquals(null, source.readStatement());
} }
assertEquals(null, source.readStatement());
} }
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论