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.Collection;
025: import java.util.Iterator;
026: import javax.naming.InitialContext;
027: import junit.framework.TestCase;
028: import org.jboss.test.util.ejb.EJBTestCase;
029:
030: public class OneToOneUniTest extends EJBTestCase {
031:
032: public OneToOneUniTest(String name) {
033: super (name);
034: }
035:
036: private OrderHome getOrderHome() {
037: try {
038: InitialContext jndiContext = new InitialContext();
039:
040: return (OrderHome) jndiContext.lookup("commerce/Order");
041: } catch (Exception e) {
042: e.printStackTrace();
043: fail("Exception in getOrder: " + e.getMessage());
044: }
045: return null;
046: }
047:
048: private AddressHome getAddressHome() {
049: try {
050: InitialContext jndiContext = new InitialContext();
051:
052: return (AddressHome) jndiContext.lookup("commerce/Address");
053: } catch (Exception e) {
054: e.printStackTrace();
055: fail("Exception in getAddressHome: " + e.getMessage());
056: }
057: return null;
058: }
059:
060: private Order a1;
061: private Order a2;
062:
063: private Address b1;
064: private Address b2;
065:
066: public void setUpEJB() throws Exception {
067: OrderHome orderHome = getOrderHome();
068: AddressHome addressHome = getAddressHome();
069:
070: // clean out the db
071: deleteAllOrders(orderHome);
072: deleteAllAddresses(addressHome);
073:
074: // setup the before change part of the test
075: beforeChange(orderHome, addressHome);
076: }
077:
078: private void beforeChange(OrderHome orderHome,
079: AddressHome addressHome) throws Exception {
080:
081: // Before change:
082: a1 = orderHome.create();
083: a2 = orderHome.create();
084:
085: b1 = addressHome.create();
086: a1.setShippingAddress(b1);
087:
088: b2 = addressHome.create();
089: a2.setShippingAddress(b2);
090:
091: assertTrue(a1.getShippingAddress().isIdentical(b1));
092: assertTrue(a2.getShippingAddress().isIdentical(b2));
093: }
094:
095: // a1.setB(a2.getB());
096: public void test_a1setB_a2getB() {
097: // Change:
098: // a1.setB(a2.getB());
099: 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 {
108: }
109:
110: public void deleteAllOrders(OrderHome orderHome) throws Exception {
111: // delete all Orders
112: Iterator 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)
120: throws Exception {
121: // delete all Addresses
122: Iterator currentAddresses = addressHome.findAll().iterator();
123: while (currentAddresses.hasNext()) {
124: Address a = (Address) currentAddresses.next();
125: a.remove();
126: }
127: }
128: }
|