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

automated.process.io.validations.ValidateExtension Maven / Gradle / Ivy

package automated.process.io.validations;

import org.apache.tika.Tika;
import automated.reporting.TestReporter;

import java.io.File;
import java.io.IOException;

/**
 * Created by Ismail on 12/26/2017.
 * This class contains all related methods to validate
 * The Provided File extension
 */
public class ValidateExtension {

    /*************** Class Variables Section ***************/
    // This variable is a detector of a file content
    private static final Tika tika = new Tika();

    /*************** Class Methods Section ***************/
    // This method to validate provided file path type valid or not
    private static String fileTypeDetector(String filePath) {
        // define file variable
        File file = new File(filePath);
        // define fileType variable to save the type inside
        String fileType = null;
        try {// detect the file content and save type of content inside provided file
            fileType = tika.detect(file);
        } catch (IOException e) {// throw an error to report in case the file type not match the content or something went wrong
            TestReporter.error("Unable to detect the provided file " +
                    "type, maybe corrupted/invalid file or not exist\n" +
                    "Please check message: " + e.getMessage(), true);
        }// return fileType as string
        return fileType;
    }

    // This method to validate provided file type with
    // it's content as Properties file type
    public static void verifyPropertiesFileType(String filePath) {
        // define fileType String for Properties file
        String fileType = "text/x-java-properties";
        // validate file content matches fileType String
        if (!fileTypeDetector(filePath).equalsIgnoreCase(fileType)) {
            // Id not then send report error and fail test case
            TestReporter.error("The Provided File isn't Properties" +
                    " fileType, please check the file: " + filePath, true);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy