📄 daotest.xdt
字号:
<XDtTagDef:tagDef namespace="Form" handler="org.example.antbook.xdoclet.FormTagsHandler"/><XDtTagDef:tagDef namespace="MethodEx" handler="org.example.antbook.xdoclet.MethodExTagsHandler"/>package <XDtForm:parentPackageName/>.dao;
import java.util.List;
import <XDtConfig:configParameterValue paramName="basePackageName"/>.dao.BaseDaoTestCase;
import <XDtPackage:packageName/>.<XDtClass:className/>;
import org.springframework.dao.InvalidDataAccessApiUsageException;
import org.springframework.orm.ObjectRetrievalFailureException;
public class <XDtClass:className/>DaoTest extends BaseDaoTestCase {
private <XDtMethodEx:idField getType="propertyType"/> <XDtForm:classNameLower/>Id = new <XDtMethodEx:idField getType="propertyType"/>("1");
private <XDtClass:className/>Dao dao = null;
public void set<XDtForm:className/>Dao(<XDtForm:className/>Dao dao) {
this.dao = dao;
}
public void testAdd<XDtForm:className/>() throws Exception {
<XDtForm:className/> <XDtForm:classNameLower/> = new <XDtForm:className/>();
// set required fields
<XDtMethod:forAllMethods>
<XDtMethod:ifMethodTagValueEquals tagName="hibernate.id" paramName="generator-class" value="assigned">
<XDtMethodEx:methodType/> <XDtMethod:propertyName/> = <XDtMethodEx:randomValueForSetter/>;
<XDtForm:classNameLower/>.<XDtMethod:setterMethod/>(<XDtMethod:propertyName/>);
</XDtMethod:ifMethodTagValueEquals>
<XDtMethod:ifMethodTagValueEquals tagName="hibernate.property" paramName="not-null" value="true">
<XDtMethodEx:methodType/> <XDtMethod:propertyName/> = <XDtMethodEx:randomValueForSetter/>;
<XDtForm:classNameLower/>.<XDtMethod:setterMethod/>(<XDtMethod:propertyName/>);
</XDtMethod:ifMethodTagValueEquals>
</XDtMethod:forAllMethods>
dao.save<XDtForm:className/>(<XDtForm:classNameLower/>);
// verify a primary key was assigned
assertNotNull(<XDtForm:classNameLower/>.<XDtMethodEx:idField getType="getterName"/>());
// verify set fields are same after save
<XDtMethod:forAllMethods>
<XDtMethod:ifMethodTagValueEquals tagName="hibernate.property" paramName="not-null" value="true">
assertEquals(<XDtMethod:propertyName/>, <XDtForm:classNameLower/>.<XDtMethod:getterMethod/>());
</XDtMethod:ifMethodTagValueEquals>
</XDtMethod:forAllMethods>
}
public void testGet<XDtForm:className/>() throws Exception {
<XDtForm:className/> <XDtForm:classNameLower/> = dao.get<XDtForm:className/>(<XDtForm:classNameLower/>Id);
assertNotNull(<XDtForm:classNameLower/>);
}
public void testGet<XDtForm:className/>s() throws Exception {
<XDtForm:className/> <XDtForm:classNameLower/> = new <XDtForm:className/>();
<XDtMethod:forAllMethods>
<XDtMethod:ifMethodTagValueEquals tagName="hibernate.id" paramName="generator-class" value="assigned">
<XDtType:ifIsOfType value="return-type" type="java.lang.String" extent="concrete-type">
//set value for primary key
<XDtForm:classNameLower/>.<XDtMethod:setterMethod/>("1");
</XDtType:ifIsOfType>
</XDtMethod:ifMethodTagValueEquals>
</XDtMethod:forAllMethods>
List results = dao.get<XDtForm:className/>s(<XDtForm:classNameLower/>);
assertTrue(results.size() > 0);
}
public void testSave<XDtForm:className/>() throws Exception {
<XDtForm:className/> <XDtForm:classNameLower/> = dao.get<XDtForm:className/>(<XDtForm:classNameLower/>Id);
// update required fields
<XDtMethod:forAllMethods>
<XDtMethod:ifMethodTagValueEquals tagName="hibernate.property" paramName="not-null" value="true">
<XDtMethodEx:methodType/> <XDtMethod:propertyName/> = <XDtMethodEx:randomValueForSetter/>;
<XDtForm:classNameLower/>.<XDtMethod:setterMethod/>(<XDtMethod:propertyName/>);
</XDtMethod:ifMethodTagValueEquals>
</XDtMethod:forAllMethods>
dao.save<XDtForm:className/>(<XDtForm:classNameLower/>);
<XDtMethod:forAllMethods>
<XDtMethod:ifMethodTagValueEquals tagName="hibernate.property" paramName="not-null" value="true">
assertEquals(<XDtMethod:propertyName/>, <XDtForm:classNameLower/>.<XDtMethod:getterMethod/>());
</XDtMethod:ifMethodTagValueEquals>
</XDtMethod:forAllMethods>
}
public void testRemove<XDtForm:className/>() throws Exception {
<XDtMethodEx:idField getType="propertyType"/> removeId = new <XDtMethodEx:idField getType="propertyType"/>("3");
dao.remove<XDtForm:className/>(removeId);
try {
dao.get<XDtForm:className/>(removeId);
fail("<XDtForm:classNameLower/> found in database");
} catch (ObjectRetrievalFailureException e) {
assertNotNull(e.getMessage());
} catch (InvalidDataAccessApiUsageException e) { // Spring 2.0 throws this one
assertNotNull(e.getMessage());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -