PageRenderTime 78ms CodeModel.GetById 25ms app.highlight 49ms RepoModel.GetById 2ms app.codeStats 0ms

/tst/org/diffkit/diff/engine/tst/TestTableComparison.groovy

http://diffkit.googlecode.com/
Groovy | 247 lines | 186 code | 38 blank | 23 comment | 11 complexity | 5f02330a7eef68ced2391ed05919b615 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.engine.tst
 17
 18
 19
 20import org.apache.commons.collections.comparators.ComparatorChain 
 21
 22import org.diffkit.diff.diffor.DKEqualsDiffor;
 23import org.diffkit.diff.engine.DKColumnComparison 
 24import org.diffkit.diff.engine.DKColumnModel
 25import org.diffkit.diff.engine.DKDiff;
 26import org.diffkit.diff.engine.DKSide;
 27import org.diffkit.diff.engine.DKStandardTableComparison 
 28import org.diffkit.diff.engine.DKTableModel 
 29
 30import groovy.util.GroovyTestCase
 31
 32
 33/**
 34 * @author jpanico
 35 */
 36public class TestTableComparison extends GroovyTestCase {
 37	
 38	public void testRowDisplayValues(){
 39		DKColumnModel column1_1 = [0, 'column1', DKColumnModel.Type.STRING]
 40		DKColumnModel column1_2 = [1, 'column2', DKColumnModel.Type.STRING]
 41		DKColumnModel column1_3 = [2, 'column3', DKColumnModel.Type.INTEGER]
 42		DKColumnModel[] columns = [column1_1, column1_2, column1_3]
 43		int[] key = [0,2]
 44		DKTableModel lhsTable = ["lhs_table",columns, key]
 45		
 46		DKColumnModel column2_1 = [0, 'column1', DKColumnModel.Type.STRING]
 47		DKColumnModel column2_2 = [1, 'column2', DKColumnModel.Type.INTEGER]
 48		DKColumnModel column2_3 = [2, 'column3', DKColumnModel.Type.INTEGER]
 49		columns = [column2_1, column2_2, column2_3]
 50		key = [0,2]
 51		DKTableModel rhsTable = ["rhs_table",columns, key]
 52		
 53		DKColumnComparison[] map = DKColumnComparison.createColumnPlans(lhsTable, rhsTable, (int[])[1], DKEqualsDiffor.instance)
 54		int[][] displayIndexes = [(int[])[0,2],(int[])[0,2]]
 55		DKStandardTableComparison plan = new DKStandardTableComparison(lhsTable, rhsTable, DKDiff.Kind.BOTH, map, (int[])[1], displayIndexes, 100)
 56		
 57		def rowDisplayValues = plan.getRowDisplayValues((Object[])['zzzz', 'zzzz', 1], (Object[])['zzzz', 'zzzz', 1])
 58		//		println "rowDisplayValues->$rowDisplayValues"
 59		assert rowDisplayValues == [column1:'zzzz', column3:'1']
 60		
 61		rowDisplayValues = plan.getRowDisplayValues((Object[])['zzzz', 'zzzz', 1], (Object[])['aaaa', 'zzzz', 1])
 62		assert rowDisplayValues == [column1:'zzzz:aaaa', column3:'1']
 63		
 64		rowDisplayValues = plan.getRowDisplayValues((Object[])[null, 'zzzz', 1], (Object[])['aaaa', 'zzzz', 1])
 65		assert rowDisplayValues == [column1:'<null>:aaaa', column3:'1']
 66		
 67		displayIndexes = [(int[])[0,2],(int[])[1]]
 68		plan = new DKStandardTableComparison(lhsTable, rhsTable, DKDiff.Kind.BOTH, map, (int[])[1], displayIndexes, 100)
 69		rowDisplayValues = plan.getRowDisplayValues((Object[])['zzzz', 'zzzz', 1], (Object[])['zzzz', 'cccc', 1])
 70		assert rowDisplayValues == [column1:'zzzz:', column2:':cccc', column3:'1:']
 71		
 72		column1_1 = [0, 'column1_1', DKColumnModel.Type.STRING]
 73		column1_2 = [1, 'column1_2', DKColumnModel.Type.STRING]
 74		column1_3 = [2, 'column1_3', DKColumnModel.Type.INTEGER]
 75		columns = [column1_1, column1_2, column1_3]
 76		key = [0,2]
 77		lhsTable = ["lhs_table",columns, key]
 78		
 79		column2_1 = [0, 'column2_1', DKColumnModel.Type.STRING]
 80		column2_2 = [1, 'column2_2', DKColumnModel.Type.INTEGER]
 81		column2_3 = [2, 'column2_3', DKColumnModel.Type.INTEGER]
 82		columns = [column2_1, column2_2, column2_3]
 83		key = [0,2]
 84		rhsTable = ["rhs_table",columns, key]
 85		
 86		displayIndexes = [(int[])[0,2],(int[])[0,2]]
 87		map =DKColumnComparison.createColumnPlans(lhsTable, rhsTable, (int[])[1], DKEqualsDiffor.instance)
 88		plan = new DKStandardTableComparison(lhsTable, rhsTable, DKDiff.Kind.BOTH, map, (int[])[1], displayIndexes, 100)
 89		rowDisplayValues = plan.getRowDisplayValues((Object[])['zzzz', 'zzzz', 1], (Object[])['zzzz', 'zzzz', 1])
 90		println "rowDisplayValues->$rowDisplayValues"
 91		assert rowDisplayValues == [column1_1:'zzzz:', column2_1:':zzzz', column1_3:'1:', column2_3:':1']
 92		
 93		column1_1 = [0, 'column1_1', DKColumnModel.Type.STRING]
 94		column1_2 = [1, 'column1_2', DKColumnModel.Type.STRING]
 95		column1_3 = [2, 'column3', DKColumnModel.Type.INTEGER]
 96		columns = [column1_1, column1_2, column1_3]
 97		key = [0,2]
 98		lhsTable = ["lhs_table",columns, key]
 99		
100		column2_1 = [0, 'column2_1', DKColumnModel.Type.STRING]
101		column2_2 = [1, 'column2_2', DKColumnModel.Type.INTEGER]
102		column2_3 = [2, 'column3', DKColumnModel.Type.INTEGER]
103		columns = [column2_1, column2_2, column2_3]
104		key = [0,2]
105		rhsTable = ["rhs_table",columns, key]
106		
107		displayIndexes = [(int[])[0,2],(int[])[0,2]]
108		map =DKColumnComparison.createColumnPlans(lhsTable, rhsTable, (int[])[1], DKEqualsDiffor.instance)
109		plan = new DKStandardTableComparison(lhsTable, rhsTable, DKDiff.Kind.BOTH, map, (int[])[1], displayIndexes, 100)
110		rowDisplayValues = plan.getRowDisplayValues((Object[])['zzzz', 'zzzz', 1], (Object[])['zzzz', 'zzzz', 1])
111		assert rowDisplayValues == [column1_1:'zzzz:', column2_1:':zzzz', column3:'1']
112	}
113	
114	public void testSimpleRowDisplayValues() {
115		DKColumnModel column1_1 = [0, 'column1', DKColumnModel.Type.STRING]
116		DKColumnModel column1_2 = [1, 'column2', DKColumnModel.Type.STRING]
117		DKColumnModel column1_3 = [2, 'column3', DKColumnModel.Type.INTEGER]
118		DKColumnModel[] columns = [column1_1, column1_2, column1_3]
119		int[] key = [0,2]
120		int[][] displayIndexes = [(int[])[0,2],(int[])[0,2]]
121		DKTableModel lhsTable = ["lhs_table",columns, key]
122		
123		DKColumnComparison[] map = DKColumnComparison.createColumnPlans(lhsTable, lhsTable, (int[])[1], DKEqualsDiffor.instance)
124		DKStandardTableComparison plan = [lhsTable, lhsTable, DKDiff.Kind.BOTH, map, (int[])[1], displayIndexes, 100]
125		def rowDisplayValues = plan.getRowDisplayValues((Object[])['zzzz', 'aaaa', 1], DKSide.LEFT_INDEX)
126		//		println "rowDisplayValues->$rowDisplayValues"
127		assert rowDisplayValues == [column1:'zzzz', column3:'1']
128		rowDisplayValues = plan.getRowDisplayValues((Object[])['zzzz', 'aaaa', null], DKSide.LEFT_INDEX)
129		//		println "rowDisplayValues->$rowDisplayValues"
130		assert rowDisplayValues == [column1:'zzzz', column3:'<null>']
131	}
132	
133	public void testComparatorSort(){
134		DKColumnModel column1_1 = [0, 'column1_1', DKColumnModel.Type.STRING]
135		DKColumnModel column1_2 = [1, 'column1_2', DKColumnModel.Type.STRING]
136		DKColumnModel column1_3 = [2, 'column1_3', DKColumnModel.Type.INTEGER]
137		DKColumnModel[] columns = [column1_1, column1_2, column1_3]
138		int[] key = [0,2]
139		DKTableModel lhsTable = ["lhs_table",columns, key]
140		
141		DKColumnModel column2_1 = [0, 'column2_1', DKColumnModel.Type.STRING]
142		DKColumnModel column2_2 = [1, 'column2_2', DKColumnModel.Type.INTEGER]
143		DKColumnModel column2_3 = [2, 'column2_3', DKColumnModel.Type.INTEGER]
144		columns = [column2_1, column2_2, column2_3]
145		key = [0,2]
146		DKTableModel rhsTable = ["rhs_table",columns, key]
147		
148		DKColumnComparison[] map = DKColumnComparison.createColumnPlans(lhsTable, rhsTable, (int[])[1], DKEqualsDiffor.instance)
149		DKStandardTableComparison plan = new DKStandardTableComparison(lhsTable, rhsTable, DKDiff.Kind.BOTH, map,  (int[])[1], (int[][])null, 100)
150		def comparator = plan.rowComparator
151		
152		Object[] l1 = ['zzzz', 'aaaa', 1]
153		Object[] l2 = ['zzzz', 'zzzz', 2]
154		Object[] l3 = ['bbbb', 'zzzz', 1]
155		Object[] l4 = ['aaaa', 'bbbb', 2]
156		def rows = [l1, l2, l3, l4]
157		Collections.sort( rows, comparator)
158		
159		assert rows == [l4,l3,l1,l2]
160	}
161	
162	public void testIdenticalTablesSimpleKeyComparator(){
163		DKColumnModel column1 = [0, 'column1', DKColumnModel.Type.STRING]
164		DKColumnModel column2 = [1, 'column2', DKColumnModel.Type.STRING]
165		DKColumnModel column3 = [2, 'column3', DKColumnModel.Type.INTEGER]
166		DKColumnModel column4 = [3, 'column4', DKColumnModel.Type.STRING]
167		DKColumnModel[] columns = [column1, column2, column3, column4]
168		int[] key = [1]
169		DKTableModel tableModel = ["table_model",columns, key]
170		DKColumnComparison[] map = DKColumnComparison.createColumnPlans(tableModel,tableModel, (int[])[1], DKEqualsDiffor.instance)
171		DKStandardTableComparison plan = [tableModel, tableModel, DKDiff.Kind.BOTH, map, (int[])[1], null, 100]
172		def comparator = plan.rowComparator
173		assert comparator
174		assert comparator instanceof ComparatorChain
175		assert comparator.size() == 1
176		
177		Object[] lhs = ['1111', 'aaaa', 3, 'value4'] 
178		Object[] rhs = ['2222', 'bbbb', 3, 'value4'] 
179		
180		assert comparator.compare(lhs, rhs) <0
181		
182		key = [2]
183		tableModel = ["table_model",columns, key]
184		plan = [tableModel, tableModel, DKDiff.Kind.BOTH, map, (int[])[1], null, 100]
185		comparator = plan.rowComparator     
186		assert comparator.compare(lhs, rhs) ==0
187		
188		key = [3]
189		tableModel = ["table_model",columns, key]
190		plan = [tableModel, tableModel, DKDiff.Kind.BOTH, map, (int[])[1], null, 100]
191		comparator = plan.rowComparator     
192		assert comparator.compare(lhs, rhs) ==0
193		
194	}
195	
196	public void testFailures(){
197		DKColumnModel column1_1 = [0, 'column1_1', DKColumnModel.Type.STRING]
198		DKColumnModel[] columns = [column1_1]
199		int[] key = [0]
200		DKTableModel lhsTable = ["lhs_table",columns, key]
201		
202		DKColumnModel column2_1 = [0, 'column2_1', DKColumnModel.Type.STRING]
203		DKColumnModel column2_2 = [1, 'column2_2', DKColumnModel.Type.INTEGER]
204		DKColumnModel column2_3 = [2, 'column2_3', DKColumnModel.Type.STRING]
205		columns = [column2_1, column2_2, column2_3]
206		key = [0,2]
207		DKTableModel rhsTable = ["rhs_table",columns, key]
208		DKColumnComparison[] map = DKColumnComparison.createColumnPlans(lhsTable, rhsTable, (int[])[0], DKEqualsDiffor.instance)
209		// key sizes don't match
210		shouldFail(RuntimeException) {
211			def DKStandardTableComparison plan = new DKStandardTableComparison(lhsTable, rhsTable, DKDiff.Kind.BOTH, map, (int[])[1], null, 100)
212		}
213		
214		DKColumnModel column1_2 = [1, 'column1_2', DKColumnModel.Type.INTEGER]
215		DKColumnModel column1_3 = [2, 'column1_3', DKColumnModel.Type.STRING]
216		columns = [column1_1, column1_2, column1_3]
217		key = [0,1]
218		lhsTable = ["lhs_table",columns, key]
219		// key type don't match
220		shouldFail(RuntimeException) {
221			def DKStandardTableComparison plan = new DKStandardTableComparison(lhsTable, rhsTable,  DKDiff.Kind.BOTH, map, (int[])[1], null, 100)
222		}
223	}
224	
225	public void testDifferentTablesCompoundKeyComparator(){
226		DKColumnModel column1_1 = [0, 'column1_1', DKColumnModel.Type.STRING]
227		DKColumnModel column1_2 = [1, 'column1_2', DKColumnModel.Type.STRING]
228		DKColumnModel column1_3 = [2, 'column1_3', DKColumnModel.Type.INTEGER]
229		DKColumnModel[] columns = [column1_1, column1_2, column1_3]
230		int[] key = [0,2]
231		DKTableModel lhsTable = ["lhs_table",columns, key]
232		
233		DKColumnModel column2_1 = [0, 'column2_1', DKColumnModel.Type.STRING]
234		DKColumnModel column2_2 = [1, 'column2_2', DKColumnModel.Type.STRING]
235		DKColumnModel column2_3 = [2, 'column2_3', DKColumnModel.Type.INTEGER]
236		DKColumnModel column2_4 = [3, 'column2_4', DKColumnModel.Type.INTEGER]
237		columns = [column2_1, column2_2, column2_3, column2_4]
238		key = [1,3]
239		DKTableModel rhsTable = ["rhs_table",columns, key]
240		DKColumnComparison[] map = DKColumnComparison.createColumnPlans(lhsTable, rhsTable, (int[])[1], DKEqualsDiffor.instance)
241		DKStandardTableComparison plan = new DKStandardTableComparison(lhsTable, rhsTable, DKDiff.Kind.BOTH, map,  (int[])[1], null, 100)
242		def comparator = plan.rowComparator
243		assert comparator
244		assert comparator instanceof ComparatorChain
245		assert comparator.size() == 2
246	}
247}