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