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.fkmapping.ejb;
023:
024: import org.apache.log4j.Category;
025:
026: import javax.ejb.SessionBean;
027: import javax.ejb.EJBException;
028: import javax.ejb.SessionContext;
029: import javax.ejb.CreateException;
030: import javax.ejb.RemoveException;
031: import javax.ejb.EJBLocalObject;
032: import javax.naming.NamingException;
033: import java.rmi.RemoteException;
034:
035: /**
036: * @ejb.bean
037: * type="Stateless"
038: * name="Manager"
039: * view-type="remote"
040: * @ejb.util generate="physical"
041: * @ejb.transaction type="Required"
042: *
043: * @author <a href="mailto:alex@jboss.org">Alex Loubyansky</a>
044: */
045: public class ManagerSessionBean implements SessionBean {
046: // Attributes ------------------------------------------------
047: static Category log = Category
048: .getInstance(ManagerSessionBean.class);
049: private InstituteLocalHome instituteHome;
050: private DepartmentLocalHome departmentHome;
051: private GroupLocalHome groupHome;
052: private StudentLocalHome studentHome;
053: private ExamenationLocalHome examHome;
054:
055: // Scenarious
056: /**
057: * @ejb.interface-method
058: */
059: public void testStandaloneFKMapping() {
060: InstituteLocal institute = null;
061: DepartmentLocal department = null;
062: try {
063: institute = getInstituteHome().create("NTUUKPI",
064: "Natinal Technical University Of The Ukraine KPI");
065: department = getDepartmentHome().create("KV",
066: "Specialized Computer Systems");
067: institute.getDepartments().add(department);
068: assertTrue(
069: "department.getInstitute().isIdentical(institute)",
070: department.getInstitute().isIdentical(institute));
071: department.setInstitute(null);
072: assertTrue("institute.getDepartments().isEmpty()",
073: institute.getDepartments().isEmpty());
074: } catch (EJBException ejbe) {
075: throw ejbe;
076: } catch (Exception e) {
077: throw new EJBException(e);
078: } finally {
079: if (institute != null)
080: removeEntity(institute);
081: if (department != null)
082: removeEntity(department);
083: }
084: }
085:
086: /**
087: * @ejb.interface-method
088: */
089: public void testCompleteFKToPKMapping() {
090: DepartmentLocal department = null;
091: GroupLocal kv62Group = null;
092: try {
093: // one-side instance created before many-side instance
094: department = getDepartmentHome().create("KV",
095: "Specialized Computer Systems");
096: assertTrue("department.getGroups().isEmpty()", department
097: .getGroups().isEmpty());
098:
099: kv62Group = getGroupHome().create("KV", 62, "KV-62");
100: assertTrue("department.getGroups().contains(kv62Group)",
101: department.getGroups().contains(kv62Group));
102: assertTrue(
103: "kv62Group.getDepartment().isIdentical(department)",
104: kv62Group.getDepartment().isIdentical(department));
105:
106: kv62Group.remove();
107: assertTrue("department.getGroups().isEmpty()", department
108: .getGroups().isEmpty());
109:
110: // many-side instance created before one-side instance
111: department.remove();
112: kv62Group = getGroupHome().create("KV", 62, "KV-62");
113: assertTrue("kv62Group.getDepartment() == null", kv62Group
114: .getDepartment() == null);
115:
116: department = getDepartmentHome().create("KV",
117: "Specialized Computer Systems");
118: assertTrue(
119: "kv62Group.getDepartment().isIdentical(department)",
120: kv62Group.getDepartment().isIdentical(department));
121: assertTrue("department.getGroups().contains(kv62Group)",
122: department.getGroups().contains(kv62Group));
123:
124: department.remove();
125: department = null;
126: assertTrue("kv62Group.getDepartment() == null", kv62Group
127: .getDepartment() == null);
128: } catch (EJBException ejbe) {
129: throw ejbe;
130: } catch (Exception e) {
131: throw new EJBException(e);
132: } finally {
133: if (department != null)
134: removeEntity(department);
135: if (kv62Group != null)
136: removeEntity(kv62Group);
137: }
138: }
139:
140: /**
141: * @ejb.interface-method
142: */
143: public void testPartialFKToPKMapping() {
144: StudentLocal petrovStudent = null;
145: StudentLocal sidorovStudent = null;
146: GroupLocal group = null;
147: try {
148: petrovStudent = getStudentHome().create("KV", "Petrov",
149: "Petrov works on KV department.");
150: group = getGroupHome().create("KV", 62, "KV-62");
151: assertTrue("petrovStudent.getGroup() == null",
152: petrovStudent.getGroup() == null);
153:
154: petrovStudent.setGroup(group);
155: assertTrue("group.isIdentical(petrovStudent.getGroup())",
156: group.isIdentical(petrovStudent.getGroup()));
157: assertTrue("group.getStudents().contains(petrovStudent)",
158: group.getStudents().contains(petrovStudent));
159:
160: sidorovStudent = getStudentHome().create("KV", "Sidorov",
161: "Sidorov works on KV department.");
162: group.getStudents().add(sidorovStudent);
163: assertTrue("sidorovStudent.getGroup().isIdentical(group)",
164: sidorovStudent.getGroup().isIdentical(group));
165: assertTrue("group.getStudents().contains(petrovStudent)",
166: group.getStudents().contains(petrovStudent));
167: assertTrue("group.getStudents().contains(sidorovStudent)",
168: group.getStudents().contains(sidorovStudent));
169:
170: group.remove();
171: group = null;
172: assertTrue("petrovStudent.getGroup() == null",
173: petrovStudent.getGroup() == null);
174: assertTrue("sidorovStudent.getGroup() == null",
175: sidorovStudent.getGroup() == null);
176:
177: /*
178: group = getGroupHome().create("KV", 62, "KV-62");
179: assertTrue("group.getStudents().contains(petrovStudent)", group.getStudents().contains(petrovStudent));
180: assertTrue("group.isIdentical(petrovStudent.getGroup())", group.isIdentical(petrovStudent.getGroup()));
181: */
182: } catch (EJBException ejbe) {
183: throw ejbe;
184: } catch (Exception e) {
185: throw new EJBException(e);
186: } finally {
187: if (petrovStudent != null)
188: removeEntity(petrovStudent);
189: if (sidorovStudent != null)
190: removeEntity(sidorovStudent);
191: if (group != null)
192: removeEntity(group);
193: }
194: }
195:
196: /**
197: * @ejb.interface-method
198: */
199: public void testFKToCMPMapping() {
200: GroupLocal kv61Group = null;
201: GroupLocal kv62Group = null;
202: ExamenationLocal exam = null;
203: try {
204: kv62Group = getGroupHome().create("KV", 62, "KV-62");
205: exam = getExamHome().create("kv61-1", "Math", "KV", 62);
206: assertTrue("kv62Group.isIdentical(exam.getGroup())",
207: kv62Group.isIdentical(exam.getGroup()));
208: assertTrue("kv62Group.getExamenations().contains(exam)",
209: kv62Group.getExamenations().contains(exam));
210:
211: kv61Group = getGroupHome().create("KV", 61, "KV-61");
212: exam.setGroup(kv61Group);
213: assertTrue("expected: exam.getGroupNumber() == 61;"
214: + " got: exam.getGroupNumber() == "
215: + exam.getGroupNumber(),
216: exam.getGroupNumber() == 61);
217:
218: exam.setGroupNumber(62);
219: assertTrue("kv62Group.isIdentical(exam.getGroup())",
220: kv62Group.isIdentical(exam.getGroup()));
221: assertTrue("kv62Group.getExamenations().contains(exam);",
222: kv62Group.getExamenations().contains(exam));
223: assertTrue("kv61Group.getExamenations().isEmpty();",
224: kv61Group.getExamenations().isEmpty());
225:
226: exam.setDepartmentCode("KM");
227: assertTrue("exam.getGroup() == null",
228: exam.getGroup() == null);
229: assertTrue("kv62Group.getExamenations().isEmpty();",
230: kv62Group.getExamenations().isEmpty());
231:
232: exam.setDepartmentCode("KV");
233: assertTrue("kv62Group.isIdentical(exam.getGroup())",
234: kv62Group.isIdentical(exam.getGroup()));
235: assertTrue("kv62Group.getExamenations().contains(exam);",
236: kv62Group.getExamenations().contains(exam));
237: } catch (EJBException ejbe) {
238: throw ejbe;
239: } catch (Exception e) {
240: throw new EJBException(e);
241: } finally {
242: if (exam != null)
243: removeEntity(exam);
244: if (kv61Group != null)
245: removeEntity(kv61Group);
246: if (kv62Group != null)
247: removeEntity(kv62Group);
248: }
249: }
250:
251: /**
252: * @ejb.interface-method
253: * @ejb.transaction type="RequiresNew"
254: */
255: public void createParent(Long id, String firstName)
256: throws Exception {
257: ParentUtil.getLocalHome().create(id, firstName);
258: }
259:
260: /**
261: * @ejb.interface-method
262: * @ejb.transaction type="RequiresNew"
263: */
264: public void createChild(Long id, String firstName) throws Exception {
265: ChildUtil.getLocalHome().create(id, firstName);
266: }
267:
268: /**
269: * @ejb.interface-method
270: * @ejb.transaction type="RequiresNew"
271: */
272: public void createChild(Long id, String firstName, Long parentId,
273: String parentName) throws Exception {
274: ChildUtil.getLocalHome().create(id, firstName, parentId,
275: parentName);
276: }
277:
278: /**
279: * @ejb.interface-method
280: * @ejb.transaction type="RequiresNew"
281: */
282: public void assertChildHasMother(Long childId, Long parentId,
283: String parentName) throws Exception {
284: ChildLocal child = ChildUtil.getLocalHome().findByPrimaryKey(
285: childId);
286: ParentLocal parent = child.getMother();
287: if (parent == null)
288: throw new EJBException(
289: "No parent assigned to child: expected parentId="
290: + parentId);
291: ParentPK parentPK = new ParentPK(parentId, parentName);
292: if (!parent.getPrimaryKey().equals(parentPK))
293: throw new EJBException("Wrong parent: expected parentPK="
294: + parentPK + ", got " + parent.getPrimaryKey());
295: }
296:
297: /**
298: * @ejb.interface-method
299: * @ejb.transaction type="RequiresNew"
300: */
301: public Object createChildUPKWithMother() throws Exception {
302: ChildUPKLocal child = ChildUPKUtil.getLocalHome().create(
303: "Avoka");
304: ParentLocal mother = ParentUtil.getLocalHome().create(
305: new Long(11), "Irene");
306: child.setMother(mother);
307: return child.getPrimaryKey();
308: }
309:
310: /**
311: * @ejb.interface-method
312: * @ejb.transaction type="RequiresNew"
313: */
314: public void loadChildUPKWithMother(Object pk) throws Exception {
315: // find
316: ChildUPKLocal child = ChildUPKUtil.getLocalHome()
317: .findByPrimaryKey(pk);
318: // load child and check its mother
319: assertTrue("child.getMother().getFirstName() is Irene", "Irene"
320: .equals(child.getMother().getFirstName()));
321: }
322:
323: /**
324: * @ejb.interface-method
325: * @ejb.transaction type="RequiresNew"
326: */
327: public Object createChildUPKWithFather() throws Exception {
328: ChildUPKLocal child = ChildUPKUtil.getLocalHome().create(
329: "Avoka");
330: ParentLocal father = ParentUtil.getLocalHome().create(
331: new Long(12), "Gregory");
332: child.setFather(father);
333: return child.getPrimaryKey();
334: }
335:
336: /**
337: * @ejb.interface-method
338: * @ejb.transaction type="RequiresNew"
339: */
340: public void loadChildUPKWithFather(Object pk) throws Exception {
341: log.debug("loadChildUPK");
342: // find
343: ChildUPKLocal child = ChildUPKUtil.getLocalHome()
344: .findByPrimaryKey(pk);
345: // load child and check its mother
346: assertTrue("child.getFather().getFirstName() is Gregory",
347: "Gregory".equals(child.getFather().getFirstName()));
348: }
349:
350: // Private ---------------------------------------------------
351: private void assertTrue(String message, boolean expression) {
352: if (!expression)
353: throw new AssertionException(message);
354: }
355:
356: private void removeEntity(EJBLocalObject localEntity) {
357: try {
358: localEntity.remove();
359: } catch (RemoveException re) {
360: throw new EJBException("Couldn't remove local entity "
361: + localEntity.getPrimaryKey());
362: }
363: }
364:
365: private StudentLocalHome getStudentHome() throws NamingException {
366: if (studentHome == null)
367: studentHome = StudentUtil.getLocalHome();
368: return studentHome;
369: }
370:
371: private ExamenationLocalHome getExamHome() throws NamingException {
372: if (examHome == null)
373: examHome = ExamenationUtil.getLocalHome();
374: return examHome;
375: }
376:
377: private InstituteLocalHome getInstituteHome()
378: throws NamingException {
379: if (instituteHome == null)
380: instituteHome = InstituteUtil.getLocalHome();
381: return instituteHome;
382: }
383:
384: private DepartmentLocalHome getDepartmentHome()
385: throws NamingException {
386: if (departmentHome == null)
387: departmentHome = DepartmentUtil.getLocalHome();
388: return departmentHome;
389: }
390:
391: private GroupLocalHome getGroupHome() throws NamingException {
392: if (groupHome == null)
393: groupHome = GroupUtil.getLocalHome();
394: return groupHome;
395: }
396:
397: // SessionBean implementation --------------------------------
398: /**
399: * @ejb.create-method
400: */
401: public void ejbCreate() throws CreateException {
402: }
403:
404: public void ejbActivate() throws EJBException, RemoteException {
405: }
406:
407: public void ejbPassivate() throws EJBException, RemoteException {
408: }
409:
410: public void ejbRemove() throws EJBException, RemoteException {
411: }
412:
413: public void setSessionContext(SessionContext sessionContext)
414: throws EJBException, RemoteException {
415: }
416: }
|