KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cmp2 > commerce > CommerceTest


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.cmp2.commerce;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Collection JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Set JavaDoc;
28 import javax.naming.InitialContext JavaDoc;
29 import junit.framework.Test;
30 import net.sourceforge.junitejb.EJBTestCase;
31
32 import org.jboss.test.JBossTestCase;
33
34 public class CommerceTest extends EJBTestCase {
35    public static Test suite() throws Exception JavaDoc {
36         return JBossTestCase.getDeploySetup(
37             CommerceTest.class,
38             "cmp2-commerce.jar");
39    }
40
41    public CommerceTest(String JavaDoc name) {
42       super(name);
43    }
44
45    private OrderHome getOrderHome() {
46       try {
47          InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
48
49          return (OrderHome) jndiContext.lookup("commerce/Order");
50       } catch(Exception JavaDoc e) {
51          e.printStackTrace();
52          fail("Exception in getOrderHome: " + e.getMessage());
53       }
54       return null;
55    }
56
57    private LineItemHome getLineItemHome() {
58       try {
59          InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
60
61          return (LineItemHome) jndiContext.lookup("commerce/LineItem");
62       } catch(Exception JavaDoc e) {
63          e.printStackTrace();
64          fail("Exception in getLineItemHome: " + e.getMessage());
65       }
66       return null;
67    }
68
69    private ProductHome getProductHome() {
70       try {
71          InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
72
73          return (ProductHome) jndiContext.lookup("commerce/Product");
74       } catch(Exception JavaDoc e) {
75          e.printStackTrace();
76          fail("Exception in getProductHome: " + e.getMessage());
77       }
78       return null;
79    }
80
81    private AddressHome getAddressHome() {
82       try {
83          InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
84
85          return (AddressHome) jndiContext.lookup("commerce/Address");
86       } catch(Exception JavaDoc e) {
87          e.printStackTrace();
88          fail("Exception in getAddressHome: " + e.getMessage());
89       }
90       return null;
91    }
92
93    public void test_setInPostCreate() throws Exception JavaDoc {
94
95       OrderHome oh = getOrderHome();
96       LineItemHome lh = getLineItemHome();
97
98       LineItem l =lh.create();
99       
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 JavaDoc {
106       OrderHome orderHome = getOrderHome();
107
108       Order order = orderHome.create();
109       Long JavaDoc 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', "Sundstrom"));
116       creditCard.setBillingZip(55414);
117
118       order.setCreditCard(creditCard);
119       assertEquals(order.getCreditCard(), creditCard);
120
121       order = null;
122       order = orderHome.findByPrimaryKey(ordernumber);
123       assertEquals(order.getCreditCard(), creditCard);
124    }
125
126    public void test_getOrdersShippedToCA() throws Exception JavaDoc {
127       OrderHome orderHome = getOrderHome();
128       AddressHome addressHome = getAddressHome();
129       
130       Order orderCA1 = orderHome.create();
131       Address shipCA1 = addressHome.create();
132       shipCA1.setState("CA");
133       orderCA1.setShippingAddress(shipCA1);
134       
135       Order orderCA2 = orderHome.create();
136       Address shipCA2 = addressHome.create();
137       shipCA2.setState("CA");
138       orderCA2.setShippingAddress(shipCA2);
139
140       Order orderMN = orderHome.create();
141       Address shipMN = addressHome.create();
142       shipMN.setState("MN");
143       orderMN.setShippingAddress(shipMN);
144
145       Set JavaDoc s = orderMN.getOrdersShippedToCA();
146       System.out.println(s);
147       assertTrue(s.contains(orderCA1));
148       assertTrue(s.contains(orderCA2));
149       assertTrue(!s.contains(orderMN));
150       assertTrue(s.size()==2);
151
152       s = orderMN.getOrdersShippedToCA2();
153       System.out.println(s);
154       assertTrue(s.contains(orderCA1));
155       assertTrue(s.contains(orderCA2));
156       assertTrue(!s.contains(orderMN));
157       assertTrue(s.size()==2);
158    }
159
160    public void test_getStatesShipedTo() throws Exception JavaDoc {
161       OrderHome orderHome = getOrderHome();
162       AddressHome addressHome = getAddressHome();
163       
164       Order orderCA1 = orderHome.create();
165       Address shipCA1 = addressHome.create();
166       shipCA1.setState("CA");
167       orderCA1.setShippingAddress(shipCA1);
168       
169       Order orderCA2 = orderHome.create();
170       Address shipCA2 = addressHome.create();
171       shipCA2.setState("CA");
172       orderCA2.setShippingAddress(shipCA2);
173
174       Order orderMN = orderHome.create();
175       Address shipMN = addressHome.create();
176       shipMN.setState("MN");
177       orderMN.setShippingAddress(shipMN);
178
179       System.out.println("orderMN.getStatesShipedTo();");
180       Collection JavaDoc c = orderMN.getStatesShipedTo();
181       System.out.println(c);
182       assertTrue(c.contains("CA"));
183       assertTrue(c.contains("MN"));
184       assertTrue(c.size()==3);
185
186       c = orderMN.getStatesShipedTo2();
187       System.out.println(c);
188       assertTrue(c.contains("CA"));
189       assertTrue(c.contains("MN"));
190       assertTrue(c.size()==3);
191    }
192
193    public void test_getAddressesInCA() throws Exception JavaDoc {
194       OrderHome orderHome = getOrderHome();
195       AddressHome addressHome = getAddressHome();
196       
197       Order orderCA1 = orderHome.create();
198       Address shipCA1 = addressHome.create();
199       shipCA1.setState("CA");
200       orderCA1.setShippingAddress(shipCA1);
201       Address billCA1 = addressHome.create();
202       billCA1.setState("CA");
203       orderCA1.setBillingAddress(billCA1);
204
205       Order orderCA2 = orderHome.create();
206       Address shipCA2 = addressHome.create();
207       shipCA2.setState("CA");
208       orderCA2.setShippingAddress(shipCA2);
209       orderCA2.setBillingAddress(shipCA2);
210
211       Order orderMN = orderHome.create();
212       Address shipMN = addressHome.create();
213       shipMN.setState("MN");
214       orderMN.setShippingAddress(shipMN);
215
216       Collection JavaDoc c = orderMN.getAddressesInCA();
217       System.out.println(c);
218       assertTrue(c.contains(shipCA1));
219       assertTrue(c.contains(shipCA2));
220       assertTrue(c.contains(billCA1));
221       assertTrue(c.size()==3);
222
223       c = orderMN.getAddressesInCA2();
224       System.out.println(c);
225       assertTrue(c.contains(shipCA1));
226       assertTrue(c.contains(shipCA2));
227       assertTrue(c.contains(billCA1));
228       assertTrue(c.size()==3);
229    }
230
231
232    public void test_findDoubleJoin() throws Exception JavaDoc {
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       
250       Collection JavaDoc c = orderHome.findDoubleJoin(1, 2);
251       assertTrue(c.contains(order1));
252       assertTrue(!c.contains(order2));
253       assertTrue(c.size()==1);
254    }
255
256    public void testCMRReset() throws Exception JavaDoc {
257       OrderHome orderHome = getOrderHome();
258       LineItemHome lineItemHome = getLineItemHome();
259       
260       Order order = orderHome.create();
261       Collection JavaDoc lineItems = order.getLineItems();
262
263       LineItem line1 = lineItemHome.create();
264       line1.setQuantity(1);
265       lineItems.add(line1);
266
267       LineItem line2 = lineItemHome.create();
268       line2.setQuantity(2);
269       lineItems.add(line2);
270
271       LineItem line3 = lineItemHome.create();
272       line3.setQuantity(2);
273       lineItems.add(line3);
274
275       
276       assertEquals(lineItems.size(), 3);
277       assertTrue(lineItems == order.getLineItems());
278       order.setLineItems(lineItems);
279       assertEquals(lineItems.size(), 3);
280       assertTrue(lineItems == order.getLineItems());
281    }
282
283    public void testCMRSetFromNewCollection() throws Exception JavaDoc {
284       OrderHome orderHome = getOrderHome();
285       LineItemHome lineItemHome = getLineItemHome();
286       
287       Order order = orderHome.create();
288       Collection JavaDoc lineItems = new ArrayList JavaDoc();
289
290       LineItem line1 = lineItemHome.create();
291       line1.setQuantity(1);
292       lineItems.add(line1);
293
294       LineItem line2 = lineItemHome.create();
295       line2.setQuantity(2);
296       lineItems.add(line2);
297
298       LineItem line3 = lineItemHome.create();
299       line3.setQuantity(2);
300       lineItems.add(line3);
301
302       
303       assertEquals(lineItems.size(), 3);
304       order.setLineItems(lineItems);
305       assertEquals(lineItems.size(), 3);
306       assertEquals(order.getLineItems().size(), 3);
307    }
308
309    public void testIsIdentical() throws Exception JavaDoc
310    {
311       OrderHome orderHome = getOrderHome();
312       Order order = orderHome.create(new Long JavaDoc(111));
313
314       LineItemHome liHome = getLineItemHome();
315       LineItem lineItem = liHome.create(new Long JavaDoc(111));
316
317       assertTrue("!order.isIdentical(lineItem)", !order.isIdentical(lineItem));
318       assertTrue("order.isIdentical(order)", order.isIdentical(order));
319    }
320
321    public void testOverloadedEjbSelects() throws Exception JavaDoc
322    {
323       getAddressHome().selectAddresses("");
324    }
325
326    public void setUpEJB() throws Exception JavaDoc {
327       deleteAllOrders(getOrderHome());
328       deleteAllLineItems(getLineItemHome());
329       deleteAllProducts(getProductHome());
330       deleteAllAddresses(getAddressHome());
331    }
332    
333    public void tearDownEJB() throws Exception JavaDoc {
334       deleteAllOrders(getOrderHome());
335       deleteAllLineItems(getLineItemHome());
336       deleteAllProducts(getProductHome());
337       deleteAllAddresses(getAddressHome());
338    }
339    
340    public void deleteAllOrders(OrderHome orderHome) throws Exception JavaDoc {
341       Iterator JavaDoc orders = orderHome.findAll().iterator();
342       while(orders.hasNext()) {
343          Order order = (Order)orders.next();
344          order.remove();
345       }
346    }
347
348    public void deleteAllLineItems(LineItemHome lineItemHome) throws Exception JavaDoc {
349       Iterator JavaDoc lineItems = lineItemHome.findAll().iterator();
350       while(lineItems.hasNext()) {
351          LineItem lineItem = (LineItem)lineItems.next();
352          lineItem.remove();
353       }
354    }
355
356    public void deleteAllProducts(ProductHome productHome) throws Exception JavaDoc {
357       Iterator JavaDoc products = productHome.findAll().iterator();
358       while(products.hasNext()) {
359          Product product = (Product)products.next();
360          product.remove();
361       }
362    }
363
364    public void deleteAllAddresses(AddressHome addressHome) throws Exception JavaDoc {
365       Iterator JavaDoc addresses = addressHome.findAll().iterator();
366       while(addresses.hasNext()) {
367          Address address = (Address)addresses.next();
368          address.remove();
369       }
370    }
371 }
372
Popular Tags