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