⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testcase.java

📁 Excel Report是一款基于Excel的报表生成工具
💻 JAVA
字号:
/*
 * Created on 2006-8-25
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package excel.report.test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;

import net.excel.report.ReportEngine;
import net.excel.report.config.ReportConfig;

/**
 * Description: 报表测试类,所有报表测试用例的基类,
 * 该类主要封装了一些在报表写报表测试用例过程中可能会用到的一些共用操作,
 * 如取得reportEngine对象,初始化报表生成器等工作。无需在每个用例当中又去单独再重新写。
 * @author juny
 * @since 1.0
 */
public class TestCase extends junit.framework.TestCase{
    /**
     * 报表测试类的构造函数
     * @param x
     */
    public TestCase(String x) {
		super(x);
		
		//先初始化测试环境和路径信息.
		templetFilePath = System.getProperty("TempletFilePath");
        if(null == templetFilePath){
            templetFilePath = ".";
        }
        
        outputFilePath = System.getProperty("OutputFilePath");
        if(null == outputFilePath){
            outputFilePath = ".";
        }
	}
    
    /**
     * 运行测试用例,在该函数中会统计本次case测试花费时间。
     */
    protected void runTest() throws Throwable {
        super.runTest();
        
    }
    
    /**
     * 取得报表引擎对象
     * @return
     */
    protected final ReportEngine getReportEngine(){
        if(null == reportEngine){
            reportEngine = ReportEngine.getReportEngine(templetFilePath + File.separator + reportConfigFile);
            reportEngine.setTempletFilePath(templetFilePath);
            //设置数据源实现类。
            /*r.setDataSourceImplement(DataSourceConfig.DS_TYPE_DATABASE, 
                    new TestDataSource());*/
        }
        return reportEngine;
    }
    private static String templetFilePath = null;
    private static String outputFilePath = null;
    private static final String reportConfigFile = "ReportConfig.xml";
    private static ReportEngine reportEngine = null;
    
    public String getTempletFilePath(){
        return templetFilePath;
    }
    
    /**
     * 设置本次测试Case的名称
     * @param testName
     */
    public void setTestName(String testName){
        this.testName = testName;
    }
    
    /**
     * 运行一个报表输出测试过程,该函数会根据报表参数生成相应报表。<br>
     * 用来测试生成一个报表的完整过程的正确性。
     * @param param 输出报表参数
     */
    public void runReportTestProcess(Map param){
        String reportName = (String)param.get(ReportConfig.REPORT);
        if(null == reportName || "".equals(reportName)){
            assertTrue("The test program couldn't find report name.", false);
            return;
        }
        
        String rName = (String)param.get("reportName");
        FileOutputStream out = null;
        try {
            ReportEngine r = getReportEngine();
            if(null == rName){
                reportName = outputFilePath + File.separator + reportName + "_out.xls";
            }else{
                reportName = outputFilePath + File.separator + rName + "_out.xls";
            }
            
            File outFile = new File(reportName);
            
            System.out.println(outFile.getAbsolutePath());
            out = new FileOutputStream(outFile);
            
            if(null != r){
                r.getReportAsExcel(out, param);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(null != out){
                try {
                    out.close();
                } catch (IOException e1) {}
            }
        }
    }
    private String testName = null;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -