01: /*
02: * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
03: * (license2)
04: * Initial Developer: H2 Group
05: */
06: package org.h2.test.db;
07:
08: import java.io.InputStream;
09: import java.io.InputStreamReader;
10: import java.io.LineNumberReader;
11: import java.sql.Connection;
12: import java.sql.ResultSet;
13: import java.sql.SQLException;
14:
15: import org.h2.test.TestBase;
16: import org.h2.util.ScriptReader;
17:
18: /**
19: * This test runs a simple SQL script file and compares the output with the
20: * expected output.
21: */
22: public class TestScriptSimple extends TestBase {
23:
24: private Connection conn;
25:
26: public void test() throws Exception {
27: if (config.memory || config.big || config.networked) {
28: return;
29: }
30: deleteDb("scriptSimple");
31: reconnect();
32: String inFile = "org/h2/test/testSimple.in.txt";
33: InputStream is = getClass().getClassLoader()
34: .getResourceAsStream(inFile);
35: LineNumberReader lineReader = new LineNumberReader(
36: new InputStreamReader(is, "Cp1252"));
37: ScriptReader reader = new ScriptReader(lineReader);
38: while (true) {
39: String sql = reader.readStatement();
40: if (sql == null) {
41: break;
42: }
43: sql = sql.trim();
44: try {
45: if ("@reconnect".equals(sql.toLowerCase())) {
46: reconnect();
47: } else if (sql.length() == 0) {
48: // ignore
49: } else if (sql.toLowerCase().startsWith("select")) {
50: ResultSet rs = conn.createStatement().executeQuery(
51: sql);
52: while (rs.next()) {
53: String expected = reader.readStatement().trim();
54: String got = "> " + rs.getString(1);
55: check(expected, got);
56: }
57: } else {
58: conn.createStatement().execute(sql);
59: }
60: } catch (SQLException e) {
61: System.out.println(sql);
62: throw e;
63: }
64: }
65: is.close();
66: conn.close();
67: }
68:
69: private void reconnect() throws Exception {
70: if (conn != null) {
71: conn.close();
72: }
73: conn = getConnection("scriptSimple");
74: }
75:
76: }
|