Search in sources :

Example 41 with FileSystemOperationException

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");
    }
}
Also used : LocalFileSystemOperations(com.axway.ats.core.filesystem.LocalFileSystemOperations) FileSystemOperationException(com.axway.ats.common.filesystem.FileSystemOperationException)

Example 42 with FileSystemOperationException

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());
}
Also used : FileDoesNotExistException(com.axway.ats.core.filesystem.exceptions.FileDoesNotExistException) BufferedInputStream(java.io.BufferedInputStream) FileSystemOperationException(com.axway.ats.common.filesystem.FileSystemOperationException) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) DataInputStream(java.io.DataInputStream) TarArchiveInputStream(org.apache.commons.compress.archivers.tar.TarArchiveInputStream) GzipCompressorInputStream(org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) MessageDigest(java.security.MessageDigest) FileInputStream(java.io.FileInputStream)

Example 43 with FileSystemOperationException

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]);
}
Also used : FileSystemOperationException(com.axway.ats.common.filesystem.FileSystemOperationException) RandomAccessFile(java.io.RandomAccessFile) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) File(java.io.File)

Example 44 with FileSystemOperationException

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);
            }
        }
    }
}
Also used : FileSystemOperationException(com.axway.ats.common.filesystem.FileSystemOperationException) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) BufferedOutputStream(java.io.BufferedOutputStream)

Example 45 with FileSystemOperationException

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);
        }
    }
}
Also used : FileSystemOperationException(com.axway.ats.common.filesystem.FileSystemOperationException) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) ZipFile(org.apache.commons.compress.archivers.zip.ZipFile) File(java.io.File)

Aggregations

FileSystemOperationException (com.axway.ats.common.filesystem.FileSystemOperationException)58 IOException (java.io.IOException)32 File (java.io.File)31 RandomAccessFile (java.io.RandomAccessFile)29 ZipFile (org.apache.commons.compress.archivers.zip.ZipFile)29 FileDoesNotExistException (com.axway.ats.core.filesystem.exceptions.FileDoesNotExistException)16 FileNotFoundException (java.io.FileNotFoundException)16 SocketTimeoutException (java.net.SocketTimeoutException)14 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)14 AttributeNotSupportedException (com.axway.ats.core.filesystem.exceptions.AttributeNotSupportedException)13 EOFException (java.io.EOFException)13 OverlappingFileLockException (java.nio.channels.OverlappingFileLockException)13 FileOutputStream (java.io.FileOutputStream)10 FileInputStream (java.io.FileInputStream)9 BufferedOutputStream (java.io.BufferedOutputStream)8 DataOutputStream (java.io.DataOutputStream)8 BaseTest (com.axway.ats.action.BaseTest)7 LocalFileSystemOperations (com.axway.ats.core.filesystem.LocalFileSystemOperations)7 Test (org.junit.Test)7 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)7