KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cmp2 > relationship > manyToOneUnidirectional > ABTest


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.relationship.manyToOneUnidirectional;
23
24 import java.util.Collection JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import javax.naming.InitialContext JavaDoc;
27 import junit.framework.Test;
28 import junit.framework.TestCase;
29 import net.sourceforge.junitejb.EJBTestCase;
30 import org.jboss.test.JBossTestCase;
31
32 public class ABTest extends EJBTestCase {
33     static org.jboss.logging.Logger log =
34        org.jboss.logging.Logger.getLogger(ABTest.class);
35
36     public static Test suite() throws Exception JavaDoc {
37         return JBossTestCase.getDeploySetup(ABTest.class, "cmp2-relationship.jar");
38    }
39
40    public ABTest(String JavaDoc name) {
41       super(name);
42    }
43
44    private AHome getTableAHome() {
45       try {
46          InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
47          return (AHome) jndiContext.lookup("relation/manyToOne/unidirectional/table/A");
48       } catch(Exception JavaDoc e) {
49          log.debug("failed", e);
50          fail("Exception in getTableAHome: " + e.getMessage());
51       }
52       return null;
53    }
54
55    private BHome getTableBHome() {
56       try {
57          InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
58
59          return (BHome) jndiContext.lookup("relation/manyToOne/unidirectional/table/B");
60       } catch(Exception JavaDoc e) {
61          log.debug("failed", e);
62          fail("Exception in getTableBHome: " + e.getMessage());
63       }
64       return null;
65    }
66
67    private AHome getFKAHome() {
68       try {
69          InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
70
71          return (AHome) jndiContext.lookup("relation/manyToOne/unidirectional/fk/A");
72       } catch(Exception JavaDoc e) {
73          log.debug("failed", e);
74          fail("Exception in getFKAHome: " + e.getMessage());
75       }
76       return null;
77    }
78
79    private BHome getFKBHome() {
80       try {
81          InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
82
83          return (BHome) jndiContext.lookup("relation/manyToOne/unidirectional/fk/B");
84       } catch(Exception JavaDoc e) {
85          log.debug("failed", e);
86          fail("Exception in getFKBHome: " + e.getMessage());
87       }
88       return null;
89    }
90
91    // b1j.setA(b2k.getA());
92
public void test_b1jSetA_b2kGetA_Table() throws Exception JavaDoc {
93       AHome aHome = getTableAHome();
94       BHome bHome = getTableBHome();
95       b1jSetA_b2kGetA(aHome, bHome);
96    }
97
98    // b1j.setA(b2k.getA());
99
public void test_b1jSetA_b2kGetA_FK() throws Exception JavaDoc {
100       AHome aHome = getFKAHome();
101       BHome bHome = getFKBHome();
102       b1jSetA_b2kGetA(aHome, bHome);
103    }
104
105    // b1j.setA(b2k.getA());
106
private void b1jSetA_b2kGetA(AHome aHome, BHome bHome) throws Exception JavaDoc {
107       // Before change:
108
A a1 = aHome.create(new Integer JavaDoc(1));
109       A a2 = aHome.create(new Integer JavaDoc(2));
110       
111       B[] b1x = new B[20];
112       B[] b2x = new B[30];
113       
114       for(int i=0; i<b1x.length; i++) {
115          b1x[i] = bHome.create(new Integer JavaDoc(10000 + i));
116          b1x[i].setA(a1);
117       }
118       
119       for(int i=0; i<b2x.length; i++) {
120          b2x[i] = bHome.create(new Integer JavaDoc(20000 + i));
121          b2x[i].setA(a2);
122       }
123       
124       // (a1.isIdentical(b11.getA())) && ... && (a1.isIdentical(b1n.getA()
125
for(int i=0; i<b1x.length; i++) {
126          a1.isIdentical(b1x[i].getA());
127       }
128       
129       // (a2.isIdentical(b21.getA())) && ... && (a2.isIdentical(b2m.getA()
130
for(int i=0; i<b2x.length; i++) {
131          a2.isIdentical(b2x[i].getA());
132       }
133       
134       // Change:
135

136       // b1j.setA(b2k.getA());
137
int j = b1x.length / 3;
138       int k = b2x.length / 2;
139       b1x[j].setA(b2x[k].getA());
140       
141       // Expected result:
142

143       // a1.isIdentical(b11.getA())
144
// a1.isIdentical(b12.getA())
145
// ...
146
// a2.isIdentical(b1j.getA())
147
// ...
148
// a1.isIdentical(b1n.getA())
149
for(int i=0; i<b1x.length; i++) {
150          if(i != j) {
151             assertTrue(a1.isIdentical(b1x[i].getA()));
152          } else {
153             assertTrue(a2.isIdentical(b1x[i].getA()));
154          }
155       }
156
157       // a2.isIdentical(b21.getA())
158
// a2.isIdentical(b22.getA())
159
// ...
160
// a2.isIdentical(b2k.getA())
161
// ...
162
// a2.isIdentical(b2m.getA())
163
for(int i=0; i<b2x.length; i++) {
164          assertTrue(a2.isIdentical(b2x[i].getA()));
165       }
166    }
167
168    public void setUpEJB() throws Exception JavaDoc {
169       AHome aHome;
170       BHome bHome;
171
172       aHome = getTableAHome();
173       bHome = getTableBHome();
174       deleteAllAsAndBs(aHome, bHome);
175
176       aHome = getFKAHome();
177       bHome = getFKBHome();
178       deleteAllAsAndBs(aHome, bHome);
179    }
180    
181    public void tearDownEJB() throws Exception JavaDoc {
182    }
183    
184    public void deleteAllAsAndBs(AHome aHome, BHome bHome) throws Exception JavaDoc {
185       // delete all As
186
Iterator JavaDoc currentAs = aHome.findAll().iterator();
187       while(currentAs.hasNext()) {
188          A a = (A)currentAs.next();
189          a.remove();
190       }
191
192       // delete all Bs
193
Iterator JavaDoc currentBs = bHome.findAll().iterator();
194       while(currentBs.hasNext()) {
195          B b = (B)currentBs.next();
196          b.remove();
197       }
198    }
199 }
200
201
202
203
Popular Tags