我需要一种将POJO对象绑定到外部实体的方法,可以是XML、YAML、结构化文本或任何易于编写和维护的东西,以便为单元测试和TDD创建Mock数据。以下是我尝试过的一些库,但它们的主要问题是我被Java 1.4卡住了(至少3个多月)。我想了解一下我可以使用什么,尽可能低的开销和预先设置(例如使用Schema或DTD),并且不需要复杂的XML。以下是我非常喜欢的库(但这显然不适用于1.4或不支持构造函数——你必须有setter):
RE-JAXB(或真正简单的Java XML绑定)
http://jvalentino.blogspot.com/2008/07/in-response-to-easiest-java-xml-binding.htmlhttp://sourceforge.net/projects/rejaxb/
Seamless绑定此:
<item>
<title>Astronauts' Dirty Laundry</title>
<link>http://liftoff.msfc.nasa.gov/news/2003/news-laundry.asp</link>
<description>Compared to earlier spacecraft, the International Space
Station has many luxuries, but laundry facilities are not one of them.
Instead, astronauts have other options.</description>
<pubDate>Tue, 20 May 2003 08:56:02 GMT</pubDate>
<guid>http://liftoff.msfc.nasa.gov/2003/05/20.html#item570</guid>
</item>
对此:
@ClassXmlNodeName("item")
public class Item {
private String title;
private String link;
private String description;
private String pubDate;
private String guid;
//getters and settings go here...
}
使用:
Rss rss = new Rss();
XmlBinderFactory.newInstance().bind(rss, new File("Rss2Test.xml"));
问题:它依赖于注释,所以对Java 1.4 没有好处
jYamlhttp://jyaml.sourceforge.net/
无缝绑定:
--- !user
name: Felipe Coury
password: felipe
modules:
- !module
id: 1
name: Main Menu
admin: !user
name: Admin
password: password
对此:
public class User {
private String name;
private String password;
private List modules;
}
public class Module {
private int id;
private String name;
private User admin;
}
使用:
YamlReader reader = new YamlReader(new FileReader("example.yaml"));
reader.getConfig().setClassTag("user", User.class);
reader.getConfig().setClassTag("module", Module.class);
User user = (User) reader.read(User.class);
问题:它不适用于构造函数(所以对不可变对象没有好处)。我必须要么更改我的对象,要么编写自定义代码来处理YAML解析。
请记住,我希望尽可能避免写数据描述符,我希望写一些"有效"的东西。
你有什么建议吗?
如果要填充的对象是简单的bean,那么查看apachecommon的BeanUtils类可能是个好主意。populate()方法可能适合所描述的情况。一般来说,像Spring这样的依赖注入框架可能非常有用,但这可能不能解决当前的问题。对于xml形式的输入,jibx可能是一个不错的选择,jaxb1.0也是如此。
只需使用XStream(对于XML,或者您可以尝试使用JSON)。
但是。。。
伙计,我只是不可避免地认为,把测试数据放在单元测试之外会导致无法读取的测试。在读取测试用例时,您需要查看两个文件,您将丢失重构工具(在更改属性名称时)。Jay Fields比我更能解释:
http://blog.jayfields.com/2007/06/testing-inline-setup.html
亲切问候
您可以尝试一下在Java1.4 中添加到平台上的deefault XMLEncoder/XMLDecoder
这是我使用它的方法。
import java.beans.XMLEncoder;
import java.beans.XMLDecoder;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class ToXml {
/**
* Write an object to a file in XML format.
* @param o - The object to serialize.
* @param file - The file where to write the object.
*/
public static void writeObject( Object o, String file ) {
XMLEncoder e = null;
try {
e = new XMLEncoder( new BufferedOutputStream( new FileOutputStream(file)));
e.writeObject(o);
}catch( IOException ioe ) {
throw new RuntimeException( ioe );
}finally{
if( e != null ) {
e.close();
}
}
}
/**
* Read a xml serialized object from the specified file.
* @param file - The file where the serialized xml version of the object is.
* @return The object represented by the xmlfile.
*/
public static Object readObject( String file ){
XMLDecoder d = null;
try {
d = new XMLDecoder( new BufferedInputStream( new FileInputStream(file)));
return d.readObject();
}catch( IOException ioe ) {
throw new RuntimeException( ioe );
}finally{
if( d != null ) {
d.close();
}
}
}
}
它很简单,很简单,就在核心库中。
您只需要编写加载机制。
我有一个swing应用程序,它可以在5-10秒内从远程EJB加载数据。我所做的是像这样将上一个会话存储在XML中,当应用程序加载时,它在不到1秒的时间内就拥有了上一会话的所有数据
当用户开始使用该应用程序时,后台线程会获取自上次会话以来更改的元素。