PageRenderTime 17ms CodeModel.GetById 6ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

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