2008-10-12 31 views
0

我需要一种方法将POJO对象绑定到外部实体,可以是XML,YAML,结构化文本或易于编写和维护的任何内容,以便为单元测试和TDD创建模拟数据。下面是我尝试过的一些库,但是他们遇到的主要问题是我被卡住了(至少3个月)到Java 1.4。我希望获得关于我可以使用的任何见解,并尽可能降低开销和前期设置(如使用Schema或DTD),而不使用复杂的XML。这里是图书馆我真的很喜欢(但显然不能与1.4工作或不支持的构造 - 你总得有制定者):我可以使用哪些库将POJO绑定到TDD的外部文件,而无需太多开销?

RE-JAXB(或非常简单的Java XML绑定)

http://jvalentino.blogspot.com/2008/07/in-response-to-easiest-java-xml-binding.html http://sourceforge.net/projects/rejaxb/

Seamlessy结合本:

<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

jYaml http://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解析的自定义代码。请记住,我想避免 - 尽可能地 - 写数据描述符,我想要一些“正常工作”的东西。

你有什么建议吗?

回答

1

如果要填充的对象是简单的bean,那么查看apache common的BeanUtils类可能是个好主意。 populate()方法可能适用于所描述的情况。通常,像Spring这样的依赖注入框架可能非常有用,但这可能不是当前问题的答案。对于xml格式的输入,jibx可能是一个很好的选择,所以会是jaxb 1.0。

+0

只是简单地看了看,并且它看起来很简单,我会看得更远。现在感谢。 – kolrie 2008-10-12 17:00:06

0

只需使用XStream(对于XML或者您可以尝试JSON)。

但是......

男人,我不能避免地认为把单元测试本身会导致你无法读取的测试之外的测试数据。阅读测试用例时,您需要查看两个文件,您将失去重构工具(更改属性名称时)。周杰伦字段可以解释它比我好:

http://blog.jayfields.com/2007/06/testing-inline-setup.html

亲切的问候

+0

完全是我的观点与我的团队!但他们坚持。我目前使用混合了Builder模式的Fluent Interfaces和(至少我)喜欢它! :-) – kolrie 2008-10-12 23:18:54

0

你可以给它一个尝试对已添加到平台中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(); 
      } 
     } 
    } 

}

这很容易,很简单,就是在核心库。

你只需要编写加载机制。

我有这个摆动应用程序,在5-10秒内从远程EJB加载数据。我所做的就是像以前一样以XML格式存储以前的会话,当应用程序加载时,它会在不到1秒的时间内获得前一会话的所有数据。

当用户开始使用应用程序时,后台线程将获取自上次会话以来发生更改的那些元素。

相关问题