01: /*
02: * InsertAnalyzerTest.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.gui.completion;
13:
14: import java.sql.SQLException;
15: import java.sql.Statement;
16: import java.util.List;
17: import junit.framework.TestCase;
18: import workbench.TestUtil;
19: import workbench.db.ColumnIdentifier;
20: import workbench.db.TableIdentifier;
21: import workbench.db.WbConnection;
22: import workbench.util.SqlUtil;
23:
24: /**
25: *
26: * @author thomas
27: */
28: public class InsertAnalyzerTest extends TestCase {
29: private TestUtil util;
30:
31: public InsertAnalyzerTest(String testName) {
32: super (testName);
33: }
34:
35: protected void setUp() throws Exception {
36: super .setUp();
37: util = new TestUtil("InsertAnalyzerTest");
38: }
39:
40: protected void tearDown() throws Exception {
41: util.emptyBaseDirectory();
42: super .tearDown();
43: }
44:
45: public void testCheckContext() {
46: WbConnection con = null;
47: try {
48: con = util.getConnection("insert_completion_test");
49: prepareDatabase(con);
50: String sql = "insert into ";
51: int pos = sql.length() - 1;
52: StatementContext context = new StatementContext(con, sql,
53: pos);
54: assertTrue(context.isStatementSupported());
55: BaseAnalyzer analyzer = context.getAnalyzer();
56: assertTrue(analyzer instanceof InsertAnalyzer);
57: List tables = analyzer.getData();
58: assertEquals(3, tables.size());
59: Object t1 = tables.get(0);
60: assertTrue(t1 instanceof TableIdentifier);
61: TableIdentifier tbl = (TableIdentifier) t1;
62: assertEquals("one", tbl.getTableName().toLowerCase());
63:
64: sql = "insert into one ( )";
65: pos = sql.length() - 2;
66: context = new StatementContext(con, sql, pos);
67: assertTrue(context.isStatementSupported());
68: analyzer = context.getAnalyzer();
69: assertTrue(analyzer instanceof InsertAnalyzer);
70: List columns = analyzer.getData();
71: assertEquals(3, tables.size());
72:
73: Object c1 = columns.get(0);
74: assertTrue(c1 instanceof ColumnIdentifier);
75: ColumnIdentifier col = (ColumnIdentifier) c1;
76: assertEquals("firstname", col.getColumnName().toLowerCase());
77: } catch (Exception e) {
78: e.printStackTrace();
79: fail(e.getMessage());
80: }
81: }
82:
83: private void prepareDatabase(WbConnection con) throws SQLException {
84: Statement stmt = null;
85: try {
86: stmt = con.createStatement();
87: stmt
88: .executeUpdate("create table one (id1 integer, firstname varchar(100), lastname varchar(100))");
89: stmt
90: .executeUpdate("create table two (id2 integer, some_data varchar(100))");
91: stmt
92: .executeUpdate("create table three (id3 integer, more_data varchar(100))");
93: } finally {
94: SqlUtil.closeStatement(stmt);
95: }
96: }
97: }
|