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.QualifiedName;
030: import net.sf.hajdbc.SequenceProperties;
031:
032: import org.easymock.EasyMock;
033: import org.testng.annotations.DataProvider;
034: import org.testng.annotations.Test;
035:
036: /**
037: * @author Paul Ferraro
038: *
039: */
040: @SuppressWarnings("nls")
041: public class TestFirebirdDialect extends TestStandardDialect {
042: @Override
043: protected Dialect createDialect() {
044: return new FirebirdDialect();
045: }
046:
047: @Override
048: @Test(dataProvider="sequence-long")
049: public String getAlterSequenceSQL(SequenceProperties sequence,
050: long value) throws SQLException {
051: EasyMock.expect(sequence.getName()).andReturn("sequence");
052:
053: this .replay();
054:
055: String sql = this .dialect.getAlterSequenceSQL(sequence, value);
056:
057: this .verify();
058:
059: assert sql.equals("SET GENERATOR sequence TO 1000") : sql;
060:
061: return sql;
062: }
063:
064: @Override
065: @Test(dataProvider="meta-data")
066: public Collection<QualifiedName> getSequences(
067: DatabaseMetaData metaData) throws SQLException {
068: EasyMock.expect(metaData.getConnection()).andReturn(
069: this .connection);
070: EasyMock.expect(this .connection.createStatement()).andReturn(
071: this .statement);
072: EasyMock
073: .expect(
074: this .statement
075: .executeQuery("SELECT RDB$GENERATOR_NAME FROM RDB$GENERATORS"))
076: .andReturn(this .resultSet);
077: EasyMock.expect(this .resultSet.next()).andReturn(true);
078: EasyMock.expect(this .resultSet.getString(1)).andReturn(
079: "sequence1");
080: EasyMock.expect(this .resultSet.next()).andReturn(true);
081: EasyMock.expect(this .resultSet.getString(1)).andReturn(
082: "sequence2");
083: EasyMock.expect(this .resultSet.next()).andReturn(false);
084:
085: this .statement.close();
086:
087: this .replay();
088:
089: Collection<QualifiedName> sequences = this .dialect
090: .getSequences(metaData);
091:
092: this .verify();
093:
094: assert sequences.size() == 2 : sequences;
095:
096: Iterator<QualifiedName> iterator = sequences.iterator();
097: QualifiedName sequence = iterator.next();
098: String schema = sequence.getSchema();
099: String name = sequence.getName();
100:
101: assert schema == null : schema;
102: assert name.equals("sequence1") : name;
103:
104: sequence = iterator.next();
105: schema = sequence.getSchema();
106: name = sequence.getName();
107:
108: assert schema == null : schema;
109: assert name.equals("sequence2") : name;
110:
111: return sequences;
112: }
113:
114: @Override
115: @Test(dataProvider="sequence")
116: public String getNextSequenceValueSQL(SequenceProperties sequence)
117: throws SQLException {
118: EasyMock.expect(sequence.getName()).andReturn("sequence");
119:
120: this .replay();
121:
122: String sql = this .dialect.getNextSequenceValueSQL(sequence);
123:
124: this .verify();
125:
126: assert sql
127: .equals("SELECT GEN_ID(sequence, 1) FROM RDB$DATABASE") : sql;
128:
129: return sql;
130: }
131:
132: @Override
133: @Test
134: public String getSimpleSQL() throws SQLException {
135: String sql = this .dialect.getSimpleSQL();
136:
137: assert sql.equals("SELECT CURRENT_TIMESTAMP FROM RDB$DATABASE") : sql;
138:
139: return sql;
140: }
141:
142: @Override
143: @DataProvider(name="select-for-update-sql")
144: Object[][] selectForUpdateProvider() {
145: return new Object[][] {
146: new Object[] { "SELECT * FROM success WITH LOCK" },
147: new Object[] { "SELECT * FROM failure" }, };
148: }
149:
150: @Override
151: @DataProvider(name="sequence-sql")
152: Object[][] sequenceSQLProvider() {
153: return new Object[][] {
154: new Object[] { "SELECT GEN_ID(success, 1) FROM RDB$DATABASE" },
155: new Object[] { "SELECT GEN_ID(success, 1), * FROM table" },
156: new Object[] { "INSERT INTO table VALUES (GEN_ID(success, 1), 0)" },
157: new Object[] { "UPDATE table SET id = GEN_ID(success, 1)" },
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: }
|