1 package org.apache.ojb.broker; 2 3 import junit.framework.TestCase; 4 5 import java.util.Collection ; 6 import java.util.ArrayList ; 7 8 import org.apache.ojb.broker.query.Criteria; 9 import org.apache.ojb.broker.query.Query; 10 import org.apache.ojb.broker.query.QueryFactory; 11 12 19 public class ComplexReferenceTest extends TestCase 20 { 21 24 private static String PRE = "ComplexReferenceTest"; 25 private PersistenceBroker broker; 26 27 public static void main(String [] args) 28 { 29 String [] arr = {ComplexReferenceTest.class.getName()}; 30 junit.textui.TestRunner.main(arr); 31 } 32 33 public void setUp() throws PBFactoryException 34 { 35 broker = PersistenceBrokerFactory.defaultPersistenceBroker(); 37 } 38 39 public void tearDown() 40 { 41 try 42 { 43 if(broker != null) broker.close(); 44 } 45 catch (PersistenceBrokerException e) 46 { 47 } 48 } 49 50 public void testCreateDeleteProject() throws Exception 51 { 52 String prefix = getStamp() + "testCreateDeleteProject"; 53 String projectName = prefix + "project"; 54 55 Project project = createProject(projectName, null, null); 56 try 57 { 58 broker.beginTransaction(); 59 broker.store(project); 60 broker.commitTransaction(); 61 Collection result = getProjectsByName(broker, projectName); 62 assertNotNull(result); 63 assertEquals(1, result.size()); 64 65 broker.beginTransaction(); 66 broker.delete(project); 67 broker.commitTransaction(); 68 result = getProjectsByName(broker, projectName); 69 assertNotNull(result); 70 assertEquals(0, result.size()); 71 } 72 finally 73 { 74 if(broker != null) broker.close(); 75 } 76 } 77 78 public void testCreateDeleteProjectWithSubProjects() throws Exception 79 { 80 String prefix = getStamp() + "testCreateDeleteProjectWithSubProjects"; 81 String projectName = prefix + "_project"; 82 String subName = prefix + "_subproject"; 83 84 Project project = createProject(projectName, null, null); 85 SubProject sub_1 = createSubProject(subName, project, null); 86 SubProject sub_2 = createSubProject(subName, project, null); 87 ArrayList subs = new ArrayList (); 88 subs.add(sub_1); 89 subs.add(sub_2); 90 project.setSubProjects(subs); 91 92 try 93 { 94 broker.beginTransaction(); 95 broker.store(project); 96 broker.commitTransaction(); 97 Collection result = getProjectsByName(broker, projectName); 98 assertNotNull(result); 99 assertEquals(1, result.size()); 100 Collection subProj = getSubProjectsByName(broker, subName); 101 assertNotNull(subProj); 102 assertEquals(2, subProj.size()); 103 104 broker.beginTransaction(); 105 broker.delete(project); 106 broker.commitTransaction(); 107 result = getProjectsByName(broker, projectName); 108 assertNotNull(result); 109 assertEquals(0, result.size()); 110 subProj = getSubProjectsByName(broker, subName); 111 assertNotNull(subProj); 112 assertEquals(0, subProj.size()); 113 } 114 finally 115 { 116 if(broker != null) broker.close(); 117 } 118 } 119 120 121 private Collection getProjectsByName(PersistenceBroker pb, String name) 125 { 126 return getObjectsByField(pb, Project.class, "name", name); 127 } 128 129 private Collection getSubProjectsByName(PersistenceBroker pb, String name) 130 { 131 return getObjectsByField(pb, SubProject.class, "name", name); 132 } 133 134 private Collection getRolesByDescription(PersistenceBroker pb, String description) 135 { 136 return getObjectsByField(pb, Role.class, "description", description); 137 } 138 139 private Collection getAllRoles(PersistenceBroker pb) 140 { 141 return getObjectsByField(pb, Role.class, null, null); 142 } 143 144 private Collection getAllTeamMembers(PersistenceBroker pb) 145 { 146 return getObjectsByField(pb, TeamMember.class, null, null); 147 } 148 149 private Collection getAllProjectEngineers(PersistenceBroker pb) 150 { 151 return getObjectsByField(pb, ProjectEngineer.class, null, null); 152 } 153 154 private Employee getEmployeeByName(PersistenceBroker pb, String name) 155 { 156 return (Employee)getSingleObjectByField(pb, Employee.class, "name", name); 157 } 158 159 private Collection getObjectsByField(PersistenceBroker pb, Class target, String fieldName, String match) 160 { 161 Criteria crit = new Criteria(); 162 if(fieldName != null) crit.addEqualTo(fieldName, match); 163 Query query = QueryFactory.newQuery(target, crit); 164 return pb.getCollectionByQuery(query); 165 } 166 167 private Object getSingleObjectByField(PersistenceBroker pb, Class target, String fieldName, String match) 168 { 169 Criteria crit = new Criteria(); 170 crit.addEqualTo(fieldName, match); 171 Query query = QueryFactory.newQuery(target, crit); 172 return pb.getObjectByQuery(query); 173 } 174 175 private String getStamp() 176 { 177 return PRE + "_" + System.currentTimeMillis(); 178 } 179 180 private Project createProject(String name, ProjectEngineer leader, Collection subProjects) 181 { 182 Project p = new Project(); 183 p.setName(name); 184 p.setLeader(leader); 185 p.setSubProjects(subProjects); 186 return p; 187 } 188 189 private SubProject createSubProject(String name, Project project, TeamMember tutor) 190 { 191 SubProject s = new SubProject(); 192 s.setName(name); 193 s.setProject(project); 194 s.setTutor(tutor); 195 return s; 196 } 197 198 private Employee createEmployee(String name, Collection roles) 199 { 200 Employee e = new Employee(); 201 e.setName(name); 202 e.setRoles(roles); 203 return e; 204 } 205 206 private ProjectEngineer createEngineer(String description, Employee employee, Collection projects) 207 { 208 ProjectEngineer result = new ProjectEngineer(); 209 result.setDescription(description); 210 result.setEmployee(employee); 211 result.setProjects(projects); 212 return result; 213 } 214 215 private TeamMember createMember(String description, Employee employee, Collection subProjects) 216 { 217 TeamMember result = new TeamMember(); 218 result.setDescription(description); 219 result.setEmployee(employee); 220 result.setSubProjects(subProjects); 221 return result; 222 } 223 224 public static class Employee 228 { 229 Integer id; 230 String name; 231 Collection roles = new ArrayList (); 232 233 public Collection getRoles() 234 { 235 return roles; 236 } 237 238 public void setRoles(Collection roles) 239 { 240 this.roles = roles; 241 } 242 243 public void addRole(Role role) 244 { 245 ArrayList list = new ArrayList (getRoles()); 246 list.add(role); 247 setRoles(list); 248 } 249 250 public Integer getId() 251 { 252 return id; 253 } 254 255 public void setId(Integer id) 256 { 257 this.id = id; 258 } 259 260 public String getName() 261 { 262 return name; 263 } 264 265 public void setName(String name) 266 { 267 this.name = name; 268 } 269 } 270 271 public static interface Role 272 { 273 public Employee getEmployee(); 274 275 public void setEmployee(Employee employee); 276 277 public Integer getId(); 278 279 public void setId(Integer id); 280 281 public String getDescription(); 282 283 public void setDescription(String description); 284 } 285 286 public static class AbstractRole implements Role 287 { 288 Integer id; 289 String description; 290 Employee employee; 291 292 public Employee getEmployee() 293 { 294 return employee; 295 } 296 297 public void setEmployee(Employee employee) 298 { 299 this.employee = employee; 300 } 301 302 public Integer getId() 303 { 304 return id; 305 } 306 307 public void setId(Integer id) 308 { 309 this.id = id; 310 } 311 312 public String getDescription() 313 { 314 return description; 315 } 316 317 public void setDescription(String description) 318 { 319 this.description = description; 320 } 321 } 322 323 public static class TeamMember extends AbstractRole 324 { 325 Collection subProjects; 326 327 public Collection getSubProjects() 328 { 329 return subProjects; 330 } 331 332 public void setSubProjects(Collection subProjects) 333 { 334 this.subProjects = subProjects; 335 } 336 } 337 338 public static class ProjectEngineer extends AbstractRole 339 { 340 Collection projects; 341 342 public Collection getProjects() 343 { 344 return projects; 345 } 346 347 public void setProjects(Collection projects) 348 { 349 this.projects = projects; 350 } 351 } 352 353 public static class Project 354 { 355 Integer id; 356 String name; 357 ProjectEngineer leader; 358 Collection subProjects; 359 360 public Integer getId() 361 { 362 return id; 363 } 364 365 public void setId(Integer id) 366 { 367 this.id = id; 368 } 369 370 public ProjectEngineer getLeader() 371 { 372 return leader; 373 } 374 375 public void setLeader(ProjectEngineer leader) 376 { 377 this.leader = leader; 378 } 379 380 public Collection getSubProjects() 381 { 382 return subProjects; 383 } 384 385 public void setSubProjects(Collection subProjects) 386 { 387 this.subProjects = subProjects; 388 } 389 390 public String getName() 391 { 392 return name; 393 } 394 395 public void setName(String name) 396 { 397 this.name = name; 398 } 399 } 400 401 public static class SubProject 402 { 403 Integer id; 404 String name; 405 Project project; 406 TeamMember tutor; 407 408 public Integer getId() 409 { 410 return id; 411 } 412 413 public void setId(Integer id) 414 { 415 this.id = id; 416 } 417 418 public String getName() 419 { 420 return name; 421 } 422 423 public void setName(String name) 424 { 425 this.name = name; 426 } 427 428 public Project getProject() 429 { 430 return project; 431 } 432 433 public void setProject(Project project) 434 { 435 this.project = project; 436 } 437 438 public TeamMember getTutor() 439 { 440 return tutor; 441 } 442 443 public void setTutor(TeamMember tutor) 444 { 445 this.tutor = tutor; 446 } 447 } 448 } 449 | Popular Tags |