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