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

automated.process.io.read.ReadPropFile Maven / Gradle / Ivy

The newest version!
package automated.process.io.read;

import automated.core.enums.PathEnum;
import automated.process.io.validations.ValidateExtension;
import automated.process.io.validations.ValidateFile;
import automated.reporting.TestReporter;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.Properties;
import java.util.Set;

/**
 * Created by Ismail on 12/26/2017.
 * This class contains all related methods for Read From
 * Properties File (*.properties) and do actions or retrieve data
 * from the file
 */
public class ReadPropFile {

    /*************** Class Methods Section ***************/
    // This method read File Properties content
    // and return all contents as Properties Object
    private static Properties readPropFile(String propFile) {
        // get file path without resource path if provided
        propFile = propFile.replace(PathEnum.RESOURCES_TEST.toString(), "");
        // build full path for properties file
        propFile = PathEnum.RESOURCES_TEST + propFile;
        // Verify propFile Status
        ValidateFile.verifyFileStatus(propFile);
        // Verify propFile content Type
        ValidateExtension.verifyPropertiesFileType(propFile);
        // Initialize Properties Object
        Properties properties = new Properties();
        // Start operating to Load Properties from file
        try {// Read The Provided File content as inputStream
            InputStream inputStream = new FileInputStream(propFile);
            properties.load(inputStream);// Load inputStream Object to Properties
            // Convert all Properties Keys to Lower Case
            Set keys = properties.keySet();
            // Define a new Properties object to save all keys,values there
            Properties newProperties = new Properties();
            for (Object key : keys) {// Get all Properties Keys
                String propKey = (String) key;// Get Prop key
                // Get Prop Value
                String propValue = properties.getProperty(propKey);
//                properties.remove(propKey);// Remove the key,value that saved
                // Save the key, value again but after convert Key toLowerCase
                newProperties.setProperty(propKey.toLowerCase(), propValue);
            }
            // Assign newProperties that contains lower case keys
            // To properties object
            properties = newProperties;
            // Return Properties Object
            return properties;
        } catch (Throwable throwable) {// In case something went wrong while
            // reading Properties file then add error to report and fail test case
            TestReporter.error("Something went wrong in reading " +
                    "Properties file: " + propFile + "\nPlease check message: "
                    + throwable.getMessage(), true);
        }// Default return is null
        return null;
    }

    // This method return a value string of provided Key from
    // Properties File
    public static String getProperty(String propFile, String propertyName) {
        // Return Property value from readPropFile Method
        // Check if Property Name exist and if yes then return value as string
        if (readPropFile(propFile).containsKey(propertyName.toLowerCase()))
            return readPropFile(propFile).getProperty(propertyName.toLowerCase());
        else// Else will return a null string
            return "";
    }
}