001: package liquibase.database;
002:
003: import liquibase.change.ColumnConfig;
004: import liquibase.test.TestContext;
005: import static org.easymock.EasyMock.*;
006: import static org.junit.Assert.*;
007: import org.junit.Test;
008:
009: import java.sql.Connection;
010: import java.sql.DatabaseMetaData;
011: import java.sql.SQLException;
012:
013: /**
014: * Base test class for database-specific tests
015: */
016: public abstract class AbstractDatabaseTest {
017:
018: protected AbstractDatabase database;
019:
020: protected AbstractDatabaseTest(AbstractDatabase database)
021: throws Exception {
022: this .database = database;
023: TestContext context = TestContext.getInstance();
024: DatabaseConnection connection = context.getConnection(context
025: .getTestUrl(database));
026: if (connection != null) {
027: database.setConnection(connection);
028: }
029: }
030:
031: public AbstractDatabase getDatabase() {
032: return database;
033: }
034:
035: protected abstract String getProductNameString();
036:
037: public abstract void getBlobType();
038:
039: public abstract void supportsInitiallyDeferrableColumns();
040:
041: public abstract void getBooleanType();
042:
043: public abstract void getCurrencyType();
044:
045: public abstract void getUUIDType();
046:
047: public abstract void getClobType();
048:
049: public abstract void getDateType();
050:
051: public abstract void getDateTimeType();
052:
053: public abstract void getCurrentDateTimeFunction();
054:
055: @Test
056: public void getColumnType() {
057: ColumnConfig column = new ColumnConfig();
058:
059: column.setType("boolean");
060: assertEquals(database.getBooleanType(), database
061: .getColumnType(column));
062: column.setType("BooLean");
063: assertEquals(database.getBooleanType(), database
064: .getColumnType(column));
065:
066: column.setType("currency");
067: assertEquals(database.getCurrencyType(), database
068: .getColumnType(column));
069: column.setType("currEncy");
070: assertEquals(database.getCurrencyType(), database
071: .getColumnType(column));
072:
073: column.setType("uuid");
074: assertEquals(database.getUUIDType(), database
075: .getColumnType(column));
076: column.setType("UUID");
077: assertEquals(database.getUUIDType(), database
078: .getColumnType(column));
079:
080: column.setType("blob");
081: assertEquals(database.getBlobType(), database
082: .getColumnType(column));
083: column.setType("BLOB");
084: assertEquals(database.getBlobType(), database
085: .getColumnType(column));
086:
087: column.setType("clob");
088: assertEquals(database.getClobType(), database
089: .getColumnType(column));
090: column.setType("CLOB");
091: assertEquals(database.getClobType(), database
092: .getColumnType(column));
093:
094: column.setType("SomethingElse");
095: assertEquals("SomethingElse", database.getColumnType(column));
096: }
097:
098: @Test
099: public void getDriverName() throws Exception {
100: Connection connection = createMock(Connection.class);
101: DatabaseMetaData metaData = createMock(DatabaseMetaData.class);
102:
103: connection.setAutoCommit(false);
104: expect(connection.getMetaData()).andReturn(metaData);
105: expect(metaData.getDriverName()).andReturn("DriverNameHere");
106: replay(connection);
107: replay(metaData);
108:
109: Database database = getDatabase();
110: database.setConnection(connection);
111:
112: assertEquals("DriverNameHere", database.getDriverName());
113: }
114:
115: @Test
116: public void getConnectionURL() throws Exception {
117: Connection connection = createMock(Connection.class);
118: DatabaseMetaData metaData = createMock(DatabaseMetaData.class);
119: connection.setAutoCommit(false);
120: expect(connection.getMetaData()).andReturn(metaData);
121: expect(metaData.getURL()).andReturn("URLHere");
122: replay(connection);
123: replay(metaData);
124:
125: Database database = getDatabase();
126: database.setConnection(connection);
127:
128: assertEquals("URLHere", database.getConnectionURL());
129: }
130:
131: @Test
132: public void testGetConnectionUsername() throws Exception {
133: Connection connection = createMock(Connection.class);
134: DatabaseMetaData metaData = createMock(DatabaseMetaData.class);
135:
136: connection.setAutoCommit(false);
137: expect(connection.getMetaData()).andReturn(metaData);
138: expect(metaData.getUserName()).andReturn("usernameHere");
139: replay(connection);
140: replay(metaData);
141:
142: Database database = getDatabase();
143: database.setConnection(connection);
144:
145: assertEquals("usernameHere", database.getConnectionUsername());
146: }
147:
148: @Test
149: public void isCorrectDatabaseImplementation() throws Exception {
150: assertTrue(getDatabase().isCorrectDatabaseImplementation(
151: getMockConnection()));
152: }
153:
154: protected Connection getMockConnection() throws SQLException {
155: Connection conn = createMock(Connection.class);
156: DatabaseMetaData metaData = createMock(DatabaseMetaData.class);
157: conn.setAutoCommit(false);
158:
159: expectLastCall().anyTimes();
160: expect(conn.getMetaData()).andReturn(metaData).anyTimes();
161: expect(metaData.getDatabaseProductName()).andReturn(
162: getProductNameString()).anyTimes();
163: replay(conn);
164: replay(metaData);
165: return conn;
166: }
167:
168: @Test
169: public void escapeTableName_noSchema() {
170: Database database = getDatabase();
171: assertEquals("tableName", database.escapeTableName(null,
172: "tableName"));
173: }
174:
175: @Test
176: public void escapeTableName_withSchema() {
177: Database database = getDatabase();
178: assertEquals("schemaName.tableName", database.escapeTableName(
179: "schemaName", "tableName"));
180: }
181:
182: @Test
183: public void getColumnType_javaTypes() throws SQLException {
184: Database database = getDatabase();
185: DatabaseConnection connection = database.getConnection();
186: if (connection != null) {
187: connection.rollback();
188: assertEquals(database.getDateType().toUpperCase(), database
189: .getColumnType("java.sql.Types.DATE", false)
190: .toUpperCase());
191: assertEquals(database.getBooleanType().toUpperCase(),
192: database.getColumnType("java.sql.Types.BOOLEAN",
193: false).toUpperCase());
194: assertEquals("VARCHAR(255)", database.getColumnType(
195: "java.sql.Types.VARCHAR(255)", false).toUpperCase());
196: }
197: }
198: }
|