该方法实现读取本地文件--》解析文件(调用其他方法)-》操作完改数据库状态
public static void analysis(String test_name,int do_number){
// String test_name,test_result;
// int do_number;
String result;
int id;
try {
//register driver and connect the database
Class.forName("com.mysql.jdbc.Driver");
try {
String url = String
.format("jdbc:mysql://%s/%s", host, database);
con = DriverManager.getConnection(url, username, pwd);
statement = con.createStatement();
statement1=con.createStatement();
statement2=con.createStatement();
FileWriter fw=null;
rs = statement.executeQuery("SELECT id FROM service_testcase WHERE test_name ='"+test_name+"'");
if(rs.next()){
try{
id = rs.getInt("id");//1 id
System.out.println("id,testcase_id is "+id);
rs1 = statement.executeQuery("SELECT result FROM service_jmetertask WHERE testcase_id ='"+id+"' and do_number ='"+do_number+"' and status = 3");//通过id+do_number+status的到result
if(rs1.next()){
try{
result = rs1.getString("result");
//将结果写入jtl文件
//anlyaze xml file
PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
/* PathMatchingResourcePatternResolver是一个通配符的Resource查找器,包括:
/WEB-INF/*-context.xml
com/mycompany/**/applicationContext.xml
file:C:/some/path/*-context.xml
classpath:com/mycompany/**/applicationContext.xml
类文件等*/
Resource[] resources = resolver.getResources("file:"+result);
-----------------------------------------------------------------------------------------------------------------------------------------
java代码:
查看复制到剪贴板打印 @Test public void testClasspathAsteriskPrefixLimit() throws IOException { ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); //将首先通过ClassLoader.getResources("")加载目录, //将只返回文件系统的类路径不返回jar的跟路径
//然后进行遍历模式匹配 Resource[] resources = resolver.getResources("classpath*:asm-*.txt"); Assert.assertTrue(resources.length == 0); //将通过ClassLoader.getResources("asm-license.txt")加载 //asm-license.txt存在于com.springsource.net.sf.cglib-2.2.0.jar resources = resolver.getResources("classpath*:asm-license.txt"); Assert.assertTrue(resources.length > 0); //将只加载文件系统类路径匹配的Resource resources = resolver.getResources("classpath*:LICENS*"); Assert.assertTrue(resources.length == 1); }
对于“resolver.getResources("classpath*:asm-*.txt");”,由于在项目“resources”目录下没有 所以应该返回0个资源;“resolver.getResources("classpath*:asm-license.txt");”将返回jar包 里的Resource;“resolver.getResources("classpath*:LICENS*");”,因为将只返回文件系统类路径资 源,所以返回1个资源。
因此在通过前缀“classpath*”加载通配符路径时,必须包含一个根目录才能保证加载的资源是所有的,而不是部分。
三、“file”:加载一个或多个文件系统中的Resource。如“file:D:/*.txt”将返回D盘下的所有txt文件;
-----------------------------------------------------------------------------------------------------------------------------------------------
if(resources.length == 0) {
throw new IllegalArgumentException("Property source not set correctly, no JMeter Result XML file found matching "+result);
}
//get first file for now.
//TODO: what to do if there are more files matching the pattern?
File resource = resources[0].getFile();
//getFile():返回当前资源对应的File。如果当前资源不能以绝对路径解析为一个File则会抛出异常
Reader data;
if(resource.getName().endsWith(".gz")) {
data = new InputStreamReader(new GZIPInputStream(new FileInputStream(resource)));
}
else {
data = new FileReader(resource);
}
try {
new parser().analyze(data,id,test_name,do_number,username, pwd, host, database);//call the analyze() to get the results
}
finally {
data.close();
}
//更改service_jmetertask的status状态
String sql="UPDATE service_jmetertask SET status= 4 WHERE testcase_id = '"+id+"'";
try {
statement1.executeUpdate(sql);//use new connection to execute new sql
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
e.printStackTrace();
log.warn(e);
}
}catch(Exception e){
System.out.println("数据库读取错误rs");
e.printStackTrace();
log.warn(e);
}
}
}catch(Exception e){
System.out.println("数据库读取错误rs1");
e.printStackTrace();
log.warn(e);
}
}
}catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if (statement != null) {
try {
statement.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (con != null) {
try {
con.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}