当前位置:首页 > 开发 > 编程语言 > XML > 正文

测试XmlHandler,接上篇(自己实现的XML和Entity映射工具)

发表于: 2012-10-12   作者:cug_xw   来源:转载   浏览:
摘要: 关于XmlHandler的单元测试代码: package test; import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Set; import org.dom4j.DocumentExcep

关于XmlHandler的单元测试代码:

package test;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.dom4j.DocumentException;

import com.cugxw.entity.Book;
import com.cugxw.entity.Publish;
import com.cugxw.utils.XmlHandler;
public class Test {

	@SuppressWarnings("unchecked")
	public static void main(String[] args) throws Exception{
		
		/*Initialize the entity object*/
		Book b = new Book();
		b.setBookId("1001");
		b.setBookName("English 4");
		b.setIsbn("00000000001");
		Publish pubA = new Publish();
		pubA.setPubId("pub-000000000000001");
		pubA.setPubName("The dog of China");
		b.setPublish(pubA);
		
		Publish pubB = new Publish();
		pubB.setPubId("pub-1111111111111110");
		pubB.setPubName("The dog of America");
		
		Set<Publish> set = new HashSet<Publish>();
		set.add(pubA);
		set.add(pubB);
		b.setSet(set);
		
		List<Publish> list = new ArrayList<Publish>();
		list.add(pubA);
		list.add(pubB);
		b.setList(list);
		
		XmlHandler xmlHandler = null;
		xmlHandler = new XmlHandler("com.cugxw.entity");
//		xmlHandler = new XmlHandler();
		xmlHandler.setUseXmlAnnotation(true);
		xmlHandler.showRegMap();
		String resXml = null;
		
		/*Test parse the xml*/
		resXml = xmlHandler.toXml(b);
		System.out.println("---------------------Entity To Xml---------------------");
		System.out.println("Return xml: \n" + resXml + "\n");
		
		/*Test xml to entity*/
		try {
			Book book = (Book)xmlHandler.fromXml(Book.class, resXml);
			System.out.println("---------------------Xml To Entity---------------------");
			System.out.println("Return entity: ");
			System.out.println("Book-BookId: " + book.getBookId());
			System.out.println("Book-BookName: " + book.getBookName());
			System.out.println("Book-ISBN: " + book.getIsbn());
			Publish publish = book.getPublish();
			if(publish != null)
			{
				System.out.println("Book-Publish-pubId: " 
						+ publish.getPubId());
				System.out.println("Book-Publish-pubName: " 
						+ publish.getPubName());
			}
			Set aset = book.getSet();
			if(aset != null && aset.size() > 0)
			{
				Iterator it = aset.iterator();
				while(it.hasNext())
				{
					Publish pub = (Publish)it.next();
					System.out.println("Book-Set-Publish-pubId:"
							+ pub.getPubId());
					System.out.println("Book-Set-Publish-pubName: " 
							+ pub.getPubName());
				}
				
				List<Publish> alist = book.getList();
				for(Publish publ : alist)
				{
					System.out.println("Book-List-Publish-pubId:"
							+ publ.getPubId());
					System.out.println("Book-List-Publish-pubName: " 
							+ publ.getPubName());
				}
			}
		} catch (DocumentException e) {
			e.printStackTrace();
		}
	}

}

 

测试XmlHandler,接上篇(自己实现的XML和Entity映射工具)

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号