/*
* FileUtilsTests.groovy
* Copyright (c) 1998-2008, The University of Sheffield.
*
* This code is from the GATE project (http://gate.ac.uk/) and is free
* software licenced under the GNU General Public License version 3. It is
* distributed without any warranty. For more details see COPYING.txt in the
* top level directory (or at http://gatewiki.sf.net/COPYING.txt).
*/
import gate.util.FileUtils
import org.apache.log4j.Logger
/**
* Some tests for file utilities.
* @author Hamish Cunningham
*/
class FileUtilsTests extends GroovyTestCase {
void setUp() {
// to print the current state of the classpath:
// this.getClass().classLoader.rootLoader.URLs.each{ println it }
}
void testSetBodyDiv() {
log.debug("doing testSetBodyDiv")
String pageString = '''
'''
String expected = '''
new text!
'''.replace(' ', 'X').replace('\n', 'Y').replace('\t', 'Z')
String text = "new text!"
String divId = "leftBar"
String result = FileUtils.setBodyDiv(pageString, divId, text)
.replace(' ', 'X').replace('\n', 'Y').replace('\t', 'Z')
assertEquals( "result wrong:\n${result}\n${expected}", result, expected )
log.debug("finished doing testSetBodyDiv")
} // testSetBodyDiv()
void testFindFileInTree() {
log.debug("doing testFileFileInTree")
// get a couple of directories from the test resource tree
String pageFileDirPath =
this.getClass().getResource("/gate/yam/resources").getFile()
File wikiTopDir =
new File(
this.getClass().getResource("/gate/yam/resources").getFile()
).parentFile.parentFile
File pageFileDir = new File(pageFileDirPath)
log.debug("pageFileDir: ${pageFileDir}")
// find a file two directories up
File foundFile =
FileUtils.findFileInTree("navigation.html", wikiTopDir, pageFileDir)
assertTrue(
"wrong name found: ${foundFile.path}",
foundFile.path.endsWith("/gate/navigation.html")
)
// non-existant file
foundFile =
FileUtils.findFileInTree("xxx.html", wikiTopDir, pageFileDir)
assertTrue(
"name should not be found but was", foundFile == null
)
// file in the starting dir
foundFile =
FileUtils.findFileInTree("yam-minimal.html", wikiTopDir, pageFileDir)
assertTrue(
"wrong name found (2): ${foundFile.path}",
foundFile.path.endsWith("/gate/yam/resources/yam-minimal.html")
)
log.debug("finished testFileFileInTree test")
} // testFindFileInTree()
void testReplaceFileSuffix() {
log.debug("doing testReplaceFileSuffix")
File f = new File("/home/hamish/thing.html")
String p = FileUtils.replaceFileSuffix(f, "yam")
String OSType = System.getProperty("os.name")
if(OSType.contains("Windows")){
//p=p.replaceAll("\\","/"), not working, why?
assertTrue(p.equals("\\home\\hamish\\thing.yam"))
}else{
assertTrue(p.equals("/home/hamish/thing.yam"))
}
f = new File("/home/hamish/thing")
p = FileUtils.replaceFileSuffix(f, "yam")
if(OSType.contains("Windows")){
assertTrue(p.equals("\\home\\hamish\\thing"))
}else{
assertTrue(p.equals("/home/hamish/thing"))
}
f = new File("thing")
p = FileUtils.replaceFileSuffix(f, "yam")
assertTrue(p.equals("thing"))
f = new File("")
p = FileUtils.replaceFileSuffix(f, "yam")
assertTrue(p.equals(""))
f = new File("thing.pdf")
p = FileUtils.replaceFileSuffix(f, "yam")
assertTrue(p.equals("thing.yam"))
f = new File(".html")
p = FileUtils.replaceFileSuffix(f, "yam")
assertTrue(p.equals(".yam"))
} // testReplaceFileSuffix()
void testIsValidFileName() {
assertTrue(
"disallowed a normal name",
FileUtils.isValidFileName("/a/b/c.html")
)
assertFalse(
"allowed a file with euro symbol",
FileUtils.isValidFileName("/a/b/€.html")
)
} // testIsValidFileName()
/** Logger */
static Logger log = Logger.getLogger("FileUtilsTests")
} // FileUtilsTests