001: /******************************************************************************
002: * JBoss, a division of Red Hat *
003: * Copyright 2006, Red Hat Middleware, LLC, and individual *
004: * contributors as indicated by the @authors tag. See the *
005: * copyright.txt in the distribution for a full listing of *
006: * individual contributors. *
007: * *
008: * This is free software; you can redistribute it and/or modify it *
009: * under the terms of the GNU Lesser General Public License as *
010: * published by the Free Software Foundation; either version 2.1 of *
011: * the License, or (at your option) any later version. *
012: * *
013: * This software is distributed in the hope that it will be useful, *
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of *
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
016: * Lesser General Public License for more details. *
017: * *
018: * You should have received a copy of the GNU Lesser General Public *
019: * License along with this software; if not, write to the Free *
020: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *
021: * 02110-1301 USA, or see the FSF site: http://www.fsf.org. *
022: ******************************************************************************/package org.jboss.portal.test.migration;
023:
024: import junit.framework.TestCase;
025: import org.dbunit.database.DatabaseConfig;
026: import org.dbunit.database.DatabaseConnection;
027: import org.dbunit.database.IDatabaseConnection;
028: import org.dbunit.dataset.IDataSet;
029: import org.hibernate.Session;
030: import org.hibernate.SessionFactory;
031: import org.hibernate.Transaction;
032: import org.hibernate.cfg.Configuration;
033: import org.hibernate.tool.hbm2ddl.SchemaExport;
034:
035: import java.sql.Connection;
036: import java.sql.DriverManager;
037:
038: /**
039: * @author <a href="mailto:boleslaw.dawidowicz@jboss.org">Boleslaw Dawidowicz</a>
040: * @author <a href="mailto:julien@jboss.org">Julien Viet </a>
041: * @version $Revision: 8784 $
042: */
043: public abstract class MigrationTestCase extends TestCase {
044: public MigrationTestCase(String name) {
045: super (name);
046: }
047:
048: protected SessionFactory fromFactory;
049: protected SessionFactory toFactory;
050: protected Session fromSession;
051: protected Session toSession;
052: protected Transaction fromTx;
053: protected Transaction toTx;
054:
055: //dbunit stuff
056: //Connections to databases to easily inject data
057: protected IDatabaseConnection fromConnection;
058: protected IDatabaseConnection toConnection;
059:
060: //Datasets to check if data is valid after migrations
061: protected IDataSet fromRefDataSet;
062: protected IDataSet toRefDataSet;
063:
064: String cfgPath;
065:
066: public void setUp() throws Exception {
067: cfgPath = System.getProperty("build.resources");
068: //Set up hibernate configuration
069: /*Configuration fromCfg = new Configuration();
070: Configuration toCfg = new Configuration();
071: */
072: Configuration fromCfg = getFromTestConfiguration();
073: Configuration toCfg = getToTestConfiguration();
074:
075: //hibernate config files - we get files from abstract methods implemented in subclass
076: /*fromCfg.configure(getFromConfigurationFile());
077: toCfg.configure(getToConfigurationFile());
078:
079: //hibernate mapping files
080: fromCfg.addFile(getFromMappingFile());
081: toCfg.addFile(getToMappingFile());*/
082:
083: //set system properties to pass test cfg files to Migrator
084: /*System.setProperty("org.jboss.portal.migration.schema20.cfg", cfgPath + "/test/migration/schema20/hibernate.cfg.xml");
085: System.setProperty("org.jboss.portal.migration.schema22.cfg", cfgPath + "/test/migration/schema22/hibernate.cfg.xml");
086: System.setProperty("org.jboss.portal.migration.schema20.mapping", cfgPath + "/migration/schema20/domain.hbm.xml");
087: System.setProperty("org.jboss.portal.migration.schema22.mapping", cfgPath + "/migration/schema22/domain.hbm.xml");*/
088:
089: //hibernate properties
090: /*Properties fromProps = new Properties();
091: fromProps.setProperty("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
092: fromProps.setProperty("hibernate.connection.driver_class", "org.hsqldb.jdbcDriver");
093: fromProps.setProperty("hibernate.connection.url", "jdbc:hsqldb:TestMigrationFromDB");
094: fromProps.setProperty("hibernate.connection.username", "sa");
095: fromProps.setProperty("hibernate.connection.password", "");
096: fromProps.setProperty("hibernate.connection.pool_size", "1");
097: fromProps.setProperty("hibernate.cache.provider_class", "org.hibernate.cache.HashtableCacheProvider");
098: fromCfg.setProperties(fromProps);
099:
100: Properties toProps = new Properties();
101: toProps.setProperty("hibernate.dialect", "org.hibernate.dialect.HSQLDialect");
102: toProps.setProperty("hibernate.connection.driver_class", "org.hsqldb.jdbcDriver");
103: toProps.setProperty("hibernate.connection.url", "jdbc:hsqldb:TestMigrationToDB");
104: toProps.setProperty("hibernate.connection.username", "sa");
105: toProps.setProperty("hibernate.connection.password", "");
106: toProps.setProperty("hibernate.connection.pool_size", "1");
107: toProps.setProperty("hibernate.cache.provider_class", "org.hibernate.cache.HashtableCacheProvider");
108: toCfg.setProperties(toProps);*/
109:
110: //DBunit connetion setup
111: Class driverClass = Class.forName(fromCfg
112: .getProperty("hibernate.connection.driver_class"));
113: String dbURL = fromCfg.getProperty("hibernate.connection.url");
114: String dbUser = fromCfg
115: .getProperty("hibernate.connection.username");
116: String dbPassword = fromCfg
117: .getProperty("hibernate.connection.password");
118:
119: Connection jdbcConnection = DriverManager.getConnection(dbURL,
120: dbUser, dbPassword);
121: /*Connection jdbcConnection = DriverManager.getConnection(
122: "jdbc:hsqldb:TestMigrationFromDB",
123: "sa",
124: "");*/
125:
126: fromConnection = new DatabaseConnection(jdbcConnection);
127:
128: dbURL = toCfg.getProperty("hibernate.connection.url");
129: dbUser = toCfg.getProperty("hibernate.connection.username");
130: dbPassword = toCfg.getProperty("hibernate.connection.password");
131: jdbcConnection = DriverManager.getConnection(dbURL, dbUser,
132: dbPassword);
133:
134: toConnection = new DatabaseConnection(jdbcConnection);
135:
136: //small hack for dbunit boolean bug
137: DatabaseConfig config = fromConnection.getConfig();
138: config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY,
139: new HsqlDataTypeFactory());
140: config = toConnection.getConfig();
141: config.setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY,
142: new HsqlDataTypeFactory());
143:
144: //create clear db schema for init
145: SchemaExport export = new SchemaExport(fromCfg);
146: export.create(false, true);
147:
148: export = new SchemaExport(toCfg);
149: export.create(false, true);
150:
151: //Set up SessionFactories
152: fromFactory = fromCfg.buildSessionFactory();
153: toFactory = toCfg.buildSessionFactory();
154:
155: // Set up session and tx
156: fromSession = fromFactory.openSession();
157: fromTx = fromSession.beginTransaction();
158:
159: toSession = toFactory.openSession();
160: toTx = toSession.beginTransaction();
161:
162: //DBunit reference data sets
163: fromRefDataSet = Utils.getDataSet(getFromRefDatasetFile());
164:
165: // Populate
166: populate();
167: nextFromSession();
168: nextToSession();
169:
170: }
171:
172: protected void tearDown() throws Exception {
173: fromTx.commit();
174: fromSession.close();
175: fromFactory.close();
176: toTx.commit();
177: toSession.close();
178: toFactory.close();
179:
180: //migrator.disconnect();
181: }
182:
183: protected void nextFromSession() throws Exception {
184: fromTx.commit();
185: fromSession.close();
186: fromSession = fromFactory.openSession();
187: fromTx = fromSession.beginTransaction();
188: }
189:
190: protected void nextToSession() throws Exception {
191: toTx.commit();
192: toSession.close();
193: toSession = toFactory.openSession();
194: toTx = toSession.beginTransaction();
195: }
196:
197: protected void populate() throws Exception {
198:
199: }
200:
201: /*protected abstract File getFromMappingFile();
202:
203: protected abstract File getToMappingFile();
204:
205: protected abstract File getFromConfigurationFile();
206:
207: protected abstract File getToConfigurationFile();*/
208:
209: protected abstract Configuration getFromTestConfiguration();
210:
211: protected abstract Configuration getToTestConfiguration();
212:
213: protected abstract String getFromRefDatasetFile();
214:
215: }
|