TDD应用试例(根据领域驱动模型设计的培训内容)

前端之家收集整理的这篇文章主要介绍了TDD应用试例(根据领域驱动模型设计的培训内容)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
下面的内容是根据李总培训资料里面的内容
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();
}



}

4.OrderService.java
*/
package com.tdd.service;

import com.tdd.model.Product;

/**
* @ClassName: OrderService
* @Description: TODO(这里用一句话描述这个类的作用)
* @author xuzhongming Email: xuzhongming@gz.iscas.ac.cn
* @date 2012-2-16 - 上午10:37:34
*@version : 1.0
*/
public interface OrderService {

/**
* @Title: addProduct
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param @param p
* @param @param i
* @return void
* @throws
*/
void addProduct(Product p,int i);

}

5.OrderServiceImple.java,All Rights Reserved.
*/
package com.tdd.service.impl;

import com.tdd.factory.OrderFactory;
import com.tdd.model.Order;
import com.tdd.model.Product;
import com.tdd.service.OrderService;
import com.tdd.utils.ExceedOrderAmountException;

/**
* @ClassName: OrderServiceImpl
* @Description: TODO(訂單服務類)
* @author xuzhongming Email: xuzhongming@gz.iscas.ac.cn
* @date 2012-2-16 - 上午10:33:08
*@version : 1.0
*/
public class OrderServiceImpl implements OrderService{
public void addProduct(Product p,int amount) {
Order o = getCustomerOrder();
try {
o.addOrderLine(p,amount);
} catch (ExceedOrderAmountException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

/**
* @Title: getCustomerOrder
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param @return
* @return Order
* @throws
*/
private Order getCustomerOrder() {
// TODO Auto-generated method stub
return OrderFactory.getInstance();
}
}

6.自定义异常类ExceedOrderAmountException.java,All Rights Reserved.
*/
package com.tdd.utils;

/**
* @ClassName: ExceedOrderAmountException
* @Description: TODO(超過定單額所導致的異常)
* @author xuzhongming Email: xuzhongming@gz.iscas.ac.cn
* @date 2012-2-16 - 下午1:44:19
*@version : 1.0
*/
public class ExceedOrderAmountExceptionextends Exception {

/**
* @Fields serialVersionUID : TODO(用一句话描述这个变量表示什么)
*/
private static final long serialVersionUID = 1L;
private String message;
public ExceedOrderAmountException(String_message)
{
message = _message;
}
public String toString()
{
return message;
}

}

(完,待续.........)

附件:

http://download.csdn.net/detail/haha_mingg/4069213

原文链接:https://www.f2er.com/javaschema/286924.html

猜你在找的设计模式相关文章