001: package liquibase.lock;
002:
003: import liquibase.DatabaseChangeLogLock;
004: import liquibase.database.Database;
005: import liquibase.database.sql.RawSqlStatement;
006: import liquibase.database.sql.SqlStatement;
007: import liquibase.database.sql.UpdateStatement;
008: import liquibase.database.template.JdbcTemplate;
009: import liquibase.exception.LockException;
010: import static org.easymock.classextension.EasyMock.*;
011: import static org.junit.Assert.*;
012: import org.junit.Test;
013:
014: import java.util.*;
015:
016: public class LockHandlerTest {
017:
018: @Test
019: public void acquireLock_tableExistsNotLocked() throws Exception {
020: Database database = createMock(Database.class);
021: JdbcTemplate template = createMock(JdbcTemplate.class);
022: RawSqlStatement selectLockStatement = new RawSqlStatement(
023: "SELECT LOCK");
024:
025: expect(database.getJdbcTemplate()).andReturn(template)
026: .anyTimes();
027: expect(database.doesChangeLogLockTableExist()).andReturn(true);
028: expect(database.getSelectChangeLogLockSQL()).andReturn(
029: selectLockStatement);
030: expect(database.getDatabaseChangeLogLockTableName()).andReturn(
031: "LOCK_TABLE").anyTimes();
032: expect(database.getDatabaseChangeLogTableName()).andReturn(
033: "DATABASECHANGELOG").anyTimes();
034: expect(database.getDefaultSchemaName()).andReturn(null)
035: .anyTimes();
036:
037: database.commit();
038: expectLastCall();
039:
040: expect(
041: template.queryForObject(selectLockStatement,
042: Boolean.class)).andReturn(Boolean.FALSE);
043: expect(template.update(isA(UpdateStatement.class)))
044: .andReturn(1);
045: template.comment("Lock Database");
046: expectLastCall();
047:
048: replay(database);
049: replay(template);
050:
051: LockHandler handler = LockHandler.getInstance(database);
052: handler.acquireLock();
053:
054: verify(database);
055: verify(template);
056: }
057:
058: @Test
059: public void acquireLock_tableNotExists() throws Exception {
060: Database database = createMock(Database.class);
061: JdbcTemplate template = createMock(JdbcTemplate.class);
062:
063: expect(database.getJdbcTemplate()).andReturn(template)
064: .anyTimes();
065: expect(database.doesChangeLogLockTableExist()).andReturn(false);
066: expectLastCall();
067:
068: replay(database);
069: replay(template);
070:
071: LockHandler handler = LockHandler.getInstance(database);
072: try {
073: handler.acquireLock();
074: fail("did not throw exception");
075: } catch (LockException e) {
076: assertEquals(
077: "Could not acquire lock, table does not exist", e
078: .getMessage());
079: }
080:
081: verify(database);
082: verify(template);
083: }
084:
085: @Test
086: public void acquireLock_tableExistsIsLocked() throws Exception {
087: Database database = createMock(Database.class);
088: JdbcTemplate template = createMock(JdbcTemplate.class);
089: RawSqlStatement selectLockStatement = new RawSqlStatement(
090: "SELECT LOCK");
091:
092: expect(database.getJdbcTemplate()).andReturn(template)
093: .anyTimes();
094: expect(database.doesChangeLogLockTableExist()).andReturn(true);
095: expect(database.getSelectChangeLogLockSQL()).andReturn(
096: selectLockStatement);
097: expectLastCall();
098:
099: expect(
100: template.queryForObject(selectLockStatement,
101: Boolean.class)).andReturn(Boolean.TRUE);
102:
103: replay(database);
104: replay(template);
105:
106: LockHandler handler = LockHandler.getInstance(database);
107: handler.acquireLock();
108:
109: verify(database);
110: verify(template);
111: }
112:
113: @Test
114: public void releaseLock_tableExistsAndLocked() throws Exception {
115: Database database = createMock(Database.class);
116: JdbcTemplate template = createMock(JdbcTemplate.class);
117:
118: expect(database.getJdbcTemplate()).andReturn(template)
119: .anyTimes();
120: expect(database.doesChangeLogLockTableExist()).andReturn(true);
121: expect(database.getDatabaseChangeLogTableName()).andReturn(
122: "DATABASECHANGELOG").anyTimes();
123: database.commit();
124: expectLastCall().atLeastOnce();
125: expect(database.getDefaultSchemaName()).andReturn(null)
126: .anyTimes();
127:
128: expect(database.getDatabaseChangeLogLockTableName()).andReturn(
129: "lock_table").anyTimes();
130: expectLastCall();
131:
132: expect(template.update(isA(UpdateStatement.class)))
133: .andReturn(1);
134: template.comment("Lock Database");
135: expectLastCall().anyTimes();
136: template.comment("Release Database Lock");
137: expectLastCall().anyTimes();
138:
139: replay(database);
140: replay(template);
141:
142: LockHandler handler = LockHandler.getInstance(database);
143: handler.releaseLock();
144:
145: verify(database);
146: verify(template);
147:
148: }
149:
150: @Test
151: public void listLocks_tableExistsWithLock() throws Exception {
152: Database database = createMock(Database.class);
153: JdbcTemplate template = createMock(JdbcTemplate.class);
154:
155: expect(database.getJdbcTemplate()).andReturn(template)
156: .anyTimes();
157: expect(database.doesChangeLogLockTableExist()).andReturn(true);
158: expect(database.getDatabaseChangeLogLockTableName()).andReturn(
159: "lock_table");
160: expect(database.getDefaultSchemaName()).andReturn(
161: "default_schema");
162: expect(database.escapeTableName("default_schema", "lock_table"))
163: .andReturn("default_schema.lock_table");
164: expectLastCall();
165:
166: List<Map> locksList = new ArrayList<Map>();
167: Map lock = new HashMap();
168: lock.put("ID", 1);
169: lock.put("LOCKGRANTED", new Date());
170: lock.put("LOCKED", Boolean.TRUE);
171: lock.put("LOCKEDBY", "127.0.0.1");
172: locksList.add(lock);
173:
174: expect(template.queryForList(isA(SqlStatement.class)))
175: .andReturn(locksList);
176:
177: replay(database);
178: replay(template);
179:
180: LockHandler handler = LockHandler.getInstance(database);
181: DatabaseChangeLogLock[] locks = handler.listLocks();
182: assertEquals(1, locks.length);
183:
184: verify(database);
185: verify(template);
186:
187: }
188:
189: @Test
190: public void listLocks_tableExistsUnlocked() throws Exception {
191: Database database = createMock(Database.class);
192: JdbcTemplate template = createMock(JdbcTemplate.class);
193:
194: expect(database.getJdbcTemplate()).andReturn(template)
195: .anyTimes();
196: expect(database.doesChangeLogLockTableExist()).andReturn(true);
197: expect(database.getDatabaseChangeLogLockTableName()).andReturn(
198: "lock_table");
199: expectLastCall();
200: expect(database.getDefaultSchemaName()).andReturn(
201: "default_schema");
202: expect(database.escapeTableName("default_schema", "lock_table"))
203: .andReturn("default_schema.lock_table");
204:
205: List<Map> locksList = new ArrayList<Map>();
206: Map lock = new HashMap();
207: lock.put("ID", 1);
208: lock.put("LOCKGRANTED", new Date());
209: lock.put("LOCKED", Boolean.FALSE);
210: lock.put("LOCKEDBY", "127.0.0.1");
211: locksList.add(lock);
212:
213: expect(template.queryForList(isA(SqlStatement.class)))
214: .andReturn(locksList);
215:
216: replay(database);
217: replay(template);
218:
219: LockHandler handler = LockHandler.getInstance(database);
220: DatabaseChangeLogLock[] locks = handler.listLocks();
221: assertEquals(0, locks.length);
222:
223: verify(database);
224: verify(template);
225:
226: }
227:
228: @Test
229: public void listLocks_tableExistsNoLocks() throws Exception {
230: Database database = createMock(Database.class);
231: JdbcTemplate template = createMock(JdbcTemplate.class);
232:
233: expect(database.getJdbcTemplate()).andReturn(template)
234: .anyTimes();
235: expect(database.doesChangeLogLockTableExist()).andReturn(true);
236: expect(database.getDefaultSchemaName()).andReturn(
237: "default_schema");
238: expect(database.getDatabaseChangeLogLockTableName()).andReturn(
239: "lock_table");
240: expectLastCall();
241: expect(database.escapeTableName("default_schema", "lock_table"))
242: .andReturn("default_schema.lock_table");
243:
244: List<Map> locksList = new ArrayList<Map>();
245:
246: expect(template.queryForList(isA(SqlStatement.class)))
247: .andReturn(locksList);
248:
249: replay(database);
250: replay(template);
251:
252: LockHandler handler = LockHandler.getInstance(database);
253: DatabaseChangeLogLock[] locks = handler.listLocks();
254: assertEquals(0, locks.length);
255:
256: verify(database);
257: verify(template);
258:
259: }
260:
261: @Test
262: public void listLocks_tableDoesNotExists() throws Exception {
263: Database database = createMock(Database.class);
264:
265: expect(database.doesChangeLogLockTableExist()).andReturn(false);
266:
267: replay(database);
268:
269: LockHandler handler = LockHandler.getInstance(database);
270: DatabaseChangeLogLock[] locks = handler.listLocks();
271: assertEquals(0, locks.length);
272:
273: verify(database);
274: }
275: }
|