You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by oz...@apache.org on 2004/06/11 14:48:40 UTC
cvs commit: jakarta-commons-sandbox/test/src/java/org/apache/commons/test/file FileTest.java
ozeigermann 2004/06/11 05:48:40
Added: test/src/java/org/apache/commons/test/file FileTest.java
Log:
Added initial version of file test helpers
Revision Changes Path
1.1 jakarta-commons-sandbox/test/src/java/org/apache/commons/test/file/FileTest.java
Index: FileTest.java
===================================================================
/*
* $Header: /home/cvs/jakarta-commons-sandbox/test/src/java/org/apache/commons/test/file/FileTest.java,v 1.1 2004/06/11 12:48:40 ozeigermann Exp $
* $Revision: 1.1 $
* $Date: 2004/06/11 12:48:40 $
*
* ====================================================================
*
* Copyright 1999-2002 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.apache.commons.test.file;
import junit.framework.*;
import java.io.*;
import java.util.*;
/**
* Test helpers for collections.
*
* @author <a href="mailto:ozeigermann@apache.org">Oliver Zeigermann</a>
*/
public class FileTest {
// creation / deletion of test files
public static void removeRec(File toRemove) {
if (toRemove.isDirectory()) {
File fileList[] = toRemove.listFiles();
for (int a = 0; a < fileList.length; a++) {
removeRec(fileList[a]);
}
}
toRemove.delete();
}
public static final void createFiles(String[] filePaths) {
createFiles(filePaths, null, null);
}
public static final void createFiles(String[] filePaths, String dirPath) {
createFiles(filePaths, null, dirPath, null);
}
public static final void createFiles(String[] filePaths, String[] contents, String encoding) {
createFiles(filePaths, contents, null, encoding);
}
public static final void createFiles(String[] filePaths, String[] contents, String dirPath, String encoding) {
for (int i = 0; i < filePaths.length; i++) {
String filePath = filePaths[i];
File file;
if (dirPath != null) {
file = new File(new File(dirPath), filePath);
} else {
file = new File(filePath);
}
file.getParentFile().mkdirs();
try {
file.delete();
file.createNewFile();
String content = null;
if (contents != null && contents.length > i) {
content = contents[i];
}
if (content != null) {
FileOutputStream stream = new FileOutputStream(file);
stream.write(contents[i].getBytes(encoding));
stream.close();
}
} catch (IOException e) {
}
}
}
public static final void deleteInDir(String dirPath, String[] fileNames) {
File dir = new File(dirPath);
if (dir.isDirectory()) {
for (int i = 0; i < fileNames.length; i++) {
String fileName = fileNames[i];
File file = new File(dir, fileName);
file.delete();
}
}
}
// checks on test files
public static final void checkIsEmpty(String dirPath) {
checkExactlyContains(dirPath, null);
}
public static final void checkExactlyContains(String dirPath, String[] fileNames) {
checkExactlyContains(dirPath, fileNames, null, null);
}
public static final void checkExactlyContains(String dirPath, String[] fileNames, String[] contents, String encoding) {
File dir = new File(dirPath);
if (dir.isDirectory()) {
File[] files = dir.listFiles();
if (fileNames == null) {
if (files.length != 0) {
TestCase.fail(dirPath + " must be empty");
} else {
return;
}
}
List fileNameList = new ArrayList(Arrays.asList(fileNames));
for (int i = 0; i < files.length; i++) {
File file = files[i];
String fileName = file.getName();
if (!fileNameList.contains(fileName)) {
TestCase.fail(dirPath + " does not contain required " + fileName);
} else {
fileNameList.remove(fileName);
String content = null;
if (contents != null && contents.length > i) {
content = contents[i];
}
if (content != null && !compare(file, content, encoding)) {
TestCase.fail(
"Contents of "
+ fileName
+ " in "
+ dirPath
+ " does not contain required content '"
+ content
+ "'");
}
}
}
if (fileNameList.size() != 0) {
StringBuffer missingFiles = new StringBuffer("Missing files: ");
for (Iterator it = fileNameList.iterator(); it.hasNext();) {
String fileName = (String) it.next();
missingFiles.append(fileName + (it.hasNext() ? ", " : ""));
}
TestCase.fail(missingFiles.toString());
}
} else {
TestCase.fail(dirPath + " is not directoy");
}
}
public static boolean compare(FileInputStream stream, byte[] bytes) {
int read;
int count = 0;
try {
while ((read = stream.read()) != -1) {
if (bytes[count++] != read) {
return false;
}
}
} catch (IOException e) {
return false;
}
return true;
}
public static boolean compare(File file, String content, String encoding) {
FileInputStream stream = null;
try {
byte[] bytes = content.getBytes(encoding);
stream = new FileInputStream(file);
return compare(stream, bytes);
} catch (Throwable t) {
return false;
} finally {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
}
}
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org