⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 testcartjunit1.java~93~

📁 购物车模块实例。购物车模块可以浏览商品类别;可以根据商品类别浏览商品信息;可以购买商品;可以查看购物车的商品;可以修改购买商品的数量和删除购买的商品。
💻 JAVA~93~
字号:
package junittest;import junit.framework.*;import cmscartpro.*;import javax.naming.*;import java.util.Properties;import javax.rmi.PortableRemoteObject;import java.rmi.RemoteException;import java.util.*;public class TestCartJUnit1 extends TestCase {  private static final String ERROR_NULL_REMOTE = "Remote interface reference is null. "      + "It must be created by calling one of the Home interface methods first.  "      + "Make sure that the setUp() method calls one of the create() or finder Home "      + "interfacewrappers, or the userTableCPRemote remote reference is assigned there "      + "in some other way.";  private static final int MAX_OUTPUT_LINE_LENGTH = 100;  private boolean logging = false;  private CartHome cartHome = null;  private Cart cart = null;  public TestCartJUnit1(String name) {    super(name);  }  public void initialize() throws Exception {    //创建服务器查找类    Context context = getInitialContext();    //取得服务器的EJB对象    Object ref = context.lookup("Cart");    //取得EJB的本地接口    cartHome = (CartHome) PortableRemoteObject.narrow(ref, CartHome.class);  }  //服务器查找类初始化方法  private Context getInitialContext() throws Exception {    String url = "t3://bemyfriend:7001";    String user = null;    String password = null;    Properties properties = null;    try {      properties = new Properties();      properties.put(Context.INITIAL_CONTEXT_FACTORY,                     "weblogic.jndi.WLInitialContextFactory");      properties.put(Context.PROVIDER_URL, url);      if (user != null) {        properties.put(Context.SECURITY_PRINCIPAL, user);        properties.put(Context.SECURITY_CREDENTIALS, password == null ? "" : password);      }      return new InitialContext(properties);    }    catch(Exception e) {      System.out.println("Unable to connect to WebLogic server at " + url);      System.out.println("Please make sure that the server is running.");      throw e;    }  }  public void setUp() throws Exception {    super.setUp();    initialize();    //创建顾客字符串    String customer = "jack";    //取得EJB的远程接口    create("jack");  }  public void tearDown() throws Exception {    //激活状态EJB的    cart.remove();    cartHome = null;    cart = null;    super.tearDown();  }  public Cart create(String customer) throws Exception {    cart = cartHome.create(customer);    return cart;  }  //测试添加商品的方法  public void testAddGoods() throws RemoteException {    assertNotNull(ERROR_NULL_REMOTE, cart);    //创建商品集合    Hashtable goods = new Hashtable();    goods.put("1", "糖果@@每箱30盒@@10@@5");    goods.put("2", "花生@@每箱30包@@10.0@@20");    goods.put("3", "花生A@@每箱15包@@10.0@@5");    goods.put("3", "花生A@@每箱15包@@10.0@@15");    //应用addGoods方法添加商品    cart.addGoods(goods);    this.assertEquals("购物车剩余的商品数", 3, cart.getGoods().size());  }  //测试更新商品的方法  public void testUpdateGoods() throws RemoteException {    assertNotNull(ERROR_NULL_REMOTE, cart);    Hashtable goods = new Hashtable();    //将糖果的数量从5更新到10    goods.put("1", "糖果@@每箱30盒@@10@@10");    //应用updateGoods方法更新商品    cart.updateGoods(goods);    this.assertEquals("糖果@@每箱30盒@@10@@10", (cart.getGoods().get("1")).toString());  }  //测试删除商品的方法  public void testRemoveGoods() throws RemoteException {    assertNotNull(ERROR_NULL_REMOTE, cart);    //创建商品集合    Hashtable goods = new Hashtable();    goods.put("1", "糖果@@每箱30盒@@10@@5");    goods.put("2", "花生@@每箱30包@@10.0@@20");    goods.put("3", "花生A@@每箱15包@@10.0@@5");    goods.put("3", "花生A@@每箱15包@@10.0@@15");    //应用addGoods方法添加商品    cart.addGoods(goods);    Collection goodsId = new ArrayList();    goodsId.add("1");    goodsId.add("2");    //应用remove方法删除商品    cart.removeGoods(goodsId);    this.assertEquals("购物车剩余的商品数", 1, cart.getGoods().size());  }  //测试设置顾客名字的方法  public void testSetCustomer() throws RemoteException {    String customer = "jack111";    assertNotNull(ERROR_NULL_REMOTE, cart);    //设置顾客的名字    cart.setCustomer(customer);    this.assertEquals("顾客名字应该是jack111", customer, cart.getCustomer());  }  public CartHome getHome() {    return cartHome;  }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -