Search in sources :

Example 1 with FileSystemSnapshotException

use of com.axway.ats.common.filesystem.snapshot.FileSystemSnapshotException in project ats-framework by Axway.

the class LocalFileSystemSnapshot method toFile.

@Override
public void toFile(String backupFile) {
    log.info("SAVE TO FILE " + backupFile + " - START");
    // create the directory if does not exist
    File dirPath = new File(IoUtils.getFilePath(backupFile));
    if (!dirPath.exists()) {
        dirPath.mkdirs();
    }
    Document dom;
    try {
        dom = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    } catch (Exception e) {
        throw new FileSystemSnapshotException("Error creating DOM parser for " + backupFile, e);
    }
    // TODO - add DTD or schema for manual creation and easy validation
    Element fileSystemNode = dom.createElement(NODE_FILE_SYSTEM);
    fileSystemNode.setAttribute("name", this.name);
    fileSystemNode.setAttribute("time", SnapshotUtils.dateToString(this.snapshotTimestamp));
    dom.appendChild(fileSystemNode);
    for (Entry<String, DirectorySnapshot> dirSnapshotEntry : this.dirSnapshots.entrySet()) {
        Element dirSnapshotNode = dom.createElement(NODE_DIRECTORY);
        fileSystemNode.appendChild(dirSnapshotNode);
        dirSnapshotNode.setAttribute("alias", dirSnapshotEntry.getKey());
        dirSnapshotEntry.getValue().toFile(dom, dirSnapshotNode);
    }
    // save the XML file
    try {
        OutputFormat format = new OutputFormat(dom);
        format.setIndenting(true);
        format.setIndent(4);
        format.setLineWidth(1000);
        XMLSerializer serializer = new XMLSerializer(new FileOutputStream(new File(backupFile)), format);
        serializer.serialize(dom);
    } catch (Exception e) {
        throw new FileSystemSnapshotException("Error saving " + backupFile, e);
    }
    log.info("SAVE TO FILE " + backupFile + " - END");
}
Also used : XMLSerializer(org.apache.xml.serialize.XMLSerializer) Element(org.w3c.dom.Element) FileOutputStream(java.io.FileOutputStream) OutputFormat(org.apache.xml.serialize.OutputFormat) Document(org.w3c.dom.Document) FileSystemSnapshotException(com.axway.ats.common.filesystem.snapshot.FileSystemSnapshotException) File(java.io.File) FileSystemSnapshotException(com.axway.ats.common.filesystem.snapshot.FileSystemSnapshotException)

Example 2 with FileSystemSnapshotException

use of com.axway.ats.common.filesystem.snapshot.FileSystemSnapshotException in project ats-framework by Axway.

the class LocalFileSystemSnapshot method loadFromFile.

