001: /*
002: * HA-JDBC: High-Availability JDBC
003: * Copyright (c) 2004-2007 Paul Ferraro
004: *
005: * This library is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU Lesser General Public License as published by the
007: * Free Software Foundation; either version 2.1 of the License, or (at your
008: * option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful, but WITHOUT
011: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
012: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
013: * for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public License
016: * along with this library; if not, write to the Free Software Foundation,
017: * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *
019: * Contact: ferraro@users.sourceforge.net
020: */
021: package net.sf.hajdbc.dialect;
022:
023: import java.sql.DatabaseMetaData;
024: import java.sql.SQLException;
025: import java.util.Collection;
026: import java.util.Iterator;
027:
028: import net.sf.hajdbc.Dialect;
029: import net.sf.hajdbc.ForeignKeyConstraint;
030: import net.sf.hajdbc.QualifiedName;
031: import net.sf.hajdbc.SequenceProperties;
032: import net.sf.hajdbc.TableProperties;
033:
034: import org.easymock.EasyMock;
035: import org.testng.annotations.DataProvider;
036: import org.testng.annotations.Test;
037:
038: /**
039: * @author Paul Ferraro
040: *
041: */
042: @SuppressWarnings("nls")
043: public class TestMaxDBDialect extends TestStandardDialect {
044: /**
045: * @see net.sf.hajdbc.dialect.TestStandardDialect#createDialect()
046: */
047: @Override
048: protected Dialect createDialect() {
049: return new MaxDBDialect();
050: }
051:
052: @Override
053: @Test(dataProvider="foreign-key")
054: public String getCreateForeignKeyConstraintSQL(
055: ForeignKeyConstraint constraint) throws SQLException {
056: this .replay();
057:
058: String sql = this .dialect
059: .getCreateForeignKeyConstraintSQL(constraint);
060:
061: this .verify();
062:
063: assert sql
064: .equals("ALTER TABLE table ADD CONSTRAINT name FOREIGN KEY (column1, column2) REFERENCES foreign_table (foreign_column1, foreign_column2) ON DELETE CASCADE") : sql;
065:
066: return sql;
067: }
068:
069: @Override
070: @Test(dataProvider="meta-data")
071: public Collection<QualifiedName> getSequences(
072: DatabaseMetaData metaData) throws SQLException {
073: EasyMock.expect(metaData.getConnection()).andReturn(
074: this .connection);
075: EasyMock.expect(this .connection.createStatement()).andReturn(
076: this .statement);
077: EasyMock
078: .expect(
079: this .statement
080: .executeQuery("SELECT SEQUENCE_NAME FROM USER_SEQUENCES"))
081: .andReturn(this .resultSet);
082: EasyMock.expect(this .resultSet.next()).andReturn(true);
083: EasyMock.expect(this .resultSet.getString(1)).andReturn(
084: "sequence1");
085: EasyMock.expect(this .resultSet.next()).andReturn(true);
086: EasyMock.expect(this .resultSet.getString(1)).andReturn(
087: "sequence2");
088: EasyMock.expect(this .resultSet.next()).andReturn(false);
089:
090: this .statement.close();
091:
092: this .replay();
093:
094: Collection<QualifiedName> sequences = this .dialect
095: .getSequences(metaData);
096:
097: this .verify();
098:
099: assert sequences.size() == 2 : sequences.size();
100:
101: Iterator<QualifiedName> iterator = sequences.iterator();
102: QualifiedName sequence = iterator.next();
103: String schema = sequence.getSchema();
104: String name = sequence.getName();
105:
106: assert schema == null : schema;
107: assert name.equals("sequence1") : name;
108:
109: sequence = iterator.next();
110: schema = sequence.getSchema();
111: name = sequence.getName();
112:
113: assert schema == null : schema;
114: assert name.equals("sequence2") : name;
115:
116: return sequences;
117: }
118:
119: @Override
120: @Test
121: public String getSimpleSQL() throws SQLException {
122: this .replay();
123:
124: String sql = this .dialect.getSimpleSQL();
125:
126: this .verify();
127:
128: assert sql.equals("SELECT SYSDATE FROM DUAL") : sql;
129:
130: return sql;
131: }
132:
133: @Override
134: @Test(dataProvider="table")
135: public String getTruncateTableSQL(TableProperties properties)
136: throws SQLException {
137: EasyMock.expect(properties.getName()).andReturn("table");
138:
139: this .replay();
140:
141: String sql = this .dialect.getTruncateTableSQL(properties);
142:
143: this .verify();
144:
145: assert sql.equals("TRUNCATE TABLE table");
146:
147: return sql;
148: }
149:
150: @Override
151: @DataProvider(name="sequence-sql")
152: Object[][] sequenceSQLProvider() {
153: return new Object[][] {
154: new Object[] { "SELECT success.nextval" },
155: new Object[] { "SELECT success.currval" },
156: new Object[] { "SELECT success.nextval, * FROM table" },
157: new Object[] { "SELECT success.currval, * FROM table" },
158: new Object[] { "INSERT INTO table VALUES (success.nextval, 0)" },
159: new Object[] { "INSERT INTO table VALUES (success.currval, 0)" },
160: new Object[] { "UPDATE table SET id = success.nextval" },
161: new Object[] { "UPDATE table SET id = success.currval" },
162: new Object[] { "SELECT * FROM table" }, };
163: }
164:
165: @Override
166: @Test(dataProvider="insert-table-sql")
167: public String parseInsertTable(String sql) throws SQLException {
168: this .replay();
169:
170: String table = this .dialect.parseInsertTable(sql);
171:
172: this .verify();
173:
174: assert (table == null) : table;
175:
176: return table;
177: }
178:
179: @Override
180: @Test(dataProvider="sequence")
181: public String getNextSequenceValueSQL(SequenceProperties sequence)
182: throws SQLException {
183: EasyMock.expect(sequence.getName()).andReturn("sequence");
184:
185: this .replay();
186:
187: String sql = this .dialect.getNextSequenceValueSQL(sequence);
188:
189: this .verify();
190:
191: assert sql.equals("SELECT sequence.NEXTVAL FROM DUAL") : sql;
192:
193: return sql;
194: }
195: }
|