001    package com.mockrunner.mock.connector.cci;
002    
003    import java.util.ArrayList;
004    import java.util.List;
005    
006    import javax.resource.ResourceException;
007    import javax.resource.cci.IndexedRecord;
008    import javax.resource.cci.MappedRecord;
009    import javax.resource.cci.Record;
010    import javax.resource.cci.RecordFactory;
011    
012    /**
013     * Mock implementation of <code>RecordFactory</code>.
014     */
015    public class MockRecordFactory implements RecordFactory
016    {
017        private List indexedRecords;
018        private List mappedRecords;
019        
020        public MockRecordFactory()
021        {
022            indexedRecords = new ArrayList();
023            mappedRecords = new ArrayList();
024        }
025        
026        /**
027         * Resets the list of created indexed records.
028         */
029        public void resetIndexedRecords()
030        {
031            indexedRecords.clear();
032        }
033        
034        /**
035         * Resets the list of created mapped records.
036         */
037        public void resetMappedRecords()
038        {
039            mappedRecords.clear();
040        }
041        
042        /**
043         * Returns the number of created indexed records.
044         * @return the number of created indexed records
045         */
046        public int getNumberCreatedIndexedRecords()
047        {
048            return indexedRecords.size();
049        }
050        
051        /**
052         * Returns the number of created mapped records.
053         * @return the number of created mapped records
054         */
055        public int getNumberCreatedMappedRecords()
056        {
057            return mappedRecords.size();
058        }
059        
060        /**
061         * Returns a list of all created indexed records.
062         * @return the <code>List</code> of all created indexed records
063         */
064        public List getCreatedIndexedRecords()
065        {
066            return getCreatedRecords(null, false, indexedRecords);
067        }
068        
069        /**
070         * Returns a list of created indexed records that match the specified name.
071         * @param recordName the name of the record
072         * @return the <code>List</code> of matching indexed records
073         */
074        public List getCreatedIndexedRecords(String recordName)
075        {
076            return getCreatedRecords(recordName, true, indexedRecords);
077        }
078        
079        /**
080         * Returns a list of all created mapped records.
081         * @return the <code>List</code> of all created mapped records
082         */
083        public List getCreatedMappedRecords()
084        {
085            return getCreatedRecords(null, false, mappedRecords);
086        }
087        
088        /**
089         * Returns a list of created mapped records that match the specified name.
090         * @param recordName the name of the record
091         * @return the <code>List</code> of matching mapped records
092         */
093        public List getCreatedMappedRecords(String recordName)
094        {
095            return getCreatedRecords(recordName, true, mappedRecords);
096        }
097        
098        private List getCreatedRecords(String recordName, boolean recognizeRecordName, List recordWrapperList)
099        {
100            List result = new ArrayList();
101            for(int ii = 0; ii < recordWrapperList.size(); ii++)
102            {
103                RecordWrapper currentWrapper = (RecordWrapper)recordWrapperList.get(ii);
104                if(!recognizeRecordName)
105                {
106                    result.add(currentWrapper.getRecord());
107                }
108                else
109                {
110                    addRecordIfMatching(recordName, result, currentWrapper);
111                }
112            }
113            return result;
114        }
115    
116        private void addRecordIfMatching(String recordName, List result, RecordWrapper currentWrapper)
117        {
118            if(null == recordName)
119            {
120                if(null == currentWrapper.getRecordName())
121                {
122                    result.add(currentWrapper.getRecord());
123                }
124            }
125            else
126            {
127                if(recordName.equals(currentWrapper.getRecordName()))
128                {
129                    result.add(currentWrapper.getRecord());
130                }
131            }
132        }
133    
134        public IndexedRecord createIndexedRecord(String recordName) throws ResourceException
135        {
136            MockIndexedRecord record = new MockIndexedRecord(recordName);
137            indexedRecords.add(new RecordWrapper(recordName, record));
138            return record;
139        }
140    
141        public MappedRecord createMappedRecord(String recordName) throws ResourceException
142        {
143            MockMappedRecord record = new MockMappedRecord(recordName);
144            mappedRecords.add(new RecordWrapper(recordName, record));
145            return record;
146        }
147        
148        private class RecordWrapper
149        {
150            private String recordName;
151            private Record record;
152            
153            public RecordWrapper(String recordName, Record record)
154            {
155                this.recordName = recordName;
156                this.record = record;
157            }
158            
159            public String getRecordName()
160            {
161                return recordName;
162            }
163    
164            public Record getRecord()
165            {
166                return record;
167            } 
168        }
169    }