/test/unit/grails/plugin/extproc/FileHandlingServiceSpec.groovy

https://github.com/hennito/grails-ext-proc-plugin · Groovy · 243 lines · 189 code · 51 blank · 3 comment · 6 complexity · 1f56a0bd7992880531a7d674d8f85db5 MD5 · raw file

  1. package grails.plugin.extproc
  2. import grails.test.GrailsUnitTestCase
  3. import grails.test.mixin.TestMixin
  4. import grails.test.mixin.support.GrailsUnitTestMixin
  5. import grails.test.mixin.TestFor
  6. import grails.test.mixin.Mock
  7. import spock.lang.Specification
  8. import spock.lang.Unroll
  9. import spock.lang.Shared
  10. /**
  11. * See the API for {@link grails.test.mixin.web.ControllerUnitTestMixin} for usage instructions
  12. */
  13. @TestFor(FileHandlingService)
  14. class FileHandlingServiceSpec extends Specification {
  15. static final String nl = isWindows() ? "\r\n" : "\n"
  16. static final String HELLO = "hello world"
  17. static final String HELLO_LINE = "hello world" + nl
  18. @Shared File tmp
  19. def setup() {
  20. tmp = service.createTempDir()
  21. }
  22. def cleanup() {
  23. service.delDirectory tmp
  24. }
  25. private static boolean isWindows() {
  26. String nameOS = "os.name"
  27. return System.properties.get(nameOS).toString().toUpperCase().startsWith("WINDOWS")
  28. }
  29. void "test createTempDir no parameter"() {
  30. when:
  31. File tmpDir = service.createTempDir ()
  32. then:
  33. tmpDir.exists()
  34. tmpDir.isDirectory()
  35. when:
  36. service.delDirectory tmpDir
  37. then:
  38. !tmpDir.exists()
  39. }
  40. void "test createTempDir under system temp"() {
  41. when:
  42. String tempDirStr = tmp.absolutePath + "/myTmpTestDirFileHandlingService"
  43. File existingDir = new File(tempDirStr)
  44. existingDir.delete()
  45. existingDir.mkdir()
  46. then:
  47. existingDir.exists()
  48. existingDir.isDirectory()
  49. existingDir.absolutePath.startsWith(tempDirStr)
  50. when:
  51. File tmpDir = service.createTempDir(tempDirStr)
  52. then:
  53. tmpDir.exists()
  54. tmpDir.isDirectory()
  55. tmpDir.absolutePath.startsWith(tempDirStr)
  56. existingDir.absolutePath == tmpDir.absolutePath
  57. when:
  58. service.delDirectory tmpDir
  59. then:
  60. !tmpDir.exists()
  61. }
  62. void "test fileInTemp"() {
  63. when:
  64. File myFile = service.fileInTemp (tmp, "name.txt")
  65. then:
  66. myFile.absolutePath.startsWith(tmp.absolutePath)
  67. when:
  68. myFile << HELLO_LINE
  69. then:
  70. myFile.exists()
  71. !myFile.isDirectory()
  72. }
  73. void "test zipDir to byte array And Unzip to new temp file"() {
  74. given: "a file of known content"
  75. File myFile = createHelloFileInTmp("name.txt", 500)
  76. when: "we zip the directory content (=the file) into a bytearray"
  77. byte[] zip = service.zipDir (tmp, null)
  78. and: "delete the file"
  79. myFile.delete()
  80. then: "the file is gone but we got it zipped"
  81. zip.length > 0
  82. !myFile.exists()
  83. when: "we then unzip the bytearray to a new tempdir "
  84. File tmp2 = service.createTempDir ()
  85. service.unzipByteArrayToDir(zip, tmp2, null)
  86. and: "read the unzipped file of known content"
  87. myFile = service.fileInTemp (tmp2, "name.txt")
  88. then: "the content matches the known content"
  89. 500 == countHelloLines(myFile)
  90. cleanup:
  91. service.delDirectory tmp2
  92. }
  93. void "test zipTempDir to File and Unzip from File"() {
  94. given:
  95. String filename = "hello.txt"
  96. createHelloFileInTmp(filename, 4)
  97. when:
  98. byte[] zip = service.zipDir (tmp, null)
  99. service.delDirectory tmp
  100. then:
  101. !tmp.exists()
  102. when:
  103. tmp = service.createTempDir()
  104. File zipped = service.fileInTemp (tmp, "zip.zip")
  105. zipped << zip
  106. and:
  107. File tmp2 = service.createTempDir ()
  108. service.unzipFileToDir(zipped, tmp2, null)
  109. zipped.delete()
  110. then:
  111. 4 == countHelloLines(service.fileInTemp(tmp2, filename))
  112. cleanup:
  113. service.delDirectory tmp2
  114. }
  115. void "test regex closure for zip and unzip"() {
  116. given: "2 files with known content"
  117. File myFile1 = createHelloFileInTmp("name.txt", 500)
  118. File myFile2 = createHelloFileInTmp("name2.txt", 300)
  119. when: "we zip those files (null as closure includes the file) and delete them"
  120. byte[] zip = service.zipDir(tmp, null)
  121. myFile1.delete()
  122. myFile2.delete()
  123. then: "the zip has content and the files are gone"
  124. zip.size() > 0
  125. !myFile1.exists()
  126. !myFile2.exists()
  127. when: "we unzip the file ending in 2.txt from the zip to a new tmp folder"
  128. File tmp2 = service.createTempDir ()
  129. service.unzipByteArrayToDir(zip, tmp2) { fn -> fn =~ /.*2.txt$/}
  130. and: "we try to reconstruct the files"
  131. myFile1 = service.fileInTemp (tmp2, "name.txt")
  132. myFile2 = service.fileInTemp (tmp2, "name2.txt")
  133. then: "only the file ending in 2.txt exists and matches the known content"
  134. !myFile1.exists()
  135. 300 == countHelloLines(myFile2)
  136. cleanup:
  137. service.delDirectory tmp2
  138. }
  139. void "test zip is empty when regex/closure mismatch"() {
  140. given: "2 files with known content"
  141. File myFile1 = createHelloFileInTmp("name.txt", 5)
  142. File myFile2 = createHelloFileInTmp("name2.txt", 3)
  143. when:
  144. byte[] zip = service.zipDir (tmp, null)
  145. then:
  146. zip.size() > 0
  147. when:
  148. File tempDirectory = service.createTempDir()
  149. and:
  150. service.unzipByteArrayToDir(zip, tempDirectory) { fn -> fn =~ / nomatch / }
  151. then:
  152. tempDirectory.list().collect().isEmpty()
  153. cleanup:
  154. service.delDirectory tempDirectory
  155. }
  156. void "test closure works on zipDir and unzipDir"() {
  157. given: "2 files with known content"
  158. File myFile1 = createHelloFileInTmp("name.txt", 5)
  159. File myFile2 = createHelloFileInTmp("name2.txt", 3)
  160. when: "we zip them both"
  161. byte[] zip = service.zipDir (tmp) { fn -> ["name.txt","name2.txt"].contains(fn) }
  162. then:
  163. zip.size() > 0
  164. when:
  165. myFile1.delete()
  166. myFile2.delete()
  167. then:
  168. !myFile1.exists()
  169. !myFile2.exists()
  170. when: "we unzip just one of them"
  171. File tmpD = service.createTempDir()
  172. service.unzipByteArrayToDir(zip, tmpD) { fn -> fn == "name2.txt" }
  173. and:
  174. myFile1 = service.fileInTemp (tmpD, "name.txt")
  175. myFile2 = service.fileInTemp (tmpD, "name2.txt")
  176. then: "only one exists"
  177. !myFile1.exists()
  178. myFile2.exists()
  179. cleanup:
  180. service.delDirectory tmpD
  181. }
  182. private File createHelloFileInTmp(String filename, int lines) {
  183. File myFile = service.fileInTemp (tmp, filename)
  184. lines.times { myFile << HELLO_LINE }
  185. return myFile
  186. }
  187. private int countHelloLines(File input) {
  188. int cnt = 0
  189. input.eachLine { l -> if (HELLO.equals(l)) cnt++ }
  190. return cnt
  191. }
  192. }