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

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

There is a newer version: 5.1.13
Show newest version
/**
 * Orignal work: Copyright 2015 www.seleniumtests.com
 * Modified work: Copyright 2016 www.infotel.com
 * 				Copyright 2017-2019 B.Hecquet
 *
 * 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 com.seleniumtests.reporter.logger;

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

import org.json.JSONObject;

import com.seleniumtests.core.SeleniumTestsContextManager;

public class GenericFile extends TestAction {

	private FileContent file;
	private String relativeFilePath; // path relative to the root of test output directory
	

	public GenericFile(File file, String description) throws IOException {
		this(file, description, true);
	}
	public GenericFile(FileContent file, String description, String relativeFilePath)  {
		super(description, false, new ArrayList<>());
		this.file = file;
		this.relativeFilePath = relativeFilePath;
	}
	
	/**
	 * Store a file in log folder
	 * @param file
	 * @param description
	 * @param move			if true, move the file to log folder
	 * @throws IOException
	 */
	public GenericFile(File file, String description, boolean move) throws IOException {
		super(description, false, new ArrayList<>());
		
		if (file == null || !file.exists()) {
			throw new FileNotFoundException("GenericFile needs a file");
		}
		
		// in case file is not in output directory, move it
		try {
			relativeFilePath = Paths.get(SeleniumTestsContextManager.getThreadContext().getOutputDirectory()).relativize(file.toPath()).toString().replace("\\", "/");
		} catch (IllegalArgumentException e) {
			relativeFilePath = file.getName();
			move = true;
		}
		
		// move the file to the root of test specific output directory
		if (move) {
			File loggedFile = Paths.get(SeleniumTestsContextManager.getThreadContext().getOutputDirectory(), file.getName()).toFile();
			relativeFilePath = file.getName(); // correct relavtive path, as we moved the file
			
			try {
				loggedFile.mkdirs();
				Files.move(file.toPath(), loggedFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
				this.file = new FileContent(loggedFile);
			} catch (Exception e) {
				logger.error(String.format("Failed to move file %s to %s: %s", file.getAbsolutePath(), loggedFile.getAbsolutePath(), e.getMessage()));
				this.file = new FileContent(file);
			}
		
		} else {
			this.file = new FileContent(file);
		}

	}
	
	/**
	 * Build a string that can be used in HTML logs
	 * @return
	 */
	public String buildLog() {		
		return String.format("%s: file", name, relativeFilePath);
    }

	

	@Override
	public JSONObject toJson() {
		JSONObject actionJson = new JSONObject();
		
		actionJson.put("type", "file");
		actionJson.put("name", name);
		actionJson.put("id", file.getId() == null ? JSONObject.NULL: file.getId());
		
		return actionJson;
	}
	
	@Override
	public GenericFile encode(String format) {
		return new GenericFile(file, encodeString(name, format), relativeFilePath);
	}

	public File getFile() {
		return file.getFile();
	}
	public FileContent getFileContent() {
		return file;
	}

	public String getRelativeFilePath() {
		return relativeFilePath;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy