问题:
一个系统有一些信息放在一个XML文件中,由于此XML文件会经常变动,所以写Test Case不能依赖此XML文件,否则在使用中每一次XML正常的变动都会导致TestCase出错。那么该如何测试呢。
解决方案一:
将XML的内容用字符串的方式写在TestCase中。如下所示,这个TestCase主要是测试ViewXMLParser这个类的parse方法,这个方法的作用是将XML配置文件中的信息转化成一个个的Bean。
一个系统有一些信息放在一个XML文件中,由于此XML文件会经常变动,所以写Test Case不能依赖此XML文件,否则在使用中每一次XML正常的变动都会导致TestCase出错。那么该如何测试呢。
解决方案一:
将XML的内容用字符串的方式写在TestCase中。如下所示,这个TestCase主要是测试ViewXMLParser这个类的parse方法,这个方法的作用是将XML配置文件中的信息转化成一个个的Bean。
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.List;
import junit.framework.TestCase;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
/**
* @author chengang 2006-3-6
*/
public class TestViewParser extends TestCase {
private String xml1;
private String xml2;
public TestViewParser() {
{
// 一个完整的测试,及测试可选项
StringBuffer sb = new StringBuffer();
sb.append("<?xml version=\"1.0\" encoding=\"GB2312\"?>");
sb.append("<jmxui>");
sb.append(" <views>");
sb.append(" <view id=\"LogTestView\">");
sb.append(" <operation name=\"storeConfig1\"/>");
sb.append(" <attributes>");
sb.append(" <attribute name=\"times\" description=\"时间值\"/>");
sb.append(" <attribute name=\"logs\" description=\"日志数\"/>");
sb.append(" </attributes>");
sb.append(" </view>");
sb.append(" <view id=\"LogConfigView\" class=\"com.wxxr.management.admin.console.jmx.custom.ui.CustomComposite\"/>");
sb.append(" </views>");
sb.append("</jmxui>");
xml1 = sb.toString();
}
{
// 测试特别情况
StringBuffer sb = new StringBuffer();
sb.append("<?xml version=\"1.0\" encoding=\"GB2312\"?>");
sb.append("<jmxui>");
sb.append(" <views>");
sb.append(" </views>");
sb.append("</jmxui>");
xml2 = sb.toString();
}
}
/*
* Test method for 'com.wxxr.management.admin.console.jmx.custom.JmxXmlReader.getGroupNodes()'
*/
public void testParse() throws Exception {
Element viewsElement = getViewsElement(xml1);
List<ViewBean> list = new ViewXMLParser().parse(viewsElement);
assertEquals(2, list.size());
{
ScriptViewBean bean = (ScriptViewBean) list.get(0);
assertEquals("LogTestView", bean.getId());
assertEquals(ScriptComposite. class.getName(), bean.getViewClass());
assertEquals("org.jnp.interfaces.NamingContextFactory", node.getFactory());
assertEquals("jnp://localhost:1099", node.getUrl());
assertEquals("jmx/rmi/RMIAdaptor", node.getJndi());
}
{
CustomViewBean bean = (CustomViewBean) list.get(1);
assertEquals("LogConfigView", bean.getId());
assertEquals("com.wxxr.management.admin.console.jmx.custom.ui.CustomComposite", bean.getViewClass());
}
}
private Element getViewsElement(String xml) {
InputStream inputStream = new ByteArrayInputStream(xml.getBytes());
Document doc = null;
try {
SAXReader reader = new SAXReader();
doc = reader.read(inputStream);
} catch (DocumentException e) {
e.printStackTrace();
}
Element root = doc.getRootElement();
Element viewsElement = root.element("views");
return viewsElement;
}
}
import java.io.InputStream;
import java.util.List;
import junit.framework.TestCase;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
/**
* @author chengang 2006-3-6
*/
public class TestViewParser extends TestCase {
private String xml1;
private String xml2;
public TestViewParser() {
{
// 一个完整的测试,及测试可选项
StringBuffer sb = new StringBuffer();
sb.append("<?xml version=\"1.0\" encoding=\"GB2312\"?>");
sb.append("<jmxui>");
sb.append(" <views>");
sb.append(" <view id=\"LogTestView\">");
sb.append(" <operation name=\"storeConfig1\"/>");
sb.append(" <attributes>");
sb.append(" <attribute name=\"times\" description=\"时间值\"/>");
sb.append(" <attribute name=\"logs\" description=\"日志数\"/>");
sb.append(" </attributes>");
sb.append(" </view>");
sb.append(" <view id=\"LogConfigView\" class=\"com.wxxr.management.admin.console.jmx.custom.ui.CustomComposite\"/>");
sb.append(" </views>");
sb.append("</jmxui>");
xml1 = sb.toString();
}
{
// 测试特别情况
StringBuffer sb = new StringBuffer();
sb.append("<?xml version=\"1.0\" encoding=\"GB2312\"?>");
sb.append("<jmxui>");
sb.append(" <views>");
sb.append(" </views>");
sb.append("</jmxui>");
xml2 = sb.toString();
}
}
/*
* Test method for 'com.wxxr.management.admin.console.jmx.custom.JmxXmlReader.getGroupNodes()'
*/
public void testParse() throws Exception {
Element viewsElement = getViewsElement(xml1);
List<ViewBean> list = new ViewXMLParser().parse(viewsElement);
assertEquals(2, list.size());
{
ScriptViewBean bean = (ScriptViewBean) list.get(0);
assertEquals("LogTestView", bean.getId());
assertEquals(ScriptComposite. class.getName(), bean.getViewClass());
assertEquals("org.jnp.interfaces.NamingContextFactory", node.getFactory());
assertEquals("jnp://localhost:1099", node.getUrl());
assertEquals("jmx/rmi/RMIAdaptor", node.getJndi());
}
{
CustomViewBean bean = (CustomViewBean) list.get(1);
assertEquals("LogConfigView", bean.getId());
assertEquals("com.wxxr.management.admin.console.jmx.custom.ui.CustomComposite", bean.getViewClass());
}
}
private Element getViewsElement(String xml) {
InputStream inputStream = new ByteArrayInputStream(xml.getBytes());
Document doc = null;
try {
SAXReader reader = new SAXReader();
doc = reader.read(inputStream);
} catch (DocumentException e) {
e.printStackTrace();
}
Element root = doc.getRootElement();
Element viewsElement = root.element("views");
return viewsElement;
}
}