@Override
public void loadFromFile(String sourceFile) throws FileSystemSnapshotException {
    log.info("Load snapshot from file " + sourceFile + " - START");
    // first clean up the current instance, in case some snapshot was taken before
    this.dirSnapshots.clear();
    Document doc;
    try {
        doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File(sourceFile));
        doc.getDocumentElement().normalize();
    } catch (Exception e) {
        throw new FileSystemSnapshotException("Error reading backup file " + sourceFile);
    }
    Element fileSystemNode = doc.getDocumentElement();
    if (!NODE_FILE_SYSTEM.equals(fileSystemNode.getNodeName())) {
        throw new FileSystemSnapshotException("Bad backup file. Root node name is expeced to be '" + NODE_FILE_SYSTEM + "', but it is '" + fileSystemNode.getNodeName() + "'");
    }
    // the file system snapshot
    log.info("Loading snapshot with name [" + this.name + "]");
    this.snapshotTimestamp = SnapshotUtils.stringToDate(fileSystemNode.getAttribute("time"));
    // the root directories
    List<Element> dirNodes = SnapshotUtils.getChildrenByTagName(fileSystemNode, NODE_DIRECTORY);
    for (Element dirNode : dirNodes) {
        String dirAlias = dirNode.getAttributes().getNamedItem("alias").getNodeValue();
        this.dirSnapshots.put(dirAlias, DirectorySnapshot.fromFile(dirNode, new EqualityState(this.name, null)));
    }
    // sub-dir snapshots are deleted, and then added according to those Rules
    for (DirectorySnapshot topLevelDirSnapshot : this.dirSnapshots.values()) {
        for (DirectorySnapshot subDirSnapshot : topLevelDirSnapshot.getDirSnapshots().values()) {
            for (String fileName : subDirSnapshot.getFileRules().keySet()) {
                String fileAbsPath = fileName;
                if (!fileAbsPath.startsWith(subDirSnapshot.getPath())) {
                    fileAbsPath = subDirSnapshot.getPath() + fileName;
                }
                topLevelDirSnapshot.addFindRules(fileAbsPath, subDirSnapshot.getFileRules().get(fileName).getRules());
            }
            for (String skippedSubDir : subDirSnapshot.getSkippedSubDirectories()) {
                topLevelDirSnapshot.skipSubDirectory(skippedSubDir);
            }
        }
    }
    log.info("Load snapshot from file " + sourceFile + " - END");
}
Also used : EqualityState(com.axway.ats.common.filesystem.snapshot.equality.EqualityState) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) FileSystemSnapshotException(com.axway.ats.common.filesystem.snapshot.FileSystemSnapshotException) File(java.io.File) FileSystemSnapshotException(com.axway.ats.common.filesystem.snapshot.FileSystemSnapshotException)

Example 3 with FileSystemSnapshotException

use of com.axway.ats.common.filesystem.snapshot.FileSystemSnapshotException in project ats-framework by Axway.

the class LocalFileSystemSnapshot method compare.

public void compare(LocalFileSystemSnapshot that) {
    if (that == null) {
        throw new FileSystemSnapshotException("Snapshot to compare is null");
    }
    if (this.name.equals(that.name)) {
        throw new FileSystemSnapshotException("You are trying to compare snapshots with same name: " + this.name);
    }
    if (this.snapshotTimestamp == -1) {
        throw new FileSystemSnapshotException("You are trying to compare snapshots but [" + this.name + "] snapshot is still not created");
    }
    if (that.snapshotTimestamp == -1) {
        throw new FileSystemSnapshotException("You are trying to compare snapshots but [" + that.name + "] snapshot is still not created");
    }
    log.debug("Comparing snapshots [" + this.name + "] taken on " + SnapshotUtils.dateToString(this.snapshotTimestamp) + " and [" + that.name + "] taken on " + SnapshotUtils.dateToString(that.snapshotTimestamp));
    this.equality = new EqualityState(this.name, that.name);
    SnapshotUtils.checkDirSnapshotsTopLevel(this.name, this.dirSnapshots, that.name, that.dirSnapshots, equality);
    if (equality.getDifferences().size() > 0) {
        // there are some unexpected differences
        throw new FileSystemSnapshotException(equality);
    } else {
        log.debug("Successful verification");
    }
}
Also used : EqualityState(com.axway.ats.common.filesystem.snapshot.equality.EqualityState) FileSystemSnapshotException(com.axway.ats.common.filesystem.snapshot.FileSystemSnapshotException)

Example 4 with FileSystemSnapshotException

use of com.axway.ats.common.filesystem.snapshot.FileSystemSnapshotException in project ats-framework by Axway.

the class Test_FileSystemSnapshot method checkPermissions.

