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:
033: import org.easymock.EasyMock;
034: import org.testng.annotations.DataProvider;
035: import org.testng.annotations.Test;
036:
037: /**
038: * @author Paul Ferraro
039: *
040: */
041: @SuppressWarnings("nls")
042: public class TestHSQLDBDialect extends TestStandardDialect {
043: @Override
044: protected Dialect createDialect() {
045: return new HSQLDBDialect();
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 ON UPDATE RESTRICT") : 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_SCHEMA, SEQUENCE_NAME FROM INFORMATION_SCHEMA.SYSTEM_SEQUENCES"))
077: .andReturn(this .resultSet);
078: EasyMock.expect(this .resultSet.next()).andReturn(true);
079: EasyMock.expect(this .resultSet.getString(1)).andReturn(
080: "schema1");
081: EasyMock.expect(this .resultSet.getString(2)).andReturn(
082: "sequence1");
083: EasyMock.expect(this .resultSet.next()).andReturn(true);
084: EasyMock.expect(this .resultSet.getString(1)).andReturn(
085: "schema2");
086: EasyMock.expect(this .resultSet.getString(2)).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;
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.equals("schema1") : schema;
107: assert name.equals("sequence1") : name;
108:
109: sequence = iterator.next();
110: schema = sequence.getSchema();
111: name = sequence.getName();
112:
113: assert schema.equals("schema2") : 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("CALL CURRENT_TIMESTAMP") : sql;
129:
130: return sql;
131: }
132:
133: @Test(dataProvider="sequence")
134: @Override
135: public String getNextSequenceValueSQL(SequenceProperties sequence)
136: throws SQLException {
137: EasyMock.expect(sequence.getName()).andReturn("sequence");
138:
139: this .replay();
140:
141: String sql = this .dialect.getNextSequenceValueSQL(sequence);
142:
143: this .verify();
144:
145: assert sql.equals("CALL NEXT VALUE FOR sequence") : sql;
146:
147: return sql;
148: }
149:
150: @Override
151: @DataProvider(name="current-date")
152: Object[][] currentDateProvider() {
153: java.sql.Date date = new java.sql.Date(System
154: .currentTimeMillis());
155:
156: return new Object[][] {
157: new Object[] { "SELECT CURRENT_DATE FROM success", date },
158: new Object[] { "SELECT CURDATE() FROM success", date },
159: new Object[] { "SELECT CURDATE ( ) FROM success", date },
160: new Object[] { "SELECT CCURRENT_DATE FROM failure",
161: date },
162: new Object[] { "SELECT CURRENT_DATES FROM failure",
163: date },
164: new Object[] { "SELECT CCURDATE() FROM failure", date },
165: new Object[] { "SELECT 1 FROM failure", date }, };
166: }
167:
168: @Override
169: @DataProvider(name="current-time")
170: Object[][] currentTimeProvider() {
171: java.sql.Time date = new java.sql.Time(System
172: .currentTimeMillis());
173:
174: return new Object[][] {
175: new Object[] { "SELECT CURRENT_TIME FROM success", date },
176: new Object[] { "SELECT CURTIME() FROM success", date },
177: new Object[] { "SELECT CURTIME ( ) FROM success", date },
178: new Object[] { "SELECT CURRENT_TIMESTAMP FROM failure",
179: date },
180: new Object[] { "SELECT CCURRENT_TIME FROM failure",
181: date },
182: new Object[] { "SELECT CCURTIME() FROM failure", date },
183: new Object[] { "SELECT 1 FROM failure", date }, };
184: }
185:
186: @Override
187: @DataProvider(name="current-timestamp")
188: Object[][] currentTimestampProvider() {
189: java.sql.Timestamp date = new java.sql.Timestamp(System
190: .currentTimeMillis());
191:
192: return new Object[][] {
193: new Object[] { "SELECT CURRENT_TIMESTAMP FROM success",
194: date },
195: new Object[] { "SELECT NOW() FROM success", date },
196: new Object[] { "SELECT NOW ( ) FROM success", date },
197: new Object[] {
198: "SELECT CCURRENT_TIMESTAMP FROM failure", date },
199: new Object[] {
200: "SELECT CURRENT_TIMESTAMPS FROM failure", date },
201: new Object[] { "SELECT NNOW() FROM failure", date },
202: new Object[] { "SELECT 1 FROM failure", date }, };
203: }
204: }
|