username=test password=123456
public class PropertiesHandler {
private static Properties loadPropertiesFile(String filePath){
Properties p = new Properties();
InputStream in = null;
try {
in = new FileInputStream(new File(filePath));
p.load(in);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(in != null){
in.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return p;
}
/**
* 将property转换成Map
* @param key
* @return
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static Map<String, String> getPropertyData(String filePath){
try{
return new HashMap<String, String>((Map)PropertiesHandler.loadPropertiesFile(filePath));
}catch(Exception e){
e.printStackTrace();
}
return new HashMap<String, String>();
}
public static void main(String[] args) {
System.out.println(PropertiesHandler.getPropertyData("file/data.properties"));
}
}
public class TestBase {
@DataProvider
public Object[][] dataProvider(){
return this.getTestData();
}
private Object[][] getTestData(){
PropertiesData testData = new PropertiesData();
List<Map<String, String>> listData = testData.getTestMethodData();
Object[][] object = new Object[listData.size()][];
for (int i = 0; i < listData.size(); i++) {
object[i] = new Object[]{listData.get(i)};
}
return object;
}
}
public class PropertiesData {
public List<Map<String, String>> getTestMethodData(){
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
list.add(PropertiesHandler.getPropertyData("file/data.properties"));
return list;
}
}
public class TestDemo extends TestBase{
@Test(dataProvider="dataProvider")
public void testDemo(Map<String, String> param){
System.out.println(param.get("username"));
System.out.println(param.get("password"));
}
}
{
"username":"test",
"password":"123456"
}
public class FileUtils {
public static String readFile(String fileName) {
InputStream is = null;
StringBuffer sb = new StringBuffer();
try {
is = new FileInputStream(fileName);
byte[] byteBuffer = new byte[is.available()];
int read = 0;
while((read = is.read(byteBuffer)) != -1){
sb.append(new String(byteBuffer, 0, read));
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
if(is!=null){
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
public static void main(String[] args) {
System.out.println(FileUtils.readFile("file/data.txt"));
}
}
public class TxtData {
public List<Map<String, String>> getTestMethodData(){
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
String data = FileUtils.readFile("file/data.txt");
Gson gson = new Gson();
Map<String, String> dataMap = gson.fromJson(data, new TypeToken<Map<String, String>>(){}.getType());
list.add(dataMap);
return list;
}
}
private Object[][] getTestData(){
TxtData testData = new TxtData();
List<Map<String, String>> listData = testData.getTestMethodData();
Object[][] object = new Object[listData.size()][];
for (int i = 0; i < listData.size(); i++) {
object[i] = new Object[]{listData.get(i)};
}
return object;
}
public interface DataInterface {
public List<Map<String, String>> getTestMethodData();
}
public class PropertiesData implements DataInterface{
public List<Map<String, String>> getTestMethodData(){
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
list.add(PropertiesHandler.getPropertyData("file/data.properties"));
return list;
}
}
public class TxtData implements DataInterface{
public List<Map<String, String>> getTestMethodData(){
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
String data = FileUtils.readFile("file/data.txt");
Gson gson = new Gson();
Map<String, String> dataMap = gson.fromJson(data, new TypeToken<Map<String, String>>(){}.getType());
list.add(dataMap);
return list;
}
}
private DataInterface getDataInstance(String key){
DataInterface data = null;
try {
data = (DataInterface) Class.forName(key).newInstance();
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
e.printStackTrace();
}
return data;
}
private Object[][] getTestData(){
DataInterface testData = this.getDataInstance("com.test.testdata.PropertiesData");
List<Map<String, String>> listData = testData.getTestMethodData();
Object[][] object = new Object[listData.size()][];
for (int i = 0; i < listData.size(); i++) {
object[i] = new Object[]{listData.get(i)};
}
return object;
}
private Object[][] getTestData(){
DataInterface testData = this.getDataInstance("com.test.testdata.TxtData");
List<Map<String, String>> listData = testData.getTestMethodData();
Object[][] object = new Object[listData.size()][];
for (int i = 0; i < listData.size(); i++) {
object[i] = new Object[]{listData.get(i)};
}
return object;
}
DataSource=com.test.testdata.TxtData
public class Config {
public static String DATA_SOURCE;
static{
Map<String, String> map = PropertiesHandler.getPropertyData("config/config.properties");
DATA_SOURCE = map.get("DataSource");
}
}
private Object[][] getTestData(){
DataInterface testData = this.getDataInstance(Config.DATA_SOURCE);
List<Map<String, String>> listData = testData.getTestMethodData();
Object[][] object = new Object[listData.size()][];
for (int i = 0; i < listData.size(); i++) {
object[i] = new Object[]{listData.get(i)};
}
return object;
}
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface DataSource {
String value();
}
public class DataSources {
public static String getDataSource(Method method){
DataSource ds = method.getAnnotation(DataSource.class);
if(ds != null){
return ds.value();
}
return null;
}
}
@DataSource("com.test.testdata.PropertiesData")
@Test(dataProvider="dataProvider")
public void testDemo(Map<String, String> param){
System.out.println(param.get("username"));
System.out.println(param.get("password"));
}
private Object[][] getTestData(Method method){
String sourceKey = DataSources.getDataSource(method);
if(sourceKey==null){
sourceKey = Config.DATA_SOURCE;
}
DataInterface testData = this.getDataInstance(sourceKey);
List<Map<String, String>> listData = testData.getTestMethodData();
Object[][] object = new Object[listData.size()][];
for (int i = 0; i < listData.size(); i++) {
object[i] = new Object[]{listData.get(i)};
}
return object;
}
public class TestDemo extends TestBase{
@DataSource("com.test.testdata.PropertiesData")
@Test(dataProvider="dataProvider")
public void testDemo(Map<String, String> param){
System.out.println(param.get("username"));
System.out.println(param.get("password"));
}
@Test(dataProvider="dataProvider")
public void testDemo1(Map<String, String> param){
System.out.println(param.get("username"));
System.out.println(param.get("password"));
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有