KickJava   Java API By Example, From Geeks To Geeks.

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


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 ManyToOneUniTest extends EJBTestCase {
31
32    public ManyToOneUniTest(String JavaDoc name) {
33       super(name);
34    }
35
36    private ProductHome getProductHome() {
37       try {
38          InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
39
40          return (ProductHome) jndiContext.lookup("commerce/Product");
41       } catch(Exception JavaDoc e) {
42          e.printStackTrace();
43          fail("Exception in getProduct: " + e.getMessage());
44       }
45       return null;
46    }
47
48    private LineItemHome getLineItemHome() {
49       try {
50          InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
51
52          return (LineItemHome) jndiContext.lookup("commerce/LineItem");
53       } catch(Exception JavaDoc e) {
54          e.printStackTrace();
55          fail("Exception in getLineItemHome: " + e.getMessage());
56       }
57       return null;
58    }
59
60    private Product a1;
61    private Product a2;
62
63    private LineItem[] b1x = new LineItem[20];
64    private LineItem[] b2x = new LineItem[30];
65
66    public void setUpEJB() throws Exception JavaDoc {
67       ProductHome productHome = getProductHome();
68       LineItemHome lineItemHome = getLineItemHome();
69
70       // clean out the db
71
deleteAllProducts(productHome);
72       deleteAllLineItems(lineItemHome);
73
74       // setup the before change part of the test
75
beforeChange(productHome, lineItemHome);
76    }
77
78    private void beforeChange(ProductHome productHome, LineItemHome lineItemHome)
79          throws Exception JavaDoc {
80
81       // Before change:
82
a1 = productHome.create();
83       a2 = productHome.create();
84
85       for(int i=0; i<b1x.length; i++) {
86          b1x[i] = lineItemHome.create();
87          b1x[i].setProduct(a1);
88       }
89
90       for(int i=0; i<b2x.length; i++) {
91          b2x[i] = lineItemHome.create();
92          b2x[i].setProduct(a2);
93       }
94
95       // (a1.isIdentical(b11.getA())) && ... && (a1.isIdentical(b1n.getA()
96
for(int i=0; i<b1x.length; i++) {
97          a1.isIdentical(b1x[i].getProduct());
98       }
99
100       // (a2.isIdentical(b21.getA())) && ... && (a2.isIdentical(b2m.getA()
101
for(int i=0; i<b2x.length; i++) {
102          a2.isIdentical(b2x[i].getProduct());
103       }
104    }
105
106    // b1j.setA(b2k.getA());
107
public void test_b1jSetA_b2kGetA() {
108       // Change:
109

110       // b1j.setA(b2k.getA());
111
int j = b1x.length / 3;
112       int k = b2x.length / 2;
113       b1x[j].setProduct(b2x[k].getProduct());
114
115       // Expected result:
116

117       // a1.isIdentical(b11.getA())
118
// a1.isIdentical(b12.getA())
119
// ...
120
// a2.isIdentical(b1j.getA())
121
// ...
122
// a1.isIdentical(b1n.getA())
123
for(int i=0; i<b1x.length; i++) {
124          if(i != j) {
125             assertTrue(a1.isIdentical(b1x[i].getProduct()));
126          } else {
127             assertTrue(a2.isIdentical(b1x[i].getProduct()));
128          }
129       }
130
131       // a2.isIdentical(b21.getA())
132
// a2.isIdentical(b22.getA())
133
// ...
134
// a2.isIdentical(b2k.getA())
135
// ...
136
// a2.isIdentical(b2m.getA())
137
for(int i=0; i<b2x.length; i++) {
138          assertTrue(a2.isIdentical(b2x[i].getProduct()));
139       }
140    }
141
142    public void tearDownEJB() throws Exception JavaDoc {
143       ProductHome productHome = getProductHome();
144       LineItemHome lineItemHome = getLineItemHome();
145       // clean out the db
146
deleteAllProducts(productHome);
147       deleteAllLineItems(lineItemHome);
148    }
149
150    public void deleteAllProducts(ProductHome productHome) throws Exception JavaDoc {
151       // delete all Products
152
Iterator JavaDoc currentProducts = productHome.findAll().iterator();
153       while(currentProducts.hasNext()) {
154          Product p = (Product)currentProducts.next();
155          p.remove();
156       }
157    }
158
159    public void deleteAllLineItems(LineItemHome lineItemHome) throws Exception JavaDoc {
160       // delete all LineItems
161
Iterator JavaDoc currentLineItems = lineItemHome.findAll().iterator();
162       while(currentLineItems.hasNext()) {
163          LineItem l = (LineItem)currentLineItems.next();
164          l.remove();
165       }
166    }
167 }
168
169
170
171
Popular Tags