下面的内容是根据李总培训资料里面的内容。
1.首先书写的TestTDDSuit.java,如下所示:
/*
* Copyright (C) 2011 GZ-ISCAS Inc.,All Rights Reserved.
*/
package com.tdd.test.app;
import java.util.List;
import junit.framework.TestCase;
import org.junit.Test;
import com.tdd.model.Customer;
import com.tdd.model.Order;
import com.tdd.model.OrderLine;
import com.tdd.model.Product;
import com.tdd.repsotory.OrderRepository;
import com.tdd.service.OrderService;
import com.tdd.service.impl.OrderServiceImpl;
import com.tdd.utils.ExceedOrderAmountException;
/**
* @ClassName: TestTDDSuit
* @Description: TODO(这里用一句话描述这个类的作用)
* @author ????? Email: ??????@gz.iscas.ac.cn
* @date 2012-2-15 - 下午4:44:58
* @version : 1.0
*/
public class TestTDDSuit extends TestCase {
//需求点
//T1: 订单可以有多个订单明绅
@Test
public void testOrderSize() {
Order o = new Order();
int size = o.getOrderOrderSize();
OrderLine ol = new OrderLine();
o.addOrderLine(ol);
// o.getOrderLines().add(ol);
assertEquals(size + 1,o.getOrderOrderSize());
}
//T2: 一个订单明绅必须对应一个订单
@Test
public void testOrderLineBelongsToOrder()
{
Order o = new Order();
OrderLine ol= new OrderLine();
o.addOrderLine(ol);
assertEquals(ol.getOrder(),o);
}
//T3: 一个订单明绅对应一个购买的产品
@Test
public void testOrderLineHasProduct() {
Product p = new Product();
OrderLine ol = new OrderLine(p,5);
assertEquals(ol.getProduct(),p);
assertEquals(ol.getProductAmount(),5);
}
//T4: Order中包吨的OrderLine对应的产品,丌允许重复
// @Test
// public void testProductCanOnlyInOneOrderLineOfAnOrder() {
// Order o = new Order();
// Product p = new Product();
// OrderLine ol1 = new OrderLine(p,5);
// OrderLine ol2 = new OrderLine(p,3);
// o.addOrderLine(ol1);
// o.addOrderLine(ol2);
// assertEquals(o.getOrderSize(),1);
// //assertEquals(o.getOrderLines().get(0).getProduct(),p);
// //assertEquals(o.getOrderLines().get(0).getProductAmount();
// }
//对上面的进行重构
@Test
public void testProductCanOnlyInOneOrderLineOfAnOrder2() {
Order o = new Order();
Product p = new Product();
try{
o.addOrderLine(p,5);
o.addOrderLine(p,3);
}catch(ExceedOrderAmountException evt){
evt.printStackTrace();
fail();
}
assertEquals(o.getOrderSize(),1);
assertEquals(((OrderLine) o.getOrderLines().get(0)).getProduct(),p);
assertEquals(((OrderLine) o.getOrderLines().get(0)).getProductAmount(),5 +3);
}
// @Test
public void testAddProduct(){
OrderService orderService = new OrderServiceImpl();
Product p = new Product();
orderService.addProduct(p,3);
//assertEquals((),1);
}
/**
* customer作为order的聚合根,可以获得所属的
order集合
*/
@Test
public void testCustomerHasSeveralOrders() {
Customer c = new Customer();
Order o = c. buildAndAddOrder();
//将o添加到customer的表单集合中
assertEquals(c.getOrderSize(),1);
Order o2= c.buildAndAddOrder();
assertEquals(c.getOrderSize(),2);
}
/**
* 只有从Order到Customer的引用关系
*/
@Test
public void testCustomerHasSeveralOrders2() {
Customer c = new Customer();
//客戶同訂單相關聯
Order o = new Order(c);
Order o2 = new Order(c);
//将订单添加到仓库
OrderRepository orderRepo= OrderRepository.getOrderRepository();
orderRepo.addOrder(o);
orderRepo.addOrder(o2);
//根据customer来查找相关的订单集合
List<Order> orders = orderRepo.findOrdersByCustomer(c);
assertEquals(orders.size(),2);
assertEquals(orders.get(0),o);
assertEquals(orders.get(1),o2);
}
@Test
public void testOrderBelongsToCustomer()
{
Customer c = new Customer();
Order o = new Order();
o.assignTo(c);
assertEquals(o.getBelongsTo(),c);
}
//订单总额丌能超过80万元
@Test
public void testOrderAmountCantExceed80w() {
Order o = new Order();
Product p = new Product("龙迈笔记本",10000);
Product p2 = new Product("龙迈PC",5000);
try {
o.addOrderLine(p,5);
//如果該異常不可到大家則會報錯為“不可到達的異常”
}catch(ExceedOrderAmountException ex) {
fail();
}
try {
o.addOrderLine(p2,10);
//fail();
}catch(ExceedOrderAmountException ex) {
}
}
//測試并發操作
@Test
public void testSys(){
Customer c = new Customer();
Order o = new Order(c);
Product p = new Product("龙迈笔记本",10000);
try {
o.addOrderLine(p,5);
} catch (ExceedOrderAmountException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//更新到仓库
OrderRepository orderRepo= OrderRepository.getOrderRepository();
orderRepo.updateOrderInfo(o);//丌能提供接口直接迚行OrderLine的更新
}
}
2.书写 Customer.java,Order.java,OrderLine.java,Produce.java
详细
内容,请查看附件
内容。
(略)
3.OrderRepository.java,
内容如下:
*/
package com.tdd.repsotory;
import java.util.ArrayList;
import java.util.List;
import com.tdd.model.Customer;
import com.tdd.model.Order;
/**
* @ClassName: OrderRepositoryorder
* @Description: TODO(倉庫類)
* @author xuzhongming Email:
xuzhongming@gz.iscas.ac.cn
* @date 2012-2-16 - 上午11:07:23
*@version : 1.0
*/
public class OrderRepository {
/**
* @Title: getOrderRepository
* @Description: TODO(这里用一句话描述这个
方法的作用)
* @param @return
* @return OrderRepositoryorder
* @throws
*/
public static OrderRepository getOrderRepository() {
// TODO Auto-generated method stub
return new OrderRepository();
}
/**
* @Title: addOrder
* @Description: TODO(这里用一句话描述这个
方法的作用)
* @param @param o
* @return void
* @throws
*/
public void addOrder(Order o) {
// TODO Auto-generated method stub
o.getCustomer().buildAndAddOrder(o);
}
/**
* @Title: findOrdersByCustomer
* @Description: TODO(得到該用戶的所有訂單列表)
* @param @param c
* @param @return
* @return List<Order>
* @throws
*/
public List<Order> findOrdersByCustomer(Customer c) {
// TODO Auto-generated method stub
return c.getOrderList();
}
/**
* @Title: updateOrderInfo
* @Description: TODO(更新OrderLine操作,在這里只能在聚合根中
修改其內容,而不能直接
修改OrderLine)
* @param @param o
* @return void
* @throws
*/
public void updateOrderInfo(Order o) {
// TODO Auto-generated method stub
o.updateOrderLine();
}
}