/tst/org/diffkit/diff/testcase/TestCaseRun.groovy

http://diffkit.googlecode.com/ · Groovy · 129 lines · 93 code · 17 blank · 19 comment · 12 complexity · 4b15aea21637d4e4d50e3b2faae37c36 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.testcase
  17. import org.apache.commons.lang.ClassUtils;
  18. import org.apache.commons.lang.StringUtils;
  19. import org.apache.commons.lang.exception.ExceptionUtils;
  20. import org.slf4j.Logger;
  21. import org.slf4j.LoggerFactory;
  22. import org.diffkit.common.DKValidate;
  23. import org.diffkit.diff.conf.DKApplication;
  24. import org.diffkit.diff.conf.DKPassthroughPlan;
  25. import org.diffkit.diff.conf.DKPlan
  26. import org.diffkit.diff.engine.DKDiffEngine
  27. import org.diffkit.util.DKFileUtil;
  28. /**
  29. * @author jpanico
  30. */
  31. public class TestCaseRun {
  32. public final TestCase testCase
  33. public DKPlan plan
  34. private Date _start
  35. private Date _end
  36. private String _actualFile
  37. private boolean _isExecuted
  38. private Boolean _failed
  39. private Exception _exception
  40. private final Logger _log = LoggerFactory.getLogger(this.getClass())
  41. public TestCaseRun(TestCase testCase_, DKPlan plan_){
  42. this(testCase_, plan_, null, null, null)
  43. }
  44. public TestCaseRun(TestCase testCase_, DKPassthroughPlan plan_, Date start_, Date end_,
  45. String actualFile_){
  46. testCase = testCase_
  47. this.setPlan(plan_)
  48. _start = start_
  49. _end = end_
  50. _actualFile = actualFile_
  51. DKValidate.notNull(testCase)
  52. }
  53. public void setPlan(DKPassthroughPlan plan_){
  54. plan = plan_
  55. }
  56. public void diff(){
  57. DKApplication.doDiff plan.lhsSource, plan.rhsSource, plan.sink, plan.tableComparison, null
  58. }
  59. public void setIsExecuted(boolean isExecuted_){
  60. _isExecuted = isExecuted_
  61. }
  62. public void setException(Exception exception_){
  63. _exception = exception_;
  64. }
  65. public Boolean getFailed(){
  66. if(_failed)
  67. return _failed
  68. _log.debug("expectDiff->{}",testCase.expectDiff())
  69. if(testCase.expectDiff())
  70. return this.getDiffFailed();
  71. else if(testCase.expectException())
  72. return this.getExceptionFailed();
  73. throw new RuntimeException("reached unanticipated point in code")
  74. }
  75. private Boolean getDiffFailed(){
  76. File expectedFile = testCase.expectedFile
  77. // N.B. TestCaseRunner ensures that sink is File type
  78. File actualFile = plan.sink.file
  79. String expectedContent = DKFileUtil.readFullyAsString(expectedFile)
  80. String actualContent = DKFileUtil.readFullyAsString(actualFile)
  81. _failed = ! StringUtils.equals( expectedContent, actualContent)
  82. return _failed
  83. }
  84. private Boolean getExceptionFailed(){
  85. if(!testCase.expectException()){
  86. if(_exception)
  87. return true
  88. }
  89. if(!_exception)
  90. return true
  91. Exception rootException = ExceptionUtils.getRootCause(_exception)
  92. Class expectedExceptionClass = testCase.exceptionClass
  93. _log.debug("rootException->{}",rootException)
  94. _log.debug("expectedExceptionClass->{}",expectedExceptionClass)
  95. _log.debug("rootException.class != expectedExceptionClass->{}",rootException.class != expectedExceptionClass)
  96. if(rootException.class != expectedExceptionClass)
  97. return true
  98. _log.debug("rootException.class != expectedExceptionClass->{}",rootException.class != expectedExceptionClass)
  99. if(!rootException.message.startsWith(testCase.exceptionMessage ))
  100. return true
  101. return false
  102. }
  103. public String getReport(){
  104. _log.debug("_exception->{}",_exception)
  105. def resultString = (!this.failed ? 'PASSED' : '*FAILED*')
  106. return "${testCase.name} $resultString"
  107. }
  108. public String toString() {
  109. return String.format("%s(%s)",
  110. ClassUtils.getShortClassName(this.getClass()), testCase.name);
  111. }
  112. }