- 浏览: 1223664 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (242)
- java (58)
- netty (14)
- javascript (21)
- commons (13)
- 读书笔记 (5)
- java测试 (6)
- database (5)
- struts2 (8)
- hibernate (6)
- english (27)
- spring (10)
- 生活 (4)
- 多线程 (4)
- 正则表达式 (1)
- 杂项 (1)
- maven (4)
- 数据库 (10)
- 学习笔记 (1)
- mongodb (1)
- 百度bcs (4)
- 云推送javasdk (2)
- webservice (3)
- IllegalAnnotationException: Two classes have the same XML type name (0)
- drools (3)
- freemarker (3)
- tomcat (1)
- html5 (2)
- mq (11)
- fastjson (3)
- 小算法 (2)
最新评论
-
longxitian:
https://www.cnblogs.com/jeffen/ ...
万恶的Mybatis的EnumTypeHandler -
asialee:
ddnzero 写道博主请问FileUtils这个类是哪个包的 ...
使用mockftpserver进行ftp测试 -
ddnzero:
博主请问FileUtils这个类是哪个包的?还是自己的呢?能放 ...
使用mockftpserver进行ftp测试 -
yizishou:
为什么会intMap.get("bbb") ...
浅谈System.identityHashCode -
liguanqun811:
感觉LogManager打开了所有的LogSegment(文件 ...
jafka学习之LogManager
前面还记得有前辈说过看一个开源项目在不懂的情况下要去努力的去看它的单元测试,这样就能更好的领悟作者的思想。首先我们先看一下单元测试用的一些xml。
<!-- Test1.xml --> <?xml version="1.0"?> <employee firstName="First Name" lastName="Last Name"> <address type="home" street="Home Street" city="Home City" state="HS" zipCode="HmZip"/> <address type="office" street="Office Street" city="Office City" state="OS" zipCode="OfZip"/> </employee>
首先来看第一个单元测试:
package org.apache.commons.digester; import java.io.InputStream; import java.io.IOException; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; /** * <p>Test Case for the Digester class. These tests perform parsing of * XML documents to exercise the built-in rules.</p> * * @author Craig R. McClanahan * @version $Revision: 1.5 $ $Date: 2001/10/11 00:28:42 $ */ public class RuleTestCase extends TestCase { // ----------------------------------------------------- Instance Variables /** * The digester instance we will be processing. */ protected Digester digester = null; // ----------------------------------------------------------- Constructors /** * Construct a new instance of this test case. * * @param name Name of the test case */ public RuleTestCase(String name) { super(name); } // --------------------------------------------------- Overall Test Methods /** * Set up instance variables required by this test case. */ public void setUp() { digester = new Digester(); } /** * Return the tests included in this test suite. */ public static Test suite() { return (new TestSuite(RuleTestCase.class)); } /** * Tear down instance variables required by this test case. */ public void tearDown() { digester = null; } // ------------------------------------------------ Individual Test Methods /** * Test object creation (and associated property setting) with nothing on * the stack, which should cause an appropriate Employee object to be * returned. */ public void testObjectCreate1() { // Configure the digester as required digester.addObjectCreate("employee", "org.apache.commons.digester.Employee"); digester.addSetProperties("employee"); // Parse our test input. Object root = null; try { root = digester.parse(getInputStream("Test1.xml")); } catch (Throwable t) { fail("Digester threw IOException: " + t); } // 测试非空 assertNotNull("Digester returned an object", root); // 测试栈顶的元素时候Employee或者时候他的子类型 assertTrue("Digester returned an Employee", root instanceof Employee); Employee employee = (Employee) root; // 测试firstName和lastName有没有被正确的设置 assertEquals("First name is correct", "First Name", employee.getFirstName()); assertEquals("Last name is correct", "Last Name", employee.getLastName()); } /** * Test object creation (and associated property setting) with nothing on * the stack, which should cause an appropriate Employee object to be * returned. The processing rules will process the nested Address elements * as well, but will not attempt to add them to the Employee. */ public void testObjectCreate2() { // Configure the digester as required digester.addObjectCreate("employee", "org.apache.commons.digester.Employee"); digester.addSetProperties("employee"); digester.addObjectCreate("employee/address", "org.apache.commons.digester.Address"); digester.addSetProperties("employee/address"); // Parse our test input. Object root = null; try { root = digester.parse(getInputStream("Test1.xml")); } catch (Throwable t) { fail("Digester threw IOException: " + t); } // 测试非空 assertNotNull("Digester returned an object", root); // 测试栈顶的元素是不是Employee或者是它的子类 assertTrue("Digester returned an Employee", root instanceof Employee); // 测试firstName和lastName有没有被正确设置 Employee employee = (Employee) root; assertEquals("First name is correct", "First Name", employee.getFirstName()); assertEquals("Last name is correct", "Last Name", employee.getLastName()); } /** * Test object creation (and associated property setting) with nothing on * the stack, which should cause an appropriate Employee object to be * returned. The processing rules will process the nested Address elements * as well, and will add them to the owning Employee. */ public void testObjectCreate3() { // Configure the digester as required digester.addObjectCreate("employee", "org.apache.commons.digester.Employee"); digester.addSetProperties("employee"); digester.addObjectCreate("employee/address", "org.apache.commons.digester.Address"); digester.addSetProperties("employee/address"); digester.addSetNext("employee/address", "addAddress"); // Parse our test input once Object root = null; try { root = digester.parse(getInputStream("Test1.xml")); } catch (Throwable t) { fail("Digester threw IOException: " + t); } validateObjectCreate3(root); // Parse the same input again try { root = digester.parse(getInputStream("Test1.xml")); } catch (Throwable t) { fail("Digester threw IOException: " + t); } validateObjectCreate3(root); } /** * Same as testObjectCreate1(), except use individual call method rules * to set the properties of the Employee. */ public void testObjectCreate4() { // Configure the digester as required digester.addObjectCreate("employee", "org.apache.commons.digester.Employee"); digester.addCallMethod("employee", "setFirstName", 1); digester.addCallParam("employee", 0, "firstName"); digester.addCallMethod("employee", "setLastName", 1); digester.addCallParam("employee", 0, "lastName"); // Parse our test input. Object root = null; try { root = digester.parse(getInputStream("Test1.xml")); } catch (Throwable t) { fail("Digester threw IOException: " + t); } // 测试非空和类型检验 assertNotNull("Digester returned an object", root); assertTrue("Digester returned an Employee", root instanceof Employee); // 测试了CallMethodRule和CallParamRule没有能被正确调用 Employee employee = (Employee) root; assertEquals("First name is correct", "First Name", employee.getFirstName()); assertEquals("Last name is correct", "Last Name", employee.getLastName()); } /** * It should be possible to parse the same input twice, and get trees * of objects that are isomorphic but not be identical object instances. */ public void testRepeatedParse() { // 这个主要时候测试重复的parse后生成的元素时候不同的。 // Configure the digester as required digester.addObjectCreate("employee", "org.apache.commons.digester.Employee"); digester.addSetProperties("employee"); digester.addObjectCreate("employee/address", "org.apache.commons.digester.Address"); digester.addSetProperties("employee/address"); digester.addSetNext("employee/address", "addAddress"); // Parse our test input the first time Object root1 = null; try { root1 = digester.parse(getInputStream("Test1.xml")); } catch (Throwable t) { fail("Digester #1 threw Exception: " + t); } validateObjectCreate3(root1); // Parse our test input the second time Object root2 = null; try { root2 = digester.parse(getInputStream("Test1.xml")); } catch (Throwable t) { fail("Digester #2 threw Exception: " + t); } validateObjectCreate3(root2); // Make sure that it was a different root assertTrue("Different tree instances were returned", root1 != root2); } /** * Test object creation (and associated property setting) with nothing on * the stack, which should cause an appropriate Employee object to be * returned. The processing rules will process the nested Address elements * as well, but will not attempt to add them to the Employee. */ public void testRuleSet1() { // Configure the digester as required RuleSet rs = new TestRuleSet(); digester.addRuleSet(rs); // Parse our test input. Object root = null; try { root = digester.parse(getInputStream("Test1.xml")); } catch (Throwable t) { fail("Digester threw IOException: " + t); } // 主要时候测试使用RuleSet时候Rule能被正确的解析和执行 assertNotNull("Digester returned an object", root); assertTrue("Digester returned an Employee", root instanceof Employee); Employee employee = (Employee) root; assertEquals("First name is correct", "First Name", employee.getFirstName()); assertEquals("Last name is correct", "Last Name", employee.getLastName()); assertNotNull("Can retrieve home address", employee.getAddress("home")); assertNotNull("Can retrieve office address", employee.getAddress("office")); } /** * Same as <code>testRuleSet1</code> except using a single namespace. */ public void testRuleSet2() { // Configure the digester as required digester.setNamespaceAware(true); RuleSet rs = new TestRuleSet(null, "http://jakarta.apache.org/digester/Foo"); digester.addRuleSet(rs); // Parse our test input. Object root = null; try { root = digester.parse(getInputStream("Test2.xml")); } catch (Throwable t) { fail("Digester threw IOException: " + t); } assertNotNull("Digester returned an object", root); assertTrue("Digester returned an Employee", root instanceof Employee); Employee employee = (Employee) root; assertEquals("First name is correct", "First Name", employee.getFirstName()); assertEquals("Last name is correct", "Last Name", employee.getLastName()); assertNotNull("Can retrieve home address", employee.getAddress("home")); assertNotNull("Can retrieve office address", employee.getAddress("office")); } /** * Same as <code>testRuleSet2</code> except using a namespace * for employee that we should recognize, and a namespace for * address that we should skip. */ public void testRuleSet3() { // Configure the digester as required digester.setNamespaceAware(true); RuleSet rs = new TestRuleSet(null, "http://jakarta.apache.org/digester/Foo"); digester.addRuleSet(rs); // Parse our test input. Object root = null; try { root = digester.parse(getInputStream("Test3.xml")); } catch (Throwable t) { fail("Digester threw IOException: " + t); } assertNotNull("Digester returned an object", root); assertTrue("Digester returned an Employee", root instanceof Employee); Employee employee = (Employee) root; assertEquals("First name is correct", "First Name", employee.getFirstName()); assertEquals("Last name is correct", "Last Name", employee.getLastName()); // 这个主要时候测试把namespaceURL不添加到上面,其实通过这个testcase我们 // 可以看到RuleSet里面的namespaceURI的作用了,它会给加到他里面的所有的 // rule都加上这个namespaceURI,然后将这个namespaceURI应用到所有的rule // 上面,在获得rule的时候只会得到相匹配的rule,起到一个过滤功能 assertNull("Can not retrieve home address", employee.getAddress("home")); assertNull("Can not retrieve office address", employee.getAddress("office")); } // ------------------------------------------------ Utility Support Methods /** * Return an appropriate InputStream for the specified test file (which * must be inside our current package. * * @param name Name of the test file we want * * @exception IOException if an input/output error occurs */ protected InputStream getInputStream(String name) throws IOException { return (this.getClass().getResourceAsStream ("/org/apache/commons/digester/" + name)); } /** * Validate the assertions for ObjectCreateRule3. * * @param object Root object returned by <code>digester.parse()</code> */ protected void validateObjectCreate3(Object root) { // 检验Employee的信息 // Validate the retrieved Employee assertNotNull("Digester returned an object", root); assertTrue("Digester returned an Employee", root instanceof Employee); Employee employee = (Employee) root; assertEquals("First name is correct", "First Name", employee.getFirstName()); assertEquals("Last name is correct", "Last Name", employee.getLastName()); // 检验名称时候home的address元素的相关属性 // Validate the corresponding "home" Address Address home = employee.getAddress("home"); assertNotNull("Retrieved home address", home); assertEquals("Home street", "Home Street", home.getStreet()); assertEquals("Home city", "Home City", home.getCity()); assertEquals("Home state", "HS", home.getState()); assertEquals("Home zip", "HmZip", home.getZipCode()); // 检验名称叫office的address的相关属性 // Validate the corresponding "office" Address Address office = employee.getAddress("office"); assertNotNull("Retrieved office address", office); assertEquals("Office street", "Office Street", office.getStreet()); assertEquals("Office city", "Office City", office.getCity()); assertEquals("Office state", "OS", office.getState()); assertEquals("Office zip", "OfZip", office.getZipCode()); } }
发表评论
-
使用commons-net对ftp文件上传下载
2012-08-01 18:38 2487项目中由于要使用到ftp服务,虽然之前对edtFT ... -
commons-io之inputstream学习
2010-08-20 19:41 2549ProxyInputStream类的学习 ... -
commons-io之WildcardFileFilter的实现
2010-08-02 11:35 2432上次这个是最后一个FileFilter,没 ... -
commons-io之filefilter学习
2010-07-27 10:23 50121. IOFileFilter接口 这个接口就是 ... -
common-io之Comparator阅读
2010-07-17 00:17 9571. 首先我们来查看AbstractFileCompa ... -
common-io 之ByteArrayOutputStream阅读
2010-07-16 18:56 3065首先我们来分析它里面的的实例变量:buffers: 可以看成是 ... -
一款文件上传信息即时同步刷新的代码的学习
2010-07-06 18:04 1252对下面链接提供的文件上传的代码的学习。 http://mao ... -
FileUpload之FileItem
2010-07-05 18:17 22628FileItem类主要是封装了一个File Item ... -
commons-io之ThresholdingOutputStream 和 DeferredFileOutputStream
2010-06-26 19:44 25101. ThresholdingOutputStream 这个 ... -
Digester 1.1 源码阅读
2010-02-03 20:36 1470前一段时间我们阅读了Digester的最初始的版本1 ... -
Digester 1.0 源码阅读
2010-02-01 20:15 1556近来在学习tomcat ... -
Digester学习
2010-01-26 00:45 1861近来在学习tomcat的源码,其中有个解析XML的 ...
相关推荐
资源为Digester的xml解析应用案例,
digester
Castor、digester实例 Castor、digester实例 Castor、digester实例
用Digester開發的一個小程序。
赠送jar包:commons-digester3-3.2.jar; 赠送原API文档:commons-digester3-3.2-javadoc.jar; 赠送源代码:commons-digester3-3.2-sources.jar; 赠送Maven依赖信息文件:commons-digester3-3.2.pom; 包含翻译后...
Digester java解析xml jar包
此为官网版本的commons-digester-2.1.jar maven中下载的commons-digester-2.1.jar无法使用
Digester不是一个XML Parser,它只是对SAX更高层次上的一个封装使用Digester,将XML映射成javaBean. 我们无须了解SAX和DOM的解析过程,只要给Digester添加一些解析规则,就能对一个xml文件进行解析。Digester使用...
commons-digester-2.0.rar源文件及jar文件
不错的解析XML的类,主要利用org.apache.commons.digester.Digester;
赠送jar包:commons-digester3-3.2.jar; 赠送原API文档:commons-digester3-3.2-javadoc.jar; 赠送源代码:commons-digester3-3.2-sources.jar; 赠送Maven依赖信息文件:commons-digester3-3.2.pom; 包含翻译后...
赠送jar包:commons-digester-1.8.1.jar; 赠送原API文档:commons-digester-1.8.1-javadoc.jar; 赠送源代码:commons-digester-1.8.1-sources.jar; 赠送Maven依赖信息文件:commons-digester-1.8.1.pom; 包含...
digester3.2 源码 and digester3.2 application sample
Digester读取xml教程.rar
commons-digester.jar
digester相关jar包,包括:commons-beanutils.jar、commons-collections.jar、commons-digester.jar、commons-logging-1.1.3.jar
digester组件简化了xml文件处理操作
digester是解析xml的一个架包,有他的方法
Digester的XML规则文件配置: 在Digester中提供了一个包:org.apache.commons.digester.xmlrules。这个包中的类可以从一个XML文档中读取并返回一个指定的配置信息。XML文档配置文件必须使用DTD,这是xmlrules包的一...