01: package org.osbl.orga.model;
02:
03: import junit.framework.TestCase;
04: import org.hibernate.*;
05: import org.hibernate.cfg.Configuration;
06:
07: import java.util.List;
08: import java.util.ArrayList;
09:
10: public class OrgaMappingTest extends TestCase {
11: private Session session;
12: private Transaction transaction;
13:
14: protected void setUp() throws Exception {
15: Configuration configuration = new Configuration();
16: configuration.configure("test-hibernate.cfg.xml");
17: configuration.addResource("Identity.hbm.xml");
18: configuration.addResource("Orga.hbm.xml");
19:
20: SessionFactory sessionFactory = configuration
21: .buildSessionFactory();
22: session = sessionFactory.openSession();
23: transaction = session.beginTransaction();
24: }
25:
26: public void testCRUD() {
27: Organisation organisation = new Organisation();
28:
29: Group rootGroup = new Group();
30: rootGroup.setKey("root");
31: rootGroup.setTree(organisation);
32: rootGroup.setPath("/");
33:
34: Member me = new Member();
35: me.setKey("me");
36: me.setOrganisation(organisation);
37:
38: Member you = new Member();
39: you.setKey("you");
40: you.setOrganisation(organisation);
41:
42: session.save(organisation);
43: session.save(rootGroup);
44: session.save(me);
45: session.save(you);
46:
47: List<Membership> memberships = new ArrayList<Membership>();
48: memberships.add(new Membership(me, rootGroup, "chief"));
49: memberships.add(new Membership(you, rootGroup, "employee"));
50: rootGroup.setMemberships(memberships);
51:
52: List<Relationship> relationships = new ArrayList<Relationship>();
53: relationships.add(new Relationship(me, you, "superior"));
54: me.setRelationships(relationships);
55:
56: session.flush();
57: session.clear();
58:
59: Group loadedGroup = (Group) session.createQuery("from Group")
60: .uniqueResult();
61: assertNotNull(loadedGroup);
62: assertEquals(2, loadedGroup.getMemberships().size());
63: assertEquals("me", loadedGroup.getMemberships().get(0)
64: .getMember().getKey());
65: assertEquals("chief", loadedGroup.getMemberships().get(0)
66: .getRole());
67: assertEquals("you", loadedGroup.getMemberships().get(1)
68: .getMember().getKey());
69: assertEquals("employee", loadedGroup.getMemberships().get(1)
70: .getRole());
71:
72: assertEquals(1, loadedGroup.getMemberships().get(0).getMember()
73: .getRelationships().size());
74: assertEquals("you", loadedGroup.getMemberships().get(0)
75: .getMember().getRelationships().get(0).getPeer()
76: .getKey());
77: assertEquals("superior", loadedGroup.getMemberships().get(0)
78: .getMember().getRelationships().get(0).getRelation());
79: }
80:
81: protected void tearDown() throws Exception {
82: transaction.rollback();
83: session.close();
84: }
85: }
|