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.List;
027:
028: import net.sf.hajdbc.ColumnProperties;
029: import net.sf.hajdbc.Dialect;
030: import net.sf.hajdbc.ForeignKeyConstraint;
031: import net.sf.hajdbc.QualifiedName;
032: import net.sf.hajdbc.TableProperties;
033: import net.sf.hajdbc.UniqueConstraint;
034:
035: import org.easymock.EasyMock;
036: import org.testng.annotations.DataProvider;
037: import org.testng.annotations.Test;
038:
039: /**
040: * @author Paul Ferraro
041: *
042: */
043: @SuppressWarnings("nls")
044: public class TestMySQLDialect extends TestStandardDialect {
045: @Override
046: protected Dialect createDialect() {
047: return new MySQLDialect();
048: }
049:
050: @Override
051: @Test(dataProvider="foreign-key")
052: public String getCreateForeignKeyConstraintSQL(
053: ForeignKeyConstraint constraint) throws SQLException {
054: this .replay();
055:
056: String sql = this .dialect
057: .getCreateForeignKeyConstraintSQL(constraint);
058:
059: this .verify();
060:
061: assert sql
062: .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;
063:
064: return sql;
065: }
066:
067: @Override
068: @Test(dataProvider="foreign-key")
069: public String getDropForeignKeyConstraintSQL(
070: ForeignKeyConstraint constraint) throws SQLException {
071: this .replay();
072:
073: String sql = this .dialect
074: .getDropForeignKeyConstraintSQL(constraint);
075:
076: this .verify();
077:
078: assert sql.equals("ALTER TABLE table DROP FOREIGN KEY name") : sql;
079:
080: return sql;
081: }
082:
083: @Override
084: @Test(dataProvider="unique-constraint")
085: public String getCreateUniqueConstraintSQL(
086: UniqueConstraint constraint) throws SQLException {
087: this .replay();
088:
089: String sql = this .dialect
090: .getCreateUniqueConstraintSQL(constraint);
091:
092: this .verify();
093:
094: assert sql
095: .equals("ALTER TABLE table ADD UNIQUE name (column1, column2)") : sql;
096:
097: return sql;
098: }
099:
100: @Override
101: @Test(dataProvider="unique-constraint")
102: public String getDropUniqueConstraintSQL(UniqueConstraint constraint)
103: throws SQLException {
104: this .replay();
105:
106: String sql = this .dialect
107: .getDropUniqueConstraintSQL(constraint);
108:
109: this .verify();
110:
111: assert sql.equals("ALTER TABLE table DROP INDEX name") : sql;
112:
113: return sql;
114: }
115:
116: @Override
117: @Test(dataProvider="sequence-sql")
118: public String parseSequence(String sql) throws SQLException {
119: this .replay();
120:
121: String sequence = this .dialect.parseSequence(sql);
122:
123: this .verify();
124:
125: assert sequence == null : sequence;
126:
127: return sequence;
128: }
129:
130: @Override
131: @Test(dataProvider="meta-data")
132: public Collection<QualifiedName> getSequences(
133: DatabaseMetaData metaData) throws SQLException {
134: this .replay();
135:
136: Collection<QualifiedName> sequences = this .dialect
137: .getSequences(metaData);
138:
139: this .verify();
140:
141: assert sequences.isEmpty() : sequences;
142:
143: return sequences;
144: }
145:
146: @Override
147: @Test(dataProvider="meta-data")
148: public List<String> getDefaultSchemas(DatabaseMetaData metaData)
149: throws SQLException {
150: EasyMock.expect(metaData.getConnection()).andReturn(
151: this .connection);
152: EasyMock.expect(this .connection.createStatement()).andReturn(
153: this .statement);
154: EasyMock.expect(
155: this .statement.executeQuery("SELECT DATABASE()"))
156: .andReturn(this .resultSet);
157: EasyMock.expect(this .resultSet.next()).andReturn(false);
158: EasyMock.expect(this .resultSet.getString(1)).andReturn(
159: "database");
160:
161: this .resultSet.close();
162: this .statement.close();
163:
164: this .replay();
165:
166: List<String> schemaList = this .dialect
167: .getDefaultSchemas(metaData);
168:
169: this .verify();
170:
171: assert schemaList.size() == 1 : schemaList.size();
172:
173: assert schemaList.get(0).equals("database") : schemaList.get(0);
174:
175: return schemaList;
176: }
177:
178: @Override
179: @Test(dataProvider="column")
180: public boolean isIdentity(ColumnProperties properties)
181: throws SQLException {
182: EasyMock.expect(properties.getNativeType()).andReturn("SERIAL");
183:
184: this .replay();
185:
186: boolean identity = this .dialect.isIdentity(properties);
187:
188: this .verify();
189:
190: assert identity;
191:
192: this .reset();
193:
194: EasyMock.expect(properties.getNativeType())
195: .andReturn("INTEGER");
196: EasyMock.expect(properties.getRemarks()).andReturn(
197: "AUTO_INCREMENT");
198:
199: this .replay();
200:
201: identity = this .dialect.isIdentity(properties);
202:
203: this .verify();
204:
205: assert identity;
206:
207: this .reset();
208:
209: EasyMock.expect(this .columnProperties.getNativeType())
210: .andReturn("INTEGER");
211: EasyMock.expect(this .columnProperties.getRemarks()).andReturn(
212: null);
213:
214: this .replay();
215:
216: identity = this .dialect.isIdentity(properties);
217:
218: this .verify();
219:
220: return identity;
221: }
222:
223: @Override
224: @Test(dataProvider="table-column-long")
225: public String getAlterIdentityColumnSQL(TableProperties table,
226: ColumnProperties column, long value) throws SQLException {
227: EasyMock.expect(table.getName()).andReturn("table");
228: EasyMock.expect(column.getName()).andReturn("column");
229:
230: this .replay();
231:
232: String sql = this .dialect.getAlterIdentityColumnSQL(table,
233: column, value);
234:
235: this .verify();
236:
237: assert sql.equals("ALTER TABLE table AUTO_INCREMENT = 1000") : sql;
238:
239: return sql;
240: }
241:
242: @Override
243: @DataProvider(name="current-date")
244: Object[][] currentDateProvider() {
245: java.sql.Date date = new java.sql.Date(System
246: .currentTimeMillis());
247:
248: return new Object[][] {
249: new Object[] { "SELECT CURRENT_DATE FROM success", date },
250: new Object[] { "SELECT CURDATE() FROM success", date },
251: new Object[] { "SELECT CURDATE ( ) FROM success", date },
252: new Object[] { "SELECT CCURRENT_DATE FROM failure",
253: date },
254: new Object[] { "SELECT CURRENT_DATES FROM failure",
255: date },
256: new Object[] { "SELECT CCURDATE() FROM failure", date },
257: new Object[] { "SELECT 1 FROM failure", date }, };
258: }
259:
260: @Override
261: @Test(dataProvider="current-date")
262: public String evaluateCurrentDate(String sql, java.sql.Date date)
263: throws SQLException {
264: String expected = sql.contains("success") ? "SELECT '"
265: + date.toString() + "' FROM success" : sql;
266:
267: String evaluated = this .dialect.evaluateCurrentDate(sql, date);
268:
269: assert evaluated.equals(expected) : evaluated;
270:
271: return evaluated;
272: }
273:
274: @Override
275: @DataProvider(name="current-time")
276: Object[][] currentTimeProvider() {
277: java.sql.Time date = new java.sql.Time(System
278: .currentTimeMillis());
279:
280: return new Object[][] {
281: new Object[] { "SELECT CURRENT_TIME FROM success", date },
282: new Object[] { "SELECT CURRENT_TIME(2) FROM success",
283: date },
284: new Object[] {
285: "SELECT CURRENT_TIME ( 2 ) FROM success", date },
286: new Object[] { "SELECT LOCALTIME FROM success", date },
287: new Object[] { "SELECT LOCALTIME(2) FROM success", date },
288: new Object[] { "SELECT LOCALTIME ( 2 ) FROM success",
289: date },
290: new Object[] { "SELECT CURTIME() FROM success", date },
291: new Object[] { "SELECT CURTIME ( ) FROM success", date },
292: new Object[] { "SELECT CCURRENT_TIME FROM failure",
293: date },
294: new Object[] { "SELECT CURRENT_TIMESTAMP FROM failure",
295: date },
296: new Object[] { "SELECT LLOCALTIME FROM failure", date },
297: new Object[] { "SELECT LOCALTIMESTAMP FROM failure",
298: date },
299: new Object[] { "SELECT CCURTIME() FROM failure", date },
300: new Object[] { "SELECT 1 FROM failure", date }, };
301: }
302:
303: @Override
304: @Test(dataProvider="current-time")
305: public String evaluateCurrentTime(String sql, java.sql.Time date)
306: throws SQLException {
307: String expected = sql.contains("success") ? "SELECT '"
308: + date.toString() + "' FROM success" : sql;
309:
310: String evaluated = this .dialect.evaluateCurrentTime(sql, date);
311:
312: assert evaluated.equals(expected) : evaluated;
313:
314: return evaluated;
315: }
316:
317: @Override
318: @DataProvider(name="current-timestamp")
319: Object[][] currentTimestampProvider() {
320: java.sql.Timestamp date = new java.sql.Timestamp(System
321: .currentTimeMillis());
322:
323: return new Object[][] {
324: new Object[] { "SELECT CURRENT_TIMESTAMP FROM success",
325: date },
326: new Object[] {
327: "SELECT CURRENT_TIMESTAMP(2) FROM success",
328: date },
329: new Object[] {
330: "SELECT CURRENT_TIMESTAMP ( 2 ) FROM success",
331: date },
332: new Object[] { "SELECT LOCALTIMESTAMP FROM success",
333: date },
334: new Object[] { "SELECT LOCALTIMESTAMP(2) FROM success",
335: date },
336: new Object[] {
337: "SELECT LOCALTIMESTAMP ( 2 ) FROM success",
338: date },
339: new Object[] { "SELECT NOW() FROM success", date },
340: new Object[] { "SELECT NOW ( ) FROM success", date },
341: new Object[] { "SELECT SYSDATE() FROM success", date },
342: new Object[] { "SELECT SYSDATE ( ) FROM success", date },
343: new Object[] {
344: "SELECT CCURRENT_TIMESTAMP FROM failure", date },
345: new Object[] {
346: "SELECT CURRENT_TIMESTAMPS FROM failure", date },
347: new Object[] { "SELECT LLOCALTIMESTAMP FROM failure",
348: date },
349: new Object[] { "SELECT LOCALTIMESTAMPS FROM failure",
350: date },
351: new Object[] { "SELECT NNOW() FROM failure", date },
352: new Object[] { "SELECT SSYSDATE() FROM failure", date },
353: new Object[] { "SELECT 1 FROM failure", date }, };
354: }
355:
356: @Override
357: @Test(dataProvider="current-timestamp")
358: public String evaluateCurrentTimestamp(String sql,
359: java.sql.Timestamp date) throws SQLException {
360: String expected = sql.contains("success") ? "SELECT '"
361: + date.toString() + "' FROM success" : sql;
362:
363: String evaluated = this.dialect.evaluateCurrentTimestamp(sql,
364: date);
365:
366: assert evaluated.equals(expected) : evaluated;
367:
368: return evaluated;
369: }
370: }
|