<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    [原創]基于驗證框架的action的測試

    當引用了驗證框架的時候,測試 action 的時候,程序會自動去找驗證框架進行驗證,那么如何進行這種情況下的 action 測試呢?經過一天的研究,發現必須先將驗證所需要的 xml 文件夾在進去,然后才能夠順利的進行!具體的實施如下:

    ??? protected void setUp() throws Exception {

    ??????? super .setUp();

    ??????? // set WEB-INF directory

    ??????? setContextDirectory( new File( "WebContent" ));

    ??????? // set struts- test -config.xml

    ??????? setConfigFile( "/WEB-INF/test/struts-test-config.xml" );

    ???????

    ??????? // set request's attribute for test

    ??????? // 創建一個 MessageResourcesFactory 類,目的是得到下面的 MessageResources, 作為其中的一個參數

    MessageResourcesFactory messageResourcesFactory = new PropertyMessageResourcesFactory();

    // 獲得 MessageResources

    MessageResources messageResources = new PropertyMessageResources(messageResourcesFactory, "org.apache.struts.action.ActionResources" );? ?
    ?
    getActionServlet().getServletContext().setAttribute( "org.apache.commons.validator.VALIDATOR_RESOURCES" ,getResources());

    ???????? ???????????????????????? ?
    getActionServlet().getServletContext().setAttribute( "org.apache.struts.action.MESSAGE" ,messageResources);

    ??? }

    ?

    以下的方法為了得到一個 MessageResources ,是從 org.apache.struts.validator. ValidatorPlugIn 中的 initResources ()方法得到的!

    /**

    ? * get the validator resources.

    ? *

    ? * @throws IOException ????? if an input/output error is encountered

    ? * @throws ServletException if we cannot initialize these resources

    ? */

    protected ValidatorResources getResources() throws IOException, ServletException {

    String pathnames = "/WEB-INF/validator-rules.xml,/WEB-INF/test/struts-test-config.xml" ;

    ??????? StringTokenizer st = new StringTokenizer(pathnames, "," );

    ?

    ??????? List streamList = new ArrayList();

    ??????? try {

    ??????????? while (st.hasMoreTokens()) {

    ??????????????? String validatorRules = st.nextToken().trim();

    ??????????????? if (log.isInfoEnabled()) {

    ??????????????????? log.info( "Loading validation rules file from '" + validatorRules + "'" );

    ??????????????? }

    ?

    ??????????????? InputStream input =getActionServlet().getServletContext().getResourceAsStream(validatorRules);

    ???????????????

    ??????????????? // If the config isn't in the servlet context, try the class loader

    ??????????????? // which allows the config files to be stored in a jar

    ??????????????? if (input == null ) {

    ??????????????????? input = getClass().getResourceAsStream(validatorRules);

    ??????????????? }

    ?

    ??????????????? if (input != null ) {

    ??????????????????? BufferedInputStream bis = new BufferedInputStream(input);

    ??????????????????? streamList.add(bis);

    ??????????????? } else {

    ??????????????????? throw new ServletException( "Skipping validation rules file from '"

    ??????????????????????????? ? + validatorRules + "'.? No stream could be opened." );

    ??????????????? }

    ??????????? }

    ??????????? int streamSize = streamList.size();

    ??????????? InputStream[] streamArray = new InputStream[streamSize];

    ??????????? for ( int streamIndex = 0;streamIndex < streamSize;streamIndex++) {

    ??????????????? InputStream is = (InputStream) streamList.get(streamIndex);

    ??????????????? streamArray[streamIndex] = is;

    ??????????? }

    ?

    ??????????? return new ValidatorResources(streamArray);

    ??????? } catch (SAXException sex) {

    ??????????? log.error( "Skipping all validation" ,sex);

    ??????????? throw new ServletException(sex);

    ??????? } finally {

    ??????????? Iterator streamIterator = streamList.iterator();

    ??????????? while (streamIterator.hasNext()) {

    ??????????????? InputStream is = (InputStream) streamIterator.next();

    ??????????????? is.close();

    ??????????? }

    ??????? }

    ??? }

    ?

    通過這樣的配置以后,就可以正常的對于 action 進行測試了!

    posted on 2006-03-17 13:31 扭轉乾坤 閱讀(254) 評論(0)  編輯  收藏 所屬分類: 測試相關

    <2006年3月>
    2627281234
    567891011
    12131415161718
    19202122232425
    2627282930311
    2345678

    導航

    統計

    常用鏈接

    留言簿(2)

    隨筆分類(31)

    隨筆檔案(30)

    文章分類(32)

    文章檔案(33)

    相冊

    PHP小站-首頁

    搜索

    積分與排名

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 午夜成人免费视频| 午夜国产羞羞视频免费网站| 久久精品国产亚洲AV蜜臀色欲| 精品国产免费观看| 两个人www免费高清视频| 亚洲精品国产福利片| 日本免费人成黄页网观看视频| 99在线视频免费观看| 亚洲成a人片在线观看精品| 亚洲一区二区三区乱码A| 18禁黄网站禁片免费观看不卡| 精品国产日韩亚洲一区在线| 亚洲天堂视频在线观看| 亚洲av高清在线观看一区二区 | 亚洲中文无码亚洲人成影院| 自拍偷自拍亚洲精品被多人伦好爽| 最近中文字幕mv免费高清在线| 黄色三级三级免费看| 97久久精品亚洲中文字幕无码 | 亚洲AV无码精品蜜桃| 亚洲深深色噜噜狠狠爱网站| 67194成是人免费无码| 免费观看久久精彩视频| 精品国产日韩亚洲一区在线| 亚洲综合激情九月婷婷| 亚洲一级Av无码毛片久久精品| 手机在线毛片免费播放| 99爱在线观看免费完整版| 成人免费网站视频www| 亚洲人成自拍网站在线观看| 亚洲黄色免费观看| 一本色道久久综合亚洲精品| 国产免费无遮挡精品视频| 在线观看日本免费a∨视频| 少妇人妻偷人精品免费视频| 一级成人毛片免费观看| 亚洲AV无码资源在线观看| 亚洲三级在线观看| 亚洲jjzzjjzz在线播放| 亚洲综合综合在线| 亚洲视频在线观看|