What 라이브러리를 사용할 수 있습니 바인딩 Pojo 외부 파일에 대한 TDD 없이 많은 오버헤드?
문제
는 방법이 필요합니다 바인딩 POJO 개체 외부 entity,할 수 있는 XML,YAML,구조화된 텍스트 또는 아무것도 쉽게 작성하고 유지를 만들기 위해 모의 데이터에 대한 단위 테스트 및 TDD.아래는 일부 라이브러리려고 했지만,주요 문제들이 나를 붙어(적어도에 대한 더 많은 3 개월)Java1.4.나는 다음과 같은 통찰이에서 무엇을 사용할 수 있는 사람이라 저렴한 오버헤드 upfront 설치 프로그램(같은 사용하여 스키마 또는 Dtd,예를 들어)가능한 없이 복잡한 XML.여기에는 라이브러리가 정말 좋아한다(그러나 분명히 작동하지 않 1.4 또는 지원하지 않는 생성자-이 있어야 돼 setters):
다시 포함(또는 정말 쉽 Java XML 바인딩)
http://jvalentino.blogspot.com/2008/07/in-response-to-easiest-java-xml-binding.html http://sourceforge.net/projects/rejaxb/
견은 크게 감 묶 this:
<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"));
문제:그것은에 의존하는 주석,그래서 더 좋은 Java1.4
jYaml http://jyaml.sourceforge.net/
완벽하게 결합 this:
--- !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);
문제:그것은 작동하지 않을 것으로 생성자를(그래서 아무도 좋은 변경되지 않는 객체).나는 변경하거나 객체를 또는 사용자 지정 코드를 작성 por 처리 YAML 구축하고 있음을 보여준다
는 것을 기억하고 싶지로 많은 내가 할 수 있는 데이터를 쓰기술자,나는 뭔가 좋아"단지 작동합니다."
Do you have any suggestions?
해결책
채워질 물체가 간단한 콩이라면 Apache Common의 Beanutils 클래스를 보는 것이 좋습니다. Populate () 메소드는 설명 된 사례에 적합 할 수 있습니다. 일반적으로 Spring과 같은 종속성 주입 프레임 워크는 매우 유용 할 수 있지만 현재 문제에 대한 답이 아닐 수도 있습니다. XML 형태의 입력의 경우 JIBX는 좋은 대안이 될 수 있으므로 JAXB 1.0이 될 수 있습니다.
다른 팁
XSTREAM을 사용하기 만하면 (XML의 경우 또는 JSON을 시도해보십시오).
하지만...
남자, 나는 단지 테스트 데이터를 단위 테스트 자체 외부에 놓으면 읽을 수없는 테스트로 이어질 것이라고 생각하지 않습니다. 테스트 케이스를 읽을 때는 두 파일을 찾아야합니다. 리팩토링 도구를 잃게됩니다 (속성 이름을 변경할 때). Jay Fields는 나보다 더 잘 설명 할 수 있습니다.
http://blog.jayfields.com/2007/06/testing-inline-setup.html
친절한 안부
을 줄 수 있습니다 그것을 시도하 deefault XMLEncoder/XMLDecoder 추가된 플랫폼에서는 Java1.4
여기에는 방법이 사용하고 있습니다.
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();
}
}
}
}
그것은 쉽게,는 간단하는 핵심 라이브러리입니다.
당신이 쓰기 로드 메커니즘이 있습니다.
나는 이 스윙 응용 프로그램을 로드하는 데이터를 원격지에서 EJB 에 5-10secs.내가 무엇을를 저장하는 것이 이전 세션에서는 XML 가 이 때 응용 프로그램.그것은 모든 데이터 이전 세션에서 보다는 더 적은에서 1sec.
사용자 작업을 시작 응용 프로그램과 함께,백그라운드 스레드를 가져옵 이러한 요소는 변경되었을 때 마지막 세션이 있습니다.