KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > cmp2 > fkmapping > test > FKMappingUnitTestCase


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.fkmapping.test;
23
24 import javax.ejb.EJBException JavaDoc;
25 import javax.ejb.EJBLocalObject JavaDoc;
26 import javax.ejb.RemoveException JavaDoc;
27 import javax.ejb.NoSuchObjectLocalException JavaDoc;
28 import javax.naming.NamingException JavaDoc;
29 import javax.naming.InitialContext JavaDoc;
30 import javax.transaction.UserTransaction JavaDoc;
31
32 import junit.framework.Test;
33 import net.sourceforge.junitejb.EJBTestCase;
34 import org.jboss.test.JBossTestCase;
35 import org.jboss.test.cmp2.fkmapping.ejb.DepartmentLocal;
36 import org.jboss.test.cmp2.fkmapping.ejb.DepartmentLocalHome;
37 import org.jboss.test.cmp2.fkmapping.ejb.DepartmentUtil;
38 import org.jboss.test.cmp2.fkmapping.ejb.ExamenationLocal;
39 import org.jboss.test.cmp2.fkmapping.ejb.ExamenationLocalHome;
40 import org.jboss.test.cmp2.fkmapping.ejb.ExamenationUtil;
41 import org.jboss.test.cmp2.fkmapping.ejb.GroupLocal;
42 import org.jboss.test.cmp2.fkmapping.ejb.GroupLocalHome;
43 import org.jboss.test.cmp2.fkmapping.ejb.GroupUtil;
44 import org.jboss.test.cmp2.fkmapping.ejb.InstituteLocal;
45 import org.jboss.test.cmp2.fkmapping.ejb.InstituteLocalHome;
46 import org.jboss.test.cmp2.fkmapping.ejb.InstituteUtil;
47 import org.jboss.test.cmp2.fkmapping.ejb.StudentLocal;
48 import org.jboss.test.cmp2.fkmapping.ejb.StudentLocalHome;
49 import org.jboss.test.cmp2.fkmapping.ejb.StudentUtil;
50 import org.jboss.test.cmp2.fkmapping.ejb.ManagerUtil;
51 import org.jboss.test.cmp2.fkmapping.ejb.Manager;
52 import org.jboss.test.cmp2.fkmapping.ejb.ChildUPKUtil;
53
54 /**
55  *
56  * @author <a HREF="mailto:alex@jboss.org">Alex Loubyansky</a>
57  */

