下面列出了com.google.common.io.Files#equal ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private static void assertEqualFolderContent(File baselineApplication, File expected, File actual, boolean xmlSemanticComparison, TreeSet<String> different) throws IOException {
for (File expectedFile : expected.listFiles()) {
File actualFile = new File(actual, expectedFile.getName());
if (expectedFile.isDirectory() && actualFile.exists() && actualFile.isDirectory()) {
assertEqualFolderContent(baselineApplication, expectedFile, actualFile, xmlSemanticComparison, different);
} else if (expectedFile.isFile() && actualFile.exists() && actualFile.isFile()) {
boolean equal;
if (xmlSemanticComparison && expectedFile.getName().endsWith(".xml")) {
equal = xmlEqual(expectedFile, actualFile);
} else {
equal = Files.equal(expectedFile, actualFile);
}
if(!equal) {
different.add(getRelativePath(baselineApplication, expectedFile));
}
}
}
}
private static boolean filesEqual(File file1, File file2)
{
try {
return Files.equal(file1, file2);
}
catch (IOException e) {
throw new UncheckedIOException(e);
}
}
/**
* Return true if the file is not identical to how it was originally
*
* @param relativeFilePath relative path to file to be evaluated
* @return true if the file is not identical to how it was originally
* @throws IOException
*/
protected boolean fileHasChanged(String relativeFilePath) throws IOException {
File originalFile = new File(appFolder, relativeFilePath);
Assert.assertTrue(originalFile.exists());
File transformedFile = new File(transformedAppFolder, relativeFilePath);
Assert.assertTrue(transformedFile.exists());
return !Files.equal(originalFile, transformedFile);
}
public static void writeHashAndCompare(final SparkeyWriter writer2) throws IOException {
final SingleThreadedSparkeyWriter writer = (SingleThreadedSparkeyWriter) writer2;
final File indexFile = writer.indexFile;
final File memFile = Sparkey.setEnding(indexFile, ".mem.spi");
try {
writer.setConstructionMethod(SparkeyWriter.ConstructionMethod.IN_MEMORY);
writer.writeHash();
indexFile.renameTo(memFile);
final IndexHeader memHeader = IndexHeader.read(memFile);
writer.setHashSeed(memHeader.getHashSeed());
writer.setConstructionMethod(SparkeyWriter.ConstructionMethod.SORTING);
writer.writeHash();
final IndexHeader sortHeader = IndexHeader.read(indexFile);
if (!Files.equal(indexFile, memFile)) {
throw new RuntimeException(
"Files are not equal: " + indexFile + ", " + memFile + "\n" +
sortHeader.toString() + "\n" + memHeader.toString());
}
} finally {
writer.setConstructionMethod(SparkeyWriter.ConstructionMethod.AUTO);
memFile.delete();
}
}
/**
* Checks that the two directories have the same structure and that their
* files content are the same
*
* @param dir1
* @param dir2
* @throws DifferentDirectoryContentException if the two directories are
* different
*/
protected void checkDirectoriesContainSameContent(File dir1, File dir2) throws DifferentDirectoryContentException {
try {
Collection<File> listFiles1 = FileUtils.listFiles(dir1, null, true);
Collection<File> listFiles2 = FileUtils.listFiles(dir2, null, true);
// Get all the files inside the source directory, recursively
for (File file1 : listFiles1) {
Path relativePath1 = dir1.toPath().relativize(file1.toPath());
logger.debug("Test file: {}", relativePath1);
File file2 = new File(dir2, relativePath1.toString());
// Check if the file exists in the other directory
if (!file2.exists()) {
throw new DifferentDirectoryContentException("File: " + file2.toString() + " doesn't exist");
}
// If that's a file, check that the both files have the same content
if (file2.isFile() && !Files.equal(file1, file2)) {
throw new DifferentDirectoryContentException("File: " + file1.toString() +
" and file: " + file2.toString() + " have different content");
}
}
if (listFiles1.size() < listFiles2.size()) {
List<Path> listPath1 = listFiles1.stream().map(file -> dir1.toPath().relativize(file.toPath())).collect(toList());
String extraFiles = listFiles2.stream().map(file -> dir2.toPath().relativize(file.toPath()))
.filter(path -> !listPath1.contains(path))
.map(Path::toString)
.collect(joining("\n"));
throw new DifferentDirectoryContentException("Additional files in dir2:\n" + extraFiles);
} else {
logger.debug("Same file list size");
}
} catch (IOException e) {
throw new DifferentDirectoryContentException("Failed to compare ", e);
}
}