PageRenderTime 12ms CodeModel.GetById 2ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 1ms

/tst/org/diffkit/diff/conf/tst/TestMagicPlanBuilder.groovy

http://diffkit.googlecode.com/
Groovy | 160 lines | 118 code | 20 blank | 22 comment | 22 complexity | 16b6966d68fe124c5cc4b7c5bc8dc4cc MD5 | raw file
  1/**
  2 * Copyright 2010-2011 Joseph Panico
  3 *
  4 * Licensed under the Apache License, Version 2.0 (the "License");
  5 * you may not use this file except in compliance with the License.
  6 * You may obtain a copy of the License at
  7 *
  8 *   http://www.apache.org/licenses/LICENSE-2.0
  9 *
 10 * Unless required by applicable law or agreed to in writing, software
 11 * distributed under the License is distributed on an "AS IS" BASIS,
 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13 * See the License for the specific language governing permissions and
 14 * limitations under the License.
 15 */
 16package org.diffkit.diff.conf.tst
 17
 18
 19import org.apache.commons.io.FilenameUtils;
 20import org.diffkit.db.DKDBConnectionInfo 
 21import org.diffkit.db.tst.DBTestSetup;
 22import org.diffkit.diff.conf.DKMagicPlan 
 23import org.diffkit.diff.conf.DKMagicPlanBuilder 
 24import org.diffkit.diff.diffor.DKEqualsDiffor 
 25import org.diffkit.diff.engine.DKSourceSink;
 26import org.diffkit.diff.sns.DKDBSource 
 27import org.diffkit.diff.sns.DKFileSource 
 28import org.diffkit.util.DKResourceUtil;
 29import org.diffkit.db.DKDBFlavor;
 30
 31
 32
 33/**
 34 * @author jpanico
 35 */
 36public class TestMagicPlanBuilder extends GroovyTestCase {
 37   
 38   /**
 39    * test that including two mutually exclusive properties, properties that trigger 
 40    * mutually exclusive rules, results in Exception
 41    */
 42   public void testMagicExclusion() {
 43      def lhsFileResourcePath = 'org/diffkit/diff/conf/tst/test.lhs.csv'
 44      def rhsFileResourcePath = 'org/diffkit/diff/conf/tst/test.rhs.csv'
 45      def lhsFile = DKResourceUtil.findResourceAsFile(lhsFileResourcePath)
 46      def rhsFile = DKResourceUtil.findResourceAsFile(rhsFileResourcePath)
 47      
 48      DKMagicPlan magicPlan = []
 49      magicPlan.lhsFilePath =lhsFile.absolutePath
 50      magicPlan.rhsFilePath = rhsFile.absolutePath
 51      magicPlan.lhsDBTableName = 'LHS_TABLE'
 52      magicPlan.rhsDBTableName = 'RHS_TABLE'
 53      
 54      DKMagicPlanBuilder builder = [magicPlan]
 55      shouldFail(RuntimeException) {
 56         def builtPlan = builder.build()
 57         assert builtPlan
 58      }
 59   }
 60   
 61   public void testFullyMagicFileBuild(){
 62      def lhsFileResourcePath = 'org/diffkit/diff/conf/tst/test.lhs.csv'
 63      def rhsFileResourcePath = 'org/diffkit/diff/conf/tst/test.rhs.csv'
 64      DKMagicPlan magicPlan = []
 65      def lhsFile = DKResourceUtil.findResourceAsFile(lhsFileResourcePath)
 66      def rhsFile = DKResourceUtil.findResourceAsFile(rhsFileResourcePath)
 67      magicPlan.lhsFilePath =lhsFile.absolutePath
 68      magicPlan.rhsFilePath = rhsFile.absolutePath
 69      magicPlan.delimiter = '\\,'
 70      
 71      DKMagicPlanBuilder builder = [magicPlan]
 72      def builtPlan = builder.build()
 73      assert builtPlan
 74      
 75      def lhsSource = builtPlan.lhsSource
 76      assert lhsSource
 77      assert lhsSource instanceof DKFileSource
 78      def lhsModel = lhsSource.model
 79      assert lhsModel
 80      assert lhsModel.columns
 81      assert lhsModel.columns.length ==3
 82      assert lhsModel.columns[0].name == 'column1'
 83      def lhsSourceFile = lhsSource.file
 84      assert lhsSourceFile
 85      assert lhsSourceFile.exists()
 86      def normalizedLhsSourceFilePath = FilenameUtils.normalize(lhsSourceFile.path)
 87      def normalizedLhsFileResourcePath = FilenameUtils.normalize(lhsFileResourcePath)
 88      assert normalizedLhsSourceFilePath.endsWith(normalizedLhsFileResourcePath)
 89      
 90      def tableComparison = builtPlan.tableComparison
 91      assert tableComparison
 92      assert tableComparison.rhsModel == builtPlan.rhsSource.model
 93      def comparisonMap = tableComparison.map
 94      assert comparisonMap
 95      assert comparisonMap.length == 3
 96      for ( i in 0..2 ) {
 97         comparisonMap[i]._lhsColumn.name == comparisonMap[i]._rhsColumn.name
 98         comparisonMap[i]._diffor instanceof DKEqualsDiffor
 99      }
100      assert tableComparison.diffIndexes == [1,2]
101      assert tableComparison.displayIndexes == [[0],[0]]
102   }
103   
104   public void testFullyMagicDBBuild(){
105      DBTestSetup.setupDB(new File('org/diffkit/diff/conf/tst/test.dbsetup.xml'), (File[])[new File('org/diffkit/diff/conf/tst/dbConnectionInfo.xml')], 'org/diffkit/diff/conf/tst/test.lhs.csv', 'org/diffkit/diff/conf/tst/test.rhs.csv')
106      DKDBConnectionInfo dbConnectionInfo = ['test', DKDBFlavor.H2, 'mem:conf.test;DB_CLOSE_DELAY=-1', null, null, 'test', 'test']
107      
108      DKMagicPlan magicPlan = []
109      magicPlan.lhsDBTableName = 'LHS_TABLE'
110      magicPlan.rhsDBTableName = 'RHS_TABLE'
111      magicPlan.dbConnectionInfo = dbConnectionInfo
112      
113      DKMagicPlanBuilder builder = [magicPlan]
114      def builtPlan = builder.build()
115      assert builtPlan
116      
117      def tableComparison = builtPlan.tableComparison
118      assert tableComparison
119      assert tableComparison.lhsModel == builtPlan.lhsSource.model
120      assert tableComparison.rhsModel == builtPlan.rhsSource.model
121      def comparisonMap = tableComparison.map
122      assert comparisonMap
123      assert comparisonMap.length == 3
124      for ( i in 0..2 ) {
125         comparisonMap[i]._lhsColumn.name == comparisonMap[i]._rhsColumn.name
126         comparisonMap[i]._diffor instanceof DKEqualsDiffor
127      }
128      assert tableComparison.diffIndexes == [1]
129      assert tableComparison.displayIndexes == [[0,2],[0,2]]
130      
131      def lhsSource = builtPlan.lhsSource
132      assert lhsSource
133      assert lhsSource instanceof DKDBSource
134      def lhsDBTable = lhsSource.table
135      assert lhsDBTable
136      assert lhsDBTable.tableName == 'LHS_TABLE'
137      assert lhsDBTable.columns
138      assert lhsDBTable.columns.length == 3
139      assert lhsDBTable.columns[0].name == 'COLUMN1'
140      assert lhsDBTable.columns[0].DBTypeName == 'VARCHAR'
141      
142      def rhsSource = builtPlan.rhsSource
143      assert rhsSource
144      assert rhsSource instanceof DKDBSource
145      def rhsDBTable = rhsSource.table
146      assert rhsDBTable
147      assert rhsDBTable.tableName == 'RHS_TABLE'
148      def rhsModel = rhsSource.model
149      assert rhsModel
150      assert rhsModel.name == 'PUBLIC.RHS_TABLE'
151      assert rhsModel.columns
152      assert rhsModel.columns.length ==3
153      assert rhsModel.columns[0].name == 'COLUMN1'
154      
155      def sink = builtPlan.sink
156      assert sink
157      assert sink.kind == DKSourceSink.Kind.STREAM
158   }
159}
160