All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.seleniumtests.reporter.logger.FileContent Maven / Gradle / Ivy

The newest version!
package com.seleniumtests.reporter.logger;

import com.seleniumtests.util.logging.SeleniumRobotLogger;
import org.apache.logging.log4j.Logger;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;

/**
 * Class that represents a file generated by a test
 */
public class FileContent {
    protected static final Logger logger = SeleniumRobotLogger.getLogger(FileContent.class);
    
    private Integer id; // id of the file, when stored on seleniumRobot server
    private File file; // the file itself
    
    
    public FileContent(File file) {
        this.file = file;
    }
    public FileContent(File file, Integer id) {
        this.file = file;
        this.id = id;
    }
    /**
     * Change path of the file
     * @param outputDirectory
     * @throws IOException
     */
    public void relocate(String outputDirectory) throws IOException {
        relocate(outputDirectory, file.getName());
    }
    
    /**
     * Move the file to a new output directory, respecting its path
     * @param outputDirectory
     * @param newFilePath
     * @throws IOException
     */
    public void relocate(String outputDirectory, String newFilePath) throws IOException {
        if (outputDirectory == null || newFilePath == null) {
            return;
        }
        
        Path newPath = Paths.get(outputDirectory, newFilePath);
        newPath.getParent().toFile().mkdirs();
        try {
            Files.move(file.toPath(), newPath, StandardCopyOption.REPLACE_EXISTING);
            file = newPath.toFile();
        } catch (Exception e) {
            logger.error(String.format("Failed to relocate file %s to %s: %s", file.getAbsolutePath(), newPath.toString(), e.getMessage()));
        }
    }
    
    public String getName() {
        return file.getName();
    }
    
    public File getFile() {
        return file;
    }
    
    
    public void setFile(File file) {
        this.file = file;
    }
    
    public Integer getId() {
        return id;
    }
    
    public void setId(Integer id) {
        this.id = id;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy