use of com.axway.ats.common.filesystem.FileSystemOperationException in project ats-framework by Axway.
the class Test_HttpClient method verifyDownloadedFileMatch.
private void verifyDownloadedFileMatch(String expectedFile, String actualFile) throws IOException {
// check the file contents
String actualFileMD5Sum;
String expectedFileMD5Sum;
try {
LocalFileSystemOperations lfso = new LocalFileSystemOperations();
actualFileMD5Sum = lfso.computeMd5Sum(actualFile, Md5SumMode.BINARY);
expectedFileMD5Sum = lfso.computeMd5Sum(expectedFile, Md5SumMode.BINARY);
} catch (FileSystemOperationException fsoe) {
throw new RuntimeException("Error calculating MD5 sum", fsoe);
}
if (!actualFileMD5Sum.equals(expectedFileMD5Sum)) {
throw new RuntimeException("The " + actualFile + " file is different than the expected " + expectedFile);
} else {
log.info("Matched actual '" + actualFile + "' and expected '" + expectedFile + "' files");
}
}
use of com.axway.ats.common.filesystem.FileSystemOperationException in project ats-framework by Axway.
the class LocalFileSystemOperations method computeMd5Sum.
@Override
public String computeMd5Sum(String sourceFile, Md5SumMode mode) {
InputStream input = null;
MessageDigest digest;
try {
input = new BufferedInputStream(new FileInputStream(sourceFile));
// obtain MD5 digest
digest = MessageDigest.getInstance("MD5");
switch(mode) {
case BINARY:
{
digestBinContent(input, digest);
break;
}
case ASCII:
{
digestNormContent(input, digest);
break;
}
default:
{
throw new FileSystemOperationException("MD5 digest mode '" + mode + "' not supported");
}
}
} catch (FileNotFoundException fnfe) {
throw new FileDoesNotExistException(sourceFile);
} catch (NoSuchAlgorithmException nsae) {
throw new FileSystemOperationException("MD5 sum cannot be calculated", nsae);
} catch (IOException ioe) {
throw new FileSystemOperationException("Could read content of file '" + sourceFile + "'", ioe);
} finally {
IoUtils.closeStream(input, "Could not close stream");
}
// retrieve final MD5 value and convert to hex
return StringUtils.byteArray2Hex(digest.digest());
}
use of com.axway.ats.common.filesystem.FileSystemOperationException in project ats-framework by Axway.
the class LocalFileSystemOperations method findFiles.
@Override
public String[] findFiles(String location, String searchString, boolean isRegex, boolean acceptDirectories, boolean recursiveSearch) {
File startLocation = new File(location);
if (!startLocation.exists()) {
if (log.isDebugEnabled()) {
log.debug("Start location '" + location + "' does not exist");
}
return new String[0];
}
if (!startLocation.isDirectory()) {
throw new FileSystemOperationException("Start location '" + location + "' is not a directory");
}
FileNameSearchFilter fileNameSearchFilter = new FileNameSearchFilter(searchString, isRegex, acceptDirectories);
List<String> matchedFiles = getMatchingFiles(startLocation, fileNameSearchFilter, recursiveSearch);
return matchedFiles.toArray(new String[0]);
}
use of com.axway.ats.common.filesystem.FileSystemOperationException in project ats-framework by Axway.
the class LocalFileSystemOperations method createBinaryFile.
@Override
public void createBinaryFile(String filename, long size, boolean randomContent) {
BufferedOutputStream outputStream = null;
try {
outputStream = new BufferedOutputStream(new FileOutputStream(filename));
// if the file size is bigger than 0, fill the file, otherwise create an empty file
if (size > 0) {
if (randomContent) {
byte[] nextByte = new byte[1];
long currentSize = 0;
// Write random bytes until size is reached
while (currentSize < size) {
// write a byte
randomGenerator.nextBytes(nextByte);
outputStream.write(nextByte);
currentSize++;
}
} else {
byte nextByte = Byte.MIN_VALUE;
long currentSize = 0;
while (currentSize < size) {
outputStream.write(nextByte);
if (nextByte == Byte.MAX_VALUE) {
nextByte = 0;
} else {
nextByte++;
}
currentSize++;
}
}
}
} catch (IOException ioe) {
throw new FileSystemOperationException("Could not generate file", ioe);
} finally {
if (outputStream != null) {
try {
outputStream.close();
log.info("Successfully created binary file '" + filename + "' with size " + size);
} catch (IOException ioe) {
log.error("Could not close file output stream", ioe);
}
}
}
}
use of com.axway.ats.common.filesystem.FileSystemOperationException in project ats-framework by Axway.
the class LocalFileSystemOperations method deleteFile.
@Override
public void deleteFile(String fileName) {
File file = new File(fileName);
boolean fileExists = checkFileExistence(file, false);
if (fileExists) {
boolean isDirectory = file.isDirectory();
try {
Files.delete(file.toPath());
} catch (IOException e) {
throw new FileSystemOperationException("Unable to delete " + (isDirectory ? "directory" : "file") + " '" + fileName + "'" + (isDirectory ? ". Try invoking deleteDirectory() instead." : ""), e);
}
}
}
Aggregations