001: package org.osbl.inventory.model;
002:
003: import junit.framework.TestCase;
004: import org.hibernate.*;
005: import org.hibernate.cfg.Configuration;
006: import org.osbl.inventory.logic.DefaultInventoryLogic;
007: import org.osbl.identity.model.*;
008:
009: import java.math.BigDecimal;
010:
011: public class InventoryMappingTest extends TestCase {
012: private Session session;
013: private Transaction transaction;
014: private DefaultInventoryLogic inventoryLogic;
015:
016: protected void setUp() throws Exception {
017: Configuration configuration = new Configuration();
018: configuration.configure("test-hibernate.cfg.xml");
019: configuration.addResource("Identity.hbm.xml");
020: configuration.addResource("Inventory.hbm.xml");
021:
022: SessionFactory sessionFactory = configuration
023: .buildSessionFactory();
024: session = sessionFactory.openSession();
025: transaction = session.beginTransaction();
026: inventoryLogic = new DefaultInventoryLogic();
027: }
028:
029: public void testCRUD() {
030: CostTypeHierarchy costTypeHierarchy = new CostTypeHierarchy();
031:
032: CostType rootCostType = new CostType();
033: rootCostType.setKey("root");
034:
035: CostType childCostType = new CostType();
036: childCostType.setKey("child");
037:
038: session.save(costTypeHierarchy);
039:
040: session.save(rootCostType);
041: inventoryLogic.connectNodeToTree(costTypeHierarchy,
042: rootCostType);
043:
044: session.save(childCostType);
045: inventoryLogic.connectNodeToNode(rootCostType, childCostType);
046:
047: Location location = new Location();
048: location.setName("daheim");
049:
050: session.save(location);
051:
052: // supplier / person
053: Identity supplierIdentity = new Identity();
054: supplierIdentity.setKey("elh");
055:
056: Person person = new Person(supplierIdentity);
057: person.setFirstName("Elmar");
058: person.setLastName("Hirt");
059:
060: Supplier supplier = new Supplier(supplierIdentity);
061: supplier.setName("elh");
062:
063: // eployee / user
064: Identity employeeIdentity = new Identity();
065: employeeIdentity.setKey("he");
066:
067: User user = new User(employeeIdentity);
068: user.setAccount("hengels");
069: user.setPassword("hengels".toCharArray());
070:
071: Employee employee = new Employee(employeeIdentity);
072: employee.setLocation(location);
073:
074: session.save(supplierIdentity);
075: session.save(person);
076: session.save(supplier);
077:
078: session.save(employeeIdentity);
079: session.save(user);
080: session.save(employee);
081:
082: Inventory inventory = new Inventory();
083: inventory.setKey("key");
084: inventory.setCosts(new BigDecimal("200"));
085: inventory.setCostType(childCostType);
086: inventory.setEmployee(employee.getGeneral());
087: inventory.setSupplier(supplier.getGeneral());
088: inventory.setLocation(location);
089: inventory.setState(State.IN_USE);
090:
091: session.save(inventory);
092:
093: session.flush();
094: session.clear();
095:
096: CostType loadedCosttype = (CostType) session.createQuery(
097: "from CostType where key = 'root'").uniqueResult();
098: assertNotNull(loadedCosttype);
099:
100: loadedCosttype = (CostType) session.createQuery(
101: "from CostType where key = 'child'").uniqueResult();
102: assertNotNull(loadedCosttype);
103:
104: Inventory loadedInventory = (Inventory) session.createQuery(
105: "from Inventory where key = 'key'").uniqueResult();
106: assertNotNull(loadedInventory);
107: assertNotNull(loadedInventory.getEmployee());
108: assertNotNull(loadedInventory.getLocation());
109: }
110:
111: protected void tearDown() throws Exception {
112: transaction.rollback();
113: session.close();
114: }
115: }
|