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 TestDB2Dialect extends TestStandardDialect {
042: @Override
043: protected Dialect createDialect() {
044: return new DB2Dialect();
045: }
046:
047: @Override
048: @Test(dataProvider="meta-data")
049: public Collection<QualifiedName> getSequences(
050: DatabaseMetaData metaData) throws SQLException {
051: EasyMock.expect(metaData.getConnection()).andReturn(
052: this .connection);
053: EasyMock.expect(this .connection.createStatement()).andReturn(
054: this .statement);
055: EasyMock
056: .expect(
057: this .statement
058: .executeQuery("SELECT SEQSCHEMA, SEQNAME FROM SYSCAT.SEQUENCES"))
059: .andReturn(this .resultSet);
060: EasyMock.expect(this .resultSet.next()).andReturn(true);
061: EasyMock.expect(this .resultSet.getString(1)).andReturn(
062: "schema1");
063: EasyMock.expect(this .resultSet.getString(2)).andReturn(
064: "sequence1");
065: EasyMock.expect(this .resultSet.next()).andReturn(true);
066: EasyMock.expect(this .resultSet.getString(1)).andReturn(
067: "schema2");
068: EasyMock.expect(this .resultSet.getString(2)).andReturn(
069: "sequence2");
070: EasyMock.expect(this .resultSet.next()).andReturn(false);
071:
072: this .statement.close();
073:
074: this .replay();
075:
076: Collection<QualifiedName> sequences = this .dialect
077: .getSequences(metaData);
078:
079: this .verify();
080:
081: assert sequences.size() == 2 : sequences.size();
082:
083: Iterator<QualifiedName> iterator = sequences.iterator();
084: QualifiedName sequence = iterator.next();
085: String schema = sequence.getSchema();
086: String name = sequence.getName();
087:
088: assert schema.equals("schema1") : schema;
089: assert name.equals("sequence1") : name;
090:
091: sequence = iterator.next();
092: schema = sequence.getSchema();
093: name = sequence.getName();
094:
095: assert schema.equals("schema2") : schema;
096: assert name.equals("sequence2") : name;
097:
098: return sequences;
099: }
100:
101: @Override
102: @Test(dataProvider="sequence")
103: public String getNextSequenceValueSQL(SequenceProperties sequence)
104: throws SQLException {
105: EasyMock.expect(sequence.getName()).andReturn("sequence");
106:
107: this .replay();
108:
109: String sql = this .dialect.getNextSequenceValueSQL(sequence);
110:
111: this .verify();
112:
113: assert sql.equals("VALUES NEXTVAL FOR sequence") : sql;
114:
115: return sql;
116: }
117:
118: @Override
119: @Test
120: public String getSimpleSQL() throws SQLException {
121: this .replay();
122:
123: String sql = this .dialect.getSimpleSQL();
124:
125: this .verify();
126:
127: assert sql.equals("VALUES CURRENT_TIMESTAMP") : sql;
128:
129: return sql;
130: }
131:
132: @Override
133: @DataProvider(name="sequence-sql")
134: Object[][] sequenceSQLProvider() {
135: return new Object[][] {
136: new Object[] { "VALUES NEXTVAL FOR success" },
137: new Object[] { "VALUES PREVVAL FOR success" },
138: new Object[] { "INSERT INTO table VALUES (NEXTVAL FOR success, 0)" },
139: new Object[] { "INSERT INTO table VALUES (PREVVAL FOR success, 0)" },
140: new Object[] { "UPDATE table SET id = NEXTVAL FOR success" },
141: new Object[] { "UPDATE table SET id = PREVVAL FOR success" },
142: new Object[] { "SELECT * FROM table" }, };
143: }
144:
145: @Override
146: @Test(dataProvider="current-date")
147: public String evaluateCurrentDate(String sql, java.sql.Date date)
148: throws SQLException {
149: String expected = sql.contains("success") ? "SELECT '"
150: + date.toString() + "' FROM success" : sql;
151:
152: String evaluated = this .dialect.evaluateCurrentDate(sql, date);
153:
154: assert evaluated.equals(expected) : evaluated;
155:
156: return evaluated;
157: }
158:
159: @Override
160: @Test(dataProvider="current-time")
161: public String evaluateCurrentTime(String sql, java.sql.Time date)
162: throws SQLException {
163: String expected = sql.contains("success") ? "SELECT '"
164: + date.toString() + "' FROM success" : sql;
165:
166: String evaluated = this .dialect.evaluateCurrentTime(sql, date);
167:
168: assert evaluated.equals(expected) : evaluated;
169:
170: return evaluated;
171: }
172:
173: @Override
174: @Test(dataProvider="current-timestamp")
175: public String evaluateCurrentTimestamp(String sql,
176: java.sql.Timestamp date) throws SQLException {
177: String expected = sql.contains("success") ? "SELECT '"
178: + date.toString() + "' FROM success" : sql;
179:
180: String evaluated = this.dialect.evaluateCurrentTimestamp(sql,
181: date);
182:
183: assert evaluated.equals(expected) : evaluated;
184:
185: return evaluated;
186: }
187: }
|