Skip to content

Commit

Permalink
update Property Helper
Browse files Browse the repository at this point in the history
  • Loading branch information
Abdelrhman-Ellithy committed Dec 4, 2024
1 parent 7b8fb91 commit 24255f6
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 12 deletions.
11 changes: 0 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,17 +184,6 @@ Here is the updated **Getting Started** section formatted for your README file:
```bash
mvn clean test
```
### Step 4: Select The Running Mode # BDD, NonBDD

#### you find a new config file created here src/main/resources/properties/config.properties at your project
- **BDD Mode** (for running with Cucumber):
```properties
runMode=BDD
```
- **Non-BDD Mode** (for running without Cucumber):
```properties
runMode=NonBDD
```

### Option 1: BDD Mode
- **[Demo-Project](https://github.com/Abdelrhman-Ellithy/Noon-Shopping-Website-Manual-Automation-) for setup use after follow the following steps**
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
<mavenDeployPluginVersion>3.1.3</mavenDeployPluginVersion>
<mavenPublishPluginVersion>3.3.0</mavenPublishPluginVersion>
<AllureJVMVersion>2.29.1</AllureJVMVersion>
<HikariCPVersion>6.2.0</HikariCPVersion>
<HikariCPVersion>6.2.1</HikariCPVersion>
<caffineVersion>3.1.8</caffineVersion>
<mysqlVersion>9.1.0</mysqlVersion>
<postgresqlVersion>42.7.4</postgresqlVersion>
Expand Down
150 changes: 150 additions & 0 deletions src/main/java/Ellithium/Utilities/helpers/PropertyHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import Ellithium.core.logging.LogLevel;
import static Ellithium.core.reporting.Reporter.log;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class PropertyHelper {
Expand All @@ -29,4 +31,152 @@ public static void setDataToProperties(String filePath, String key, String value
log("Root Cause: ",LogLevel.ERROR,e.getCause().toString());
}
}
// Method to retrieve all key-value pairs from a properties file
public static Properties getAllProperties(String filePath) {
Properties prop = new Properties();
try (FileInputStream input = new FileInputStream(filePath + ".properties")) {
prop.load(input);
log("Successfully loaded all properties from file: ", LogLevel.INFO_GREEN, filePath);
} catch (IOException e) {
log("Failed to load properties file: ", LogLevel.ERROR, filePath);
log("Root Cause: ", LogLevel.ERROR, e.getMessage());
}
return prop;
}
// Method to remove a specific key from a properties file
public static void removeKeyFromProperties(String filePath, String key) {
Properties prop = new Properties();
try (FileInputStream input = new FileInputStream(filePath + ".properties");
FileOutputStream output = new FileOutputStream(filePath + ".properties")) {

prop.load(input);
if (prop.containsKey(key)) {
prop.remove(key);
prop.store(output, null);
log("Successfully removed key: " + key + " from properties file: ", LogLevel.INFO_GREEN, filePath);
} else {
log("Key not found in properties file: ", LogLevel.WARN, key);
}

} catch (IOException e) {
log("Failed to remove key from properties file: ", LogLevel.ERROR, filePath);
log("Root Cause: ", LogLevel.ERROR, e.getMessage());
}
}
// Method to check if a specific key exists in a properties file
public static boolean keyExists(String filePath, String key) {
Properties prop = new Properties();
try (FileInputStream input = new FileInputStream(filePath + ".properties")) {
prop.load(input);
boolean exists = prop.containsKey(key);
log("Checked key existence: " + key + " in file: ", LogLevel.INFO_GREEN, filePath);
return exists;
} catch (IOException e) {
log("Failed to check key in properties file: ", LogLevel.ERROR, filePath);
log("Root Cause: ", LogLevel.ERROR, e.getMessage());
}
return false;
}
// Method to retrieve a key's value or return a default value if the key is not found
public static String getOrDefault(String filePath, String key, String defaultValue) {
Properties prop = new Properties();
try (FileInputStream input = new FileInputStream(filePath + ".properties")) {
prop.load(input);
String value = prop.getProperty(key, defaultValue);
log("Fetched key: " + key + " with value (or default): " + value + " from file: ", LogLevel.INFO_GREEN, filePath);
return value;
} catch (IOException e) {
log("Failed to fetch key from properties file: ", LogLevel.ERROR, filePath);
log("Root Cause: ", LogLevel.ERROR, e.getMessage());
}
return defaultValue;
}
// Method to update multiple properties at once
public static void updateMultipleProperties(String filePath, Properties properties) {
Properties prop = new Properties();
try (FileInputStream input = new FileInputStream(filePath + ".properties");
FileOutputStream output = new FileOutputStream(filePath + ".properties")) {

prop.load(input);
properties.forEach((key, value) -> prop.setProperty(key.toString(), value.toString()));
prop.store(output, null);

log("Successfully updated multiple properties in file: ", LogLevel.INFO_GREEN, filePath);
} catch (IOException e) {
log("Failed to update multiple properties in file: ", LogLevel.ERROR, filePath);
log("Root Cause: ", LogLevel.ERROR, e.getMessage());
}
}
// Method to add or update properties from a Map
public static void addOrUpdatePropertiesFromMap(String filePath, Map<String, String> map) {
Properties prop = new Properties();
try (FileInputStream input = new FileInputStream(filePath + ".properties");
FileOutputStream output = new FileOutputStream(filePath + ".properties")) {

prop.load(input); // Load existing properties
map.forEach(prop::setProperty); // Add or update properties
prop.store(output, null); // Save updated properties

log("Successfully added or updated properties from Map in file: ", LogLevel.INFO_GREEN, filePath);
} catch (IOException e) {
log("Failed to add or update properties in file: ", LogLevel.ERROR, filePath);
log("Root Cause: ", LogLevel.ERROR, e.getMessage());
}
}
// Method to return properties file as a Map
public static Map<String, String> getPropertiesAsMap(String filePath) {
Properties prop = new Properties();
Map<String, String> map = new HashMap<>();

try (FileInputStream input = new FileInputStream(filePath + ".properties")) {
prop.load(input); // Load properties file
prop.forEach((key, value) -> map.put(key.toString(), value.toString())); // Convert to Map

log("Successfully retrieved properties as Map from file: ", LogLevel.INFO_GREEN, filePath);
} catch (IOException e) {
log("Failed to retrieve properties as Map from file: ", LogLevel.ERROR, filePath);
log("Root Cause: ", LogLevel.ERROR, e.getMessage());
}
return map;
}
// Method to update specific properties in a Map
public static void updatePropertiesFromMap(String filePath, Map<String, String> updates) {
Properties prop = new Properties();
try (FileInputStream input = new FileInputStream(filePath + ".properties");
FileOutputStream output = new FileOutputStream(filePath + ".properties")) {

prop.load(input); // Load existing properties
updates.forEach((key, value) -> {
if (prop.containsKey(key)) {
prop.setProperty(key, value); // Update only if the key exists
}
});
prop.store(output, null); // Save updated properties

log("Successfully updated properties from Map in file: ", LogLevel.INFO_GREEN, filePath);
} catch (IOException e) {
log("Failed to update properties in file: ", LogLevel.ERROR, filePath);
log("Root Cause: ", LogLevel.ERROR, e.getMessage());
}
}
// Method to add new properties from a Map
public static void addNewPropertiesFromMap(String filePath, Map<String, String> newProperties) {
Properties prop = new Properties();
try (FileInputStream input = new FileInputStream(filePath + ".properties");
FileOutputStream output = new FileOutputStream(filePath + ".properties")) {

prop.load(input); // Load existing properties
newProperties.forEach((key, value) -> {
if (!prop.containsKey(key)) {
prop.setProperty(key, value); // Add only if the key does not exist
}
});
prop.store(output, null); // Save updated properties

log("Successfully added new properties from Map in file: ", LogLevel.INFO_GREEN, filePath);
} catch (IOException e) {
log("Failed to add new properties in file: ", LogLevel.ERROR, filePath);
log("Root Cause: ", LogLevel.ERROR, e.getMessage());
}
}
}

0 comments on commit 24255f6

Please sign in to comment.