/*
* Name: FileUtilTest
* Author: Richard Rodger
* Release: 0.3
*
* Copyright (c) 2002-2003 Richard Rodger
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
// package
package org.jostraca.util.test;
/* Import << */
import org.jostraca.util.FileUtil;
import org.jostraca.util.Standard;
import org.jostraca.util.GnuRegExpProvider;
import org.jostraca.util.RegExpFileNameFilter;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import junit.textui.TestRunner;
import java.io.IOException;
import java.io.File;
/* Import >> */
/** <b>Description:</b><br>
* Test cases for FileUtil.
*/
public class FileUtilTest extends TestCase {
/* Test Framework << */
public FileUtilTest( String pName ) {
super( pName );
}
public static TestSuite suite() {
return new TestSuite( FileUtilTest.class );
}
public static void main( String[] pArgs ) {
TestRunner.run( suite() );
}
/* Test Framework >> */
/* Public Methods << */
public void testGetParent() {
String ns = null;
assertEquals( Standard.EMPTY, FileUtil.getParent( ns ) );
File nf = null;
assertEquals( Standard.EMPTY, FileUtil.getParent( nf ) );
assertEquals( "", FileUtil.getParent( "" ) );
assertEquals( "", FileUtil.getParent( "bar" ) );
assertEquals( "foo", FileUtil.getParent( "foo/bar" ) );
assertEquals( "foo", FileUtil.getParent( "foo\\bar" ) );
assertEquals( "\\foo", FileUtil.getParent( "/foo/bar" ) );
assertEquals( "c:\\foo", FileUtil.getParent( "c:/foo/bar" ) );
assertEquals( "d:\\foo", FileUtil.getParent( "d:\\foo\\bar" ) );
}
public void testWrite() {
String source1 = "source1";
String path1 = "/tmp/source1.txt";
try {
FileUtil.writeFile( path1, source1 );
}
catch( IOException ioe ) {
fail();
}
}
public void testRead() {
String filepath1 = "/tmp/source1.txt";
File file1 = new File( filepath1 );
String filetexta = "";
try {
filetexta = FileUtil.readFile( filepath1 );
assertEquals( "source1\n", filetexta );
}
catch(IOException ioe) {
fail();
}
String filetextb = null;
try {
filetextb = FileUtil.readFile( file1 );
assertEquals( "source1\n", filetextb );
}
catch(IOException ioe) {
fail();
}
}
public void testBinary() throws IOException {
String nl1 = "/tmp/nl1.txt";
String nl1s = "a\nb\r\nc\rd\n";
String nl1c = "a\nb\nc\nd\n";
String nl2 = "/tmp/nl2.txt";
String nl2s = "a\r";
String nl2c = "a\n";
String nl3 = "/tmp/nl3.txt";
String nl3s = "a\r\n";
String nl3c = "a\n";
String nl4 = "/tmp/nl4.txt";
String nl4s = "a\nb\r\nc\rd";
String nl4c = "a\nb\nc\nd";
FileUtil.writeBinaryFile( nl1, nl1s );
String nl1r = FileUtil.readFile( nl1 );
assertEquals( nl1r, nl1c );
FileUtil.writeBinaryFile( nl2, nl2s );
String nl2r = FileUtil.readFile( nl2 );
assertEquals( nl2r, nl2c );
FileUtil.writeBinaryFile( nl3, nl3s );
String nl3r = FileUtil.readFile( nl3 );
assertEquals( nl3r, nl3c );
FileUtil.writeBinaryFile( nl4, nl4s );
String nl4r = FileUtil.readFile( nl4 );
assertEquals( nl4r, nl4c );
}
public void testFindFile() throws Exception {
File fut = FileUtil.findFile( "org/jostraca/util/test/FileUtilTest.java" );
assertTrue( fut.exists() );
File nofut = FileUtil.findFile( "org/jostraca/util/test/TheresNoThereThere" );
assertTrue( !nofut.exists() );
}
public void testRecursiveSearch() throws Exception {
File examples = new File( FileUtil.findFile( "org/jostraca" ), "../../../examples" );
assertTrue( examples.exists() );
GnuRegExpProvider rep = new GnuRegExpProvider();
File[] files = FileUtil.recursiveSearch( examples, new RegExpFileNameFilter( rep.make( "^.*\\.txt$" ) ) );
int numF = files.length;
for( int fI = 0; fI < numF; fI++ ) {
System.out.println( files[fI] );
}
}
/* Public Methods >> */
}