PageRenderTime 26ms CodeModel.GetById 2ms app.highlight 18ms RepoModel.GetById 2ms app.codeStats 0ms

/tst/org/diffkit/util/tst/TestFileUtil.groovy

http://diffkit.googlecode.com/
Groovy | 104 lines | 65 code | 18 blank | 21 comment | 5 complexity | ed7d0deb505608b508adfe362c07ec70 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.util.tst
 17
 18
 19
 20import java.io.File;
 21import org.apache.commons.io.FileUtils 
 22import org.apache.commons.io.filefilter.SuffixFileFilter 
 23import org.diffkit.util.DKFileUtil 
 24import org.diffkit.util.DKResourceUtil 
 25
 26
 27/**
 28 * @author jpanico
 29 */
 30public class TestFileUtil extends GroovyTestCase {
 31   
 32   /**
 33    * does it work from relative paths, or just absolute?
 34    */
 35   public void testCanRead() {
 36      assert DKFileUtil.canReadFilePaths((String[]) ['conf/logback.xml'])
 37   }
 38   
 39   public void testPrepend() {
 40      File sourceTarget = DKResourceUtil.findResourceAsFile("org/diffkit/util/tst/prepend_target.txt")
 41      assert sourceTarget
 42      File testTarget = ['./prependTest.tst']
 43      FileUtils.copyFile( sourceTarget, testTarget)
 44      def prependString = 'prepend\nprepend\nprepend\n---\n'
 45      DKFileUtil.prepend( prependString, testTarget)
 46      
 47      def sourceText = FileUtils.readFileToString(sourceTarget)
 48      def prependedText = FileUtils.readFileToString(testTarget)
 49      
 50      assert prependedText == prependString + sourceText
 51   }
 52   
 53   public void testCopyWithSubstitution() {
 54      File sourceFile = DKResourceUtil.findResourceAsFile("org/diffkit/util/tst/copyWithSubstitution_target.txt")
 55      assert sourceFile
 56      File copiedFile = ['./copyWithSubstitutionTest.tst']
 57      def substitutions = ['Beware':'swear', 'frumious':'frumpy']
 58      DKFileUtil.copyFile( sourceFile, copiedFile, substitutions)
 59      
 60      File expectedFile = DKResourceUtil.findResourceAsFile("org/diffkit/util/tst/copyWithSubstitution_expected.txt")
 61      def expectedText = FileUtils.readFileToString(expectedFile)
 62      def copiedText = FileUtils.readFileToString(copiedFile)
 63      
 64      assert copiedText == expectedText
 65   }
 66   
 67   public void testCopyDirectory() {
 68      File sourceFile = DKResourceUtil.findResourceAsFile("org/diffkit/util/tst/copyWithSubstitution_target.txt")
 69      assert sourceFile
 70      File sourceDirectory = sourceFile.getParentFile()
 71      assert sourceDirectory
 72      assert sourceDirectory.isDirectory()
 73      
 74      FilenameFilter filenameFilter= new SuffixFileFilter(".txt")
 75      File[] sourceFiles = sourceDirectory.listFiles((FilenameFilter)filenameFilter)
 76      assert sourceFiles
 77      assert sourceFiles.length == 3
 78      
 79      File destDir = ['./']
 80      def substitutions = ['Beware':'swear', 'frumious':'frumpy']
 81      DKFileUtil.copyDirectory( sourceDirectory, destDir, filenameFilter,substitutions)
 82      File[] destFiles = destDir.listFiles((FilenameFilter)filenameFilter)
 83      assert destFiles
 84      assert destFiles.length == 3
 85      
 86      File expectedFile = DKResourceUtil.findResourceAsFile("org/diffkit/util/tst/copyWithSubstitution_expected.txt")
 87      def expectedText = FileUtils.readFileToString(expectedFile)
 88      def copiedFile = new File(destDir, sourceFile.name )
 89      def copiedText = FileUtils.readFileToString(copiedFile)
 90      
 91      assert copiedText == expectedText
 92   }
 93   
 94   public void testIsRelative(){
 95      println "separtor->${File.separator}"
 96      println "path->${new File('./')}"
 97      assert DKFileUtil.isRelative(new File("."))
 98      assert ! DKFileUtil.isRelative(new File(""))
 99      assert DKFileUtil.isRelative(new File('./'))
100      assert DKFileUtil.isRelative(new File("./test/"))
101      assert DKFileUtil.isRelative(new File(".\\"))
102      assert ! DKFileUtil.isRelative(new File("/Users/joe/tmp"))
103   }
104}