58 public class FKMappingUnitTestCase extends EJBTestCase
59 {
60    private InstituteLocalHome instituteHome;
61    private DepartmentLocalHome departmentHome;
62    private GroupLocalHome groupHome;
63    private StudentLocalHome studentHome;
64    private ExamenationLocalHome examHome;
65
66    // Suite ---------------------------------------------------
67
public static Test suite() throws Exception JavaDoc
68    {
69       return JBossTestCase.getDeploySetup(FKMappingUnitTestCase.class, "cmp2-fkmapping.jar");
70    }
71
72    // Constructor ---------------------------------------------
73
public FKMappingUnitTestCase(String JavaDoc name)
74    {
75       super(name);
76    }
77
78    // Tests ---------------------------------------------------
79
public void testStandaloneFKMapping() throws Exception JavaDoc
80    {
81       InstituteLocal institute = null;
82       DepartmentLocal department = null;
83       try {
84          institute = getInstituteHome().create("NTUUKPI", "Natinal Technical University Of The Ukraine KPI");
85          department = getDepartmentHome().create("KV", "Specialized Computer Systems");
86          institute.getDepartments().add(department);
87          assertTrue("department.getInstitute().isIdentical(institute)", department.getInstitute().isIdentical(institute));
88          department.setInstitute(null);
89          assertTrue("institute.getDepartments().isEmpty()", institute.getDepartments().isEmpty());
90       } finally {
91          removeEntity(institute);
92          removeEntity(department);
93       }
94    }
95
96    public void testJoing() throws Exception JavaDoc {
97       DepartmentLocal department = null;
98       GroupLocal kv62Group = null;
99       GroupLocal kv63Group = null;
100       try {
101          System.out.println("testJoin()");
102          department = getDepartmentHome().create("KV", "Specialized Computer Systems");
103          kv62Group = getGroupHome().create("KV", 62, "KV-62");
104          kv63Group = getGroupHome().create("KV", 63, "KV-63");
105          commit();
106          System.out.println("testJoin() 2");
107          getGroupHome().findAll();
108          kv62Group.getDepartment();
109          System.out.println("testJoin() 3");
110       } finally {
111          removeEntity(kv63Group);
112          removeEntity(kv62Group);
113          removeEntity(department);
114       }
115    }
116
117    public void testCompleteFKToPKMapping()
118          throws Exception JavaDoc
119    {
120       DepartmentLocal department = null;
121       GroupLocal kv62Group = null;
122       try {
123          // one-side instance created before many-side instance
124
department = getDepartmentHome().create("KV", "Specialized Computer Systems");
125          assertTrue("department.getGroups().isEmpty()", department.getGroups().isEmpty());
126
127          kv62Group = getGroupHome().create("KV", 62, "KV-62");
128          assertTrue("department.getGroups().contains(kv62Group)", department.getGroups().contains(kv62Group));
129          assertTrue("kv62Group.getDepartment().isIdentical(department)", kv62Group.getDepartment().isIdentical(department));
130
131          kv62Group.remove();
132          assertTrue("department.getGroups().isEmpty()", department.getGroups().isEmpty());
133
134          // many-side instance created before one-side instance
135
department.remove();
136          kv62Group = getGroupHome().create("KV", 62, "KV-62");
137          assertTrue("kv62Group.getDepartment() == null", kv62Group.getDepartment() == null);
138
139          department = getDepartmentHome().create("KV", "Specialized Computer Systems");
140          assertTrue("kv62Group.getDepartment().isIdentical(department)", kv62Group.getDepartment().isIdentical(department));
141          assertTrue("department.getGroups().contains(kv62Group)", department.getGroups().contains(kv62Group));
142
143          department.remove();
144          department = null;
145          assertTrue("kv62Group.getDepartment() == null", kv62Group.getDepartment() == null);
146       } finally {
147          removeEntity(department);
148          removeEntity(kv62Group);
149       }
150    }
151
152    public void testPartialFKToPKMapping()
153          throws Exception JavaDoc
154    {
155       StudentLocal petrovStudent = null;
156       StudentLocal sidorovStudent = null;
157       GroupLocal group = null;
158       try {
159          petrovStudent = getStudentHome().create("KV", "Petrov", "Petrov works on KV department.");
160          group = getGroupHome().create("KV", 62, "KV-62");
161          assertTrue("petrovStudent.getGroup() == null", petrovStudent.getGroup() == null);
162
163          petrovStudent.setGroup(group);
164          assertTrue("group.isIdentical(petrovStudent.getGroup())", group.isIdentical(petrovStudent.getGroup()));
165          assertTrue("group.getStudents().contains(petrovStudent)", group.getStudents().contains(petrovStudent));
166
167          sidorovStudent = getStudentHome().create("KV", "Sidorov", "Sidorov works on KV department.");
168          group.getStudents().add(sidorovStudent);
169          assertTrue("sidorovStudent.getGroup().isIdentical(group)", sidorovStudent.getGroup().isIdentical(group));
170          assertTrue("group.getStudents().contains(petrovStudent)", group.getStudents().contains(petrovStudent));
171          assertTrue("group.getStudents().contains(sidorovStudent)", group.getStudents().contains(sidorovStudent));
172
173          group.remove();
174          group = null;
175          assertTrue("petrovStudent.getGroup() == null", petrovStudent.getGroup() == null);
176          assertTrue("sidorovStudent.getGroup() == null", sidorovStudent.getGroup() == null);
177
178          /*
179          group = getGroupHome().create("KV", 62, "KV-62");
180          assertTrue("group.getStudents().contains(petrovStudent)", group.getStudents().contains(petrovStudent));
181          assertTrue("group.isIdentical(petrovStudent.getGroup())", group.isIdentical(petrovStudent.getGroup()));
182          */

183       } finally {
184          removeEntity(petrovStudent);
185          removeEntity(sidorovStudent);
186          removeEntity(group);
187       }
188    }
189
190    public void testFKToCMPMapping()
191          throws Exception JavaDoc
192    {
193       GroupLocal kv61Group = null;
194       GroupLocal kv62Group = null;
195       ExamenationLocal exam = null;
196       try {
197          kv62Group = getGroupHome().create("KV", 62, "KV-62");
198          exam = getExamHome().create("kv61-1", "Math", "KV", 62);
199          assertTrue("kv62Group.isIdentical(exam.getGroup())", kv62Group.isIdentical(exam.getGroup()));
200          assertTrue("kv62Group.getExamenations().contains(exam)", kv62Group.getExamenations().contains(exam));
201
202          kv61Group = getGroupHome().create("KV", 61, "KV-61");
203          exam.setGroup(kv61Group);
204          assertTrue("expected: exam.getGroupNumber() == 61;"
205                     + " got: exam.getGroupNumber() == " + exam.getGroupNumber(),
206                     exam.getGroupNumber() == 61);
207
208          exam.setGroupNumber(62);
209          assertTrue("kv62Group.isIdentical(exam.getGroup())", kv62Group.isIdentical(exam.getGroup()));
210          assertTrue("kv62Group.getExamenations().contains(exam);", kv62Group.getExamenations().contains(exam));
211          assertTrue("kv61Group.getExamenations().isEmpty();", kv61Group.getExamenations().isEmpty());
212
213          exam.setDepartmentCode("KM");
214          exam.setDepartmentCode2("XKM");
215          assertTrue("exam.getGroup() == null", exam.getGroup() == null);
216          assertTrue("kv62Group.getExamenations().isEmpty();", kv62Group.getExamenations().isEmpty());
217
218          exam.setDepartmentCode("KV");
219          exam.setDepartmentCode2("XKV");
220          assertTrue("kv62Group.isIdentical(exam.getGroup())", kv62Group.isIdentical(exam.getGroup()));
221          assertTrue("kv62Group.getExamenations().contains(exam);", kv62Group.getExamenations().contains(exam));
222       } finally {
223          removeEntity(exam);
224          removeEntity(kv61Group);
225          removeEntity(kv62Group);
226       }
227    }
228
229    public void testInsertAfterEjbPostCreate()
230       throws Exception JavaDoc
231    {
232       Long JavaDoc long1 = new Long JavaDoc(1);
233       String JavaDoc avoka = "Avoka";
234       String JavaDoc irene = "Irene";
235       Manager manager = ManagerUtil.getHome().create();
236       manager.createParent(long1, irene);
237
238       try
239       {
240          manager.createChild(long1, avoka);
241          fail("Should have filed as the foreign key field can't be null.");
242       }
243       catch(Exception JavaDoc expected){}
244
245       manager.createChild(long1, avoka, long1, irene);
246       manager.createChild(new Long JavaDoc(2), "Ataka", long1, irene);
247
248       manager.assertChildHasMother(long1, long1, irene);
249       manager.assertChildHasMother(new Long JavaDoc(2), long1, irene);
250    }
251
252    public void testGeneratedPKWithInsertAfterPostCreate()
253       throws Exception JavaDoc
254    {
255       // this will fail unless pk value is generated and set in ejbCreate
256
ChildUPKUtil.getLocalHome().create(null);
257    }
258
259    /**
260     * Tests complete foreign key load.
261     * The bug was that, when null value was loaded for a foreign key field,
262     * consequent reading of foreign key fields for current CMR was stopped and incorrect
263     * result index was returned. Further loaded fields contained messed up values.
264     * NOTE: to reproduce the bug, foreign key must be a composite key. When loading, foreign key
265     * must be null and after loading results for foreign key there should be results for other fields
266     * loaded from the same SQL SELECT.
267     */

268    public void testCompleteFKReadOnLoad() throws Exception JavaDoc
269    {
270       Manager manager = ManagerUtil.getHome().create();
271
272       // one of the following will fail (with NPE) if there is the bug
273
Object JavaDoc childPK = manager.createChildUPKWithMother();
274       manager.loadChildUPKWithMother(childPK);
275
276       childPK = manager.createChildUPKWithFather();
277       manager.loadChildUPKWithFather(childPK);
278    }
279
280    // Private
281

282    private StudentLocalHome getStudentHome()
283          throws NamingException JavaDoc
284    {
285       if (studentHome == null)
286          studentHome = StudentUtil.getLocalHome();
287       return studentHome;
288    }
289
290    private ExamenationLocalHome getExamHome()
291          throws NamingException JavaDoc
292    {
293       if (examHome == null)
294          examHome = ExamenationUtil.getLocalHome();
295       return examHome;
296    }
297
298    private InstituteLocalHome getInstituteHome()
299          throws NamingException JavaDoc
300    {
301       if (instituteHome == null)
302          instituteHome = InstituteUtil.getLocalHome();
303       return instituteHome;
304    }
305
306    private DepartmentLocalHome getDepartmentHome()
307          throws NamingException JavaDoc
308    {
309       if (departmentHome == null)
310          departmentHome = DepartmentUtil.getLocalHome();
311       return departmentHome;
312    }
313
314    private GroupLocalHome getGroupHome()
315          throws NamingException JavaDoc
316    {
317       if (groupHome == null)
318          groupHome = GroupUtil.getLocalHome();
319       return groupHome;
320    }
321
322    private void removeEntity(EJBLocalObject JavaDoc localEntity)
323    {
324       if (localEntity != null) {
325          try {
326             localEntity.remove();
327          } catch (RemoveException JavaDoc re) {
328             throw new EJBException JavaDoc("Couldn't remove local entity " + localEntity.getPrimaryKey());
329          }
330          catch(NoSuchObjectLocalException JavaDoc e)
331          {
332             // ok
333
}
334       }
335    }
336
337    private void commit() throws Exception JavaDoc {
338       UserTransaction JavaDoc tx = (UserTransaction JavaDoc) new InitialContext JavaDoc().lookup("java:comp/UserTransaction");
339       tx.commit();
340       tx.begin();
341    }
342 }
343
Popular Tags