001: package org.osbl.persistence.hibernate;
002:
003: import junit.framework.TestCase;
004: import org.hibernate.*;
005: import org.hibernate.cfg.Configuration;
006: import org.osbl.persistence.*;
007:
008: import java.util.ArrayList;
009: import java.util.List;
010:
011: public class HibernateCommandTest extends TestCase {
012:
013: private Session session;
014: private Transaction transaction;
015: private HibernatePersistence commandFactory;
016:
017: protected void setUp() throws Exception {
018: Configuration configuration = new Configuration();
019: configuration.configure("test-hibernate.cfg.xml");
020: configuration.addResource("Identity.hbm.xml");
021:
022: SessionFactory sessionFactory = configuration
023: .buildSessionFactory();
024: session = sessionFactory.openSession();
025: transaction = session.beginTransaction();
026: commandFactory = new HibernatePersistence();
027: commandFactory.setSessionFactory(new PseudoSessionFactory(
028: (org.hibernate.classic.Session) session));
029: commandFactory.registerCommand("load",
030: HibernateLoadCommand.class);
031: commandFactory.registerCommand("query",
032: HibernateQueryCommand.class);
033: commandFactory.registerCommand("simpleQuery",
034: HibernateSimpleQueryCommand.class);
035: commandFactory.registerCommand("joinedQuery",
036: HibernateJoinedQueryCommand.class);
037: }
038:
039: public void testCRUD() {
040: Identity identity = new Identity();
041: identity.setKey("HE");
042:
043: User user = new User(identity);
044: user.setAccount("hengels");
045: user.setPassword("hengels".toCharArray());
046:
047: Person person = new Person(identity);
048: person.setFirstName("Holger");
049: person.setLastName("Engels");
050:
051: Address address = new Address();
052: address.setStreet("Nowhere");
053: address.setPostcode("777");
054: address.setCity("California");
055:
056: List<Address> addresses = new ArrayList<Address>();
057: addresses.add(address);
058: person.setAddresses(addresses);
059:
060: System.out.println("identity = " + identity);
061: System.out.println("user = " + user);
062: System.out.println("person = " + person);
063:
064: session.save(identity);
065: session.save(user);
066: session.save(person);
067:
068: session.flush();
069: session.clear();
070:
071: LoadCommand<Person> loadCommand = (LoadCommand<Person>) commandFactory
072: .createCommand("load");
073: loadCommand.setType(Person.class);
074: loadCommand.setId(person.getId());
075: Person loadedPerson = loadCommand.execute();
076: assertNotNull(loadedPerson);
077:
078: QueryCommand<Person> queryCommand = (QueryCommand<Person>) commandFactory
079: .createCommand("query");
080: queryCommand.setType(Person.class);
081: List<Person> persons = queryCommand.execute();
082: assertEquals(1, persons.size());
083:
084: SimpleQueryCommand<Person> simpleQueryCommand = (SimpleQueryCommand<Person>) commandFactory
085: .createCommand("simpleQuery");
086: simpleQueryCommand.setType(Person.class);
087: simpleQueryCommand.addFilter("firstName", Operator.EQUAL,
088: "Holger");
089: simpleQueryCommand.addSort("lastName", Sort.ASCENDING);
090: persons = simpleQueryCommand.execute();
091: assertEquals(1, persons.size());
092:
093: JoinedQueryCommand<Person> joinedQueryCommand = (JoinedQueryCommand<Person>) commandFactory
094: .createCommand("joinedQuery");
095: joinedQueryCommand.setType(Person.class);
096: joinedQueryCommand.addJoin("address", "a");
097: joinedQueryCommand.addFilter("a.street", Operator.EQUAL,
098: "Nowhere");
099: persons = joinedQueryCommand.execute();
100: assertEquals(1, persons.size());
101: }
102:
103: protected void tearDown() throws Exception {
104: transaction.rollback();
105: session.close();
106: }
107: }
|