001 package wodbupdate;
002
003 import java.sql.*;
004 import junit.framework.*;
005
006
007 public class UpdaterTest extends TestCase {
008 static String performedUpdateNames = "";
009 String invokeMethodNames = "";
010
011 public void testDefaultClassNamesPrefix() {
012 Updater updater = new Updater();
013 assertEquals("wodbupdate.Update", updater.updatesClassNamesPrefix);
014 }
015
016 public void testUpdateWithUnlockedTable() {
017 MockUpdater updater = new MockUpdater();
018 updater.canLockDB = true;
019 updater.performUpdate();
020 assertEquals(" tryToLockDB executeUpdates unlockDB closeDB", invokeMethodNames);
021 }
022
023 public void testUpdateWithLockedTable() {
024 MockUpdater updater = new MockUpdater();
025 updater.canLockDB = false;
026 updater.performUpdate();
027 assertEquals(" tryToLockDB waitUntilDBUnlocked closeDB", invokeMethodNames);
028 }
029
030 public void testExecuteUpdates() throws Exception {
031 MockDatabase mockDatabase = new MockDatabase();
032 Updater updater = new Updater();
033 updater.database = mockDatabase;
034 mockDatabase.currentVersionNumber = 2;
035
036 updater.updatesClassNamesPrefix = "wodbupdate.UpdaterTest$TestUpdate";
037 updater.executeUpdates();
038 assertEquals("", performedUpdateNames);
039
040 mockDatabase.currentVersionNumber = 1;
041 updater.executeUpdates();
042 assertEquals(" 2", performedUpdateNames);
043
044 performedUpdateNames = "";
045 mockDatabase.currentVersionNumber = 0;
046 updater.executeUpdates();
047 assertEquals(" 1 2", performedUpdateNames);
048 }
049
050 public void testIncrementDBVersion() {
051 MockDatabase mockDatabase = new MockDatabase();
052 Updater updater = new Updater();
053 updater.database = mockDatabase;
054 assertEquals(0, mockDatabase.currentVersionNumber);
055 updater.incrementDBVersion();
056 assertEquals(1, mockDatabase.currentVersionNumber);
057 }
058
059 public void testCurrentDBVersion() {
060 MockDatabase mockDatabase = new MockDatabase();
061 Updater updater = new Updater();
062 updater.database = mockDatabase;
063 mockDatabase.currentVersionNumber = 31;
064 assertEquals(31, updater.currentDBVersionNumber());
065
066 mockDatabase.shouldThrow = true;
067 assertEquals(-1, updater.currentDBVersionNumber());
068 }
069
070 static public class TestUpdate1 extends Update {
071 public void executeUpdate() {
072 assertNotNull(database);
073 performedUpdateNames += " 1";
074 }
075 }
076 static public class TestUpdate2 extends Update {
077 public void executeUpdate() {
078 assertNotNull(database);
079 performedUpdateNames += " 2";
080 }
081 }
082
083 class MockUpdater extends Updater {
084 boolean canLockDB;
085
086 protected void closeDB() {
087 invokeMethodNames += " closeDB";
088 }
089
090 protected void executeUpdates() throws Exception {
091 invokeMethodNames += " executeUpdates";
092 }
093
094 protected boolean tryToLockDB() {
095 invokeMethodNames += " tryToLockDB";
096 return canLockDB;
097 }
098
099 protected void unlockDB() {
100 invokeMethodNames += " unlockDB";
101 }
102
103 protected void waitUntilDBUnlocked() {
104 invokeMethodNames += " waitUntilDBUnlocked";
105 }
106 }
107
108 class MockDatabase implements Databaseable {
109 boolean shouldThrow;
110 int currentVersionNumber;
111
112 public Statement executeSQL(String anSQLString) {
113 return null;
114 }
115
116 public int currentVersionNumber() {
117 if (shouldThrow)
118 throw new RuntimeException();
119 return currentVersionNumber;
120 }
121
122 public boolean tryToLock() {
123 return false;
124 }
125
126 public void unlock() {
127 }
128
129 public boolean isLocked() {
130 throw new RuntimeException();
131 }
132
133 public void setVersionNumber(int aVersionNumber) {
134 currentVersionNumber = aVersionNumber;
135 }
136
137 public void close() {
138 }
139 }
140
141 }
|