PageRenderTime 21ms CodeModel.GetById 9ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 1ms

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

http://diffkit.googlecode.com/
Groovy | 99 lines | 68 code | 13 blank | 18 comment | 12 complexity | bf2d4b57fa23b1a3dc3f2bd91392878b 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
18import org.apache.commons.lang.ClassUtils;
19
20import org.diffkit.diff.engine.DKColumnModel 
21import org.diffkit.diff.engine.DKTableModel;
22import org.diffkit.diff.sns.DKFileSource 
23import org.diffkit.util.DKResourceUtil;
24import org.diffkit.util.DKStringUtil;
25
26import groovy.util.GroovyTestCase;
27
28
29/**
30 * @author jpanico
31 */
32public class TestFileSource extends GroovyTestCase {
33	
34	public void testDefaultModelWithKeyColumnNames(){
35		String sourceFileName = 'lhs1.csv'
36		String sourceFilePath = ClassUtils.getPackageName(this.getClass()) 
37		sourceFilePath = DKStringUtil.packageNameToResourcePath(sourceFilePath) + sourceFileName
38		def sourceFile = DKResourceUtil.findResourceAsFile(sourceFilePath)
39		println "sourceFile->$sourceFile"
40		DKFileSource source = new DKFileSource(sourceFile.absolutePath, null, (String[])['column2'], null,'\\,',  true, true)
41		def model = source.model
42		assert model
43		def columns = model.columns
44		assert columns
45		assert columns.length == 3
46		assert columns[0].name == 'column1'
47		assert model.key == [1]
48	}
49	
50	public void testDefaultModel(){
51		String sourceFileName = 'lhs1.csv'
52		String sourceFilePath = ClassUtils.getPackageName(this.getClass()) 
53		sourceFilePath = DKStringUtil.packageNameToResourcePath(sourceFilePath) + sourceFileName
54		def sourceFile = DKResourceUtil.findResourceAsFile(sourceFilePath)
55		println "sourceFile->$sourceFile"
56		DKFileSource source = new DKFileSource(sourceFile.absolutePath, null, null, null,'\\,',  true, true)
57		def model = source.model
58		assert model
59		def columns = model.columns
60		assert columns
61		assert columns.length == 3
62		assert columns[0].name == 'column1'
63		assert model.key == [0]
64	}
65	
66	public void testRead(){
67		String sourceFileName = 'lhs1.csv'
68		String sourceFilePath = ClassUtils.getPackageName(this.getClass()) 
69		sourceFilePath = DKStringUtil.packageNameToResourcePath(sourceFilePath) + sourceFileName
70		def sourceFile = DKResourceUtil.findResourceAsFile(sourceFilePath)
71		println "sourceFile->$sourceFile"
72		
73		DKTableModel model = this.createSimpleTableModel()
74		DKFileSource source = new DKFileSource(sourceFile.absolutePath, model, null, null,'\\,',  true, true)
75		println "source->$source"
76		
77		source.open(null)
78		assert source.getNextRow() == (Object[])['1111', '1111', 1]
79		assert source.getNextRow() == (Object[])['1111', '1111', 2]
80		assert source.getNextRow() == (Object[])['4444', '4444', 1]
81		assert source.getNextRow() == (Object[])['4444', '4444', 2]
82		assert source.getNextRow() == (Object[])['6666', '6666', null]
83		assert source.getNextRow() == (Object[])['6666', '6666', 2]
84		assert !source.getNextRow()
85		assert !source.getNextRow()
86		source.close(null)
87	}
88	
89	private DKTableModel createSimpleTableModel(){
90		DKColumnModel column1 = [0, 'column1', DKColumnModel.Type.STRING]
91		DKColumnModel column2 = [1, 'column2', DKColumnModel.Type.STRING]
92		DKColumnModel column3 = [2, 'column3', DKColumnModel.Type.INTEGER, '###']
93		DKColumnModel[] columns = [column1, column2, column3]
94		int[] key = [0,2]
95		
96		return new DKTableModel("simple_table_model",columns, key) 
97	}
98	
99}