001: package liquibase.database.sql;
002:
003: import liquibase.database.Database;
004: import liquibase.database.MSSQLDatabase;
005: import liquibase.database.MySQLDatabase;
006: import liquibase.database.structure.DatabaseSnapshot;
007: import liquibase.database.template.JdbcTemplate;
008: import liquibase.exception.JDBCException;
009: import liquibase.test.DatabaseTest;
010: import liquibase.test.DatabaseTestTemplate;
011: import liquibase.test.SqlStatementDatabaseTest;
012: import liquibase.util.ISODateFormat;
013: import static org.junit.Assert.*;
014: import org.junit.Test;
015:
016: import java.util.Calendar;
017: import java.util.Date;
018:
019: public class AddDefaultValueStatementTest extends
020: AbstractSqlStatementTest {
021:
022: private static final String TABLE_NAME = "AddDVSTest";
023: private static final String COLUMN_NAME = "testCol";
024:
025: protected void setupDatabase(Database database) throws Exception {
026: dropTableIfExists(null, TABLE_NAME, database);
027: }
028:
029: protected SqlStatement generateTestStatement() {
030: return new AddDefaultValueStatement(null, null, null, null);
031: }
032:
033: @Test
034: public void execute_stringDefaultValue() throws Exception {
035: new DatabaseTestTemplate()
036: .testOnAvailableDatabases(new DatabaseTest() {
037:
038: public void performTest(Database database)
039: throws Exception {
040: new JdbcTemplate(database)
041: .execute(new CreateTableStatement(null,
042: TABLE_NAME).addColumn("id",
043: "int").addColumn(COLUMN_NAME,
044: "varchar(50)"));
045:
046: DatabaseSnapshot snapshot = new DatabaseSnapshot(
047: database);
048: assertNull(snapshot.getTable(TABLE_NAME)
049: .getColumn(COLUMN_NAME)
050: .getDefaultValue());
051:
052: new JdbcTemplate(database)
053: .execute(new AddDefaultValueStatement(
054: null, TABLE_NAME, COLUMN_NAME,
055: "New Default Value"));
056:
057: snapshot = new DatabaseSnapshot(database);
058: assertEquals("New Default Value", snapshot
059: .getTable(TABLE_NAME).getColumn(
060: COLUMN_NAME).getDefaultValue());
061: }
062: });
063: }
064:
065: @Test
066: public void execute_booleanDefaultValue() throws Exception {
067: new DatabaseTestTemplate()
068: .testOnAvailableDatabases(new SqlStatementDatabaseTest(
069: null, new AddDefaultValueStatement(null,
070: TABLE_NAME, COLUMN_NAME, Boolean.TRUE)) {
071:
072: protected void setup(Database database)
073: throws JDBCException {
074: new JdbcTemplate(database)
075: .execute(new CreateTableStatement(null,
076: TABLE_NAME).addColumn("id",
077: "int").addColumn(COLUMN_NAME,
078: database.getBooleanType()));
079: super .setup(database);
080: }
081:
082: protected void preExecuteAssert(
083: DatabaseSnapshot snapshot) {
084: assertNull(snapshot.getTable(TABLE_NAME)
085: .getColumn(COLUMN_NAME)
086: .getDefaultValue());
087: }
088:
089: protected void postExecuteAssert(
090: DatabaseSnapshot snapshot) {
091: if (snapshot.getDatabase() instanceof MySQLDatabase
092: || snapshot.getDatabase() instanceof MSSQLDatabase) {
093: assertEquals("true", snapshot.getTable(
094: TABLE_NAME).getColumn(COLUMN_NAME)
095: .getDefaultValue().toString()
096: .toLowerCase());
097: } else {
098: assertEquals(snapshot.getDatabase()
099: .getTrueBooleanValue()
100: .toLowerCase(), snapshot.getTable(
101: TABLE_NAME).getColumn(COLUMN_NAME)
102: .getDefaultValue().toString()
103: .toLowerCase());
104: }
105: }
106:
107: });
108: }
109:
110: @Test
111: public void execute_intDefaultValue() throws Exception {
112: new DatabaseTestTemplate()
113: .testOnAvailableDatabases(new SqlStatementDatabaseTest(
114: null, new AddDefaultValueStatement(null,
115: TABLE_NAME, COLUMN_NAME, 42)) {
116:
117: protected void setup(Database database)
118: throws JDBCException {
119: new JdbcTemplate(database)
120: .execute(new CreateTableStatement(null,
121: TABLE_NAME).addColumn("id",
122: "int").addColumn(COLUMN_NAME,
123: "int"));
124: super .setup(database);
125: }
126:
127: protected void preExecuteAssert(
128: DatabaseSnapshot snapshot) {
129: assertNull(snapshot.getTable(TABLE_NAME)
130: .getColumn(COLUMN_NAME)
131: .getDefaultValue());
132: }
133:
134: protected void postExecuteAssert(
135: DatabaseSnapshot snapshot) {
136: assertEquals(42, new Integer(snapshot.getTable(
137: TABLE_NAME).getColumn(COLUMN_NAME)
138: .getDefaultValue().toString()));
139: }
140:
141: });
142: }
143:
144: @Test
145: public void execute_floatDefaultValue() throws Exception {
146: new DatabaseTestTemplate()
147: .testOnAvailableDatabases(new SqlStatementDatabaseTest(
148: null, new AddDefaultValueStatement(null,
149: TABLE_NAME, COLUMN_NAME, 42.56)) {
150:
151: protected void setup(Database database)
152: throws JDBCException {
153: new JdbcTemplate(database)
154: .execute(new CreateTableStatement(null,
155: TABLE_NAME).addColumn("id",
156: "int").addColumn(COLUMN_NAME,
157: "float"));
158: super .setup(database); //To change body of overridden methods use File | Settings | File Templates.
159: }
160:
161: protected void preExecuteAssert(
162: DatabaseSnapshot snapshot) {
163: assertNull(snapshot.getTable(TABLE_NAME)
164: .getColumn(COLUMN_NAME)
165: .getDefaultValue());
166: }
167:
168: protected void postExecuteAssert(
169: DatabaseSnapshot snapshot) {
170: assertEquals(42.56f, new Float(snapshot
171: .getTable(TABLE_NAME).getColumn(
172: COLUMN_NAME).getDefaultValue()
173: .toString()));
174: }
175:
176: });
177: }
178:
179: @Test
180: public void execute_dateTimeDefaultValue() throws Exception {
181: final java.sql.Timestamp date = new java.sql.Timestamp(
182: new Date().getTime());
183:
184: new DatabaseTestTemplate()
185: .testOnAvailableDatabases(new SqlStatementDatabaseTest(
186: null, new AddDefaultValueStatement(null,
187: TABLE_NAME, COLUMN_NAME, date)) {
188:
189: protected void setup(Database database)
190: throws JDBCException {
191: new JdbcTemplate(database)
192: .execute(new CreateTableStatement(null,
193: TABLE_NAME).addColumn("id",
194: "int").addColumn(COLUMN_NAME,
195: database.getDateTimeType()));
196: }
197:
198: protected void preExecuteAssert(
199: DatabaseSnapshot snapshot) {
200: assertNull(snapshot.getTable(TABLE_NAME)
201: .getColumn(COLUMN_NAME)
202: .getDefaultValue());
203: }
204:
205: protected void postExecuteAssert(
206: DatabaseSnapshot snapshot) {
207: assertEquals(
208: date.getTime() / 1000,
209: ((Date) snapshot.getTable(TABLE_NAME)
210: .getColumn(COLUMN_NAME)
211: .getDefaultValue()).getTime() / 1000);
212: }
213:
214: });
215: }
216:
217: @Test
218: public void execute_dateDefaultValue() throws Exception {
219: final java.sql.Date date = new java.sql.Date(new Date()
220: .getTime());
221: new DatabaseTestTemplate()
222: .testOnAvailableDatabases(new SqlStatementDatabaseTest(
223: null, new AddDefaultValueStatement(null,
224: TABLE_NAME, COLUMN_NAME, date)) {
225:
226: protected void setup(Database database)
227: throws JDBCException {
228: new JdbcTemplate(database)
229: .execute(new CreateTableStatement(null,
230: TABLE_NAME).addColumn("id",
231: "int").addColumn(COLUMN_NAME,
232: database.getDateType()));
233:
234: super .setup(database);
235: }
236:
237: protected void preExecuteAssert(
238: DatabaseSnapshot snapshot) {
239: assertNull(snapshot.getTable(TABLE_NAME)
240: .getColumn(COLUMN_NAME)
241: .getDefaultValue());
242: }
243:
244: protected void postExecuteAssert(
245: DatabaseSnapshot snapshot) {
246: Object defaultValue = snapshot.getTable(
247: TABLE_NAME).getColumn(COLUMN_NAME)
248: .getDefaultValue();
249: if (defaultValue instanceof java.sql.Date) {
250: assertEquals(
251: new ISODateFormat().format(date),
252: new ISODateFormat()
253: .format(((java.sql.Date) defaultValue)));
254: } else { //mssql uses smalldatetime for date which is actually a timestamp
255: Calendar cal = Calendar.getInstance();
256: cal.setTime(date);
257: cal.set(Calendar.HOUR, 0);
258: cal.set(Calendar.MINUTE, 0);
259: cal.set(Calendar.SECOND, 0);
260: cal.set(Calendar.MILLISECOND, 0);
261: assertEquals(
262: new ISODateFormat()
263: .format(new java.sql.Date(
264: cal
265: .getTimeInMillis())),
266: new ISODateFormat()
267: .format(new java.sql.Date(
268: ((java.sql.Timestamp) defaultValue)
269: .getTime())));
270: }
271: }
272:
273: });
274: }
275:
276: @Test
277: public void execute_timeDefaultValue() throws Exception {
278: final java.sql.Time time = new java.sql.Time(new Date()
279: .getTime());
280: new DatabaseTestTemplate()
281: .testOnAvailableDatabases(new SqlStatementDatabaseTest(
282: null, new AddDefaultValueStatement(null,
283: TABLE_NAME, COLUMN_NAME, time)) {
284:
285: protected void setup(Database database)
286: throws JDBCException {
287: new JdbcTemplate(database)
288: .execute(new CreateTableStatement(null,
289: TABLE_NAME).addColumn("id",
290: "int").addColumn(COLUMN_NAME,
291: database.getTimeType()));
292: super .setup(database);
293: }
294:
295: protected void preExecuteAssert(
296: DatabaseSnapshot snapshot) {
297: assertNull(snapshot.getTable(TABLE_NAME)
298: .getColumn(COLUMN_NAME)
299: .getDefaultValue());
300: }
301:
302: protected void postExecuteAssert(
303: DatabaseSnapshot snapshot) {
304: Object defaultValue = snapshot.getTable(
305: TABLE_NAME).getColumn(COLUMN_NAME)
306: .getDefaultValue();
307: if (defaultValue instanceof java.sql.Time) {
308: assertEquals(
309: new ISODateFormat().format(time),
310: new ISODateFormat()
311: .format(((java.sql.Time) defaultValue)));
312: } else { //mssql uses smalldatetime which is a timestamp, oracle uses date which is a date
313: Calendar cal = Calendar.getInstance();
314: cal.setTime(time);
315: cal.set(Calendar.DAY_OF_YEAR, 0);
316: cal.set(Calendar.YEAR, 0);
317: assertEquals(
318: new ISODateFormat()
319: .format(new java.sql.Time(
320: cal
321: .getTimeInMillis())),
322: new ISODateFormat()
323: .format(new java.sql.Time(
324: ((Date) defaultValue)
325: .getTime())));
326: }
327: }
328: });
329: }
330: }
|