[原创]基于验证框架的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 扭转乾坤 阅读(243) 评论(0)  编辑  收藏 所属分类: 测试相关


只有注册用户登录后才能发表评论。


网站导航:
 
<2006年3月>
2627281234
567891011
12131415161718
19202122232425
2627282930311
2345678

导航

统计

常用链接

留言簿(2)

随笔分类(31)

随笔档案(30)

文章分类(32)

文章档案(33)

相册

PHP小站-首页

搜索

积分与排名

最新评论

阅读排行榜

评论排行榜