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: @SuppressWarnings("nls")
042: public class TestOracleDialect extends TestStandardDialect {
043: @Override
044: protected Dialect createDialect() {
045: return new OracleDialect();
046: }
047:
048: @Override
049: @Test(dataProvider="foreign-key")
050: public String getCreateForeignKeyConstraintSQL(
051: ForeignKeyConstraint constraint) throws SQLException {
052: this .replay();
053:
054: String sql = this .dialect
055: .getCreateForeignKeyConstraintSQL(constraint);
056:
057: this .verify();
058:
059: assert sql
060: .equals("ALTER TABLE table ADD CONSTRAINT name FOREIGN KEY (column1, column2) REFERENCES foreign_table (foreign_column1, foreign_column2) ON DELETE CASCADE") : sql;
061:
062: return sql;
063: }
064:
065: @Override
066: @Test(dataProvider="meta-data")
067: public Collection<QualifiedName> getSequences(
068: DatabaseMetaData metaData) throws SQLException {
069: EasyMock.expect(metaData.getConnection()).andReturn(
070: this .connection);
071: EasyMock.expect(this .connection.createStatement()).andReturn(
072: this .statement);
073: EasyMock
074: .expect(
075: this .statement
076: .executeQuery("SELECT SEQUENCE_NAME FROM USER_SEQUENCES"))
077: .andReturn(this .resultSet);
078: EasyMock.expect(this .resultSet.next()).andReturn(true);
079: EasyMock.expect(this .resultSet.getString(1)).andReturn(
080: "sequence1");
081: EasyMock.expect(this .resultSet.next()).andReturn(true);
082: EasyMock.expect(this .resultSet.getString(1)).andReturn(
083: "sequence2");
084: EasyMock.expect(this .resultSet.next()).andReturn(false);
085:
086: this .statement.close();
087:
088: this .replay();
089:
090: Collection<QualifiedName> sequences = this .dialect
091: .getSequences(metaData);
092:
093: this .verify();
094:
095: assert sequences.size() == 2 : sequences.size();
096:
097: Iterator<QualifiedName> iterator = sequences.iterator();
098: QualifiedName sequence = iterator.next();
099: String schema = sequence.getSchema();
100: String name = sequence.getName();
101:
102: assert schema == null : schema;
103: assert name.equals("sequence1") : name;
104:
105: sequence = iterator.next();
106: schema = sequence.getSchema();
107: name = sequence.getName();
108:
109: assert schema == null : schema;
110: assert name.equals("sequence2") : name;
111:
112: return sequences;
113: }
114:
115: @Override
116: @Test
117: public String getSimpleSQL() throws SQLException {
118: this .replay();
119:
120: String sql = this .dialect.getSimpleSQL();
121:
122: this .verify();
123:
124: assert sql.equals("SELECT CURRENT_TIMESTAMP FROM DUAL") : sql;
125:
126: return sql;
127: }
128:
129: @Override
130: @Test(dataProvider="table")
131: public String getTruncateTableSQL(TableProperties properties)
132: throws SQLException {
133: EasyMock.expect(properties.getName()).andReturn("table");
134:
135: this .replay();
136:
137: String sql = this .dialect.getTruncateTableSQL(properties);
138:
139: this .verify();
140:
141: assert sql.equals("TRUNCATE TABLE table");
142:
143: return sql;
144: }
145:
146: @Override
147: @DataProvider(name="sequence-sql")
148: Object[][] sequenceSQLProvider() {
149: return new Object[][] {
150: new Object[] { "SELECT success.nextval" },
151: new Object[] { "SELECT success.currval" },
152: new Object[] { "SELECT success.nextval, * FROM table" },
153: new Object[] { "SELECT success.currval, * FROM table" },
154: new Object[] { "INSERT INTO table VALUES (success.nextval, 0)" },
155: new Object[] { "INSERT INTO table VALUES (success.currval, 0)" },
156: new Object[] { "UPDATE table SET id = success.nextval" },
157: new Object[] { "UPDATE table SET id = success.currval" },
158: new Object[] { "SELECT * FROM table" }, };
159: }
160:
161: @Override
162: @Test(dataProvider="insert-table-sql")
163: public String parseInsertTable(String sql) throws SQLException {
164: this .replay();
165:
166: String table = this .dialect.parseInsertTable(sql);
167:
168: this .verify();
169:
170: assert (table == null) : table;
171:
172: return table;
173: }
174:
175: @Override
176: @Test(dataProvider="sequence")
177: public String getNextSequenceValueSQL(SequenceProperties sequence)
178: throws SQLException {
179: EasyMock.expect(sequence.getName()).andReturn("sequence");
180:
181: this .replay();
182:
183: String sql = this .dialect.getNextSequenceValueSQL(sequence);
184:
185: this .verify();
186:
187: assert sql.equals("SELECT sequence.NEXTVAL FROM DUAL") : sql;
188:
189: return sql;
190: }
191: }
|