Java Source Code: org.jboss.test.cmp2.fkmapping.ejb.ManagerSessionBean


   1: /*
   2:  * JBoss, the OpenSource J2EE webOS
   3:  *
   4:  * Distributable under LGPL license.
   5:  * See terms of license at gnu.org.
   6:  */
   7: package org.jboss.test.cmp2.fkmapping.ejb;
   8: 
   9: import org.apache.log4j.Category;
  10: 
  11: import javax.ejb.SessionBean;
  12: import javax.ejb.EJBException;
  13: import javax.ejb.SessionContext;
  14: import javax.ejb.CreateException;
  15: import javax.ejb.RemoveException;
  16: import javax.ejb.EJBLocalObject;
  17: import javax.naming.NamingException;
  18: import java.rmi.RemoteException;
  19: 
  20: 
  21: /**
  22:  * @ejb.bean
  23:  *    type="Stateless"
  24:  *    name="Manager"
  25:  *    view-type="remote"
  26:  * @ejb.util generate="physical"
  27:  * @ejb.transaction type="Required"
  28:  *
  29:  * @author <a href="mailto:alex@jboss.org">Alex Loubyansky</a>
  30:  */
  31: public class ManagerSessionBean
  32:    implements SessionBean
  33:	  {
  34:   // Attributes ------------------------------------------------
  35:   static Category log = Category.getInstance(ManagerSessionBean.class);
  36:   private InstituteLocalHome instituteHome;
  37:   private DepartmentLocalHome departmentHome;
  38:   private GroupLocalHome groupHome;
  39:   private StudentLocalHome studentHome;
  40:   private ExamenationLocalHome examHome;
  41:
  42:   // Scenarious
  43:   /**
  44:    * @ejb.interface-method
  45:    */
  46:   public void testStandaloneFKMapping()
  47:	     {
  48:      InstituteLocal institute = null;
  49:      DepartmentLocal department = null;
  50:      try
  51:	        {
  52:         institute = getInstituteHome().create("NTUUKPI", "Natinal Technical University Of The Ukraine KPI");
  53:         department = getDepartmentHome().create("KV", "Specialized Computer Systems");
  54:         institute.getDepartments().add(department);
  55:         assertTrue("department.getInstitute().isIdentical(institute)", department.getInstitute().isIdentical(institute));
  56:         department.setInstitute(null);
  57:         assertTrue("institute.getDepartments().isEmpty()", institute.getDepartments().isEmpty());
  58:      }
  59:      catch(EJBException ejbe)
  60:	        {
  61:         throw ejbe;
  62:      }
  63:      catch(Exception e)
  64:	        {
  65:         throw new EJBException(e);
  66:      }
  67:      finally
  68:	        {
  69:         if(institute != null)
  70:            removeEntity(institute);
  71:         if(department != null)
  72:            removeEntity(department);
  73:      }
  74:   }
  75:
  76:   /**
  77:    * @ejb.interface-method
  78:    */
  79:   public void testCompleteFKToPKMapping()
  80:	     {
  81:      DepartmentLocal department = null;
  82:      GroupLocal kv62Group = null;
  83:      try
  84:	        {
  85:         // one-side instance created before many-side instance
  86:         department = getDepartmentHome().create("KV", "Specialized Computer Systems");
  87:         assertTrue("department.getGroups().isEmpty()", department.getGroups().isEmpty());
  88:
  89:         kv62Group = getGroupHome().create("KV", 62, "KV-62");
  90:         assertTrue("department.getGroups().contains(kv62Group)", department.getGroups().contains(kv62Group));
  91:         assertTrue("kv62Group.getDepartment().isIdentical(department)", kv62Group.getDepartment().isIdentical(department));
  92:
  93:         kv62Group.remove();
  94:         assertTrue("department.getGroups().isEmpty()", department.getGroups().isEmpty());
  95:
  96:         // many-side instance created before one-side instance
  97:         department.remove();
  98:         kv62Group = getGroupHome().create("KV", 62, "KV-62");
  99:         assertTrue("kv62Group.getDepartment() == null", kv62Group.getDepartment() == null);
 100:
 101:         department = getDepartmentHome().create("KV", "Specialized Computer Systems");
 102:         assertTrue("kv62Group.getDepartment().isIdentical(department)", kv62Group.getDepartment().isIdentical(department));
 103:         assertTrue("department.getGroups().contains(kv62Group)", department.getGroups().contains(kv62Group));
 104:
 105:         department.remove();
 106:         department = null;
 107:         assertTrue("kv62Group.getDepartment() == null", kv62Group.getDepartment() == null);
 108:      }
 109:      catch(EJBException ejbe)
 110:	        {
 111:         throw ejbe;
 112:      }
 113:      catch(Exception e)
 114:	        {
 115:         throw new EJBException(e);
 116:      }
 117:      finally
 118:	        {
 119:         if(department != null)
 120:            removeEntity(department);
 121:         if(kv62Group != null)
 122:            removeEntity(kv62Group);
 123:      }
 124:   }
 125:
 126:   /**
 127:    * @ejb.interface-method
 128:    */
 129:   public void testPartialFKToPKMapping()
 130:	     {
 131:      StudentLocal petrovStudent = null;
 132:      StudentLocal sidorovStudent = null;
 133:      GroupLocal group = null;
 134:      try
 135:	        {
 136:         petrovStudent = getStudentHome().create("KV", "Petrov", "Petrov works on KV department.");
 137:         group = getGroupHome().create("KV", 62, "KV-62");
 138:         assertTrue("petrovStudent.getGroup() == null", petrovStudent.getGroup() == null);
 139:
 140:         petrovStudent.setGroup(group);
 141:         assertTrue("group.isIdentical(petrovStudent.getGroup())", group.isIdentical(petrovStudent.getGroup()));
 142:         assertTrue("group.getStudents().contains(petrovStudent)", group.getStudents().contains(petrovStudent));
 143:
 144:         sidorovStudent = getStudentHome().create("KV", "Sidorov", "Sidorov works on KV department.");
 145:         group.getStudents().add(sidorovStudent);
 146:         assertTrue("sidorovStudent.getGroup().isIdentical(group)", sidorovStudent.getGroup().isIdentical(group));
 147:         assertTrue("group.getStudents().contains(petrovStudent)", group.getStudents().contains(petrovStudent));
 148:         assertTrue("group.getStudents().contains(sidorovStudent)", group.getStudents().contains(sidorovStudent));
 149:
 150:         group.remove();
 151:         group = null;
 152:         assertTrue("petrovStudent.getGroup() == null", petrovStudent.getGroup() == null);
 153:         assertTrue("sidorovStudent.getGroup() == null", sidorovStudent.getGroup() == null);
 154:
 155:         /*
 156:         group = getGroupHome().create("KV", 62, "KV-62");
 157:         assertTrue("group.getStudents().contains(petrovStudent)", group.getStudents().contains(petrovStudent));
 158:         assertTrue("group.isIdentical(petrovStudent.getGroup())", group.isIdentical(petrovStudent.getGroup()));
 159:         */
 160:      }
 161:      catch(EJBException ejbe)
 162:	        {
 163:         throw ejbe;
 164:      }
 165:      catch(Exception e)
 166:	        {
 167:         throw new EJBException(e);
 168:      }
 169:      finally
 170:	        {
 171:         if(petrovStudent != null)
 172:            removeEntity(petrovStudent);
 173:         if(sidorovStudent != null)
 174:            removeEntity(sidorovStudent);
 175:         if(group != null)
 176:            removeEntity(group);
 177:      }
 178:   }
 179:
 180:   /**
 181:    * @ejb.interface-method
 182:    */
 183:   public void testFKToCMPMapping()
 184:	     {
 185:      GroupLocal kv61Group = null;
 186:      GroupLocal kv62Group = null;
 187:      ExamenationLocal exam = null;
 188:      try
 189:	        {
 190:         kv62Group = getGroupHome().create("KV", 62, "KV-62");
 191:         exam = getExamHome().create("kv61-1", "Math", "KV", 62);
 192:         assertTrue("kv62Group.isIdentical(exam.getGroup())", kv62Group.isIdentical(exam.getGroup()));
 193:         assertTrue("kv62Group.getExamenations().contains(exam)", kv62Group.getExamenations().contains(exam));
 194:
 195:         kv61Group = getGroupHome().create("KV", 61, "KV-61");
 196:         exam.setGroup(kv61Group);
 197:         assertTrue("expected: exam.getGroupNumber() == 61;"
 198:            + " got: exam.getGroupNumber() == " + exam.getGroupNumber(),
 199:            exam.getGroupNumber() == 61);
 200:
 201:         exam.setGroupNumber(62);
 202:         assertTrue("kv62Group.isIdentical(exam.getGroup())", kv62Group.isIdentical(exam.getGroup()));
 203:         assertTrue("kv62Group.getExamenations().contains(exam);", kv62Group.getExamenations().contains(exam));
 204:         assertTrue("kv61Group.getExamenations().isEmpty();", kv61Group.getExamenations().isEmpty());
 205:
 206:         exam.setDepartmentCode("KM");
 207:         assertTrue("exam.getGroup() == null", exam.getGroup() == null);
 208:         assertTrue("kv62Group.getExamenations().isEmpty();", kv62Group.getExamenations().isEmpty());
 209:
 210:         exam.setDepartmentCode("KV");
 211:         assertTrue("kv62Group.isIdentical(exam.getGroup())", kv62Group.isIdentical(exam.getGroup()));
 212:         assertTrue("kv62Group.getExamenations().contains(exam);", kv62Group.getExamenations().contains(exam));
 213:      }
 214:      catch(EJBException ejbe)
 215:	        {
 216:         throw ejbe;
 217:      }
 218:      catch(Exception e)
 219:	        {
 220:         throw new EJBException(e);
 221:      }
 222:      finally
 223:	        {
 224:         if(exam != null)
 225:            removeEntity(exam);
 226:         if(kv61Group != null)
 227:            removeEntity(kv61Group);
 228:         if(kv62Group != null)
 229:            removeEntity(kv62Group);
 230:      }
 231:   }
 232:
 233:   /**
 234:    * @ejb.interface-method
 235:    * @ejb.transaction type="RequiresNew"
 236:    */
 237:   public void createParent(Long id, String firstName)
 238:      throws Exception
 239:	     {
 240:      ParentUtil.getLocalHome().create(id, firstName);
 241:   }
 242:
 243:   /**
 244:    * @ejb.interface-method
 245:    * @ejb.transaction type="RequiresNew"
 246:    */
 247:   public void createChild(long id, String firstName)
 248:      throws Exception
 249:	     {
 250:      ChildUtil.getLocalHome().create(id, firstName);
 251:   }
 252:
 253:   /**
 254:    * @ejb.interface-method
 255:    * @ejb.transaction type="RequiresNew"
 256:    */
 257:   public void createChild(long id, String firstName, Long parentId, String parentName)
 258:      throws Exception
 259:	     {
 260:      ChildUtil.getLocalHome().create(id, firstName, parentId, parentName);
 261:   }
 262:
 263:   /**
 264:    * @ejb.interface-method
 265:    * @ejb.transaction type="RequiresNew"
 266:    */
 267:   public void assertChildHasMother(long childId, Long parentId, String parentName)
 268:      throws Exception
 269:	     {
 270:      ChildLocal child = ChildUtil.getLocalHome().findByPrimaryKey(new ChildPK(childId));
 271:      ParentLocal parent = child.getMother();
 272:      if(parent == null)
 273:         throw new EJBException("No parent assigned to child: expected parentId=" + parentId);
 274:      ParentPK parentPK = new ParentPK(parentId, parentName);
 275:      if(!parent.getPrimaryKey().equals(parentPK))
 276:         throw new EJBException("Wrong parent: expected parentPK=" + parentPK
 277:            + ", got " + parent.getPrimaryKey());
 278:   }
 279:
 280:   /**
 281:    * @ejb.interface-method
 282:    * @ejb.transaction type="RequiresNew"
 283:    */
 284:   public void assertParentHasNoChild(Long parentId, String parentName)
 285:      throws Exception
 286:	     {
 287:      ParentLocal parent = ParentUtil.getLocalHome()
 288:         .findByPrimaryKey(new ParentPK(parentId, parentName));
 289:      if(parent.getChild() != null)
 290:         throw new EJBException("parent.getChild() != null");
 291:   }
 292:
 293:   /**
 294:    * @ejb.interface-method
 295:    * @ejb.transaction type="RequiresNew"
 296:    */
 297:   public Object createChildUPKWithMother() throws Exception
 298:	     {
 299:      ChildUPKLocal child = ChildUPKUtil.getLocalHome().create("Avoka");
 300:      ParentLocal mother = ParentUtil.getLocalHome().create(new Long(11), "Irene");
 301:      child.setMother(mother);
 302:      return child.getPrimaryKey();
 303:   }
 304:   /**
 305:    * @ejb.interface-method
 306:    * @ejb.transaction type="RequiresNew"
 307:    */
 308:   public void loadChildUPKWithMother(Object pk) throws Exception
 309:	     {
 310:      // find
 311:      ChildUPKLocal child = ChildUPKUtil.getLocalHome().findByPrimaryKey(pk);
 312:      // load child and check its mother
 313:      assertTrue("child.getMother().getFirstName() is Irene",
 314:         "Irene".equals(child.getMother().getFirstName()));
 315:   }
 316:
 317:   /**
 318:    * @ejb.interface-method
 319:    * @ejb.transaction type="RequiresNew"
 320:    */
 321:   public Object createChildUPKWithFather() throws Exception
 322:	     {
 323:      ChildUPKLocal child = ChildUPKUtil.getLocalHome().create("Avoka");
 324:      ParentLocal father = ParentUtil.getLocalHome().create(new Long(12), "Gregory");
 325:      child.setFather(father);
 326:      return child.getPrimaryKey();
 327:   }
 328:
 329:   /**
 330:    * @ejb.interface-method
 331:    * @ejb.transaction type="RequiresNew"
 332:    */
 333:   public void loadChildUPKWithFather(Object pk) throws Exception
 334:	     {
 335:      log.debug("loadChildUPK");
 336:      // find
 337:      ChildUPKLocal child = ChildUPKUtil.getLocalHome().findByPrimaryKey(pk);
 338:      // load child and check its mother
 339:      assertTrue("child.getFather().getFirstName() is Gregory",
 340:         "Gregory".equals(child.getFather().getFirstName()));
 341:   }
 342:
 343:   // Private ---------------------------------------------------
 344:   private void assertTrue(String message, boolean expression)
 345:	     {
 346:      if(!expression)
 347:         throw new AssertionException(message);
 348:   }
 349:
 350:   private void removeEntity(EJBLocalObject localEntity)
 351:	     {
 352:      try
 353:	        {
 354:         localEntity.remove();
 355:      }
 356:      catch(RemoveException re)
 357:	        {
 358:         throw new EJBException("Couldn't remove local entity " + localEntity.getPrimaryKey());
 359:      }
 360:   }
 361:
 362:   private StudentLocalHome getStudentHome()
 363:      throws NamingException
 364:	     {
 365:      if(studentHome == null)
 366:         studentHome = StudentUtil.getLocalHome();
 367:      return studentHome;
 368:   }
 369:
 370:   private ExamenationLocalHome getExamHome()
 371:      throws NamingException
 372:	     {
 373:      if(examHome == null)
 374:         examHome = ExamenationUtil.getLocalHome();
 375:      return examHome;
 376:   }
 377:
 378:   private InstituteLocalHome getInstituteHome()
 379:      throws NamingException
 380:	     {
 381:      if(instituteHome == null)
 382:         instituteHome = InstituteUtil.getLocalHome();
 383:      return instituteHome;
 384:   }
 385:
 386:   private DepartmentLocalHome getDepartmentHome()
 387:      throws NamingException
 388:	     {
 389:      if(departmentHome == null)
 390:         departmentHome = DepartmentUtil.getLocalHome();
 391:      return departmentHome;
 392:   }
 393:
 394:   private GroupLocalHome getGroupHome()
 395:      throws NamingException
 396:	     {
 397:      if(groupHome == null)
 398:         groupHome = GroupUtil.getLocalHome();
 399:      return groupHome;
 400:   }
 401:
 402:   // SessionBean implementation --------------------------------
 403:   /**
 404:    * @ejb.create-method
 405:    */
 406:   public void ejbCreate() throws CreateException {}
 407:   public void ejbActivate() throws EJBException, RemoteException {}
 408:   public void ejbPassivate() throws EJBException, RemoteException {}
 409:   public void ejbRemove() throws EJBException, RemoteException {}
 410:   public void setSessionContext(SessionContext sessionContext) throws EJBException, RemoteException {}
 411:}