KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cmp2 > relationship > oneToOneUnidirectional > 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.oneToOneUnidirectional;
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
48          return (AHome)
49                jndiContext.lookup("relation/oneToOne/unidirectional/table/A");
50       } catch(Exception JavaDoc e) {
51          log.debug("failed", e);
52          fail("Exception in getTableAHome: " + e.getMessage());
53       }
54       return null;
55    }
56
57    private BHome getTableBHome() {
58       try {
59          InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
60
61          return (BHome)
62                jndiContext.lookup("relation/oneToOne/unidirectional/table/B");
63       } catch(Exception JavaDoc e) {
64          log.debug("failed", e);
65          fail("Exception in getTableBHome: " + e.getMessage());
66       }
67       return null;
68    }
69
70    private AHome getFKAHome() {
71       try {
72          InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
73
74          return (AHome)
75                jndiContext.lookup("relation/oneToOne/unidirectional/fk/A");
76       } catch(Exception JavaDoc e) {
77          log.debug("failed", e);
78          fail("Exception in getFKAHome: " + e.getMessage());
79       }
80       return null;
81    }
82
83    private BHome getFKBHome() {
84       try {
85          InitialContext JavaDoc jndiContext = new InitialContext JavaDoc();
86
87          return (BHome)
88                jndiContext.lookup("relation/oneToOne/unidirectional/fk/B");
89       } catch(Exception JavaDoc e) {
90          log.debug("failed", e);
91          fail("Exception in getFKBHome: " + e.getMessage());
92       }
93       return null;
94    }
95
96    private A a1;
97    private A a2;
98    private B b1;
99    private B b2;
100
101    protected void beforeChange(AHome aHome, BHome bHome) throws Exception JavaDoc {
102       a1 = aHome.create(new Integer JavaDoc(1));
103       a2 = aHome.create(new Integer JavaDoc(2));
104       b1 = bHome.create(new Integer JavaDoc(10));
105       b2 = bHome.create(new Integer JavaDoc(20));
106       a1.setB(b1);
107       a2.setB(b2);
108
109       assertTrue(b1.isIdentical(a1.getB()));
110       assertTrue(b2.isIdentical(a2.getB()));
111    }
112
113    // a1.setB(a2.getB());
114
public void test_a1SetB_a2GetB_Table() throws Exception JavaDoc {
115       AHome aHome = getTableAHome();
116       BHome bHome = getTableBHome();
117
118       beforeChange(aHome, bHome);
119       a1SetB_a2GetB(aHome, bHome);
120    }
121
122    // a1.setB(a2.getB());
123
public void test_a1SetB_a2GetB_FK() throws Exception JavaDoc {
124       AHome aHome = getFKAHome();
125       BHome bHome = getFKBHome();
126       beforeChange(aHome, bHome);
127       a1SetB_a2GetB(aHome, bHome);
128    }
129
130    // a1.setB(a2.getB());
131
protected void a1SetB_a2GetB(AHome aHome, BHome bHome) throws Exception JavaDoc {
132       // Change:
133
a1.setB(a2.getB());
134
135       // Expected result:
136

137       // b2.isIdentical(a1.getB())
138
assertTrue(b2.isIdentical(a1.getB()));
139
140       // a2.getB() == null
141
assertNull(a2.getB());
142    }
143
144    public void setUpEJB() throws Exception JavaDoc {
145       AHome aHome;
146       BHome bHome;
147
148       aHome = getTableAHome();
149       bHome = getTableBHome();
150       deleteAllAsAndBs(aHome, bHome);
151
152       aHome = getFKAHome();
153       bHome = getFKBHome();
154       deleteAllAsAndBs(aHome, bHome);
155    }
156
157    public void tearDownEJB() throws Exception JavaDoc {
158    }
159
160    public void deleteAllAsAndBs(AHome aHome, BHome bHome) throws Exception JavaDoc {
161       // delete all As
162
Iterator JavaDoc currentAs = aHome.findAll().iterator();
163       while(currentAs.hasNext()) {
164          A a = (A)currentAs.next();
165          a.remove();
166       }
167
168       // delete all Bs
169
Iterator JavaDoc currentBs = bHome.findAll().iterator();
170       while(currentBs.hasNext()) {
171          B b = (B)currentBs.next();
172          b.remove();
173       }
174    }
175 }
176
177
178
179
Popular Tags