KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ojb > broker > ComplexReferenceTest


1 package org.apache.ojb.broker;
2
3 import junit.framework.TestCase;
4
5 import java.util.Collection JavaDoc;
6 import java.util.ArrayList JavaDoc;
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 /**
13  * Test case for checking the management of references.
14  *
15  * TODO: Implement useful test cases
16  * @author <a HREF="mailto:armin@codeAuLait.de">Armin Waibel</a>
17  * @version $Id: ComplexReferenceTest.java,v 1.1 2003/12/09 13:46:46 arminw Exp $
18  */

19 public class ComplexReferenceTest extends TestCase
20 {
21     /**
22      * simple prefix, used un tests
23      */

24     private static String JavaDoc PRE = "ComplexReferenceTest";
25     private PersistenceBroker broker;
26
27     public static void main(String JavaDoc[] args)
28     {
29         String JavaDoc[] arr = {ComplexReferenceTest.class.getName()};
30         junit.textui.TestRunner.main(arr);
31     }
32
33     public void setUp() throws PBFactoryException
34     {
35         // called for each test method invocation
36
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 JavaDoc
51     {
52         String JavaDoc prefix = getStamp() + "testCreateDeleteProject";
53         String JavaDoc 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 JavaDoc 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 JavaDoc
79     {
80         String JavaDoc prefix = getStamp() + "testCreateDeleteProjectWithSubProjects";
81         String JavaDoc projectName = prefix + "_project";
82         String JavaDoc 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 JavaDoc subs = new ArrayList JavaDoc();
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 JavaDoc result = getProjectsByName(broker, projectName);
98             assertNotNull(result);
99             assertEquals(1, result.size());
100             Collection JavaDoc 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     //=====================================================================
122
// Helper methods
123
//=====================================================================
124
private Collection JavaDoc getProjectsByName(PersistenceBroker pb, String JavaDoc name)
125     {
126         return getObjectsByField(pb, Project.class, "name", name);
127     }
128
129     private Collection JavaDoc getSubProjectsByName(PersistenceBroker pb, String JavaDoc name)
130     {
131         return getObjectsByField(pb, SubProject.class, "name", name);
132     }
133
134     private Collection JavaDoc getRolesByDescription(PersistenceBroker pb, String JavaDoc description)
135     {
136         return getObjectsByField(pb, Role.class, "description", description);
137     }
138
139     private Collection JavaDoc getAllRoles(PersistenceBroker pb)
140     {
141         return getObjectsByField(pb, Role.class, null, null);
142     }
143
144     private Collection JavaDoc getAllTeamMembers(PersistenceBroker pb)
145     {
146         return getObjectsByField(pb, TeamMember.class, null, null);
147     }
148
149     private Collection JavaDoc getAllProjectEngineers(PersistenceBroker pb)
150     {
151         return getObjectsByField(pb, ProjectEngineer.class, null, null);
152     }
153
154     private Employee getEmployeeByName(PersistenceBroker pb, String JavaDoc name)
155     {
156         return (Employee)getSingleObjectByField(pb, Employee.class, "name", name);
157     }
158
159     private Collection JavaDoc getObjectsByField(PersistenceBroker pb, Class JavaDoc target, String JavaDoc fieldName, String JavaDoc 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 JavaDoc getSingleObjectByField(PersistenceBroker pb, Class JavaDoc target, String JavaDoc fieldName, String JavaDoc 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 JavaDoc getStamp()
176     {
177         return PRE + "_" + System.currentTimeMillis();
178     }
179
180     private Project createProject(String JavaDoc name, ProjectEngineer leader, Collection JavaDoc 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 JavaDoc 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 JavaDoc name, Collection JavaDoc 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 JavaDoc description, Employee employee, Collection JavaDoc 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 JavaDoc description, Employee employee, Collection JavaDoc 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     //=====================================================================
225
// used persistence capable objects
226
//=====================================================================
227
public static class Employee
228     {
229         Integer JavaDoc id;
230         String JavaDoc name;
231         Collection JavaDoc roles = new ArrayList JavaDoc();
232
233         public Collection JavaDoc getRoles()
234         {
235             return roles;
236         }
237
238         public void setRoles(Collection JavaDoc roles)
239         {
240             this.roles = roles;
241         }
242
243         public void addRole(Role role)
244         {
245             ArrayList JavaDoc list = new ArrayList JavaDoc(getRoles());
246             list.add(role);
247             setRoles(list);
248         }
249
250         public Integer JavaDoc getId()
251         {
252             return id;
253         }
254
255         public void setId(Integer JavaDoc id)
256         {
257             this.id = id;
258         }
259
260         public String JavaDoc getName()
261         {
262             return name;
263         }
264
265         public void setName(String JavaDoc 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 JavaDoc getId();
278
279         public void setId(Integer JavaDoc id);
280
281         public String JavaDoc getDescription();
282
283         public void setDescription(String JavaDoc description);
284     }
285
286     public static class AbstractRole implements Role
287     {
288         Integer JavaDoc id;
289         String JavaDoc 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 JavaDoc getId()
303         {
304             return id;
305         }
306
307         public void setId(Integer JavaDoc id)
308         {
309             this.id = id;
310         }
311
312         public String JavaDoc getDescription()
313         {
314             return description;
315         }
316
317         public void setDescription(String JavaDoc description)
318         {
319             this.description = description;
320         }
321     }
322
323     public static class TeamMember extends AbstractRole
324     {
325         Collection JavaDoc subProjects;
326
327         public Collection JavaDoc getSubProjects()
328         {
329             return subProjects;
330         }
331
332         public void setSubProjects(Collection JavaDoc subProjects)
333         {
334             this.subProjects = subProjects;
335         }
336     }
337
338     public static class ProjectEngineer extends AbstractRole
339     {
340         Collection JavaDoc projects;
341
342         public Collection JavaDoc getProjects()
343         {
344             return projects;
345         }
346
347         public void setProjects(Collection JavaDoc projects)
348         {
349             this.projects = projects;
350         }
351     }
352
353     public static class Project
354     {
355         Integer JavaDoc id;
356         String JavaDoc name;
357         ProjectEngineer leader;
358         Collection JavaDoc subProjects;
359
360         public Integer JavaDoc getId()
361         {
362             return id;
363         }
364
365         public void setId(Integer JavaDoc 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 JavaDoc getSubProjects()
381         {
382             return subProjects;
383         }
384
385         public void setSubProjects(Collection JavaDoc subProjects)
386         {
387             this.subProjects = subProjects;
388         }
389
390         public String JavaDoc getName()
391         {
392             return name;
393         }
394
395         public void setName(String JavaDoc name)
396         {
397             this.name = name;
398         }
399     }
400
401     public static class SubProject
402     {
403         Integer JavaDoc id;
404         String JavaDoc name;
405         Project project;
406         TeamMember tutor;
407
408         public Integer JavaDoc getId()
409         {
410             return id;
411         }
412
413         public void setId(Integer JavaDoc id)
414         {
415             this.id = id;
416         }
417
418         public String JavaDoc getName()
419         {
420             return name;
421         }
422
423         public void setName(String JavaDoc 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