001: /*
002: * StatementFactoryTest.java
003: *
004: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
005: *
006: * Copyright 2002-2008, Thomas Kellerer
007: * No part of this code maybe reused without the permission of the author
008: *
009: * To contact the author please send an email to: support@sql-workbench.net
010: *
011: */
012: package workbench.storage;
013:
014: import java.sql.Types;
015: import junit.framework.*;
016: import workbench.TestUtil;
017: import workbench.db.TableIdentifier;
018: import workbench.resource.Settings;
019:
020: /**
021: *
022: * @author support@sql-workbench.net
023: */
024: public class StatementFactoryTest extends TestCase {
025: public StatementFactoryTest(String testName) {
026: super (testName);
027: try {
028: TestUtil util = new TestUtil(testName);
029: util.prepareEnvironment();
030: } catch (Exception e) {
031: e.printStackTrace();
032: }
033: }
034:
035: public void setUp() throws Exception {
036: Settings.getInstance().setDoFormatInserts(false);
037: Settings.getInstance().setDoFormatUpdates(false);
038: }
039:
040: public void testCreateUpdateStatement() {
041: try {
042: String[] cols = new String[] { "key", "section",
043: "firstname", "lastname" };
044: int[] types = new int[] { Types.INTEGER, Types.VARCHAR,
045: Types.VARCHAR, Types.VARCHAR };
046: ResultInfo info = new ResultInfo(cols, types, null);
047: info.setIsPkColumn(0, true);
048: info.setIsPkColumn(1, true);
049: TableIdentifier table = new TableIdentifier("person");
050:
051: info.setUpdateTable(table);
052: StatementFactory factory = new StatementFactory(info, null);
053: RowData data = new RowData(info.getColumnCount());
054: data.setValue(0, new Integer(42));
055: data.setValue(1, "start");
056: data.setValue(2, "Zaphod");
057: data.setValue(3, "Bla");
058: data.resetStatus();
059:
060: data.setValue(2, "Beeblebrox");
061:
062: DmlStatement stmt = factory.createUpdateStatement(data,
063: false, "\n");
064: String sql = stmt.toString();
065: assertEquals(true, sql.startsWith("UPDATE"));
066:
067: SqlLiteralFormatter formatter = new SqlLiteralFormatter();
068: sql = stmt.getExecutableStatement(formatter).toString();
069: assertEquals(true, sql.indexOf("key = 42") > -1);
070: assertEquals(true, sql.indexOf("section = 'start'") > -1);
071: } catch (Exception e) {
072: e.printStackTrace();
073: fail(e.getMessage());
074: }
075: }
076:
077: public void testCreateInsertStatement() {
078: try {
079: String[] cols = new String[] { "key", "firstname",
080: "lastname" };
081: int[] types = new int[] { Types.INTEGER, Types.VARCHAR,
082: Types.VARCHAR };
083:
084: ResultInfo info = new ResultInfo(cols, types, null);
085: info.setIsPkColumn(0, true);
086: TableIdentifier table = new TableIdentifier("person");
087:
088: info.setUpdateTable(table);
089: StatementFactory factory = new StatementFactory(info, null);
090: RowData data = new RowData(3);
091: data.setValue(0, new Integer(42));
092: data.setValue(1, "Zaphod");
093: data.setValue(2, "Beeblebrox");
094:
095: DmlStatement stmt = factory.createInsertStatement(data,
096: false, "\n");
097: String sql = stmt.toString();
098: assertEquals("Not an INSERT statement", true, sql
099: .startsWith("INSERT"));
100:
101: SqlLiteralFormatter formatter = new SqlLiteralFormatter();
102: sql = stmt.getExecutableStatement(formatter).toString();
103: assertEquals(
104: "Wrong values inserted",
105: true,
106: sql.indexOf("VALUES (42, 'Zaphod', 'Beeblebrox')") > -1);
107: } catch (Exception e) {
108: e.printStackTrace();
109: fail(e.getMessage());
110: }
111: }
112:
113: public void testCreateInsertIdentityStatement() {
114: try {
115: String[] cols = new String[] { "key", "firstname",
116: "lastname" };
117: int[] types = new int[] { Types.INTEGER, Types.VARCHAR,
118: Types.VARCHAR };
119:
120: ResultInfo info = new ResultInfo(cols, types, null);
121: info.setIsPkColumn(0, true);
122: info.getColumn(0).setDbmsType("identity");
123: TableIdentifier table = new TableIdentifier("person");
124:
125: info.setUpdateTable(table);
126: StatementFactory factory = new StatementFactory(info, null);
127: RowData data = new RowData(3);
128: data.setValue(0, new Integer(42));
129: data.setValue(1, "Zaphod");
130: data.setValue(2, "Beeblebrox");
131:
132: Settings.getInstance().setFormatInsertIgnoreIdentity(true);
133: DmlStatement stmt = factory.createInsertStatement(data,
134: false, "\n");
135: String sql = stmt.toString();
136: assertEquals("Not an INSERT statement", true, sql
137: .startsWith("INSERT"));
138:
139: SqlLiteralFormatter formatter = new SqlLiteralFormatter();
140: sql = stmt.getExecutableStatement(formatter).toString();
141: assertEquals("Wrong values inserted", true, sql
142: .indexOf("VALUES ('Zaphod', 'Beeblebrox')") > -1);
143:
144: Settings.getInstance().setFormatInsertIgnoreIdentity(false);
145: stmt = factory.createInsertStatement(data, false, "\n");
146: sql = stmt.getExecutableStatement(formatter).toString();
147: assertEquals(
148: "Wrong values inserted",
149: true,
150: sql.indexOf("VALUES (42, 'Zaphod', 'Beeblebrox')") > -1);
151: } catch (Exception e) {
152: e.printStackTrace();
153: fail(e.getMessage());
154: }
155: }
156:
157: public void testCreateDeleteStatement() {
158: try {
159: String[] cols = new String[] { "key", "value", "firstname",
160: "lastname" };
161: int[] types = new int[] { Types.INTEGER, Types.VARCHAR,
162: Types.VARCHAR, Types.VARCHAR };
163:
164: ResultInfo info = new ResultInfo(cols, types, null);
165: info.setIsPkColumn(0, true);
166: info.setIsPkColumn(1, true);
167: TableIdentifier table = new TableIdentifier("person");
168:
169: info.setUpdateTable(table);
170: StatementFactory factory = new StatementFactory(info, null);
171: RowData data = new RowData(info.getColumnCount());
172: data.setValue(0, new Integer(42));
173: data.setValue(1, "otherkey");
174: data.setValue(2, "Zaphod");
175: data.setValue(3, "Beeblebrox");
176: data.resetStatus();
177:
178: DmlStatement stmt = factory.createDeleteStatement(data,
179: false);
180: String sql = stmt.toString();
181: assertEquals("Not a delete statement", true, sql
182: .startsWith("DELETE"));
183:
184: SqlLiteralFormatter formatter = new SqlLiteralFormatter();
185: sql = stmt.getExecutableStatement(formatter).toString();
186: assertEquals("Wrong WHERE clause created", true, sql
187: .indexOf("key = 42") > -1);
188: assertEquals("Wrong WHERE clause created", true, sql
189: .indexOf("value = 'otherkey'") > -1);
190: } catch (Exception e) {
191: e.printStackTrace();
192: fail(e.getMessage());
193: }
194: }
195: }
|