📄 daotest.java
字号:
/*
* 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();
ProductDao productDao = (ProductDao) ctx.lookup("ProductDao/local");
Collection categories = productDao.getCategories();
assertFalse(categories == null || categories.isEmpty());
for (Iterator ci = categories.iterator(); ci.hasNext();) {
Category category = (Category) ci.next();
System.out.println(category);
}
}
catch (Exception e) {
e.printStackTrace();
fail();
}
}
public void testGetProducts() {
try {
InitialContext ctx = getInitialContext();
ProductDao productDao = (ProductDao) ctx.lookup("ProductDao/local");
Collection categories = productDao.getCategories();
assertFalse(categories == null || categories.isEmpty());
Category category = (Category) categories.iterator().next();
Collection products = productDao.getProducts(category);
assertFalse(products == null || products.isEmpty());
for (Iterator pi = products.iterator(); pi.hasNext();) {
Product product = (Product) pi.next();
System.out.println(product);
}
}
catch (Exception e) {
e.printStackTrace();
fail();
}
}
public void testGetItems() {
try {
InitialContext ctx = getInitialContext();
ProductDao productDao = (ProductDao) ctx.lookup("ProductDao/local");
Collection categories = productDao.getCategories();
assertFalse(categories == null || categories.isEmpty());
Category category = (Category) categories.iterator().next();
Collection products = productDao.getProducts(category);
Product product = (Product) products.iterator().next();
Collection items = productDao.getItems(product);
assertFalse(items == null || items.isEmpty());
for (Iterator ii = items.iterator(); ii.hasNext();) {
Item item = (Item) ii.next();
System.out.println(item);
}
}
catch (Exception e) {
e.printStackTrace();
fail();
}
}
public void testFindProducts() {
try {
InitialContext ctx = getInitialContext();
ProductDao productDao = (ProductDao) ctx.lookup("ProductDao/local");
Collection products = productDao.findProducts("dog");
assertFalse(products == null || products.isEmpty());
for (Iterator pi = products.iterator(); pi.hasNext();) {
Product product = (Product) pi.next();
System.out.println(product);
}
}
catch (Exception e) {
e.printStackTrace();
fail();
}
}
public static InitialContext getInitialContext() throws Exception {
Hashtable props = getInitialContextProperties();
return new InitialContext(props);
}
private static Hashtable getInitialContextProperties() {
Hashtable<String, String> props = new Hashtable<String, String>();
props.put("java.naming.factory.initial", "org.jnp.interfaces.LocalOnlyContextFactory");
props.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
return props;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -