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: @SuppressWarnings("nls")
041: public class TestH2Dialect extends TestStandardDialect {
042: @Override
043: protected Dialect createDialect() {
044: return new H2Dialect();
045: }
046:
047: @Override
048: @Test(dataProvider="foreign-key")
049: public String getCreateForeignKeyConstraintSQL(
050: ForeignKeyConstraint constraint) throws SQLException {
051: this .replay();
052:
053: String sql = this .dialect
054: .getCreateForeignKeyConstraintSQL(constraint);
055:
056: this .verify();
057:
058: assert sql
059: .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;
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 SEQUENCE_SCHEMA, SEQUENCE_NAME FROM INFORMATION_SCHEMA.SYSTEM_SEQUENCES"))
076: .andReturn(this .resultSet);
077: EasyMock.expect(this .resultSet.next()).andReturn(true);
078: EasyMock.expect(this .resultSet.getString(1)).andReturn(
079: "schema1");
080: EasyMock.expect(this .resultSet.getString(2)).andReturn(
081: "sequence1");
082: EasyMock.expect(this .resultSet.next()).andReturn(true);
083: EasyMock.expect(this .resultSet.getString(1)).andReturn(
084: "schema2");
085: EasyMock.expect(this .resultSet.getString(2)).andReturn(
086: "sequence2");
087: EasyMock.expect(this .resultSet.next()).andReturn(false);
088:
089: this .statement.close();
090:
091: this .replay();
092:
093: Collection<QualifiedName> sequences = this .dialect
094: .getSequences(metaData);
095:
096: this .verify();
097:
098: assert sequences.size() == 2 : sequences;
099:
100: Iterator<QualifiedName> iterator = sequences.iterator();
101: QualifiedName sequence = iterator.next();
102: String schema = sequence.getSchema();
103: String name = sequence.getName();
104:
105: assert schema.equals("schema1") : schema;
106: assert name.equals("sequence1") : name;
107:
108: sequence = iterator.next();
109: schema = sequence.getSchema();
110: name = sequence.getName();
111:
112: assert schema.equals("schema2") : schema;
113: assert name.equals("sequence2") : name;
114:
115: return sequences;
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("CALL CURRENT_TIMESTAMP") : sql;
128:
129: return sql;
130: }
131:
132: @Test(dataProvider="sequence")
133: @Override
134: public String getNextSequenceValueSQL(SequenceProperties sequence)
135: throws SQLException {
136: EasyMock.expect(sequence.getName()).andReturn("sequence");
137:
138: this .replay();
139:
140: String sql = this .dialect.getNextSequenceValueSQL(sequence);
141:
142: this .verify();
143:
144: assert sql.equals("CALL NEXT VALUE FOR sequence") : sql;
145:
146: return sql;
147: }
148:
149: @Override
150: @DataProvider(name="current-date")
151: Object[][] currentDateProvider() {
152: java.sql.Date date = new java.sql.Date(System
153: .currentTimeMillis());
154:
155: return new Object[][] {
156: new Object[] { "SELECT CURRENT_DATE FROM success", date },
157: new Object[] { "SELECT CURRENT_DATE() FROM success",
158: date },
159: new Object[] { "SELECT CURRENT_DATE ( ) FROM success",
160: date },
161: new Object[] { "SELECT CURDATE() FROM success", date },
162: new Object[] { "SELECT CURDATE ( ) FROM success", date },
163: new Object[] { "SELECT CURRENT_DATES FROM failure",
164: date },
165: new Object[] { "SELECT CCURRENT_DATE FROM failure",
166: date },
167: new Object[] { "SELECT CCURDATE() FROM failure", date },
168: new Object[] { "SELECT 1 FROM failure", date }, };
169: }
170:
171: @Override
172: @DataProvider(name="current-time")
173: Object[][] currentTimeProvider() {
174: java.sql.Time date = new java.sql.Time(System
175: .currentTimeMillis());
176:
177: return new Object[][] {
178: new Object[] { "SELECT CURRENT_TIME FROM success", date },
179: new Object[] { "SELECT CURRENT_TIME() FROM success",
180: date },
181: new Object[] { "SELECT CURRENT_TIME ( ) FROM success",
182: date },
183: new Object[] { "SELECT CURTIME() FROM success", date },
184: new Object[] { "SELECT CURTIME ( ) FROM success", date },
185: new Object[] { "SELECT CCURRENT_TIME FROM failure",
186: date },
187: new Object[] { "SELECT CURRENT_TIMESTAMP FROM failure",
188: date },
189: new Object[] { "SELECT CCURTIME() FROM failure", date },
190: new Object[] { "SELECT 1 FROM failure", date }, };
191: }
192:
193: @Override
194: @DataProvider(name="current-timestamp")
195: Object[][] currentTimestampProvider() {
196: java.sql.Timestamp date = new java.sql.Timestamp(System
197: .currentTimeMillis());
198:
199: return new Object[][] {
200: new Object[] { "SELECT CURRENT_TIMESTAMP FROM success",
201: date },
202: new Object[] {
203: "SELECT CURRENT_TIMESTAMP() FROM success", date },
204: new Object[] {
205: "SELECT CURRENT_TIMESTAMP ( ) FROM success",
206: date },
207: new Object[] { "SELECT NOW() FROM success", date },
208: new Object[] { "SELECT NOW ( ) FROM success", date },
209: new Object[] {
210: "SELECT CURRENT_TIMESTAMPS FROM failure", date },
211: new Object[] {
212: "SELECT CCURRENT_TIMESTAMP FROM failure", date },
213: new Object[] { "SELECT NNOW() FROM failure", date },
214: new Object[] { "SELECT 1 FROM failure", date }, };
215: }
216: }
|