001    package com.mockrunner.mock.connector.cci;
002    
003    import java.util.ArrayList;
004    import java.util.Collections;
005    import java.util.List;
006    
007    import javax.resource.ResourceException;
008    import javax.resource.cci.Connection;
009    import javax.resource.cci.ConnectionMetaData;
010    import javax.resource.cci.Interaction;
011    import javax.resource.cci.LocalTransaction;
012    import javax.resource.cci.ResultSetInfo;
013    
014    import com.mockrunner.connector.InteractionHandler;
015    
016    /**
017     * Mock implementation of <code>Connection</code>.
018     */
019    public class MockConnection implements Connection
020    {
021        private boolean closed;
022        private InteractionHandler interactionHandler;
023        private ConnectionMetaData metaData;
024        private LocalTransaction localTransaction;
025        private ResultSetInfo resultSetInfo;
026        private List interactions;
027        
028        public MockConnection()
029        {
030            closed = false;
031            metaData = new MockConnectionMetaData();
032            localTransaction = new MockLocalTransaction();
033            resultSetInfo = new MockResultSetInfo();
034            interactions = new ArrayList();
035        }
036    
037        /**
038         * Returns the {@link MockLocalTransaction}. If the underlying
039         * <code>LocalTransaction</code> is not an instance of
040         * {@link MockLocalTransaction}, this method returns <code>null</code>.
041         * Otherwise it returns the same object as {@link #getLocalTransaction}.
042         * @return the {@link MockLocalTransaction}
043         */
044        public MockLocalTransaction getMockLocalTransaction()
045        {
046            if(localTransaction instanceof MockLocalTransaction)
047            {
048                return (MockLocalTransaction) localTransaction;
049            }
050            return null;
051        }
052    
053        /**
054         * Returns the list of all created <code>Interaction</code> objects.
055         * 
056         * @return the list <code>Interaction</code> objects
057         */
058        public List getInteractionList()
059        {
060            return Collections.unmodifiableList(interactions);
061        }
062    
063        public void close() throws ResourceException
064        {
065            for(int ii = 0; ii < interactions.size(); ii++)
066            {
067                ((Interaction)interactions.get(ii)).close();
068            }
069            closed = true;
070        }
071    
072        public Interaction createInteraction() throws ResourceException
073        {
074            Interaction interaction = new MockInteraction(this);
075            interactions.add(interaction);
076            return interaction;
077        }
078    
079        public LocalTransaction getLocalTransaction() throws ResourceException
080        {
081            return localTransaction;
082        }
083    
084        public ConnectionMetaData getMetaData() throws ResourceException
085        {
086            return metaData;
087        }
088    
089        public ResultSetInfo getResultSetInfo() throws ResourceException
090        {
091            return resultSetInfo;
092        }
093    
094        /**
095         * Returns if this <code>Connection</code> is closed.
096         * @return <code>true</code> if this <code>Interaction</code> is closed,
097         *         <code>false</code> otherwise
098         */
099        public boolean isClosed()
100        {
101            return closed;
102        }
103    
104        public InteractionHandler getInteractionHandler()
105        {
106            return interactionHandler;
107        }
108    
109        /**
110         * Sets this connections <code>ResultSetInfo</code>
111         * @param resultSetInfo the <code>ResultSetInfo</code>
112         */
113        public void setResultSetInfo(ResultSetInfo resultSetInfo)
114        {
115            this.resultSetInfo = resultSetInfo;
116        }
117    
118        /**
119         * Sets this connections <code>InteractionHandler</code>
120         * @param interactionHandler the <code>InteractionHandler</code>
121         */
122        public void setInteractionHandler(InteractionHandler interactionHandler)
123        {
124            this.interactionHandler = interactionHandler;
125        }
126    
127        /**
128         * Sets this connections meta data
129         * @param metaData the meta data
130         */
131        public void setMetaData(ConnectionMetaData metaData)
132        {
133            this.metaData = metaData;
134        }
135    
136        /**
137         * Sets the <code>LocalTransaction</code>
138         * @param localTransaction the <code>LocalTransaction</code>
139         */
140        public void setLocalTransaction(LocalTransaction localTransaction)
141        {
142            this.localTransaction = localTransaction;
143        }
144    }