testNg之ConfigReader
来源:互联网 发布:java消息推送 编辑:程序博客网 时间:2024/05/16 06:03
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;
}
}
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;
}
}
0 0
- testNg之ConfigReader
- ConfigReader
- testng翻译之三--testng.xml
- testng翻译之四--Running TestNG
- 单元测试之TestNG
- testNg之PowerEmailableReporter
- testNg之RetryListener
- testNg之TestngRetry
- testNg之TestResultListener
- testNG之测试报告
- TestNG参数化之DataProvider
- TestNG参数化之Parameters
- testng翻译之二--注解
- Jtester之TestNG常用注解
- Jtester之TestNG:@Factory注解
- Jtester之TestNG:@Listener注解
- testng源码阅读之二
- testng源码阅读之三
- XML语法总结
- tab标签切换
- iOS开发-文件管理(一)
- OC基础知识
- UVA 755-487--3279
- testNg之ConfigReader
- 题目1096:日期差值
- 这个数最近的素数(若是6输出为7),
- 关于今天学习的小结
- UVA 253-Cube painting
- poj3292 Semi-prime H-numbers
- testNg之PowerEmailableReporter
- 随机双色球
- cas 登出界面和登出action的修改