001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.test.cmp2.commerce;
023:
024: import java.util.ArrayList;
025: import java.util.Collection;
026: import java.util.Iterator;
027: import java.util.Set;
028: import javax.naming.InitialContext;
029: import junit.framework.Test;
030: import org.jboss.test.util.ejb.EJBTestCase;
031:
032: import org.jboss.test.JBossTestCase;
033:
034: public class CommerceTest extends EJBTestCase {
035: public static Test suite() throws Exception {
036: return JBossTestCase.getDeploySetup(CommerceTest.class,
037: "cmp2-commerce.jar");
038: }
039:
040: public CommerceTest(String name) {
041: super (name);
042: }
043:
044: private OrderHome getOrderHome() {
045: try {
046: InitialContext jndiContext = new InitialContext();
047:
048: return (OrderHome) jndiContext.lookup("commerce/Order");
049: } catch (Exception e) {
050: e.printStackTrace();
051: fail("Exception in getOrderHome: " + e.getMessage());
052: }
053: return null;
054: }
055:
056: private LineItemHome getLineItemHome() {
057: try {
058: InitialContext jndiContext = new InitialContext();
059:
060: return (LineItemHome) jndiContext
061: .lookup("commerce/LineItem");
062: } catch (Exception e) {
063: e.printStackTrace();
064: fail("Exception in getLineItemHome: " + e.getMessage());
065: }
066: return null;
067: }
068:
069: private ProductHome getProductHome() {
070: try {
071: InitialContext jndiContext = new InitialContext();
072:
073: return (ProductHome) jndiContext.lookup("commerce/Product");
074: } catch (Exception e) {
075: e.printStackTrace();
076: fail("Exception in getProductHome: " + e.getMessage());
077: }
078: return null;
079: }
080:
081: private AddressHome getAddressHome() {
082: try {
083: InitialContext jndiContext = new InitialContext();
084:
085: return (AddressHome) jndiContext.lookup("commerce/Address");
086: } catch (Exception e) {
087: e.printStackTrace();
088: fail("Exception in getAddressHome: " + e.getMessage());
089: }
090: return null;
091: }
092:
093: public void test_setInPostCreate() throws Exception {
094:
095: OrderHome oh = getOrderHome();
096: LineItemHome lh = getLineItemHome();
097:
098: LineItem l = lh.create();
099:
100: Order o = oh.create();
101: l = lh.create(o);
102: assertTrue(l.getOrder().isIdentical(o));
103: }
104:
105: public void test_dvo() throws Exception {
106: OrderHome orderHome = getOrderHome();
107:
108: Order order = orderHome.create();
109: Long ordernumber = order.getOrdernumber();
110:
111: // setup credit card
112: Card creditCard = new Card();
113: creditCard.setType(Card.VISA);
114: creditCard.setCardNumber("1111-2222-3333-4444");
115: creditCard.setCardHolder(new FormalName("Dain", 'S',
116: "Sundstrom"));
117: creditCard.setBillingZip(55414);
118:
119: order.setCreditCard(creditCard);
120: assertEquals(order.getCreditCard(), creditCard);
121:
122: order = null;
123: order = orderHome.findByPrimaryKey(ordernumber);
124: assertEquals(order.getCreditCard(), creditCard);
125: }
126:
127: public void test_getOrdersShippedToCA() throws Exception {
128: OrderHome orderHome = getOrderHome();
129: AddressHome addressHome = getAddressHome();
130:
131: Order orderCA1 = orderHome.create();
132: Address shipCA1 = addressHome.create();
133: shipCA1.setState("CA");
134: orderCA1.setShippingAddress(shipCA1);
135:
136: Order orderCA2 = orderHome.create();
137: Address shipCA2 = addressHome.create();
138: shipCA2.setState("CA");
139: orderCA2.setShippingAddress(shipCA2);
140:
141: Order orderMN = orderHome.create();
142: Address shipMN = addressHome.create();
143: shipMN.setState("MN");
144: orderMN.setShippingAddress(shipMN);
145:
146: Set s = orderMN.getOrdersShippedToCA();
147: System.out.println(s);
148: assertTrue(s.contains(orderCA1));
149: assertTrue(s.contains(orderCA2));
150: assertTrue(!s.contains(orderMN));
151: assertTrue(s.size() == 2);
152:
153: s = orderMN.getOrdersShippedToCA2();
154: System.out.println(s);
155: assertTrue(s.contains(orderCA1));
156: assertTrue(s.contains(orderCA2));
157: assertTrue(!s.contains(orderMN));
158: assertTrue(s.size() == 2);
159: }
160:
161: public void test_getStatesShipedTo() throws Exception {
162: OrderHome orderHome = getOrderHome();
163: AddressHome addressHome = getAddressHome();
164:
165: Order orderCA1 = orderHome.create();
166: Address shipCA1 = addressHome.create();
167: shipCA1.setState("CA");
168: orderCA1.setShippingAddress(shipCA1);
169:
170: Order orderCA2 = orderHome.create();
171: Address shipCA2 = addressHome.create();
172: shipCA2.setState("CA");
173: orderCA2.setShippingAddress(shipCA2);
174:
175: Order orderMN = orderHome.create();
176: Address shipMN = addressHome.create();
177: shipMN.setState("MN");
178: orderMN.setShippingAddress(shipMN);
179:
180: System.out.println("orderMN.getStatesShipedTo();");
181: Collection c = orderMN.getStatesShipedTo();
182: System.out.println(c);
183: assertTrue(c.contains("CA"));
184: assertTrue(c.contains("MN"));
185: assertTrue(c.size() == 3);
186:
187: c = orderMN.getStatesShipedTo2();
188: System.out.println(c);
189: assertTrue(c.contains("CA"));
190: assertTrue(c.contains("MN"));
191: assertTrue(c.size() == 3);
192: }
193:
194: public void test_getAddressesInCA() throws Exception {
195: OrderHome orderHome = getOrderHome();
196: AddressHome addressHome = getAddressHome();
197:
198: Order orderCA1 = orderHome.create();
199: Address shipCA1 = addressHome.create();
200: shipCA1.setState("CA");
201: orderCA1.setShippingAddress(shipCA1);
202: Address billCA1 = addressHome.create();
203: billCA1.setState("CA");
204: orderCA1.setBillingAddress(billCA1);
205:
206: Order orderCA2 = orderHome.create();
207: Address shipCA2 = addressHome.create();
208: shipCA2.setState("CA");
209: orderCA2.setShippingAddress(shipCA2);
210: orderCA2.setBillingAddress(shipCA2);
211:
212: Order orderMN = orderHome.create();
213: Address shipMN = addressHome.create();
214: shipMN.setState("MN");
215: orderMN.setShippingAddress(shipMN);
216:
217: Collection c = orderMN.getAddressesInCA();
218: System.out.println(c);
219: assertTrue(c.contains(shipCA1));
220: assertTrue(c.contains(shipCA2));
221: assertTrue(c.contains(billCA1));
222: assertTrue(c.size() == 3);
223:
224: c = orderMN.getAddressesInCA2();
225: System.out.println(c);
226: assertTrue(c.contains(shipCA1));
227: assertTrue(c.contains(shipCA2));
228: assertTrue(c.contains(billCA1));
229: assertTrue(c.size() == 3);
230: }
231:
232: public void test_findDoubleJoin() throws Exception {
233: OrderHome orderHome = getOrderHome();
234: LineItemHome lineItemHome = getLineItemHome();
235:
236: Order order1 = orderHome.create();
237: LineItem line1 = lineItemHome.create();
238: line1.setQuantity(1);
239: order1.getLineItems().add(line1);
240: LineItem line2 = lineItemHome.create();
241: line2.setQuantity(2);
242: order1.getLineItems().add(line2);
243:
244: Order order2 = orderHome.create();
245: LineItem line3 = lineItemHome.create();
246: line3.setQuantity(2);
247: order2.getLineItems().add(line3);
248:
249: Collection c = orderHome.findDoubleJoin(1, 2);
250: assertTrue(c.contains(order1));
251: assertTrue(!c.contains(order2));
252: assertTrue(c.size() == 1);
253: }
254:
255: public void testCMRReset() throws Exception {
256: OrderHome orderHome = getOrderHome();
257: LineItemHome lineItemHome = getLineItemHome();
258:
259: Order order = orderHome.create();
260: Collection lineItems = order.getLineItems();
261:
262: LineItem line1 = lineItemHome.create();
263: line1.setQuantity(1);
264: lineItems.add(line1);
265:
266: LineItem line2 = lineItemHome.create();
267: line2.setQuantity(2);
268: lineItems.add(line2);
269:
270: LineItem line3 = lineItemHome.create();
271: line3.setQuantity(2);
272: lineItems.add(line3);
273:
274: assertEquals(lineItems.size(), 3);
275: assertTrue(lineItems == order.getLineItems());
276: order.setLineItems(lineItems);
277: assertEquals(lineItems.size(), 3);
278: assertTrue(lineItems == order.getLineItems());
279: }
280:
281: public void testCMRSetFromNewCollection() throws Exception {
282: OrderHome orderHome = getOrderHome();
283: LineItemHome lineItemHome = getLineItemHome();
284:
285: Order order = orderHome.create();
286: Collection lineItems = new ArrayList();
287:
288: LineItem line1 = lineItemHome.create();
289: line1.setQuantity(1);
290: lineItems.add(line1);
291:
292: LineItem line2 = lineItemHome.create();
293: line2.setQuantity(2);
294: lineItems.add(line2);
295:
296: LineItem line3 = lineItemHome.create();
297: line3.setQuantity(2);
298: lineItems.add(line3);
299:
300: assertEquals(lineItems.size(), 3);
301: order.setLineItems(lineItems);
302: assertEquals(lineItems.size(), 3);
303: assertEquals(order.getLineItems().size(), 3);
304: }
305:
306: public void testIsIdentical() throws Exception {
307: OrderHome orderHome = getOrderHome();
308: Order order = orderHome.create(new Long(111));
309:
310: LineItemHome liHome = getLineItemHome();
311: LineItem lineItem = liHome.create(new Long(111));
312:
313: assertTrue("!order.isIdentical(lineItem)", !order
314: .isIdentical(lineItem));
315: assertTrue("order.isIdentical(order)", order.isIdentical(order));
316: }
317:
318: public void testOverloadedEjbSelects() throws Exception {
319: getAddressHome().selectAddresses("");
320: }
321:
322: public void setUpEJB() throws Exception {
323: deleteAllOrders(getOrderHome());
324: deleteAllLineItems(getLineItemHome());
325: deleteAllProducts(getProductHome());
326: deleteAllAddresses(getAddressHome());
327: }
328:
329: public void tearDownEJB() throws Exception {
330: deleteAllOrders(getOrderHome());
331: deleteAllLineItems(getLineItemHome());
332: deleteAllProducts(getProductHome());
333: deleteAllAddresses(getAddressHome());
334: }
335:
336: public void deleteAllOrders(OrderHome orderHome) throws Exception {
337: Iterator orders = orderHome.findAll().iterator();
338: while (orders.hasNext()) {
339: Order order = (Order) orders.next();
340: order.remove();
341: }
342: }
343:
344: public void deleteAllLineItems(LineItemHome lineItemHome)
345: throws Exception {
346: Iterator lineItems = lineItemHome.findAll().iterator();
347: while (lineItems.hasNext()) {
348: LineItem lineItem = (LineItem) lineItems.next();
349: lineItem.remove();
350: }
351: }
352:
353: public void deleteAllProducts(ProductHome productHome)
354: throws Exception {
355: Iterator products = productHome.findAll().iterator();
356: while (products.hasNext()) {
357: Product product = (Product) products.next();
358: product.remove();
359: }
360: }
361:
362: public void deleteAllAddresses(AddressHome addressHome)
363: throws Exception {
364: Iterator addresses = addressHome.findAll().iterator();
365: while (addresses.hasNext()) {
366: Address address = (Address) addresses.next();
367: address.remove();
368: }
369: }
370: }
|