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: private List indexedRecords;
017: private List mappedRecords;
018:
019: public MockRecordFactory() {
020: indexedRecords = new ArrayList();
021: mappedRecords = new ArrayList();
022: }
023:
024: /**
025: * Resets the list of created indexed records.
026: */
027: public void resetIndexedRecords() {
028: indexedRecords.clear();
029: }
030:
031: /**
032: * Resets the list of created mapped records.
033: */
034: public void resetMappedRecords() {
035: mappedRecords.clear();
036: }
037:
038: /**
039: * Returns the number of created indexed records.
040: * @return the number of created indexed records
041: */
042: public int getNumberCreatedIndexedRecords() {
043: return indexedRecords.size();
044: }
045:
046: /**
047: * Returns the number of created mapped records.
048: * @return the number of created mapped records
049: */
050: public int getNumberCreatedMappedRecords() {
051: return mappedRecords.size();
052: }
053:
054: /**
055: * Returns a list of all created indexed records.
056: * @return the <code>List</code> of all created indexed records
057: */
058: public List getCreatedIndexedRecords() {
059: return getCreatedRecords(null, false, indexedRecords);
060: }
061:
062: /**
063: * Returns a list of created indexed records that match the specified name.
064: * @param recordName the name of the record
065: * @return the <code>List</code> of matching indexed records
066: */
067: public List getCreatedIndexedRecords(String recordName) {
068: return getCreatedRecords(recordName, true, indexedRecords);
069: }
070:
071: /**
072: * Returns a list of all created mapped records.
073: * @return the <code>List</code> of all created mapped records
074: */
075: public List getCreatedMappedRecords() {
076: return getCreatedRecords(null, false, mappedRecords);
077: }
078:
079: /**
080: * Returns a list of created mapped records that match the specified name.
081: * @param recordName the name of the record
082: * @return the <code>List</code> of matching mapped records
083: */
084: public List getCreatedMappedRecords(String recordName) {
085: return getCreatedRecords(recordName, true, mappedRecords);
086: }
087:
088: private List getCreatedRecords(String recordName,
089: boolean recognizeRecordName, List recordWrapperList) {
090: List result = new ArrayList();
091: for (int ii = 0; ii < recordWrapperList.size(); ii++) {
092: RecordWrapper currentWrapper = (RecordWrapper) recordWrapperList
093: .get(ii);
094: if (!recognizeRecordName) {
095: result.add(currentWrapper.getRecord());
096: } else {
097: addRecordIfMatching(recordName, result, currentWrapper);
098: }
099: }
100: return result;
101: }
102:
103: private void addRecordIfMatching(String recordName, List result,
104: RecordWrapper currentWrapper) {
105: if (null == recordName) {
106: if (null == currentWrapper.getRecordName()) {
107: result.add(currentWrapper.getRecord());
108: }
109: } else {
110: if (recordName.equals(currentWrapper.getRecordName())) {
111: result.add(currentWrapper.getRecord());
112: }
113: }
114: }
115:
116: public IndexedRecord createIndexedRecord(String recordName)
117: throws ResourceException {
118: MockIndexedRecord record = new MockIndexedRecord(recordName);
119: indexedRecords.add(new RecordWrapper(recordName, record));
120: return record;
121: }
122:
123: public MappedRecord createMappedRecord(String recordName)
124: throws ResourceException {
125: MockMappedRecord record = new MockMappedRecord(recordName);
126: mappedRecords.add(new RecordWrapper(recordName, record));
127: return record;
128: }
129:
130: private class RecordWrapper {
131: private String recordName;
132: private Record record;
133:
134: public RecordWrapper(String recordName, Record record) {
135: this .recordName = recordName;
136: this .record = record;
137: }
138:
139: public String getRecordName() {
140: return recordName;
141: }
142:
143: public Record getRecord() {
144: return record;
145: }
146: }
147: }
|