ada*_*shr 13 java junit unit-testing
我正在进行集成测试,我的代码的一部分在SVN下创建目录树.这需要我测试目录结构和其中的文件是否是我期望的.
一方面我有我想要的文件,并在其他预期的目录树中的文件,从SVN出口(喜欢svn export过svn co以避免.svn噪音).
但是,是否有任何库可以断言两个目录树?我想到的最后一招是我自己进行迭代比较.
基本上我正在寻找一个可以接受两个目录并告诉我它们是否相等的API.
有点像的东西
boolean areDirectoriesEqual(File dir1, File dir2)
Run Code Online (Sandbox Code Playgroud)
我没有使用第三方lib而是使用标准的jdk lib.
private static void verifyDirsAreEqual(Path one, Path other) throws IOException {
Files.walkFileTree(one, new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file,
BasicFileAttributes attrs)
throws IOException {
FileVisitResult result = super.visitFile(file, attrs);
// get the relative file name from path "one"
Path relativize = one.relativize(file);
// construct the path for the counterpart file in "other"
Path fileInOther = other.resolve(relativize);
log.debug("=== comparing: {} to {}", file, fileInOther);
byte[] otherBytes = Files.readAllBytes(fileInOther);
byte[] thisBytes = Files.readAllBytes(file);
if (!Arrays.equals(otherBytes, thisTypes)) {
throw new AssertionFailedError(file + " is not equal to " + fileInOther);
}
return result;
}
});
}
Run Code Online (Sandbox Code Playgroud)
注意:这只是比较两个文件夹下的实际文件.如果你有空文件夹等也想要比较,你可能需要做一些额外的事情.
Aak*_*nuj -5
import java.io.File;
/**
*
* FileUtils is a collection of routines for common file system operations.
*
* @author Dan Jemiolo (danj)
*
*/
public final class FileUtils {
/**
*
* This is a convenience method that calls find(File, String, boolean) with
* the last parameter set to "false" (does not match directories).
*
* @see #find(File, String, boolean)
*
*/
public static File find(File contextRoot, String fileName) {
return find(contextRoot, fileName, false);
}
/**
*
* Searches through the directory tree under the given context directory and
* finds the first file that matches the file name. If the third parameter is
* true, the method will also try to match directories, not just "regular"
* files.
*
* @param contextRoot
* The directory to start the search from.
*
* @param fileName
* The name of the file (or directory) to search for.
*
* @param matchDirectories
* True if the method should try and match the name against directory
* names, not just file names.
*
* @return The java.io.File representing the <em>first</em> file or
* directory with the given name, or null if it was not found.
*
*/
public static File find(File contextRoot, String fileName, boolean matchDirectories) {
if (contextRoot == null)
throw new NullPointerException("NullContextRoot");
if (fileName == null)
throw new NullPointerException("NullFileName");
if (!contextRoot.isDirectory()) {
Object[] filler = { contextRoot.getAbsolutePath() };
String message = "NotDirectory";
throw new IllegalArgumentException(message);
}
File[] files = contextRoot.listFiles();
//
// for all children of the current directory...
//
for (int n = 0; n < files.length; ++n) {
String nextName = files[n].getName();
//
// if we find a directory, there are two possibilities:
//
// 1. the names match, AND we are told to match directories.
// in this case we're done
//
// 2. not told to match directories, so recurse
//
if (files[n].isDirectory()) {
if (nextName.equals(fileName) && matchDirectories)
return files[n];
File match = find(files[n], fileName);
if (match != null)
return match;
}
//
// in the case of regular files, just check the names
//
else if (nextName.equals(fileName))
return files[n];
}
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14492 次 |
| 最近记录: |