001: package liquibase;
002:
003: import liquibase.ClassLoaderFileOpener;
004: import liquibase.FileOpener;
005: import liquibase.Liquibase;
006: import liquibase.database.*;
007: import liquibase.database.template.JdbcOutputTemplate;
008: import liquibase.exception.JDBCException;
009: import static org.easymock.EasyMock.expect;
010: import static org.easymock.EasyMock.expectLastCall;
011: import static org.easymock.classextension.EasyMock.*;
012: import static org.junit.Assert.*;
013: import org.junit.Before;
014: import org.junit.Test;
015:
016: import java.io.InputStream;
017: import java.io.PrintWriter;
018: import java.net.URL;
019: import java.sql.DatabaseMetaData;
020: import java.util.Enumeration;
021: import java.util.List;
022:
023: /**
024: * Tests for {@link liquibase.Liquibase}
025: */
026: public class LiquibaseTest {
027:
028: private TestLiquibase testLiquibase;
029: private DatabaseConnection connectionForConstructor;
030:
031: @Before
032: public void setUp() throws Exception {
033: if (connectionForConstructor != null) {
034: reset(connectionForConstructor);
035: }
036: connectionForConstructor = createMock(DatabaseConnection.class);
037: connectionForConstructor.setAutoCommit(false);
038: expectLastCall().atLeastOnce();
039:
040: DatabaseMetaData metaData = createMock(DatabaseMetaData.class);
041: expect(metaData.getDatabaseProductName()).andReturn("Oracle");
042: replay(metaData);
043:
044: expect(connectionForConstructor.getMetaData()).andReturn(
045: metaData);
046: replay(connectionForConstructor);
047:
048: testLiquibase = new TestLiquibase();
049: }
050:
051: @Test
052: public void isSaveToRunMigration() throws Exception {
053: TestLiquibase liquibase = testLiquibase;
054:
055: liquibase.setUrl("jdbc:oracle:thin:@localhost:1521:latest");
056: assertTrue(liquibase.isSafeToRunMigration());
057:
058: liquibase.setUrl("jdbc:oracle:thin:@liquibase:1521:latest");
059: assertFalse(liquibase.isSafeToRunMigration());
060:
061: testLiquibase.getDatabase().setJdbcTemplate(
062: new JdbcOutputTemplate(new PrintWriter(System.out),
063: testLiquibase.getDatabase()));
064: assertTrue(
065: "Safe to run if outputing sql, even if non-localhost URL",
066: liquibase.isSafeToRunMigration());
067:
068: }
069:
070: @Test
071: public void getImplementedDatabases() throws Exception {
072: List<Database> databases = DatabaseFactory.getInstance()
073: .getImplementedDatabases();
074: assertEquals(12, databases.size());
075:
076: boolean foundOracle = false;
077: boolean foundPostgres = false;
078: boolean foundMSSQL = false;
079:
080: for (Database db : databases) {
081: if (db instanceof OracleDatabase) {
082: foundOracle = true;
083: } else if (db instanceof PostgresDatabase) {
084: foundPostgres = true;
085: } else if (db instanceof MSSQLDatabase) {
086: foundMSSQL = true;
087: }
088: }
089:
090: assertTrue("Oracle not in Implemented Databases", foundOracle);
091: assertTrue("MSSQL not in Implemented Databases", foundMSSQL);
092: assertTrue("Postgres not in Implemented Databases",
093: foundPostgres);
094: }
095:
096: private class TestLiquibase extends Liquibase {
097: private String url;
098: private Database database;
099: private InputStream inputStream;
100:
101: public TestLiquibase() {
102: super ("liquibase/test.xml", new ClassLoaderFileOpener(),
103: null);
104: inputStream = createMock(InputStream.class);
105: replay(inputStream);
106: }
107:
108: public Database getDatabase() {
109: if (database == null) {
110: database = new OracleDatabase() {
111: public String getConnectionURL() {
112: return url;
113: }
114:
115: public String getConnectionUsername() {
116: return "testUser";
117: }
118: };
119: }
120: return database;
121: }
122:
123: public void setDatabase(Database database) {
124: this .database = database;
125: }
126:
127: public Database[] getImplementedDatabases() {
128: Database mockDatabase = createMock(Database.class);
129: try {
130:
131: expect(
132: mockDatabase
133: .isCorrectDatabaseImplementation(null))
134: .andReturn(true).atLeastOnce();
135: mockDatabase.setConnection((DatabaseConnection) null);
136: expectLastCall();
137: expect(mockDatabase.getConnection()).andReturn(
138: connectionForConstructor);
139: replay(mockDatabase);
140:
141: return new Database[] { mockDatabase, };
142: } catch (JDBCException e) {
143: throw new RuntimeException(e);
144: }
145: }
146:
147: public void setUrl(String url) {
148: this .url = url;
149: }
150:
151: public FileOpener getFileOpener() {
152: return new FileOpener() {
153: public InputStream getResourceAsStream(String file) {
154: return inputStream;
155: }
156:
157: public Enumeration<URL> getResources(String packageName) {
158: return null;
159: }
160:
161: public ClassLoader toClassLoader() {
162: return null;
163: }
164: };
165: }
166: }
167: }
|