SelfDefineReport-ReportWriter

package xu.zhi.hua.report;

import xu.zhi.hua.testTestNg.annotation.Author;
import xu.zhi.hua.testTestNg.annotation.Category;
import xu.zhi.hua.testTestNg.annotation.XmlDataFactory;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.ITestResult;
import org.testng.annotations.Test;

import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

public class ReportWriter {

    private static final Logger logger = LoggerFactory.getLogger(ReportWriter.class);

    public static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss SSS";
    public static final String OLD_DIR_CHILD="yyyy-MM-dd-HH-mm-ss-SSS";
    public static final String OLD_REPORT_DIR = "old-al-reporter";
    public static final String REPORT_DIR = "al-reporter";

    public void generateOldDirectory(String outputDirectory) {
        try {
            String oldPath=outputDirectory+ File.separator + OLD_REPORT_DIR;
            File dir=new File(oldPath);
            if(!dir.exists()){
                dir.mkdirs();
            }
            DateFormat df = new SimpleDateFormat(OLD_DIR_CHILD);
            Date nowDate=new Date();
            File oldReporterDirectory=new File(outputDirectory+File.separator + OLD_REPORT_DIR
                    +File.separator+df.format(nowDate));

            if (oldReporterDirectory.exists()) {
                //删除
                FileUtils.forceDelete(oldReporterDirectory);
            }
            FileUtils.copyDirectory(new File(outputDirectory + File.separator+REPORT_DIR), oldReporterDirectory);
        } catch (IOException e) {
            logger.warn("", e);
        }
    }

    public String getExceptionTrace(ITestResult testResult) {
        Throwable exception = testResult.getThrowable();
        StringBuilder message = new StringBuilder(exception.getClass()
                .getName());
        message.append(":");
        message.append(exception.getMessage());

        System.out.println("testResult.getTestClass().getName()------------>:"+testResult.getTestClass().getName());
        System.out.println("testResult.getMethod().getMethodName()------------>:"+testResult.getMethod().getMethodName());
        System.out.println("exception.getCause()------------>:"+exception.getCause());
        System.out.println("exception.getMessage()------------>:"+exception.getMessage());
        System.out.println("exception.getStackTrace()------------>:"+exception.getStackTrace());
        System.out.println("exception.toString()------------>:"+exception.toString());

        StackTraceElement[] traces = exception.getStackTrace();
        for (StackTraceElement trace : traces) {
            message.append(String.format("\n\tat %s(%s:%d)",
                    trace.getClassName(), trace.getFileName(),
                    trace.getLineNumber()));
        }
        return message.toString();
    }

    public String getParameters(ITestResult result){
        Class[] arrayClass=result.getMethod().getMethod().getParameterTypes();
        //logger.info("ParameterTypes"+arrayClass.length);
        //logger.info("ParameterTypes"+arrayClass[0].getName());
        Object[] parameters=result.getParameters();
        if (arrayClass.length==1){
            Map map=(Map) parameters[0];
            return map.toString();
        }else {
            return "";
        }
    }

    public String getDescFromParameter(ITestResult result){
        String desc="";
        Class[] arrayClass=result.getMethod().getMethod().getParameterTypes();
        //logger.info("ParameterTypes"+arrayClass.length);
        //logger.info("ParameterTypes"+arrayClass[0].getName());
        Object[] parameters=result.getParameters();
        if (arrayClass.length==1){
            Map map=(Map) parameters[0];
            if (map.containsKey("description")){
                desc=map.get("description");
            }
            return desc;
        }
        return desc;
    }


    /**
     * 获取Desc信息(当Test的Description信息没有定义时使用)
     * @param result
     * @return
     */
//    public String getNameFromXmlDataFactoryAnnotation(ITestResult result)
//    {
//        String name = "";
//        XmlDataFactory xmlDataFactory = result.getMethod().getMethod().getAnnotation(XmlDataFactory.class);
//        if(xmlDataFactory != null)
//        {
//            name = xmlDataFactory.name();
//            return name;
//        }
//        return name;
//    }

    /**
     * 获取Test的Description信息
     * @param result
     * @return
     */
//    public String getDescFromTestAnnotation(ITestResult result)
//    {
//        String desc = "";
//        Test methodTest = result.getMethod().getMethod().getAnnotation(Test.class);
//        if(methodTest != null)
//        {
//            desc = methodTest.description();
//            if (desc.isEmpty()){
//                desc = getNameFromXmlDataFactoryAnnotation(result);
//            }
//            return desc;
//        }
//        return desc;
//    }

    /**
     * 获取Category的name信息
     * @param result
     * @return
     */
    public String getNameFromCategoryAnnotation(ITestResult result)
    {
        String category = "";
        Category methodCategory = result.getMethod().getMethod().getAnnotation(Category.class);
        if(methodCategory != null)
        {
            category = methodCategory.name();
            return category;
        }
        return category;
    }

    /**
     * 获取Author的name信息
     * @param result
     * @return
     */
    public String getNameFromAuthorAnnotation(ITestResult result)
    {
        String author = "";
        Author methodAuthor = result.getMethod().getMethod().getAnnotation(Author.class);
        if(methodAuthor != null)
        {
            author = methodAuthor.name();
            return author;
        }
        return author;
    }


}

你可能感兴趣的:(SelfDefineReport-ReportWriter)