/*
* Copyright 2006 Borys Burnayev
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package test.business;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import javax.naming.InitialContext;
import junit.extensions.TestSetup;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.jboss.ejb3.embedded.EJB3StandaloneBootstrap;
import com.rdacorp.petstore.business.dao.AccountDao;
import com.rdacorp.petstore.business.dao.LoginException;
import com.rdacorp.petstore.business.dao.OrderDao;
import com.rdacorp.petstore.business.dao.PasswordException;
import com.rdacorp.petstore.business.dao.ProductDao;
import com.rdacorp.petstore.domain.Account;
import com.rdacorp.petstore.domain.Address;
import com.rdacorp.petstore.domain.Category;
import com.rdacorp.petstore.domain.CreditCardCompany;
import com.rdacorp.petstore.domain.Item;
import com.rdacorp.petstore.domain.Order;
import com.rdacorp.petstore.domain.OrderItem;
import com.rdacorp.petstore.domain.Payment;
import com.rdacorp.petstore.domain.Person;
import com.rdacorp.petstore.domain.Product;
import com.rdacorp.petstore.domain.UserProfile;
/**
* @author Borys Burnayev
*/
public class DaoTest extends TestCase {
public DaoTest() {
super("DaoTest");
}
public static Test suite() throws Exception {
TestSuite suite = new TestSuite();
suite.addTestSuite(DaoTest.class);
// setup test so that embedded JBoss is started/stopped once for all tests
// here.
TestSetup wrapper = new TestSetup(suite) {
protected void setUp() {
startupEmbeddedJboss();
}
protected void tearDown() {
shutdownEmbeddedJboss();
}
};
return wrapper;
}
public static void startupEmbeddedJboss() {
EJB3StandaloneBootstrap.boot(null);
EJB3StandaloneBootstrap.scanClasspath();
}
public static void shutdownEmbeddedJboss() {
EJB3StandaloneBootstrap.shutdown();
}
public void testLoginNoSuchLogin() {
try {
InitialContext ctx = getInitialContext();
AccountDao accountDao = (AccountDao) ctx.lookup("AccountDao/local");
try {
accountDao.login("asdfda", "asfas");
}
catch (LoginException e) {
assertEquals("Exception message", "Login asdfda does not exist.", e.getMessage());
return;
}
catch (PasswordException e) {
fail("WrongPasswordException is not expected here.");
}
fail("NoSuchLoginException is expected here.");
}
catch (Exception e) {
e.printStackTrace();
fail();
}
}
public void testLoginWrongPassword() {
try {
InitialContext ctx = getInitialContext();
AccountDao accountDao = (AccountDao) ctx.lookup("AccountDao/local");
try {
accountDao.login("jilld", "password");
}
catch (LoginException e) {
fail("NoSuchLoginException is not expected here.");
}
catch (PasswordException e) {
assertEquals("Exception message", "Password doesn't match.", e.getMessage());
return;
}
fail("WrongPasswordException is expected here.");
}
catch (Exception e) {
e.printStackTrace();
fail();
}
}
public void testLogin() {
UserProfile profile = null;
try {
InitialContext ctx = getInitialContext();
AccountDao accountDao = (AccountDao) ctx.lookup("AccountDao/local");
try {
profile = accountDao.login("jilld", "jillDpass");
}
catch (LoginException e) {
fail("NoSuchLoginException is not expected here.");
}
catch (PasswordException e) {
fail("WrongPasswordException is not expected here.");
}
System.out.println(profile);
System.out.println();
System.out.println(profile.getPerson());
System.out.println();
System.out.println(profile.getPerson().getAccount());
}
catch (Exception e) {
e.printStackTrace();
fail();
}
}
public void testPlaceOrder() {
try {
InitialContext ctx = getInitialContext();
AccountDao accountDao = (AccountDao) ctx.lookup("AccountDao/local");
ProductDao productDao = (ProductDao) ctx.lookup("ProductDao/local");
OrderDao orderDao = (OrderDao) ctx.lookup("OrderDao/local");
// retrieve data
UserProfile userProfile = accountDao.login("jackb", "secret2");
Account account = userProfile.getPerson().getAccount();
Person personOnFile = account.getPerson();
Address addressOnFile = account.getAddress();
CreditCardCompany creditCardCompany = (CreditCardCompany) orderDao.getCreditCardCompanies().iterator().next();
Category category = (Category) productDao.getCategories().iterator().next();
Product product = (Product) productDao.getProducts(category).iterator().next();
Item item = (Item) productDao.getItems(product).iterator().next();
// create new data
OrderItem orderItem = new OrderItem(10, item);
Person billingPerson = new Person(personOnFile.getFirstName(), personOnFile.getLastName(), personOnFile
.getEmailAddress(), personOnFile.getPhoneNumber(), personOnFile.getEnableMylist(), personOnFile
.getEnableTips(), personOnFile.getFavoriteCategory(), personOnFile.getPreferredLanguage());
Person shippingPerson = new Person(personOnFile.getFirstName(), personOnFile.getLastName(), personOnFile
.getEmailAddress(), personOnFile.getPhoneNumber(), personOnFile.getEnableMylist(), personOnFile
.getEnableTips(), personOnFile.getFavoriteCategory(), personOnFile.getPreferredLanguage());
Address billingAddress = new Address(addressOnFile.getAddressLine1(), addressOnFile.getAddressLine2(),
addressOnFile.getCity(), addressOnFile.getZip(), addressOnFile.getState());
Address shippingAddress = new Address(addressOnFile.getAddressLine1(), addressOnFile.getAddressLine2(),
addressOnFile.getCity(), addressOnFile.getZip(), addressOnFile.getState());
Payment payment = new Payment(new BigDecimal(100), Calendar.getInstance().getTime(), "1111222233334444", 6,
2005, creditCardCompany, account);
Order order = new Order(new BigDecimal(orderItem.getQuantity() * item.getPrice().doubleValue()),
billingPerson, billingAddress, shippingPerson, shippingAddress, payment);
List<OrderItem> orderItems = new ArrayList<OrderItem>(1);
orderItems.add(orderItem);
order.setOrderItems(orderItems);
orderItem.setOrder(order);
orderDao.placeOrder(order);
}
catch (Exception e) {
e.printStackTrace();
fail();
}
}
public void testGetCategories() {
try {
InitialContext ctx = getInitialContext
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论




















收起资源包目录





































































































共 294 条
- 1
- 2
- 3
资源评论


yudy_zychen
- 粉丝: 0
- 资源: 2
上传资源 快速赚钱
我的内容管理 展开
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助


安全验证
文档复制为VIP权益,开通VIP直接复制