@Test
public void checkPermissions() {
    if (new LocalSystemOperations().getOperatingSystemType().isWindows()) {
        Logger.getLogger(Test_FileSystemSnapshot.class).warn("We skip this test as it is not applicable for Windows OS");
        return;
    }
    // disable all checks
    configurator.setFileSnapshotCheckFileSize(false);
    configurator.setFileSnapshotCheckModificationTime(false);
    configurator.setFileSnapshotCheckFileMd5(false);
    configurator.setFileSnapshotCheckFilePermissions(false);
    // we work with 2 files only
    String firstFile = FILES_ROOT + "permissions/sub-dir1/file3.xml";
    String secondFile = FILES_ROOT + "permissions/sub-dir2/file3.xml";
    // remember the current permissions
    String firstPermissions = new LocalFileSystemOperations().getFilePermissions(firstFile);
    String secondPermissions = new LocalFileSystemOperations().getFilePermissions(secondFile);
    // make permissions different
    new LocalFileSystemOperations().setFilePermissions(firstFile, "333");
    new LocalFileSystemOperations().setFilePermissions(secondFile, "777");
    try {
        // TEST1: The pair of files have different permissions, but we have disabled the check, so no
        // error will be thrown when comparing
        FileSystemSnapshot snapshot1 = new FileSystemSnapshot("snap1");
        snapshot1.addDirectory("F1", FILES_ROOT + "permissions/sub-dir1");
        snapshot1.takeSnapshot();
        FileSystemSnapshot snapshot2 = new FileSystemSnapshot("snap2");
        snapshot2.addDirectory("F1", FILES_ROOT + "permissions/sub-dir2");
        snapshot2.takeSnapshot();
        snapshot1.compare(snapshot2);
        // globally enable checking the file permissions
        configurator.setFileSnapshotCheckFilePermissions(true);
        // TEST2: Now we will do the check and error must be registered
        FileSystemSnapshot snapshot3 = new FileSystemSnapshot("snap1");
        snapshot3.addDirectory("F1", FILES_ROOT + "permissions/sub-dir1");
        snapshot3.takeSnapshot();
        FileSystemSnapshot snapshot4 = new FileSystemSnapshot("snap2");
        snapshot4.addDirectory("F1", FILES_ROOT + "permissions/sub-dir2");
        snapshot4.takeSnapshot();
        try {
            // expected exception - file permissions difference
            snapshot3.compare(snapshot4);
            // log permissions
            LocalFileSystemOperations lfs = new LocalFileSystemOperations();
            System.err.println("Snapshots compare is expected to fail. Permissions dump:");
            System.err.println("Permissions for " + firstFile + ": " + lfs.getFilePermissions(firstFile));
            System.err.println("Permissions for " + secondFile + ": " + lfs.getFilePermissions(secondFile));
            thisShouldNotBeReached();
        } catch (FileSystemSnapshotException se) {
            verifyError(se, ".*Permissions: .*");
        }
    } finally {
        // restore the original permissions
        new LocalFileSystemOperations().setFilePermissions(firstFile, firstPermissions);
        new LocalFileSystemOperations().setFilePermissions(secondFile, secondPermissions);
    }
}
Also used : LocalFileSystemOperations(com.axway.ats.core.filesystem.LocalFileSystemOperations) LocalSystemOperations(com.axway.ats.core.system.LocalSystemOperations) FileSystemSnapshotException(com.axway.ats.common.filesystem.snapshot.FileSystemSnapshotException) BaseTest(com.axway.ats.action.BaseTest) Test(org.junit.Test)

Example 5 with FileSystemSnapshotException

use of com.axway.ats.common.filesystem.snapshot.FileSystemSnapshotException in project ats-framework by Axway.

the class DirectorySnapshot method takeSnapshot.

