1 22 package org.jboss.test.cmp2.fkmapping.ejb; 23 24 import org.jboss.logging.Logger; 25 26 import javax.ejb.SessionBean ; 27 import javax.ejb.EJBException ; 28 import javax.ejb.SessionContext ; 29 import javax.ejb.CreateException ; 30 import javax.ejb.RemoveException ; 31 import javax.ejb.EJBLocalObject ; 32 import javax.naming.NamingException ; 33 import java.rmi.RemoteException ; 34 35 36 46 public class ManagerSessionBean 47 implements SessionBean 48 { 49 static Logger log = Logger.getLogger(ManagerSessionBean.class); 51 private InstituteLocalHome instituteHome; 52 private DepartmentLocalHome departmentHome; 53 private GroupLocalHome groupHome; 54 private StudentLocalHome studentHome; 55 private ExamenationLocalHome examHome; 56 57 61 public void testStandaloneFKMapping() 62 { 63 InstituteLocal institute = null; 64 DepartmentLocal department = null; 65 try 66 { 67 institute = getInstituteHome().create("NTUUKPI", "Natinal Technical University Of The Ukraine KPI"); 68 department = getDepartmentHome().create("KV", "Specialized Computer Systems"); 69 institute.getDepartments().add(department); 70 assertTrue("department.getInstitute().isIdentical(institute)", department.getInstitute().isIdentical(institute)); 71 department.setInstitute(null); 72 assertTrue("institute.getDepartments().isEmpty()", institute.getDepartments().isEmpty()); 73 } 74 catch(EJBException ejbe) 75 { 76 throw ejbe; 77 } 78 catch(Exception e) 79 { 80 throw new EJBException (e); 81 } 82 finally 83 { 84 if(institute != null) 85 removeEntity(institute); 86 if(department != null) 87 removeEntity(department); 88 } 89 } 90 91 94 public void testCompleteFKToPKMapping() 95 { 96 DepartmentLocal department = null; 97 GroupLocal kv62Group = null; 98 try 99 { 100 department = getDepartmentHome().create("KV", "Specialized Computer Systems"); 102 assertTrue("department.getGroups().isEmpty()", department.getGroups().isEmpty()); 103 104 kv62Group = getGroupHome().create("KV", 62, "KV-62"); 105 assertTrue("department.getGroups().contains(kv62Group)", department.getGroups().contains(kv62Group)); 106 assertTrue("kv62Group.getDepartment().isIdentical(department)", kv62Group.getDepartment().isIdentical(department)); 107 108 kv62Group.remove(); 109 assertTrue("department.getGroups().isEmpty()", department.getGroups().isEmpty()); 110 111 department.remove(); 113 kv62Group = getGroupHome().create("KV", 62, "KV-62"); 114 assertTrue("kv62Group.getDepartment() == null", kv62Group.getDepartment() == null); 115 116 department = getDepartmentHome().create("KV", "Specialized Computer Systems"); 117 assertTrue("kv62Group.getDepartment().isIdentical(department)", kv62Group.getDepartment().isIdentical(department)); 118 assertTrue("department.getGroups().contains(kv62Group)", department.getGroups().contains(kv62Group)); 119 120 department.remove(); 121 department = null; 122 assertTrue("kv62Group.getDepartment() == null", kv62Group.getDepartment() == null); 123 } 124 catch(EJBException ejbe) 125 { 126 throw ejbe; 127 } 128 catch(Exception e) 129 { 130 throw new EJBException (e); 131 } 132 finally 133 { 134 if(department != null) 135 removeEntity(department); 136 if(kv62Group != null) 137 removeEntity(kv62Group); 138 } 139 } 140 141 144 public void testPartialFKToPKMapping() 145 { 146 StudentLocal petrovStudent = null; 147 StudentLocal sidorovStudent = null; 148 GroupLocal group = null; 149 try 150 { 151 petrovStudent = getStudentHome().create("KV", "Petrov", "Petrov works on KV department."); 152 group = getGroupHome().create("KV", 62, "KV-62"); 153 assertTrue("petrovStudent.getGroup() == null", petrovStudent.getGroup() == null); 154 155 petrovStudent.setGroup(group); 156 assertTrue("group.isIdentical(petrovStudent.getGroup())", group.isIdentical(petrovStudent.getGroup())); 157 assertTrue("group.getStudents().contains(petrovStudent)", group.getStudents().contains(petrovStudent)); 158 159 sidorovStudent = getStudentHome().create("KV", "Sidorov", "Sidorov works on KV department."); 160 group.getStudents().add(sidorovStudent); 161 assertTrue("sidorovStudent.getGroup().isIdentical(group)", sidorovStudent.getGroup().isIdentical(group)); 162 assertTrue("group.getStudents().contains(petrovStudent)", group.getStudents().contains(petrovStudent)); 163 assertTrue("group.getStudents().contains(sidorovStudent)", group.getStudents().contains(sidorovStudent)); 164 165 group.remove(); 166 group = null; 167 assertTrue("petrovStudent.getGroup() == null", petrovStudent.getGroup() == null); 168 assertTrue("sidorovStudent.getGroup() == null", sidorovStudent.getGroup() == null); 169 170 175 } 176 catch(EJBException ejbe) 177 { 178 throw ejbe; 179 } 180 catch(Exception e) 181 { 182 throw new EJBException (e); 183 } 184 finally 185 { 186 if(petrovStudent != null) 187 removeEntity(petrovStudent); 188 if(sidorovStudent != null) 189 removeEntity(sidorovStudent); 190 if(group != null) 191 removeEntity(group); 192 } 193 } 194 195 198 public void testFKToCMPMapping() 199 { 200 GroupLocal kv61Group = null; 201 GroupLocal kv62Group = null; 202 ExamenationLocal exam = null; 203 try 204 { 205 kv62Group = getGroupHome().create("KV", 62, "KV-62"); 206 exam = getExamHome().create("kv61-1", "Math", "KV", 62); 207 assertTrue("kv62Group.isIdentical(exam.getGroup())", kv62Group.isIdentical(exam.getGroup())); 208 assertTrue("kv62Group.getExamenations().contains(exam)", kv62Group.getExamenations().contains(exam)); 209 210 kv61Group = getGroupHome().create("KV", 61, "KV-61"); 211 exam.setGroup(kv61Group); 212 assertTrue("expected: exam.getGroupNumber() == 61;" 213 + " got: exam.getGroupNumber() == " + exam.getGroupNumber(), 214 exam.getGroupNumber() == 61); 215 216 exam.setGroupNumber(62); 217 assertTrue("kv62Group.isIdentical(exam.getGroup())", kv62Group.isIdentical(exam.getGroup())); 218 assertTrue("kv62Group.getExamenations().contains(exam);", kv62Group.getExamenations().contains(exam)); 219 assertTrue("kv61Group.getExamenations().isEmpty();", kv61Group.getExamenations().isEmpty()); 220 221 exam.setDepartmentCode("KM"); 222 assertTrue("exam.getGroup() == null", exam.getGroup() == null); 223 assertTrue("kv62Group.getExamenations().isEmpty();", kv62Group.getExamenations().isEmpty()); 224 225 exam.setDepartmentCode("KV"); 226 assertTrue("kv62Group.isIdentical(exam.getGroup())", kv62Group.isIdentical(exam.getGroup())); 227 assertTrue("kv62Group.getExamenations().contains(exam);", kv62Group.getExamenations().contains(exam)); 228 } 229 catch(EJBException ejbe) 230 { 231 throw ejbe; 232 } 233 catch(Exception e) 234 { 235 throw new EJBException (e); 236 } 237 finally 238 { 239 if(exam != null) 240 removeEntity(exam); 241 if(kv61Group != null) 242 removeEntity(kv61Group); 243 if(kv62Group != null) 244 removeEntity(kv62Group); 245 } 246 } 247 248 252 public void createParent(Long id, String firstName) 253 throws Exception 254 { 255 ParentUtil.getLocalHome().create(id, firstName); 256 } 257 258 262 public void createChild(Long id, String firstName) 263 throws Exception 264 { 265 ChildUtil.getLocalHome().create(id, firstName); 266 } 267 268 272 public void createChild(Long id, String firstName, Long parentId, String parentName) 273 throws Exception 274 { 275 ChildUtil.getLocalHome().create(id, firstName, parentId, parentName); 276 } 277 278 282 public void assertChildHasMother(Long childId, Long parentId, String parentName) 283 throws Exception 284 { 285 ChildLocal child = ChildUtil.getLocalHome().findByPrimaryKey(childId); 286 ParentLocal parent = child.getMother(); 287 if(parent == null) 288 throw new EJBException ("No parent assigned to child: expected parentId=" + parentId); 289 ParentPK parentPK = new ParentPK(parentId, parentName); 290 if(!parent.getPrimaryKey().equals(parentPK)) 291 throw new EJBException ("Wrong parent: expected parentPK=" + parentPK 292 + ", got " + parent.getPrimaryKey()); 293 } 294 295 299 public Object createChildUPKWithMother() throws Exception 300 { 301 ChildUPKLocal child = ChildUPKUtil.getLocalHome().create("Avoka"); 302 ParentLocal mother = ParentUtil.getLocalHome().create(new Long (11), "Irene"); 303 child.setMother(mother); 304 return child.getPrimaryKey(); 305 } 306 310 public void loadChildUPKWithMother(Object pk) throws Exception 311 { 312 ChildUPKLocal child = ChildUPKUtil.getLocalHome().findByPrimaryKey(pk); 314 assertTrue("child.getMother().getFirstName() is Irene", 316 "Irene".equals(child.getMother().getFirstName())); 317 } 318 319 323 public Object createChildUPKWithFather() throws Exception 324 { 325 ChildUPKLocal child = ChildUPKUtil.getLocalHome().create("Avoka"); 326 ParentLocal father = ParentUtil.getLocalHome().create(new Long (12), "Gregory"); 327 child.setFather(father); 328 return child.getPrimaryKey(); 329 } 330 334 public void loadChildUPKWithFather(Object pk) throws Exception 335 { 336 log.debug("loadChildUPK"); 337 ChildUPKLocal child = ChildUPKUtil.getLocalHome().findByPrimaryKey(pk); 339 assertTrue("child.getFather().getFirstName() is Gregory", 341 "Gregory".equals(child.getFather().getFirstName())); 342 } 343 344 private void assertTrue(String message, boolean expression) 346 { 347 if(!expression) 348 throw new AssertionException(message); 349 } 350 351 private void removeEntity(EJBLocalObject localEntity) 352 { 353 try 354 { 355 localEntity.remove(); 356 } 357 catch(RemoveException re) 358 { 359 throw new EJBException ("Couldn't remove local entity " + localEntity.getPrimaryKey()); 360 } 361 } 362 363 private StudentLocalHome getStudentHome() 364 throws NamingException 365 { 366 if(studentHome == null) 367 studentHome = StudentUtil.getLocalHome(); 368 return studentHome; 369 } 370 371 private ExamenationLocalHome getExamHome() 372 throws NamingException 373 { 374 if(examHome == null) 375 examHome = ExamenationUtil.getLocalHome(); 376 return examHome; 377 } 378 379 private InstituteLocalHome getInstituteHome() 380 throws NamingException 381 { 382 if(instituteHome == null) 383 instituteHome = InstituteUtil.getLocalHome(); 384 return instituteHome; 385 } 386 387 private DepartmentLocalHome getDepartmentHome() 388 throws NamingException 389 { 390 if(departmentHome == null) 391 departmentHome = DepartmentUtil.getLocalHome(); 392 return departmentHome; 393 } 394 395 private GroupLocalHome getGroupHome() 396 throws NamingException 397 { 398 if(groupHome == null) 399 groupHome = GroupUtil.getLocalHome(); 400 return groupHome; 401 } 402 403 407 public void ejbCreate() throws CreateException {} 408 public void ejbActivate() throws EJBException , RemoteException {} 409 public void ejbPassivate() throws EJBException , RemoteException {} 410 public void ejbRemove() throws EJBException , RemoteException {} 411 public void setSessionContext(SessionContext sessionContext) throws EJBException , RemoteException {} 412 } 413 | Popular Tags |