/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

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