void takeSnapshot(SnapshotConfiguration configuration) {
    log.debug("Add directory " + this.path);
    // do some cleanup - in case user call this method for a second time
    dirSnapshots.clear();
    fileSnapshots.clear();
    // take the snapshot now
    if (!new File(this.path).exists()) {
        throw new FileSystemSnapshotException("Directory '" + this.path + "' does not exist");
    }
    // check current dir files;  add their absolute path;  distribute sub-directory rules
    Map<String, FindRules> tmpFilesRules = new HashMap<String, FindRules>(fileRules);
    for (Entry<String, FindRules> fileRuleEntry : tmpFilesRules.entrySet()) {
        // if takeSnapshot() was already called, the fileName is actually a file absolute path
        String fileAbsPath = fileRuleEntry.getKey();
        if (!fileRuleEntry.getKey().startsWith(this.path)) {
            fileAbsPath = IoUtils.normalizeUnixFile(this.path + fileRuleEntry.getKey());
        }
        FindRules rules = fileRuleEntry.getValue();
        if (!rules.isSearchFilenameByRegex() && !new File(fileAbsPath).exists()) {
            log.warn("File \"" + fileAbsPath + "\" doesn't exist, but there is a rule for it!");
            continue;
        } else {
            if (SnapshotUtils.isFileFromThisDirectory(this.path, fileAbsPath)) {
                fileRules.put(fileAbsPath, rules);
            } else {
                // the rule belongs to a sub-directory, so we will add it to subDirFileRules
                int subDirEndIndex = fileAbsPath.indexOf(IoUtils.FORWARD_SLASH, this.path.length());
                String subDirPath = fileAbsPath.substring(0, subDirEndIndex);
                // because it is a sub-dir, we have to skip the following /, that is why we have that '+ 1' at the end
                String subDirPathRest = fileAbsPath.substring(subDirPath.length() + 1);
                if (!subDirFileRules.containsKey(subDirPath)) {
                    subDirFileRules.put(subDirPath, new HashMap<String, FindRules>());
                }
                subDirFileRules.get(subDirPath).put(subDirPathRest, rules);
            }
        }
        fileRules.remove(fileRuleEntry.getKey());
    }
    boolean supportHiddenFiles = configuration.isSupportHidden();
    File[] files = new File(this.path).listFiles();
    if (files != null) {
        for (File file : files) {
            if (!file.isHidden() || supportHiddenFiles) {
                if (file.isDirectory()) {
                    // skipped dirs are also in UnixDir (ends with '/') format
                    String unixDirName = IoUtils.normalizeUnixDir(file.getName());
                    if (skippedSubDirs.contains(unixDirName)) {
                        // skip the sub directory
                        continue;
                    }
                    DirectorySnapshot dirSnapshot = generateDirectorySnapshot(unixDirName, file);
                    dirSnapshot.takeSnapshot(configuration);
                    dirSnapshots.put(file.getName(), dirSnapshot);
                } else {
                    FileSnapshot fileSnapshot = generateFileSnapshot(configuration, file);
                    if (fileSnapshot != null) {
                        // if the file is not skipped
                        log.debug("Add file " + fileSnapshot.toString());
                        fileSnapshots.put(file.getName(), fileSnapshot);
                    }
                }
            } else {
                log.debug("The hidden " + (file.isDirectory() ? "directory" : "file") + " '" + file.getAbsolutePath() + "' will not be processed");
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) FileSystemSnapshotException(com.axway.ats.common.filesystem.snapshot.FileSystemSnapshotException) File(java.io.File)

Aggregations

FileSystemSnapshotException (com.axway.ats.common.filesystem.snapshot.FileSystemSnapshotException)5 File (java.io.File)3 EqualityState (com.axway.ats.common.filesystem.snapshot.equality.EqualityState)2 Document (org.w3c.dom.Document)2 Element (org.w3c.dom.Element)2 BaseTest (com.axway.ats.action.BaseTest)1 LocalFileSystemOperations (com.axway.ats.core.filesystem.LocalFileSystemOperations)1 LocalSystemOperations (com.axway.ats.core.system.LocalSystemOperations)1 FileOutputStream (java.io.FileOutputStream)1 HashMap (java.util.HashMap)1 OutputFormat (org.apache.xml.serialize.OutputFormat)1 XMLSerializer (org.apache.xml.serialize.XMLSerializer)1 Test (org.junit.Test)1