deepdiff.scope.FileDiffUnit Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2011 DeepDiff Contributors
*
* 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 deepdiff.scope;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import deepdiff.core.DiffUnit;
/**
* An implementation of {@link DiffUnit} for a pair of {@link File}s (which may
* represent files, directories, or non-existent paths). UNC is supported.
*/
class FileDiffUnit implements DiffUnit {
private FileDiffScope scope;
private File f1;
private File f2;
/**
* Initializes a new FileDiffUnit object.
*
* @param scope the scope that these files were found within
* @param f1 the File on the left; non-existent paths should be represented
* as a File object, not a null
* @param f2 the File on the right; non-existent paths should be represented
* as a File object, not a null
*/
FileDiffUnit(FileDiffScope scope, File f1, File f2) {
this.scope = scope;
this.f1 = f1;
this.f2 = f2;
}
/**
* Returns whether the left File exists
*
* @return whether the left File exists
*/
public boolean leftExists() {
return f1.exists();
}
/**
* Returns whether the right File exists
*
* @return whether the right File exists
*/
public boolean rightExists() {
return f2.exists();
}
/**
* Returns whether the left File is a directory
*
* @return whether the left File is a directory
*/
public boolean leftIsDir() {
return f1.isDirectory();
}
/**
* Returns whether the right File is a directory
*
* @return whether the right File is a directory
*/
public boolean rightIsDir() {
return f2.isDirectory();
}
/**
* Returns an {@link InputStream} for the left file
*
* @return an {@link InputStream} for the left file
*
* @throws IOException if an {@link InputStream} for the left file could not
* be created
*/
public InputStream getLeftInputStream() throws IOException {
return new BufferedInputStream(new FileInputStream(f1));
}
/**
* Returns an {@link InputStream} for the right file
*
* @return an {@link InputStream} for the right file
*
* @throws IOException if an {@link InputStream} for the right file could
* not be created
*/
public InputStream getRightInputStream() throws IOException {
return new BufferedInputStream(new FileInputStream(f2));
}
/**
* Returns the scoped path for the unit. This is the path of the File
* relative to the root of the scope.
*
* @return the scoped path for the unit
*/
public String getScopedPath() {
//TODO: add support for file vs. file comparison
//If the scope is two files rather than two directories, the unit's
//scoped path is ".". Thus, none of the regular expressions based
//on file name or extension match
File leftRoot = scope.getLeftRoot();
String leftPath = f1.getPath();
String leftRootPath = leftRoot.getPath();
if (leftPath.length() > leftRootPath.length()) {
return leftPath.substring(leftRootPath.length() + 1);
} else {
return ".";
}
}
}