01: /*
02: * PreparedStatementPoolTest.java
03: *
04: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
05: *
06: * Copyright 2002-2008, Thomas Kellerer
07: * No part of this code maybe reused without the permission of the author
08: *
09: * To contact the author please send an email to: support@sql-workbench.net
10: *
11: */
12: package workbench.sql.preparedstatement;
13:
14: import java.sql.Statement;
15: import junit.framework.TestCase;
16: import workbench.TestUtil;
17: import workbench.db.ConnectionMgr;
18: import workbench.db.WbConnection;
19:
20: /**
21: *
22: * @author support@sql-workbench.net
23: */
24: public class PreparedStatementPoolTest extends TestCase {
25:
26: public PreparedStatementPoolTest(String testName) {
27: super (testName);
28: }
29:
30: public void testPool() {
31: TestUtil util = new TestUtil("PreparedStatementPoolTest");
32: try {
33: util.prepareEnvironment();
34: // Still using HSQLDB as H2 does not implement getParameterMetaData() correctly
35: WbConnection con = util.getHSQLConnection("testPool");
36: //WbConnection con = util.getConnection("testPool");
37: Statement stmt = con.createStatement();
38: stmt
39: .executeUpdate("CREATE TABLE prep_test (nr integer, name varchar(100))");
40: PreparedStatementPool pool = new PreparedStatementPool(con);
41: boolean added = pool
42: .addPreparedStatement("select * from prep_test");
43: assertEquals("Statement without parameters was added",
44: false, added);
45:
46: added = pool
47: .addPreparedStatement("select * from prep_test where nr = '?'");
48: assertEquals("Statement without parameters was added",
49: false, added);
50:
51: String insert = "insert into prep_test (nr, name) values (?,?)";
52: added = pool.addPreparedStatement(insert);
53: assertEquals("INSERT statement was not added", true, added);
54:
55: String update = "update prep_test set name = 'test' where nr = ?";
56: added = pool.addPreparedStatement(update);
57: assertEquals("UPDATE statement was not added", true, added);
58:
59: StatementParameters p = pool.getParameters(insert);
60: assertEquals("Incorrect number of parameters", 2, p
61: .getParameterCount());
62: assertEquals("Incorrect first parameter type",
63: java.sql.Types.INTEGER, p.getParameterType(0));
64: assertEquals("Incorrect second parameter type",
65: java.sql.Types.VARCHAR, p.getParameterType(1));
66:
67: p = pool.getParameters(update);
68: assertEquals("Incorrect number of parameters", 1, p
69: .getParameterCount());
70: assertEquals("Incorrect parameter type",
71: java.sql.Types.INTEGER, p.getParameterType(0));
72: } catch (Exception e) {
73: e.printStackTrace();
74: fail(e.getMessage());
75: } finally {
76: ConnectionMgr.getInstance().disconnectAll();
77: }
78: }
79:
80: }
|