01: package org.osbl.identity.model;
02:
03: import junit.framework.TestCase;
04:
05: import org.hibernate.*;
06: import org.hibernate.cfg.Configuration;
07: import org.apache.commons.lang.builder.EqualsBuilder;
08:
09: public class IdentityMappingTest extends TestCase {
10:
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:
19: SessionFactory sessionFactory = configuration
20: .buildSessionFactory();
21: session = sessionFactory.openSession();
22: transaction = session.beginTransaction();
23: }
24:
25: public void testCRUD() {
26: Identity identity = new Identity();
27: identity.setKey("HE");
28:
29: User user = new User(identity);
30: user.setAccount("hengels");
31: user.setPassword("hengels".toCharArray());
32:
33: Person person = new Person(identity);
34: person.setFirstName("Holger");
35: person.setLastName("Engels");
36:
37: System.out.println("identity = " + identity);
38: System.out.println("user = " + user);
39: System.out.println("person = " + person);
40:
41: session.save(identity);
42: session.save(user);
43: session.save(person);
44:
45: session.flush();
46: session.clear();
47:
48: User loadedUser = (User) session.createQuery("from User")
49: .uniqueResult();
50: Person loadedPerson = (Person) session.createQuery(
51: "from Person").uniqueResult();
52: assertTrue(EqualsBuilder.reflectionEquals(user, loadedUser));
53: assertTrue(EqualsBuilder.reflectionEquals(person, loadedPerson));
54: assertSame(loadedUser.getGeneral(), loadedPerson.getGeneral());
55:
56: session.clear();
57:
58: loadedUser = (User) session
59: .createQuery(
60: "select u from User u, Person p where u.id = p.id and p.firstName = 'Holger'")
61: .uniqueResult();
62: assertNotNull(loadedUser);
63: }
64:
65: protected void tearDown() throws Exception {
66: transaction.rollback();
67: session.close();
68: }
69: }
|