001    package com.mockrunner.connector;
002    
003    import java.util.List;
004    
005    import javax.resource.cci.RecordFactory;
006    
007    import com.mockrunner.base.NestedApplicationException;
008    import com.mockrunner.base.VerifyFailedException;
009    import com.mockrunner.mock.connector.cci.ConnectorMockObjectFactory;
010    import com.mockrunner.mock.connector.cci.MockInteraction;
011    import com.mockrunner.mock.connector.cci.MockLocalTransaction;
012    import com.mockrunner.mock.connector.cci.MockRecordFactory;
013    
014    /**
015     * Module for JCA tests.
016     */
017    public class ConnectorTestModule 
018    {
019            private ConnectorMockObjectFactory mockFactory;
020    
021            public ConnectorTestModule(ConnectorMockObjectFactory mockFactory)
022            {
023                    this.mockFactory = mockFactory;
024            }
025        
026        /**
027         * Returns the {@link InteractionHandler}.
028         * @return the {@link InteractionHandler}
029         */
030        public InteractionHandler getInteractionHandler()
031        {
032            return mockFactory.getInteractionHandler();
033        }
034        
035        private MockRecordFactory getMockRecordFactory()
036        {
037            try
038            {
039                RecordFactory factory = mockFactory.getMockConnectionFactory().getRecordFactory();
040                if(factory instanceof MockRecordFactory)
041                {
042                    return (MockRecordFactory)factory;
043                }
044                return null;
045            } 
046            catch(Exception exc)
047            {
048                throw new NestedApplicationException(exc);
049            }
050        }
051        
052        /**
053         * Returns a list of all created <code>Interaction</code> objects
054         * by delegating to {@link com.mockrunner.mock.connector.cci.MockConnection#getInteractionList()}.
055         * @return the <code>List</code> of all created <code>Interaction</code> objects
056         */
057        public List getInteractionList()
058        {
059            return mockFactory.getMockConnection().getInteractionList();
060        }
061        
062        /**
063         * Returns a list of all created indexed records
064         * by delegating to {@link com.mockrunner.mock.connector.cci.MockRecordFactory#getCreatedIndexedRecords()}.
065         * @return the <code>List</code> of all created indexed records
066         */
067        public List getCreatedIndexedRecords()
068        {
069            return getMockRecordFactory().getCreatedIndexedRecords();
070        }
071        
072        /**
073         * Returns a list of created indexed records that match the specified name
074         * by delegating to {@link com.mockrunner.mock.connector.cci.MockRecordFactory#getCreatedIndexedRecords(String)}.
075         * @param recordName the name of the record
076         * @return the <code>List</code> of matching indexed records
077         */
078        public List getCreatedIndexedRecords(String recordName)
079        {
080            return getMockRecordFactory().getCreatedIndexedRecords(recordName);
081        }
082        
083        /**
084         * Returns a list of all created mapped records
085         * by delegating to {@link com.mockrunner.mock.connector.cci.MockRecordFactory#getCreatedMappedRecords()}.
086         * @return the <code>List</code> of all created mapped records
087         */
088        public List getCreatedMappedRecords()
089        {
090            return getMockRecordFactory().getCreatedMappedRecords();
091        }
092        
093        /**
094         * Returns a list of created mapped records that match the specified name
095         * by delegating to {@link com.mockrunner.mock.connector.cci.MockRecordFactory#getCreatedMappedRecords(String)}.
096         * @param recordName the name of the record
097         * @return the <code>List</code> of matching mapped records
098         */
099        public List getCreatedMappedRecords(String recordName)
100        {
101            return getMockRecordFactory().getCreatedMappedRecords(recordName);
102        }
103        
104        /**
105         * Verifies that the connection is closed.
106         * @throws VerifyFailedException if verification fails
107         */
108        public void verifyConnectionClosed()
109        {
110            if(!mockFactory.getMockConnection().isClosed())
111            {
112                throw new VerifyFailedException("Connection is not closed.");
113            }
114        }
115        
116        /**
117         * Verifies that all interactions are closed.
118         * @throws VerifyFailedException if verification fails
119         */
120        public void verifyAllInteractionsClosed()
121        {
122            List interactions = getInteractionList();
123            for(int ii = 0; ii < interactions.size(); ii++)
124            {
125                MockInteraction interaction = (MockInteraction)interactions.get(ii);
126                if(!interaction.isClosed())
127                {
128                    throw new VerifyFailedException("Interaction with index " + ii + " is not closed.");
129                }
130            }
131        }
132        
133        /**
134         * Verifies that the specified interaction is closed.
135         * @param index the index of the <code>Interaction</code>
136         * @throws VerifyFailedException if verification fails
137         */
138        public void verifyInteractionClosed(int index)
139        {
140            List interactions = getInteractionList();
141            if(index >= interactions.size())
142            {
143                throw new VerifyFailedException("Interaction with index " + index + " does not exist, only " + interactions.size() + " interactions.");
144            }
145            MockInteraction interaction = (MockInteraction)interactions.get(index);
146            if(!interaction.isClosed())
147            {
148                throw new VerifyFailedException("Interaction with index " + index + " is not closed.");
149            }
150        }
151        
152        /**
153         * Verifies that <code>expected</code> number of indexed records
154         * have been created.
155         * @param expected the expected number of indexed records
156         * @throws VerifyFailedException if verification fails
157         */
158        public void verifyNumberCreatedIndexedRecords(int expected)
159        {
160            int actual = getMockRecordFactory().getNumberCreatedIndexedRecords();
161            if(actual != expected)
162            {
163                throw new VerifyFailedException("Expected " + expected + " indexed records, actual " + actual + " indexed records");
164            }
165        }
166        
167        /**
168         * Verifies that <code>expected</code> number of indexed records
169         * with the specified name have been created.
170         * @param recordName the name of the record
171         * @param expected the expected number of indexed records
172         * @throws VerifyFailedException if verification fails
173         */
174        public void verifyNumberCreatedIndexedRecords(String recordName, int expected)
175        {
176            List list = getCreatedIndexedRecords(recordName);
177            if(list.size() != expected)
178            {
179                throw new VerifyFailedException("Expected " + expected + " indexed records with the name " + recordName + ", actual " + list.size() + " indexed records");
180            }
181        }
182        
183        /**
184         * Verifies that <code>expected</code> number of mapped records
185         * have been created.
186         * @param expected the expected number of mapped records
187         * @throws VerifyFailedException if verification fails
188         */
189        public void verifyNumberCreatedMappedRecords(int expected)
190        {
191            int actual = getMockRecordFactory().getNumberCreatedMappedRecords();
192            if(actual != expected)
193            {
194                throw new VerifyFailedException("Expected " + expected + " mapped records, actual " + actual + " mapped records");
195            }
196        }
197        
198        /**
199         * Verifies that <code>expected</code> number of mapped records
200         * with the specified name have been created.
201         * @param recordName the name of the record
202         * @param expected the expected number of mapped records
203         * @throws VerifyFailedException if verification fails
204         */
205        public void verifyNumberCreatedMappedRecords(String recordName, int expected)
206        {
207            List list = getCreatedMappedRecords(recordName);
208            if(list.size() != expected)
209            {
210                throw new VerifyFailedException("Expected " + expected + " mapped records with the name " + recordName + ", actual " + list.size() + " mapped records");
211            }
212        }
213        
214        /**
215         * Verifies that the current local transaction was committed.
216         * @throws VerifyFailedException if verification fails
217         */
218        public void verifyLocalTransactionCommitted()
219        {
220            MockLocalTransaction transaction = mockFactory.getMockConnection().getMockLocalTransaction();
221            if(!transaction.wasCommitCalled())
222            {
223                throw new VerifyFailedException("Local transaction not committed");
224            }
225        }
226        
227        /**
228         * Verifies that the current local transaction was not committed.
229         * @throws VerifyFailedException if verification fails
230         */
231        public void verifyLocalTransactionNotCommitted()
232        {
233            MockLocalTransaction transaction = mockFactory.getMockConnection().getMockLocalTransaction();
234            if(transaction.wasCommitCalled())
235            {
236                throw new VerifyFailedException("Local transaction was committed");
237            }
238        }
239        
240        /**
241         * Verifies that the current local transaction was rolled back.
242         * @throws VerifyFailedException if verification fails
243         */
244        public void verifyLocalTransactionRolledBack()
245        {
246            MockLocalTransaction transaction = mockFactory.getMockConnection().getMockLocalTransaction();
247            if(!transaction.wasRollbackCalled())
248            {
249                throw new VerifyFailedException("Local transaction not rolled back");
250            }
251        }
252        
253        /**
254         * Verifies that the current local transaction was not rolled back.
255         * @throws VerifyFailedException if verification fails
256         */
257        public void verifyLocalTransactionNotRolledBack()
258        {
259            MockLocalTransaction transaction = mockFactory.getMockConnection().getMockLocalTransaction();
260            if(transaction.wasRollbackCalled())
261            {
262                throw new VerifyFailedException("Local transaction was rolled back");
263            }
264        }
265    }