KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collection JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import javax.naming.InitialContext JavaDoc;
27 import junit.framework.TestCase;
28 import net.sourceforge.junitejb.EJBTestCase;
29
30 public class OneToOneUniTest extends EJBTestCase {
31
32    public OneToOneUniTest(String JavaDoc name) {
33       super(name);
34    }
35
36    private OrderHome getOrderHome() {
37       try {
38          InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
39
40          return (OrderHome) jndiContext.lookup("commerce/Order");
41       } catch(Exception JavaDoc e) {
42          e.printStackTrace();
43          fail("Exception in getOrder: " + e.getMessage());
44       }
45       return null;
46    }
47
48    private AddressHome getAddressHome() {
49       try {
50          InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
51
52          return (AddressHome) jndiContext.lookup("commerce/Address");
53       } catch(Exception JavaDoc e) {
54          e.printStackTrace();
55          fail("Exception in getAddressHome: " + e.getMessage());
56       }
57       return null;
58    }
59
60    private Order a1;
61    private Order a2;
62
63    private Address b1;
64    private Address b2;
65
66    public void setUpEJB() throws Exception JavaDoc {
67       OrderHome orderHome = getOrderHome();
68       AddressHome addressHome = getAddressHome();
69
70       // clean out the db
71
deleteAllOrders(orderHome);
72       deleteAllAddresses(addressHome);
73
74       // setup the before change part of the test
75
beforeChange(orderHome, addressHome);
76    }
77
78    private void beforeChange(OrderHome orderHome, AddressHome addressHome)
79          throws Exception JavaDoc {
80
81       // Before change:
82
a1 = orderHome.create();
83       a2 = orderHome.create();
84
85       b1 = addressHome.create();
86       a1.setShippingAddress(b1);
87
88       b2 = addressHome.create();
89       a2.setShippingAddress(b2);
90
91       assertTrue(a1.getShippingAddress().isIdentical(b1));
92       assertTrue(a2.getShippingAddress().isIdentical(b2));
93    }
94
95    // a1.setB(a2.getB());
96
public void test_a1setB_a2getB() {
97       // Change:
98
// a1.setB(a2.getB());
99
a1.setShippingAddress(a2.getShippingAddress());
100
101       // Expected result:
102
// (b2.isIdentical(a1.getB())) && (a2.getB() == null)
103
assertTrue(b2.isIdentical(a1.getShippingAddress()));
104       assertTrue(a2.getShippingAddress() == null);
105    }
106
107    public void tearDownEJB() throws Exception JavaDoc {
108    }
109
110    public void deleteAllOrders(OrderHome orderHome) throws Exception JavaDoc {
111       // delete all Orders
112
Iterator JavaDoc currentOrders = orderHome.findAll().iterator();
113       while(currentOrders.hasNext()) {
114          Order o = (Order)currentOrders.next();
115          o.remove();
116       }
117    }
118
119    public void deleteAllAddresses(AddressHome addressHome) throws Exception JavaDoc {
120       // delete all Addresses
121
Iterator JavaDoc currentAddresses = addressHome.findAll().iterator();
122       while(currentAddresses.hasNext()) {
123          Address a = (Address)currentAddresses.next();
124          a.remove();
125       }
126    }
127 }
128
129
130
131
Popular Tags