1 22 package org.jboss.test.cmp2.fkmapping.test; 23 24 import javax.ejb.EJBException ; 25 import javax.ejb.EJBLocalObject ; 26 import javax.ejb.RemoveException ; 27 import javax.ejb.NoSuchObjectLocalException ; 28 import javax.naming.NamingException ; 29 import javax.naming.InitialContext ; 30 import javax.transaction.UserTransaction ; 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 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 public static Test suite() throws Exception 68 { 69 return JBossTestCase.getDeploySetup(FKMappingUnitTestCase.class, "cmp2-fkmapping.jar"); 70 } 71 72 public FKMappingUnitTestCase(String name) 74 { 75 super(name); 76 } 77 78 public void testStandaloneFKMapping() throws Exception 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 { 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 119 { 120 DepartmentLocal department = null; 121 GroupLocal kv62Group = null; 122 try { 123 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 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 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 183 } finally { 184 removeEntity(petrovStudent); 185 removeEntity(sidorovStudent); 186 removeEntity(group); 187 } 188 } 189 190 public void testFKToCMPMapping() 191 throws Exception 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 231 { 232 Long long1 = new Long (1); 233 String avoka = "Avoka"; 234 String 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 expected){} 244 245 manager.createChild(long1, avoka, long1, irene); 246 manager.createChild(new Long (2), "Ataka", long1, irene); 247 248 manager.assertChildHasMother(long1, long1, irene); 249 manager.assertChildHasMother(new Long (2), long1, irene); 250 } 251 252 public void testGeneratedPKWithInsertAfterPostCreate() 253 throws Exception 254 { 255 ChildUPKUtil.getLocalHome().create(null); 257 } 258 259 268 public void testCompleteFKReadOnLoad() throws Exception 269 { 270 Manager manager = ManagerUtil.getHome().create(); 271 272 Object childPK = manager.createChildUPKWithMother(); 274 manager.loadChildUPKWithMother(childPK); 275 276 childPK = manager.createChildUPKWithFather(); 277 manager.loadChildUPKWithFather(childPK); 278 } 279 280 282 private StudentLocalHome getStudentHome() 283 throws NamingException 284 { 285 if (studentHome == null) 286 studentHome = StudentUtil.getLocalHome(); 287 return studentHome; 288 } 289 290 private ExamenationLocalHome getExamHome() 291 throws NamingException 292 { 293 if (examHome == null) 294 examHome = ExamenationUtil.getLocalHome(); 295 return examHome; 296 } 297 298 private InstituteLocalHome getInstituteHome() 299 throws NamingException 300 { 301 if (instituteHome == null) 302 instituteHome = InstituteUtil.getLocalHome(); 303 return instituteHome; 304 } 305 306 private DepartmentLocalHome getDepartmentHome() 307 throws NamingException 308 { 309 if (departmentHome == null) 310 departmentHome = DepartmentUtil.getLocalHome(); 311 return departmentHome; 312 } 313 314 private GroupLocalHome getGroupHome() 315 throws NamingException 316 { 317 if (groupHome == null) 318 groupHome = GroupUtil.getLocalHome(); 319 return groupHome; 320 } 321 322 private void removeEntity(EJBLocalObject localEntity) 323 { 324 if (localEntity != null) { 325 try { 326 localEntity.remove(); 327 } catch (RemoveException re) { 328 throw new EJBException ("Couldn't remove local entity " + localEntity.getPrimaryKey()); 329 } 330 catch(NoSuchObjectLocalException e) 331 { 332 } 334 } 335 } 336 337 private void commit() throws Exception { 338 UserTransaction tx = (UserTransaction ) new InitialContext ().lookup("java:comp/UserTransaction"); 339 tx.commit(); 340 tx.begin(); 341 } 342 } 343 | Popular Tags |