PageRenderTime 28ms CodeModel.GetById 16ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/tst/org/diffkit/diff/sns/tst/TestFileSink.groovy

http://diffkit.googlecode.com/
Groovy | 96 lines | 58 code | 20 blank | 18 comment | 2 complexity | 83cfdf08b4d67ad7e00f84cff4cf06d5 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.sns.tst
17
18
19import java.io.File;
20
21import org.apache.commons.lang.ClassUtils;
22
23import org.diffkit.diff.diffor.DKEqualsDiffor;
24import org.diffkit.diff.engine.DKColumnComparison 
25import org.diffkit.diff.engine.DKColumnDiffRow 
26import org.diffkit.diff.engine.DKColumnModel 
27import org.diffkit.diff.engine.DKDiff;
28import org.diffkit.diff.engine.DKRowDiff 
29import org.diffkit.diff.engine.DKSide;
30import org.diffkit.diff.engine.DKStandardTableComparison 
31import org.diffkit.diff.engine.DKTableModel 
32import org.diffkit.diff.sns.DKFileSink 
33import org.diffkit.util.DKFileUtil;
34import org.diffkit.util.DKResourceUtil;
35import org.diffkit.util.DKStringUtil;
36
37
38/**
39 * @author jpanico
40 */
41public class TestFileSink extends GroovyTestCase {
42	
43	
44	public void testSink(){
45		File sinkFile = ['./testSink.diff']
46		if(sinkFile.exists())
47			sinkFile.delete()
48		
49		DKFileSink sink = [sinkFile, false]
50		Object[] row = ['1111', '1111', 1]
51		def plan = this.createSimplePlan()
52		def keyValues = plan.getRowKeyValues( row, DKSide.LEFT_INDEX)
53		def displayValues = plan.getRowDisplayValues( row, DKSide.LEFT_INDEX )
54		
55		sink.open()
56		
57		DKRowDiff rowDiff = [1, row, DKSide.LEFT, plan]
58		sink.record(rowDiff, null)
59		
60		DKColumnDiffRow diffRow = [1, row, row, plan]
61		def columnDiff = diffRow.createDiff(1, '1111', 'xxxx')
62		sink.record(columnDiff, null)
63		
64		sink.close()
65		
66		String expectedFileName = 'testSink.diff'
67		String expectedFilePath = ClassUtils.getPackageName(this.getClass()) 
68		expectedFilePath = DKStringUtil.packageNameToResourcePath(expectedFilePath) + expectedFileName
69		def expectedFile = DKResourceUtil.findResourceAsFile(expectedFilePath)
70		
71		String expected = DKFileUtil.readFullyAsString( expectedFile)
72		assert expected
73		String actual = DKFileUtil.readFullyAsString( sinkFile)
74		assert actual
75		
76		assert expected == actual
77	}
78	
79	private DKStandardTableComparison createSimplePlan() {
80		DKTableModel tableModel = this.createSimpleTableModel();
81		DKColumnComparison[] map = DKColumnComparison.createColumnPlans( tableModel, tableModel, (int[]) [1], DKEqualsDiffor.instance)
82		DKStandardTableComparison plan = new DKStandardTableComparison(tableModel, tableModel, DKDiff.Kind.BOTH, map, (int[])[0], (int[][])[[1,2],[1,2]], (long)100)
83		return plan
84	}
85	
86	private DKTableModel createSimpleTableModel(){
87		DKColumnModel column1 = [0, 'column1', DKColumnModel.Type.STRING]
88		DKColumnModel column2 = [1, 'column2', DKColumnModel.Type.STRING]
89		DKColumnModel column3 = [2, 'column3', DKColumnModel.Type.INTEGER]
90		DKColumnModel[] columns = [column1, column2, column3]
91		int[] key = [0,2]
92		
93		return new DKTableModel("simple_table_model",columns, key) 
94	}
95	
96}