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:
027: import net.sf.hajdbc.ColumnProperties;
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 TestDerbyDialect extends TestStandardDialect {
043: @Override
044: protected Dialect createDialect() {
045: return new DerbyDialect();
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: /*
066: @Override
067: @Test(dataProvider = "table")
068: public String getLockTableSQL(TableProperties properties) throws SQLException
069: {
070: EasyMock.expect(properties.getName()).andReturn("table");
071:
072: this.replay();
073:
074: String sql = this.dialect.getLockTableSQL(properties);
075:
076: this.verify();
077:
078: assert sql.equals("LOCK TABLE table IN SHARE MODE") : sql;
079:
080: return sql;
081: }
082: */
083: @Override
084: @Test
085: public String getSimpleSQL() throws SQLException {
086: this .replay();
087:
088: String sql = this .dialect.getSimpleSQL();
089:
090: this .verify();
091:
092: assert sql.equals("VALUES CURRENT_TIMESTAMP") : sql;
093:
094: return sql;
095: }
096:
097: @Override
098: @Test(dataProvider="sequence-sql")
099: public String parseSequence(String sql) throws SQLException {
100: this .replay();
101:
102: String sequence = this .dialect.parseSequence(sql);
103:
104: this .verify();
105:
106: assert sequence == null : sequence;
107:
108: return sequence;
109: }
110:
111: @Override
112: @Test(dataProvider="meta-data")
113: public Collection<QualifiedName> getSequences(
114: DatabaseMetaData metaData) throws SQLException {
115: this .replay();
116:
117: Collection<QualifiedName> sequences = this .dialect
118: .getSequences(metaData);
119:
120: this .verify();
121:
122: assert sequences.isEmpty() : sequences;
123:
124: return sequences;
125: }
126:
127: @Override
128: @Test(dataProvider="column")
129: public boolean isIdentity(ColumnProperties properties)
130: throws SQLException {
131: EasyMock.expect(properties.getRemarks()).andReturn(
132: "GENERATED ALWAYS AS IDENTITY");
133:
134: this .replay();
135:
136: boolean identity = this .dialect.isIdentity(properties);
137:
138: this .verify();
139:
140: assert identity;
141:
142: this .reset();
143:
144: EasyMock.expect(this .columnProperties.getRemarks()).andReturn(
145: null);
146:
147: this .replay();
148:
149: identity = this .dialect.isIdentity(properties);
150:
151: this .verify();
152:
153: assert !identity;
154:
155: return identity;
156: }
157:
158: @Override
159: @Test(dataProvider="sequence")
160: public String getNextSequenceValueSQL(SequenceProperties sequence)
161: throws SQLException {
162: EasyMock.expect(sequence.getName()).andReturn("sequence");
163:
164: this .replay();
165:
166: String sql = this .dialect.getNextSequenceValueSQL(sequence);
167:
168: this .verify();
169:
170: assert sql.equals("VALUES NEXT VALUE FOR sequence") : sql;
171:
172: return sql;
173: }
174:
175: @Override
176: @DataProvider(name="current-date")
177: Object[][] currentDateProvider() {
178: java.sql.Date date = new java.sql.Date(System
179: .currentTimeMillis());
180:
181: return new Object[][] {
182: new Object[] { "SELECT CURRENT_DATE FROM success", date },
183: new Object[] { "SELECT CURRENT DATE FROM success", date },
184: new Object[] { "SELECT CCURRENT_DATE FROM failure",
185: date },
186: new Object[] { "SELECT CURRENT_DATES FROM failure",
187: date },
188: new Object[] { "SELECT 1 FROM failure", date }, };
189: }
190:
191: @Override
192: @Test(dataProvider="current-date")
193: public String evaluateCurrentDate(String sql, java.sql.Date date)
194: throws SQLException {
195: String expected = sql.contains("success") ? "SELECT DATE('"
196: + date.toString() + "') FROM success" : sql;
197:
198: String evaluated = this .dialect.evaluateCurrentDate(sql, date);
199:
200: assert evaluated.equals(expected) : evaluated;
201:
202: return evaluated;
203: }
204:
205: @Override
206: @DataProvider(name="current-time")
207: Object[][] currentTimeProvider() {
208: java.sql.Time date = new java.sql.Time(System
209: .currentTimeMillis());
210:
211: return new Object[][] {
212: new Object[] { "SELECT CURRENT_TIME FROM success", date },
213: new Object[] { "SELECT CURRENT TIME FROM success", date },
214: new Object[] { "SELECT CCURRENT_TIME FROM failure",
215: date },
216: new Object[] { "SELECT CURRENT_TIMESTAMP FROM failure",
217: date },
218: new Object[] { "SELECT 1 FROM failure", date }, };
219: }
220:
221: @Override
222: @Test(dataProvider="current-time")
223: public String evaluateCurrentTime(String sql, java.sql.Time date)
224: throws SQLException {
225: String expected = sql.contains("success") ? "SELECT TIME('"
226: + date.toString() + "') FROM success" : sql;
227:
228: String evaluated = this .dialect.evaluateCurrentTime(sql, date);
229:
230: assert evaluated.equals(expected) : evaluated;
231:
232: return evaluated;
233: }
234:
235: @Override
236: @DataProvider(name="current-timestamp")
237: Object[][] currentTimestampProvider() {
238: java.sql.Timestamp date = new java.sql.Timestamp(System
239: .currentTimeMillis());
240:
241: return new Object[][] {
242: new Object[] { "SELECT CURRENT_TIMESTAMP FROM success",
243: date },
244: new Object[] { "SELECT CURRENT TIMESTAMP FROM success",
245: date },
246: new Object[] {
247: "SELECT CCURRENT_TIMESTAMP FROM failure", date },
248: new Object[] {
249: "SELECT CURRENT_TIMESTAMPS FROM failure", date },
250: new Object[] { "SELECT 1 FROM failure", date }, };
251: }
252:
253: @Override
254: @Test(dataProvider="current-timestamp")
255: public String evaluateCurrentTimestamp(String sql,
256: java.sql.Timestamp date) throws SQLException {
257: String expected = sql.contains("success") ? "SELECT TIMESTAMP('"
258: + date.toString() + "') FROM success"
259: : sql;
260:
261: String evaluated = this.dialect.evaluateCurrentTimestamp(sql,
262: date);
263:
264: assert evaluated.equals(expected) : evaluated;
265:
266: return evaluated;
267: }
268: }
|