001: package org.apache.ojb.broker;
002:
003: import junit.framework.TestCase;
004:
005: import java.util.Collection;
006: import java.util.ArrayList;
007:
008: import org.apache.ojb.broker.query.Criteria;
009: import org.apache.ojb.broker.query.Query;
010: import org.apache.ojb.broker.query.QueryFactory;
011:
012: /**
013: * Test case for checking the management of references.
014: *
015: * TODO: Implement useful test cases
016: * @author <a href="mailto:armin@codeAuLait.de">Armin Waibel</a>
017: * @version $Id: ComplexReferenceTest.java,v 1.1 2003/12/09 13:46:46 arminw Exp $
018: */
019: public class ComplexReferenceTest extends TestCase {
020: /**
021: * simple prefix, used un tests
022: */
023: private static String PRE = "ComplexReferenceTest";
024: private PersistenceBroker broker;
025:
026: public static void main(String[] args) {
027: String[] arr = { ComplexReferenceTest.class.getName() };
028: junit.textui.TestRunner.main(arr);
029: }
030:
031: public void setUp() throws PBFactoryException {
032: // called for each test method invocation
033: broker = PersistenceBrokerFactory.defaultPersistenceBroker();
034: }
035:
036: public void tearDown() {
037: try {
038: if (broker != null)
039: broker.close();
040: } catch (PersistenceBrokerException e) {
041: }
042: }
043:
044: public void testCreateDeleteProject() throws Exception {
045: String prefix = getStamp() + "testCreateDeleteProject";
046: String projectName = prefix + "project";
047:
048: Project project = createProject(projectName, null, null);
049: try {
050: broker.beginTransaction();
051: broker.store(project);
052: broker.commitTransaction();
053: Collection result = getProjectsByName(broker, projectName);
054: assertNotNull(result);
055: assertEquals(1, result.size());
056:
057: broker.beginTransaction();
058: broker.delete(project);
059: broker.commitTransaction();
060: result = getProjectsByName(broker, projectName);
061: assertNotNull(result);
062: assertEquals(0, result.size());
063: } finally {
064: if (broker != null)
065: broker.close();
066: }
067: }
068:
069: public void testCreateDeleteProjectWithSubProjects()
070: throws Exception {
071: String prefix = getStamp()
072: + "testCreateDeleteProjectWithSubProjects";
073: String projectName = prefix + "_project";
074: String subName = prefix + "_subproject";
075:
076: Project project = createProject(projectName, null, null);
077: SubProject sub_1 = createSubProject(subName, project, null);
078: SubProject sub_2 = createSubProject(subName, project, null);
079: ArrayList subs = new ArrayList();
080: subs.add(sub_1);
081: subs.add(sub_2);
082: project.setSubProjects(subs);
083:
084: try {
085: broker.beginTransaction();
086: broker.store(project);
087: broker.commitTransaction();
088: Collection result = getProjectsByName(broker, projectName);
089: assertNotNull(result);
090: assertEquals(1, result.size());
091: Collection subProj = getSubProjectsByName(broker, subName);
092: assertNotNull(subProj);
093: assertEquals(2, subProj.size());
094:
095: broker.beginTransaction();
096: broker.delete(project);
097: broker.commitTransaction();
098: result = getProjectsByName(broker, projectName);
099: assertNotNull(result);
100: assertEquals(0, result.size());
101: subProj = getSubProjectsByName(broker, subName);
102: assertNotNull(subProj);
103: assertEquals(0, subProj.size());
104: } finally {
105: if (broker != null)
106: broker.close();
107: }
108: }
109:
110: //=====================================================================
111: // Helper methods
112: //=====================================================================
113: private Collection getProjectsByName(PersistenceBroker pb,
114: String name) {
115: return getObjectsByField(pb, Project.class, "name", name);
116: }
117:
118: private Collection getSubProjectsByName(PersistenceBroker pb,
119: String name) {
120: return getObjectsByField(pb, SubProject.class, "name", name);
121: }
122:
123: private Collection getRolesByDescription(PersistenceBroker pb,
124: String description) {
125: return getObjectsByField(pb, Role.class, "description",
126: description);
127: }
128:
129: private Collection getAllRoles(PersistenceBroker pb) {
130: return getObjectsByField(pb, Role.class, null, null);
131: }
132:
133: private Collection getAllTeamMembers(PersistenceBroker pb) {
134: return getObjectsByField(pb, TeamMember.class, null, null);
135: }
136:
137: private Collection getAllProjectEngineers(PersistenceBroker pb) {
138: return getObjectsByField(pb, ProjectEngineer.class, null, null);
139: }
140:
141: private Employee getEmployeeByName(PersistenceBroker pb, String name) {
142: return (Employee) getSingleObjectByField(pb, Employee.class,
143: "name", name);
144: }
145:
146: private Collection getObjectsByField(PersistenceBroker pb,
147: Class target, String fieldName, String match) {
148: Criteria crit = new Criteria();
149: if (fieldName != null)
150: crit.addEqualTo(fieldName, match);
151: Query query = QueryFactory.newQuery(target, crit);
152: return pb.getCollectionByQuery(query);
153: }
154:
155: private Object getSingleObjectByField(PersistenceBroker pb,
156: Class target, String fieldName, String match) {
157: Criteria crit = new Criteria();
158: crit.addEqualTo(fieldName, match);
159: Query query = QueryFactory.newQuery(target, crit);
160: return pb.getObjectByQuery(query);
161: }
162:
163: private String getStamp() {
164: return PRE + "_" + System.currentTimeMillis();
165: }
166:
167: private Project createProject(String name, ProjectEngineer leader,
168: Collection subProjects) {
169: Project p = new Project();
170: p.setName(name);
171: p.setLeader(leader);
172: p.setSubProjects(subProjects);
173: return p;
174: }
175:
176: private SubProject createSubProject(String name, Project project,
177: TeamMember tutor) {
178: SubProject s = new SubProject();
179: s.setName(name);
180: s.setProject(project);
181: s.setTutor(tutor);
182: return s;
183: }
184:
185: private Employee createEmployee(String name, Collection roles) {
186: Employee e = new Employee();
187: e.setName(name);
188: e.setRoles(roles);
189: return e;
190: }
191:
192: private ProjectEngineer createEngineer(String description,
193: Employee employee, Collection projects) {
194: ProjectEngineer result = new ProjectEngineer();
195: result.setDescription(description);
196: result.setEmployee(employee);
197: result.setProjects(projects);
198: return result;
199: }
200:
201: private TeamMember createMember(String description,
202: Employee employee, Collection subProjects) {
203: TeamMember result = new TeamMember();
204: result.setDescription(description);
205: result.setEmployee(employee);
206: result.setSubProjects(subProjects);
207: return result;
208: }
209:
210: //=====================================================================
211: // used persistence capable objects
212: //=====================================================================
213: public static class Employee {
214: Integer id;
215: String name;
216: Collection roles = new ArrayList();
217:
218: public Collection getRoles() {
219: return roles;
220: }
221:
222: public void setRoles(Collection roles) {
223: this .roles = roles;
224: }
225:
226: public void addRole(Role role) {
227: ArrayList list = new ArrayList(getRoles());
228: list.add(role);
229: setRoles(list);
230: }
231:
232: public Integer getId() {
233: return id;
234: }
235:
236: public void setId(Integer id) {
237: this .id = id;
238: }
239:
240: public String getName() {
241: return name;
242: }
243:
244: public void setName(String name) {
245: this .name = name;
246: }
247: }
248:
249: public static interface Role {
250: public Employee getEmployee();
251:
252: public void setEmployee(Employee employee);
253:
254: public Integer getId();
255:
256: public void setId(Integer id);
257:
258: public String getDescription();
259:
260: public void setDescription(String description);
261: }
262:
263: public static class AbstractRole implements Role {
264: Integer id;
265: String description;
266: Employee employee;
267:
268: public Employee getEmployee() {
269: return employee;
270: }
271:
272: public void setEmployee(Employee employee) {
273: this .employee = employee;
274: }
275:
276: public Integer getId() {
277: return id;
278: }
279:
280: public void setId(Integer id) {
281: this .id = id;
282: }
283:
284: public String getDescription() {
285: return description;
286: }
287:
288: public void setDescription(String description) {
289: this .description = description;
290: }
291: }
292:
293: public static class TeamMember extends AbstractRole {
294: Collection subProjects;
295:
296: public Collection getSubProjects() {
297: return subProjects;
298: }
299:
300: public void setSubProjects(Collection subProjects) {
301: this .subProjects = subProjects;
302: }
303: }
304:
305: public static class ProjectEngineer extends AbstractRole {
306: Collection projects;
307:
308: public Collection getProjects() {
309: return projects;
310: }
311:
312: public void setProjects(Collection projects) {
313: this .projects = projects;
314: }
315: }
316:
317: public static class Project {
318: Integer id;
319: String name;
320: ProjectEngineer leader;
321: Collection subProjects;
322:
323: public Integer getId() {
324: return id;
325: }
326:
327: public void setId(Integer id) {
328: this .id = id;
329: }
330:
331: public ProjectEngineer getLeader() {
332: return leader;
333: }
334:
335: public void setLeader(ProjectEngineer leader) {
336: this .leader = leader;
337: }
338:
339: public Collection getSubProjects() {
340: return subProjects;
341: }
342:
343: public void setSubProjects(Collection subProjects) {
344: this .subProjects = subProjects;
345: }
346:
347: public String getName() {
348: return name;
349: }
350:
351: public void setName(String name) {
352: this .name = name;
353: }
354: }
355:
356: public static class SubProject {
357: Integer id;
358: String name;
359: Project project;
360: TeamMember tutor;
361:
362: public Integer getId() {
363: return id;
364: }
365:
366: public void setId(Integer id) {
367: this .id = id;
368: }
369:
370: public String getName() {
371: return name;
372: }
373:
374: public void setName(String name) {
375: this .name = name;
376: }
377:
378: public Project getProject() {
379: return project;
380: }
381:
382: public void setProject(Project project) {
383: this .project = project;
384: }
385:
386: public TeamMember getTutor() {
387: return tutor;
388: }
389:
390: public void setTutor(TeamMember tutor) {
391: this.tutor = tutor;
392: }
393: }
394: }
|