您的位置:軟件測試 > 開源軟件測試 > 開源單元測試工具 > Cactus
用Cactus來測試J2ee應(yīng)用
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時間:[ 2012/11/29 14:52:41 ] 推薦標(biāo)簽:

try {
javax.naming.InitialContext ctx=new javax.naming.InitialContext ();
Object ref = ctx.lookup("User");
//cast to Home interface
UserHome userHome = (UserHome) PortableRemoteObject.narrow(ref, UserHome.class);
return userHome;
}
catch (ClassCastException ex) {
ex.printStackTrace() ;
return null;
}catch (NamingException ex) {
ex.printStackTrace() ;
return null;
}

}

/**
* 返回UserInfoHome接口
* @return
*/
private UserInfoHome getUserInfoHome(){

try {
javax.naming.InitialContext ctx=new javax.naming.InitialContext ();
Object ref = ctx.lookup("UserInfo");
//cast to Home interface
UserInfoHome userInfoHome = (UserInfoHome) PortableRemoteObject.narrow(ref,

UserInfoHome.class);
return userInfoHome;
}
catch (ClassCastException ex) {
throw new EJBException();
}catch (NamingException ex) {
throw new EJBException(ex.toString());
}

}

/**
* 返回所有用戶記錄
* @return c
* @throws javax.ejb.FinderException
*/
public java.util.Collection findAll() {
Collection c = null;
try {
UserHome uh=this.getUserHome() ;
c=uh.findAll() ;
}
catch (FinderException ex) {
throw new javax.ejb.EJBException ();
}
return c;
}

/**
* 刪除所有記錄
*/
public void delAll(){
try {
UserHome u=getUserHome();

java.util.Collection c=u.findAll() ;
java.util.Iterator i=c.iterator() ;
while(i.hasNext() ){
u.remove(((User)i.next()).getName()) ;
}
}
catch (Exception ex) {
throw new EJBException(ex.toString());
}
}

/**
* 根據(jù)用戶名刪除記錄
* @param name
*/
public void delByName(String name) {
try {
User user=findByName(name);
UserHome uh=getUserHome();
uh.remove(user.getName()) ;
}
catch (Exception ex) {
throw new javax.ejb.EJBException (ex.toString());
}
}

/**
* 通過用戶名查找用戶記錄
* @param name
* @return
*/
public User findByName(String name) {
try {
UserHome uh=this.getUserHome() ;
User user=(User)uh.findByPrimaryKey(name) ;
UserHome u=this.getUserHome() ;
User uu=u.findByPrimaryKey(name) ;
return user;
}
catch (FinderException ex) {
throw new EJBException(ex.toString());
}

}

}

UserManagerBean是一個session bean ,它主要是對user的管理,和客戶端通訊,其實(shí)是session facade模式 。代碼里面有注釋,這里不多敘述了。

ejb-jar.xml 部署文件描述

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ejb-jar PUBLIC "-//Sun Microsystems, Inc.//DTD Enterprise JavaBeans 2.0//EN"

<ejb-jar>
<enterprise-beans>
<session>
<display-name>UserManager</display-name>
<ejb-name>UserManager</ejb-name>
<local-home>usersystem.UserManagerLocalHome</local-home>
<local>usersystem.UserManagerLocal</local>
<ejb-class>usersystem.UserManagerBean</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
<ejb-local-ref>
<description />
<ejb-ref-name>User</ejb-ref-name>
<ejb-ref-type>Entity</ejb-ref-type>
<local-home>usersystem.UserHome</local-home>
<local>usersystem.User</local>
<ejb-link>User</ejb-link>
</ejb-local-ref>
<ejb-local-ref>
<description />

上一頁12345下一頁
軟件測試工具 | 聯(lián)系我們 | 投訴建議 | 誠聘英才 | 申請使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd