PageRenderTime 33ms CodeModel.GetById 17ms app.highlight 12ms RepoModel.GetById 2ms app.codeStats 0ms

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

http://diffkit.googlecode.com/
Groovy | 162 lines | 120 code | 18 blank | 24 comment | 19 complexity | 5a831c3aba37beb6a496d3115ef82506 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 java.io.File;
 19import java.lang.reflect.Constructor;
 20
 21import org.apache.commons.io.FileUtils;
 22import org.apache.commons.lang.ClassUtils;
 23import org.apache.commons.lang.StringUtils;
 24import org.apache.commons.lang.builder.ReflectionToStringBuilder;
 25
 26import org.diffkit.common.DKValidate;
 27
 28
 29
 30/**
 31 * @author jpanico
 32 */
 33public class TestCase implements Comparable<TestCase>{
 34   public static final String DEFAULT_CONNECTION_INFO_FILE_NAME = 'dbConnectionInfo.xml'
 35   
 36   public final Integer id
 37   public final String name
 38   public final String description
 39   public final File dbSetupFile
 40   public final File lhsSourceFile
 41   public final File rhsSourceFile
 42   public final File lhsConnectionInfoFile
 43   public final File rhsConnectionInfoFile
 44   public final File planFile
 45   public final File expectedFile
 46   public final File exceptionFile
 47   public final Class exceptionClass
 48   public final String exceptionMessage
 49   private Boolean _expectedDiff
 50   private File _defaultConnectionInfoFile
 51   
 52   public TestCase(Integer id_, String name_, String description_, 
 53   File dbSetupFile_, File lhsSourceFile_, File rhsSourceFile_, 
 54   File lhsConnectionInfoFile_, File rhsConnectionInfoFile_, File planFile_, 
 55   File expectedFile_, File exceptionFile_){
 56      
 57      id = id_
 58      name = name_
 59      description = description_
 60      dbSetupFile = dbSetupFile_
 61      planFile = planFile_
 62      lhsSourceFile = lhsSourceFile_
 63      rhsSourceFile = rhsSourceFile_
 64      lhsConnectionInfoFile = lhsConnectionInfoFile_
 65      rhsConnectionInfoFile = rhsConnectionInfoFile_
 66      expectedFile = expectedFile_
 67      exceptionFile = exceptionFile_
 68      DKValidate.notNull(id, name)
 69      this.validate()
 70      Exception exception = this.parseExceptionFile()
 71      if(exception != null){
 72         exceptionClass = exception.class
 73         exceptionMessage = exception.message
 74      }
 75      else {
 76         exceptionClass=null
 77         exceptionMessage=null
 78      }
 79   }
 80   
 81   public File[] getConnectionInfoFiles(){
 82      def connectionInfoFiles = []
 83      if(lhsConnectionInfoFile)
 84         connectionInfoFiles.add(lhsConnectionInfoFile)
 85      if(rhsConnectionInfoFile)
 86         connectionInfoFiles.add(rhsConnectionInfoFile)
 87      if(!connectionInfoFiles)
 88         connectionInfoFiles.add(this.getDefaultConnectionInfoFile())
 89      return connectionInfoFiles
 90   }
 91   
 92   private File getDefaultConnectionInfoFile(){
 93      if(_defaultConnectionInfoFile)
 94         return _defaultConnectionInfoFile
 95      File dir = planFile.parentFile
 96      _defaultConnectionInfoFile = [dir, DEFAULT_CONNECTION_INFO_FILE_NAME]
 97      return _defaultConnectionInfoFile
 98   }
 99   
100   public String toString() {
101      return String.format("%s(%s)",
102      ClassUtils.getShortClassName(this.getClass()), name);
103   }
104   
105   public String getDescription() {
106      return ReflectionToStringBuilder.toString(this);
107   }
108   
109   @Override
110   public int compareTo(TestCase target_) {
111      if (id < target_.id)
112         return -1;
113      if (id > target_.id)
114         return 1;
115      return 0;
116   }
117   
118   public Boolean expectDiff(){
119      if(_expectedDiff!=null)
120         return _expectedDiff
121      _expectedDiff = expectedFile.canRead()
122      return _expectedDiff
123   }
124   
125   public boolean expectException() {
126      return ! (this.expectDiff())
127   }
128   
129   private Exception parseExceptionFile(){
130      if(!exceptionFile.canRead())
131         return null
132      String exceptionString = FileUtils.readFileToString(exceptionFile)
133      String[] lines = StringUtils.split(exceptionString, '\n')
134      if(!lines.length == 2)
135         throw new RuntimeException(String.format("invalid exception file contents [%s]", exceptionString))
136      Class exceptionClass = Class.forName(lines[0])
137      Constructor exceptionConstructor = exceptionClass.getDeclaredConstructor(String.class)
138      return exceptionConstructor.newInstance(lines[1])
139   }
140   /**
141    * the receiver validates itself
142    */
143   private void validate(){
144//      this.validateResourceFile('lhsSourceFile',lhsSourceFile)
145//      this.validateResourceFile('rhsSourceFile', rhsSourceFile)
146      this.validateResourceFile('planFile', planFile)
147      boolean expectedPresent = expectedFile.canRead()
148      boolean exceptionPresent = exceptionFile.canRead()
149      // need either one or the other
150      if(! (expectedPresent || exceptionPresent) )
151         throw new RuntimeException("Missing one or the other of [$expectedFile], [$exceptionFile]")
152      if( expectedPresent && exceptionPresent)
153         throw new RuntimeException("Cannot specify both files, only one or the other: [$expectedFile], [$exceptionFile]")
154   }
155   
156   private void validateResourceFile(String name_, File resourceFile_){
157      if(!resourceFile_)
158         throw new RuntimeException("does not allow null value for->$name_")
159      if(!resourceFile_.canRead())
160         throw new RuntimeException("cannot read resourceFile_->$resourceFile_")
161   }
162}