import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Properties;
import org.testng.log4testng.Logger;
/**
* Read config properties file.
*
* @author kevinkong
*
*/
public class ConfigReader {
private static Logger logger = Logger.getLogger(ConfigReader.class);
private static ConfigReader cr;
private int retryCount = 0;
private String sourceCodeDir = "src";
private String sourceCodeEncoding = "UTF-8";
private static final String RETRYCOUNT = "retrycount";
private static final String SOURCEDIR = "sourcecodedir";
private static final String SOURCEENCODING = "sourcecodeencoding";
private static final String CONFIGFILE = "./config/config.properties";
private ConfigReader() {
readConfig(CONFIGFILE);
}
public static ConfigReader getInstance() {
if (cr == null) {
cr = new ConfigReader();
}
return cr;
}
private void readConfig(String fileName) {
Properties properties = getConfig(fileName);
if (properties != null) {
String sRetryCount = null;
Enumeration<?> en = properties.propertyNames();
while (en.hasMoreElements()) {
String key = (String) en.nextElement();
if (key.toLowerCase().equals(RETRYCOUNT)) {
sRetryCount = properties.getProperty(key);
}
if (key.toLowerCase().equals(SOURCEDIR)) {
sourceCodeDir = properties.getProperty(key);
}
if (key.toLowerCase().equals(SOURCEENCODING)) {
sourceCodeEncoding = properties.getProperty(key);
}
}
if (sRetryCount != null) {
sRetryCount = sRetryCount.trim();
try {
retryCount = Integer.parseInt(sRetryCount);
} catch (final NumberFormatException e) {
throw new NumberFormatException("Parse " + RETRYCOUNT + " [" + sRetryCount + "] from String to Int Exception");
}
}
}
}
public int getRetryCount() {
return retryCount;
}
public String getSourceCodeDir() {
return this.sourceCodeDir;
}
public String getSrouceCodeEncoding() {
return this.sourceCodeEncoding;
}
/**
*
* @param propertyFileName
*
* @return
*/
private Properties getConfig(String propertyFileName) {
Properties properties = new Properties();
try {
properties.load(new FileInputStream(propertyFileName));
} catch (FileNotFoundException e) {
properties = null;
logger.warn("FileNotFoundException:" + propertyFileName);
} catch (IOException e) {
properties = null;
logger.warn("IOException:" + propertyFileName);
}
return properties;
}
}
testNg之ConfigReader
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章
testNg之RetryListener
import java.lang.reflect.Constructor; import java.lang.reflect.Method; imp
liudie_luo
2020-06-11 15:20:53
testNg多臺APP執行,輸出報告
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE suite SYSTEM "http://test
liudie_luo
2020-06-11 15:20:53
testNg之TestResultListener
import io.appium.java_client.android.AndroidDriver; import java.io.File; i
liudie_luo
2020-06-11 15:20:53
testNg之PowerEmailableReporter
import java.io.BufferedWriter; import java.io.File; import java.io.FileInp
liudie_luo
2020-06-11 15:20:53
testNg之TestngRetry
liudie_luo
2018-08-28 23:10:05