/home/mario/oci/jnb/dataaccess/source/test/java/com/ociweb/service/ServiceLocatorTest.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.service; 
44    
45   import junit.framework.TestCase; 
46   import net.sf.hibernate.MockSession; 
47   import net.sf.hibernate.MockTransaction; 
48   import net.sf.hibernate.HibernateException; 
49   import com.ociweb.domain.FooMgr; 
50   import com.ociweb.bean.Foo; 
51   import com.ociweb.service.ServiceLocatorException; 
52   import com.ociweb.service.ServiceLocatorImpl; 
53   import com.ociweb.service.ThreadSessionHolder; 
54    
55   import java.util.Collection; 
56   import java.util.ArrayList; 
57   import java.util.List; 
58    
59   public class ServiceLocatorTest extends TestCase { 
60       private MockSession session; 
61       private ServiceLocatorImpl locator; 
62       private MockTransaction transaction; 
63       private static final String HEYNOW = "heynow"; 
64    
65       protected void setUp() throws Exception { 
66           session = new MockSession(); 
67           transaction = new MockTransaction(); 
68           session.beginTransactionReturn = transaction; 
69           session.getNamedQueryReturn = new net.sf.hibernate.MockQuery(); 
70           ThreadSessionHolder.set(session); 
71    
72           locator = new ServiceLocatorImpl(); 
73       } 
74    
75       protected void tearDown() throws Exception { 
76           ThreadSessionHolder.set(null); 
77       } 
78    
79       public void testGetManagerSuccess() throws Exception { 
80           GoodMgr goodMgr = (GoodMgr) locator.getDomainObjectManager(GoodMgr.class); 
81           assertNotNull(goodMgr); 
82       } 
83    
84       public void testGetManagerFailureNotAnInterface() { 
85           try { 
86               locator.getDomainObjectManager(Object.class); 
87               fail("ServiceLocator should not allow non-interfaces to be passed in"); 
88           } catch (ServiceLocatorException sle) { 
89           } 
90       } 
91    
92       public void testGetManagerFailureDoesNotImplementCRUDlikeInterface() { 
93           try { 
94               locator.getDomainObjectManager(FooInterface.class); 
95               fail("ServiceLocator should not allow interfaces with non-crud-like APIs to be passed in"); 
96           } catch (ServiceLocatorException sle) { 
97           } 
98       } 
99    
100      public void testTargetFindByPrimaryKeySuccess() throws Exception { 
101          Object returnValue = new Object(); 
102          session.load2Return = returnValue; 
103          GoodMgr clientMgr = (GoodMgr) locator.getDomainObjectManager(GoodMgr.class); 
104          assertFalse(session.beginTransactionCalled); 
105          assertFalse(session.saveCalled); 
106          Long id = new Long(1L); 
107          Object obj = clientMgr.findByPrimaryKey(id); 
108          assertEquals(returnValue, obj); 
109          assertFalse(session.beginTransactionCalled); 
110          assertTrue(session.load2Called); 
111          assertEquals(Object.class, session.load2TheClass); 
112          assertEquals(id, session.load2Id); 
113      } 
114   
115      public void testTargetFindByPrimaryKeyFailure() throws ServiceLocatorException { 
116          session.load2Exception = new HibernateException("Foo"); 
117          GoodMgr clientMgr = (GoodMgr) locator.getDomainObjectManager(GoodMgr.class); 
118          assertFalse(session.beginTransactionCalled); 
119          assertFalse(session.saveCalled); 
120          Long id = new Long(1L); 
121          try { 
122              Object obj = clientMgr.findByPrimaryKey(id); 
123              fail("The FindByPK should throw a LookupException when no record is found"); 
124          } catch (com.ociweb.service.LookupException le) { 
125          } 
126      } 
127   
128      public void testFindAll() throws Exception { 
129          List beans = new ArrayList(); 
130          beans.add(new Foo()); 
131          session.findReturn = beans; 
132          FooMgr fooMgr = (FooMgr) locator.getDomainObjectManager(FooMgr.class); 
133          assertFalse(session.beginTransactionCalled); 
134          assertFalse(session.saveCalled); 
135          Collection result = fooMgr.findAll(); 
136          assertTrue(session.findCalled); 
137          assertEquals(beans, result); 
138          assertEquals("from Foo", session.findQuery); 
139      } 
140   
141      public void testAddTargetRecord() throws Exception { 
142          GoodMgr clientMgr = (GoodMgr) locator.getDomainObjectManager(GoodMgr.class); 
143          assertFalse(session.beginTransactionCalled); 
144          assertFalse(session.saveCalled); 
145          Object target = new Object(); 
146          clientMgr.add(target); 
147          assertTrue(session.beginTransactionCalled); 
148          assertTrue(session.saveCalled); 
149          assertTrue(transaction.commitCalled); 
150          assertEquals(target, session.saveObject); 
151      } 
152   
153      public void testAddTargetRecordFailureNullTarget() throws Exception { 
154          GoodMgr clientMgr = (GoodMgr) locator.getDomainObjectManager(GoodMgr.class); 
155          assertFalse(session.beginTransactionCalled); 
156          assertFalse(session.saveCalled); 
157          try { 
158              clientMgr.add(null); 
159              fail("ServiceLocator should not all saving null targets"); 
160          } catch (com.ociweb.service.PersistenceException pe) { 
161              assertFalse(session.beginTransactionCalled); 
162              assertFalse(session.saveCalled); 
163              assertFalse(transaction.commitCalled); 
164          } 
165      } 
166   
167      public void testUpdateTargetRecord() throws Exception { 
168          GoodMgr clientMgr = (GoodMgr) locator.getDomainObjectManager(GoodMgr.class); 
169          assertFalse(session.beginTransactionCalled); 
170          assertFalse(session.saveCalled); 
171          Object target = new Object(); 
172          clientMgr.update(target); 
173          assertTrue(session.beginTransactionCalled); 
174          assertTrue(session.updateCalled); 
175          assertTrue(transaction.commitCalled); 
176          assertEquals(target, session.updateObject); 
177      } 
178   
179      public void testRemoveTargetRecord() throws Exception { 
180          GoodMgr clientMgr = (GoodMgr) locator.getDomainObjectManager(GoodMgr.class); 
181          assertFalse(session.beginTransactionCalled); 
182          assertFalse(session.saveCalled); 
183          Object target = new Object(); 
184          clientMgr.remove(target); 
185          assertTrue(session.beginTransactionCalled); 
186          assertTrue(session.deleteCalled); 
187          assertTrue(transaction.commitCalled); 
188          assertEquals(target, session.deleteObject); 
189      } 
190   
191      public void testArbitraryFinder() throws Exception { 
192          AnotherMgr anotherMgr = (AnotherMgr)locator.getDomainObjectManager(AnotherMgr.class); 
193          assertFalse(session.beginTransactionCalled); 
194          Collection result = anotherMgr.findBySomeString(HEYNOW); 
195          assertTrue(session.getNamedQueryCalled); 
196          assertEquals("com.ociweb.service.ServiceLocatorTest$AnotherMgr.findBySomeString", 
197                       session.getNamedQueryQueryName); 
198      } 
199   
200      private static interface FooInterface { 
201          void bar(); 
202      } 
203   
204      private static interface GoodMgr { 
205          public void add(Object target) throws com.ociweb.service.PersistenceException; 
206   
207          public void remove(Object target) throws com.ociweb.service.PersistenceException; 
208   
209          public void update(Object target) throws com.ociweb.service.PersistenceException; 
210   
211          public Object findByPrimaryKey(Long id) throws com.ociweb.service.LookupException; 
212   
213          public Collection findAll() throws com.ociweb.service.LookupException; 
214      } 
215   
216      private static interface AnotherMgr extends GoodMgr { 
217          public Collection findBySomeString(String value) throws com.ociweb.service.LookupException; 
218      } 
219  } 
220