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