/home/mario/oci/jnb/dataaccess/source/test/java/com/ociweb/domain/FunctionalBusinessTest.java

1    /** 
2     * This software program, Simple Data Access Layer (SDAL), is copyrighted by Object 
3     * Computing inc of St. Louis MO USA. It is provided under the open-source model 
4     * and is free of license fees. You are free to modify this code for your own use 
5     * but you may not claim copyright. 
6     * 
7     * Since SDAL is open source and free of licensing fees, you are free to use, 
8     * modify, and distribute the source code, as long as you include this copyright 
9     * statement. 
10    * 
11    * In particular, you can use SDAL to build proprietary software and are under no 
12    * obligation to redistribute any of your source code that is built using SDAL. 
13    * Note, however, that you may not do anything to the SDAL code, such as 
14    * copyrighting it yourself or claiming authorship of the SDAL code, that will 
15    * prevent SDAL from being distributed freely using an open source development 
16    * model. 
17    * 
18    * Warranty 
19    * LICENSED PRODUCT, SDAL, IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 
20    * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE, 
21    * NONINFRINGEMENT, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 
22    * 
23    * Support 
24    * LICENSED PRODUCT, SDAL, IS PROVIDED WITH NO SUPPORT AND WITHOUT ANY OBLIGATION ON THE 
25    * PART OF OCI OR ANY OF ITS SUBSIDIARIES OR AFFILIATES TO ASSIST IN ITS USE, 
26    * CORRECTION, MODIFICATION OR ENHANCEMENT. 
27    * 
28    * Support may be available from OCI to users who have agreed to a support 
29    * contract. 
30    * 
31    * Liability 
32    * OCI OR ANY OF ITS SUBSIDIARIES OR AFFILIATES SHALL HAVE NO LIABILITY WITH 
33    * RESPECT TO THE INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY 
34    * LICENSED PRODUCT OR ANY PART THEREOF. 
35    * 
36    * IN NO EVENT WILL OCI OR ANY OF ITS SUBSIDIARIES OR AFFILIATES BE LIABLE FOR ANY 
37    * LOST REVENUE OR PROFITS OR OTHER SPECIAL, INDIRECT AND CONSEQUENTIAL DAMAGES, 
38    * EVEN IF OCI HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. 
39    * 
40    * Copyright OCI. St. Louis MO USA, 2004 
41    * 
42    */ 
43   package com.ociweb.domain; 
44    
45   import com.ociweb.bean.Product; 
46   import com.ociweb.bean.Customer; 
47   import com.ociweb.bean.Order; 
48   import com.ociweb.bean.Orderitem; 
49   import com.ociweb.service.GlobalSessionFactory; 
50   import com.ociweb.service.ThreadSessionHolder; 
51   import com.ociweb.service.GlobalServiceLocator; 
52   import com.ociweb.service.CriteriaQuery; 
53   import com.ociweb.service.GlobalQueryFactory; 
54   import com.ociweb.service.Expression; 
55   import com.ociweb.service.LookupException; 
56    
57   import junit.framework.TestCase; 
58    
59   import java.util.Collection; 
60   import java.util.Iterator; 
61   import java.util.Set; 
62   import java.util.HashSet; 
63    
64   public class FunctionalBusinessTest extends TestCase { 
65       private static final String PIANO_STRINGS = "Piano Strings"; 
66       private static final String CUSTOMER_NAME = "Keith Jarrett"; 
67       private static final String LOZENGES = "Lozenges"; 
68    
69       protected void setUp() throws Exception { 
70           net.sf.hibernate.Session hibSession = GlobalSessionFactory.get().newSession(); 
71           ThreadSessionHolder.set(hibSession); 
72       } 
73    
74       protected void tearDown() throws Exception { 
75           ThreadSessionHolder.set(null); 
76       } 
77    
78       public void testProduct() throws Exception { 
79           addProduct(PIANO_STRINGS); 
80           removeAllProducts(); 
81       } 
82    
83       public void testCustomer() throws Exception { 
84           addCustomer(); 
85           removeAllCustomers(); 
86       } 
87    
88       public void testOrderAndOrderitems() throws Exception { 
89           addOrderAndOrderItems(); 
90           removeAllOrders(); 
91           removeAllCustomers(); 
92           removeAllProducts(); 
93       } 
94    
95       private void addProduct(String productName) throws Exception { 
96           Product product = new Product(); 
97           product.setName(productName); 
98           product.setCost(10.0); 
99           ProductMgr mgr = (ProductMgr) GlobalServiceLocator.get().getDomainObjectManager(ProductMgr.class); 
100          mgr.add(product); 
101          Product addedProduct = findProductByName(productName); 
102          assertNotNull(addedProduct); 
103          assertEquals(productName, addedProduct.getName()); 
104      } 
105   
106      private Product findProductByName(String name) throws Exception { 
107          ProductMgr mgr = (ProductMgr)GlobalServiceLocator.get().getDomainObjectManager(ProductMgr.class); 
108          //Find by named query being used here 
109          Collection products = mgr.findProductByName(name); 
110          return (Product)products.iterator().next(); 
111      } 
112   
113      private void removeAllProducts() throws Exception { 
114          ProductMgr mgr = (ProductMgr) GlobalServiceLocator.get().getDomainObjectManager(ProductMgr.class); 
115          Collection products = mgr.findAll(); 
116          if (products.size() > 0) { 
117              for (Iterator iter = products.iterator(); iter.hasNext();) { 
118                  Product product = (Product) iter.next(); 
119                  mgr.remove(product); 
120              } 
121              products = mgr.findAll(); 
122              assertEquals(0, products.size()); 
123          } 
124      } 
125   
126      private void addCustomer() throws Exception { 
127          Customer customer = new Customer(); 
128          customer.setName(CUSTOMER_NAME); 
129          CustomerMgr mgr = (CustomerMgr) GlobalServiceLocator.get().getDomainObjectManager(CustomerMgr.class); 
130          mgr.add(customer); 
131          Customer addedCustomer = findCustomerByName(CUSTOMER_NAME); 
132          assertNotNull(addedCustomer); 
133          assertEquals(CUSTOMER_NAME, addedCustomer.getName()); 
134      } 
135   
136      private Customer findCustomerByName(String name) throws LookupException { 
137          //Example of a CriterialQuery 
138          CriteriaQuery query = GlobalQueryFactory.get().newCriteriaQuery(Customer.class); 
139          query.add(Expression.eq("name", name)); 
140          return (Customer) query.execute().iterator().next(); 
141      } 
142   
143      private void removeAllCustomers() throws Exception { 
144          CustomerMgr mgr = (CustomerMgr) GlobalServiceLocator.get().getDomainObjectManager(CustomerMgr.class); 
145          Collection customers = mgr.findAll(); 
146          if (customers.size() > 0) { 
147              for (Iterator iter = customers.iterator(); iter.hasNext();) { 
148                  Customer customer = (Customer) iter.next(); 
149                  mgr.remove(customer); 
150              } 
151              customers = mgr.findAll(); 
152              assertEquals(0, customers.size()); 
153          } 
154      } 
155   
156      private void addOrderAndOrderItems() throws Exception { 
157          addCustomer(); 
158          addProduct(PIANO_STRINGS); 
159          addProduct(LOZENGES); 
160   
161          Customer customer = findCustomerByName(CUSTOMER_NAME); 
162   
163          Order order = new Order(); 
164          order.setCustomer(customer); 
165   
166          Orderitem item1 = new Orderitem(); 
167          item1.setOrder(order); 
168          item1.setProduct(findProductByName(PIANO_STRINGS)); 
169          item1.setQuantity(88); 
170   
171          Orderitem item2 = new Orderitem(); 
172          item2.setOrder(order); 
173          item2.setProduct(findProductByName(LOZENGES)); 
174          item2.setQuantity(12); 
175   
176          Set orderItems = new HashSet(); 
177          orderItems.add(item1); 
178          orderItems.add(item2); 
179   
180          order.setOrderitems(orderItems); 
181   
182          OrderMgr orderMgr = (OrderMgr) GlobalServiceLocator.get().getDomainObjectManager(OrderMgr.class); 
183          orderMgr.add(order); 
184   
185          Collection orders = orderMgr.findAll(); 
186          assertEquals(1, orders.size()); 
187          Order addedOrder = (Order) orders.iterator().next(); 
188          assertEquals(2, addedOrder.getOrderitems().size()); 
189      } 
190   
191      private void removeAllOrders() throws Exception { 
192          OrderMgr mgr = (OrderMgr)GlobalServiceLocator.get().getDomainObjectManager(OrderMgr.class); 
193          Collection orders = mgr.findAll(); 
194          if (orders.size() > 0) { 
195              for (Iterator iter = orders.iterator(); iter.hasNext();) { 
196                  Order order = (Order)iter.next(); 
197                  mgr.remove(order); 
198              } 
199              orders = mgr.findAll(); 
200              assertEquals(0, orders.size()); 
201          } 
202      } 
203  } 
204