001: /*
002: * Copyright 2006-2007, Unitils.org
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.unitils.dbmaintainer.version.impl;
017:
018: import static org.apache.commons.lang.time.DateUtils.parseDate;
019: import static org.junit.Assert.assertFalse;
020: import static org.junit.Assert.assertTrue;
021: import static org.unitils.database.SQLUnitils.executeUpdate;
022: import static org.unitils.database.SQLUnitils.executeUpdateQuietly;
023: import static org.unitils.reflectionassert.ReflectionAssert.assertRefEquals;
024:
025: import java.sql.SQLException;
026: import java.util.Properties;
027:
028: import javax.sql.DataSource;
029:
030: import org.junit.After;
031: import org.junit.Before;
032: import org.junit.Test;
033: import org.unitils.UnitilsJUnit4;
034: import org.unitils.core.ConfigurationLoader;
035: import org.unitils.core.UnitilsException;
036: import org.unitils.core.dbsupport.SQLHandler;
037: import org.unitils.database.annotations.TestDataSource;
038: import org.unitils.dbmaintainer.version.Version;
039: import static org.unitils.dbmaintainer.version.impl.DBVersionSource.PROPKEY_AUTO_CREATE_VERSION_TABLE;
040:
041: /**
042: * Test class for {@link org.unitils.dbmaintainer.version.impl.DBVersionSource}. The implementation is tested using a
043: * test database. The dbms that is used depends on the database configuration in test/resources/unitils.properties
044: *
045: * @author Filip Neven
046: * @author Tim Ducheyne
047: */
048: public class DBVersionSourceTest extends UnitilsJUnit4 {
049:
050: /* The tested instance */
051: private DBVersionSource dbVersionSource;
052:
053: /* The tested instance with auto-create configured */
054: private DBVersionSource dbVersionSourceAutoCreate;
055:
056: /* The dataSource */
057: @TestDataSource
058: private DataSource dataSource = null;
059:
060: /**
061: * Initialize test fixture and creates a test version table.
062: */
063: @Before
064: public void setUp() throws Exception {
065: Properties configuration = new ConfigurationLoader()
066: .loadConfiguration();
067: SQLHandler sqlHandler = new SQLHandler(dataSource);
068: dbVersionSource = new DBVersionSource();
069: dbVersionSource.init(configuration, sqlHandler);
070:
071: configuration.setProperty(PROPKEY_AUTO_CREATE_VERSION_TABLE,
072: "true");
073: dbVersionSourceAutoCreate = new DBVersionSource();
074: dbVersionSourceAutoCreate.init(configuration, sqlHandler);
075:
076: dropVersionTable();
077: createVersionTable();
078: }
079:
080: /**
081: * Cleanup by dropping the test version table.
082: */
083: @After
084: public void tearDown() throws Exception {
085: dropVersionTable();
086: }
087:
088: /**
089: * Test setting and getting version
090: */
091: @Test
092: public void testGetAndSetDBVersion() throws Exception {
093: Version version = new Version(3L, parseDate("2006-10-08 12:00",
094: new String[] { "yyyy-MM-dd hh:mm" }).getTime());
095:
096: dbVersionSource.setDbVersion(version);
097: Version result = dbVersionSource.getDbVersion();
098: assertRefEquals(version, result);
099: }
100:
101: /**
102: * Tests getting the version, but no version table yet (e.g. first use)
103: */
104: @Test(expected=UnitilsException.class)
105: public void testGetDBVersion_noVersionTable() throws Exception {
106: dropVersionTable();
107: dbVersionSource.getDbVersion();
108: }
109:
110: /**
111: * Tests getting the version, but no version table yet and auto-create is true.
112: */
113: @Test
114: public void testGetDBVersion_noVersionTableAutoCreate()
115: throws Exception {
116: dropVersionTable();
117:
118: Version result = dbVersionSourceAutoCreate.getDbVersion();
119: assertRefEquals(0L, result.getIndex());
120: }
121:
122: /**
123: * Tests getting the version but table is empty.
124: */
125: @Test
126: public void testGetAndSetDBVersion_emptyVersionTable()
127: throws Exception {
128: clearVersionTable();
129: Version version = new Version(3L, parseDate("2006-10-08 12:00",
130: new String[] { "yyyy-MM-dd hh:mm" }).getTime());
131:
132: dbVersionSource.setDbVersion(version);
133: Version result = dbVersionSource.getDbVersion();
134: assertRefEquals(version, result);
135: }
136:
137: /**
138: * Test whether the update succeeded value can be correclty set and retrieved, when succeeded is true
139: */
140: @Test
141: public void testRegisterUpdateSucceeded_succeeded()
142: throws Exception {
143: dbVersionSource.registerUpdateSucceeded(true);
144: boolean result = dbVersionSource.isLastUpdateSucceeded();
145:
146: assertTrue(result);
147: }
148:
149: /**
150: * Test whether the update succeeded value can be correclty set and retrieved, when succeeded is false
151: */
152: @Test
153: public void testRegisterUpdateSucceeded_notSucceeded()
154: throws Exception {
155: dbVersionSource.registerUpdateSucceeded(false);
156: boolean result = dbVersionSource.isLastUpdateSucceeded();
157:
158: assertFalse(result);
159: }
160:
161: /**
162: * Utility method to create the test version table.
163: */
164: private void createVersionTable() throws SQLException {
165: executeUpdate(dbVersionSource.getCreateVersionTableStatement(),
166: dataSource);
167: }
168:
169: /**
170: * Utility method to drop the test version table.
171: */
172: private void dropVersionTable() throws SQLException {
173: executeUpdateQuietly("drop table db_version", dataSource);
174: }
175:
176: /**
177: * Utility method to clear the test version table.
178: */
179: private void clearVersionTable() throws SQLException {
180: executeUpdate("delete from db_version", dataSource);
181: }
182: }
|