PageRenderTime 28ms CodeModel.GetById 19ms app.highlight 6ms RepoModel.GetById 2ms app.codeStats 0ms

/tst/org/diffkit/db/tst/DBTestSetup.groovy

http://diffkit.googlecode.com/
Groovy | 147 lines | 107 code | 19 blank | 21 comment | 19 complexity | 0353468af9d98a577c2ee8fe592a1572 MD5 | raw file
  1package org.diffkit.db.tst
  2
  3/**
  4 * Copyright 2010-2011 Joseph Panico
  5 *
  6 * Licensed under the Apache License, Version 2.0 (the "License");
  7 * you may not use this file except in compliance with the License.
  8 * You may obtain a copy of the License at
  9 *
 10 *   http://www.apache.org/licenses/LICENSE-2.0
 11 *
 12 * Unless required by applicable law or agreed to in writing, software
 13 * distributed under the License is distributed on an "AS IS" BASIS,
 14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 15 * See the License for the specific language governing permissions and
 16 * limitations under the License.
 17 */
 18
 19import org.diffkit.util.DKResourceUtil
 20import org.slf4j.LoggerFactory
 21
 22import java.io.File;
 23import java.sql.Connection 
 24
 25import org.diffkit.db.DKDatabase;
 26import org.diffkit.db.DKDBFlavor;
 27import org.diffkit.db.DKDBH2Loader 
 28import org.diffkit.db.DKDBInsertTableLoader;
 29import org.diffkit.db.DKDBTable;
 30import org.diffkit.db.DKDBTableLoader 
 31import org.diffkit.util.DKResourceUtil;
 32import org.diffkit.util.DKSqlUtil;
 33
 34import org.slf4j.Logger;
 35import org.slf4j.LoggerFactory;
 36import org.springframework.context.ApplicationContext 
 37import org.springframework.context.support.ClassPathXmlApplicationContext 
 38import org.springframework.context.support.FileSystemXmlApplicationContext 
 39
 40
 41/**
 42 * @author jpanico
 43 */
 44public class DBTestSetup {
 45   
 46   private static final Logger _log = LoggerFactory.getLogger(DBTestSetup.class)
 47   
 48   public static void setupDB(File dbSetupFile_, File[] connectionInfoFiles_, String lhsSourcePath_, String rhsSourcePath_) {
 49      File lhsSourceFile = DKResourceUtil.findResourceAsFile(lhsSourcePath_)
 50      File rhsSourceFile = DKResourceUtil.findResourceAsFile(rhsSourcePath_)
 51      setupDB( dbSetupFile_, connectionInfoFiles_, lhsSourceFile, rhsSourceFile)
 52   }
 53   
 54   /**
 55    * dbSetupFile_ can be a FS file path, or a classpath resource path
 56    */
 57   public static void setupDB(File dbSetupFile_, File[] connectionInfoFiles_, 
 58   File lhsSourceFile_, File rhsSourceFile_) {
 59      
 60      if(!dbSetupFile_)
 61         return 
 62      ApplicationContext context = null
 63      _log.debug("dbSetupFile_->{}",dbSetupFile_.canonicalPath)
 64      def configFiles = [dbSetupFile_]
 65      if(connectionInfoFiles_)
 66         configFiles.addAll(connectionInfoFiles_)
 67      if(dbSetupFile_.exists()) {
 68         String[] paths = configFiles.collect { 'file:'+ it.absolutePath }
 69         context = new FileSystemXmlApplicationContext(paths, false)
 70      }
 71      else {
 72         String[] paths = configFiles.collect { it.path }
 73         context = new ClassPathXmlApplicationContext(paths,false)
 74      }
 75      context.setClassLoader(DBTestSetup.class.getClassLoader())
 76      context.refresh()
 77      assert context
 78      
 79      def connectionSource = (!context.containsBean('connectionSource')?null:context.getBean('connectionSource'))
 80      def lhsConnectionSource = (!context.containsBean('lhsConnectionSource')?null:context.getBean('lhsConnectionSource'))
 81      def rhsConnectionSource = (!context.containsBean('rhsConnectionSource')?null:context.getBean('rhsConnectionSource'))
 82      _log.debug("connectionSource->{}",connectionSource)
 83      _log.debug("lhsConnectionSource->{}",lhsConnectionSource)
 84      _log.debug("rhsConnectionSource->{}",rhsConnectionSource)
 85      if(!connectionSource && ! (lhsConnectionSource && rhsConnectionSource))
 86         throw new RuntimeException("connectionSource bean(s) not in dbsetup file->${dbSetupFile_}")
 87      if(connectionSource && (lhsConnectionSource || rhsConnectionSource))
 88         throw new RuntimeException("cannot specify both 'connectionSource' and ('lhsConnectionSource' or 'rhsConnectionSource') bean in dbsetup file->${dbSetupFile_}; choose one or the other")
 89      
 90      if(!lhsConnectionSource)
 91         lhsConnectionSource = connectionSource
 92      if(!rhsConnectionSource)
 93         rhsConnectionSource = connectionSource
 94      _log.debug("lhsConnectionSource->{}",lhsConnectionSource)
 95      _log.debug("rhsConnectionSource->{}",rhsConnectionSource)
 96      
 97      def beanName = 'lhs.table'
 98      if(context.containsBean(beanName)) {
 99         def lhsTable = context.getBean(beanName)
100         _log.debug("lhsTable->{}",lhsTable)
101         setupDBTable( lhsTable, lhsSourceFile_, lhsConnectionSource)
102      }
103      beanName = 'rhs.table'
104      if(context.containsBean(beanName)) {
105         def rhsTable = context.getBean(beanName)
106         _log.debug("rhsTable->{}",rhsTable)
107         setupDBTable( rhsTable, rhsSourceFile_, rhsConnectionSource)
108      }
109   }
110   
111   private static void setupDBTable(DKDBTable table_, File dataFile_, DKDatabase database_){
112      if(!table_)
113         return
114      
115      if(database_.tableExists(table_))
116         database_.dropTable(table_)
117      Connection connection = database_.connection
118      _log.debug("connection->{}",connection)
119      table_ =database_.createTable( table_)
120      DKSqlUtil.close(connection)
121      DKDBTableLoader loader = getLoader(database_)
122      _log.debug("loader->{}",loader)
123      loader.load(table_, dataFile_)
124   }
125   
126   private static Map<String,String> getTypeSubstitutionMap(DKDBFlavor flavor_){
127      if(!flavor_) 
128         return null
129      switch (flavor_) {
130         case DKDBFlavor.H2:
131            return null
132         case DKDBFlavor.DB2:
133            return null
134         case DKDBFlavor.ORACLE:
135            return this.createOracleTypeSubstitutionMap()
136         
137         default:
138            return null
139      }
140   }
141   
142   private static DKDBTableLoader getLoader(DKDatabase database_){
143      if(database_.getFlavor()==DKDBFlavor.H2)
144         return new DKDBH2Loader(database_)
145      return new DKDBInsertTableLoader(database_)
146   }
147}