001: package com.mockrunner.test.connector;
002:
003: import java.util.List;
004:
005: import javax.resource.cci.Interaction;
006: import javax.resource.cci.Record;
007:
008: import junit.framework.TestCase;
009:
010: import com.mockrunner.base.VerifyFailedException;
011: import com.mockrunner.connector.ConnectorTestModule;
012: import com.mockrunner.mock.connector.cci.ConnectorMockObjectFactory;
013:
014: public class ConnectorTestModuleTest extends TestCase {
015: private ConnectorMockObjectFactory mockFactory;
016: private ConnectorTestModule module;
017:
018: protected void setUp() throws Exception {
019: mockFactory = new ConnectorMockObjectFactory();
020: module = new ConnectorTestModule(mockFactory);
021: }
022:
023: protected void tearDown() throws Exception {
024: mockFactory = null;
025: module = null;
026: }
027:
028: private void createIndexedRecord(String name) throws Exception {
029: mockFactory.getMockConnectionFactory().getRecordFactory()
030: .createIndexedRecord(name);
031: }
032:
033: private void createMappedRecord(String name) throws Exception {
034: mockFactory.getMockConnectionFactory().getRecordFactory()
035: .createMappedRecord(name);
036: }
037:
038: public void testGetInteractionList() throws Exception {
039: Interaction interaction1 = mockFactory.getMockConnection()
040: .createInteraction();
041: Interaction interaction2 = mockFactory.getMockConnection()
042: .createInteraction();
043: List interactionList = module.getInteractionList();
044: assertEquals(2, interactionList.size());
045: assertEquals(interaction1, interactionList.get(0));
046: assertEquals(interaction2, interactionList.get(1));
047: }
048:
049: public void testVerifyConnectionClosed() throws Exception {
050: try {
051: module.verifyConnectionClosed();
052: fail();
053: } catch (VerifyFailedException exc) {
054: //expected exception
055: }
056: mockFactory.getMockConnection().close();
057: module.verifyConnectionClosed();
058: }
059:
060: public void testVerifyInteractionClosed() throws Exception {
061: Interaction interaction1 = mockFactory.getMockConnection()
062: .createInteraction();
063: Interaction interaction2 = mockFactory.getMockConnection()
064: .createInteraction();
065: Interaction interaction3 = mockFactory.getMockConnection()
066: .createInteraction();
067: try {
068: module.verifyAllInteractionsClosed();
069: fail();
070: } catch (VerifyFailedException exc) {
071: //expected exception
072: }
073: try {
074: module.verifyInteractionClosed(3);
075: fail();
076: } catch (VerifyFailedException exc) {
077: //expected exception
078: }
079: try {
080: module.verifyInteractionClosed(2);
081: fail();
082: } catch (VerifyFailedException exc) {
083: //expected exception
084: }
085: interaction3.close();
086: module.verifyInteractionClosed(2);
087: try {
088: module.verifyAllInteractionsClosed();
089: fail();
090: } catch (VerifyFailedException exc) {
091: //expected exception
092: }
093: interaction1.close();
094: interaction2.close();
095: module.verifyInteractionClosed(0);
096: module.verifyInteractionClosed(1);
097: module.verifyAllInteractionsClosed();
098: }
099:
100: public void testGetCreatedIndexedRecords() throws Exception {
101: createIndexedRecord("indexedRecord");
102: createIndexedRecord("indexedRecord");
103: createIndexedRecord("anotherIndexedRecord");
104: List list = module.getCreatedIndexedRecords();
105: assertEquals(3, list.size());
106: assertEquals("indexedRecord", ((Record) list.get(0))
107: .getRecordName());
108: assertEquals("indexedRecord", ((Record) list.get(1))
109: .getRecordName());
110: assertEquals("anotherIndexedRecord", ((Record) list.get(2))
111: .getRecordName());
112: list = module.getCreatedIndexedRecords("indexedRecord");
113: assertEquals(2, list.size());
114: assertEquals("indexedRecord", ((Record) list.get(0))
115: .getRecordName());
116: assertEquals("indexedRecord", ((Record) list.get(1))
117: .getRecordName());
118: list = module.getCreatedIndexedRecords("anotherIndexedRecord");
119: assertEquals(1, list.size());
120: assertEquals("anotherIndexedRecord", ((Record) list.get(0))
121: .getRecordName());
122: list = module.getCreatedIndexedRecords("xyz");
123: assertEquals(0, list.size());
124: }
125:
126: public void testGetCreatedMappedRecords() throws Exception {
127: createMappedRecord("mappedRecord");
128: createMappedRecord("mappedRecord");
129: createMappedRecord("anotherMappedRecord");
130: List list = module.getCreatedMappedRecords();
131: assertEquals(3, list.size());
132: assertEquals("mappedRecord", ((Record) list.get(0))
133: .getRecordName());
134: assertEquals("mappedRecord", ((Record) list.get(1))
135: .getRecordName());
136: assertEquals("anotherMappedRecord", ((Record) list.get(2))
137: .getRecordName());
138: list = module.getCreatedMappedRecords("mappedRecord");
139: assertEquals(2, list.size());
140: assertEquals("mappedRecord", ((Record) list.get(0))
141: .getRecordName());
142: assertEquals("mappedRecord", ((Record) list.get(1))
143: .getRecordName());
144: list = module.getCreatedMappedRecords("anotherMappedRecord");
145: assertEquals(1, list.size());
146: assertEquals("anotherMappedRecord", ((Record) list.get(0))
147: .getRecordName());
148: list = module.getCreatedMappedRecords("xyz");
149: assertEquals(0, list.size());
150: }
151:
152: public void testVerifyCreatedIndexedRecords() throws Exception {
153: createIndexedRecord("indexedRecord");
154: createIndexedRecord("indexedRecord");
155: createIndexedRecord("anotherIndexedRecord");
156: module.verifyNumberCreatedIndexedRecords(3);
157: module.verifyNumberCreatedIndexedRecords("indexedRecord", 2);
158: module.verifyNumberCreatedIndexedRecords(
159: "anotherIndexedRecord", 1);
160: module.verifyNumberCreatedMappedRecords("xyz", 0);
161: try {
162: module
163: .verifyNumberCreatedIndexedRecords("indexedRecord",
164: 3);
165: fail();
166: } catch (VerifyFailedException exc) {
167: //should throw exception
168: }
169: try {
170: module.verifyNumberCreatedIndexedRecords(1);
171: fail();
172: } catch (VerifyFailedException exc) {
173: //should throw exception
174: }
175: }
176:
177: public void testVerifyNumberCreatedMappedRecords() throws Exception {
178: createMappedRecord("mappedRecord");
179: createMappedRecord("anotherMappedRecord");
180: createMappedRecord("anotherMappedRecord");
181: createMappedRecord("anotherMappedRecord");
182: module.verifyNumberCreatedMappedRecords(4);
183: module.verifyNumberCreatedMappedRecords("mappedRecord", 1);
184: module.verifyNumberCreatedMappedRecords("anotherMappedRecord",
185: 3);
186: module.verifyNumberCreatedMappedRecords("xyz", 0);
187: try {
188: module.verifyNumberCreatedMappedRecords("xyz", 1);
189: fail();
190: } catch (VerifyFailedException exc) {
191: //should throw exception
192: }
193: try {
194: module.verifyNumberCreatedMappedRecords(3);
195: fail();
196: } catch (VerifyFailedException exc) {
197: //should throw exception
198: }
199: }
200:
201: public void testVerifyLocalTransaction() throws Exception {
202: module.verifyLocalTransactionNotCommitted();
203: module.verifyLocalTransactionNotRolledBack();
204: try {
205: module.verifyLocalTransactionCommitted();
206: fail();
207: } catch (VerifyFailedException exc) {
208: //should throw exception
209: }
210: try {
211: module.verifyLocalTransactionRolledBack();
212: fail();
213: } catch (VerifyFailedException exc) {
214: //should throw exception
215: }
216: mockFactory.getMockConnection().getLocalTransaction().begin();
217: mockFactory.getMockConnection().getLocalTransaction().commit();
218: module.verifyLocalTransactionCommitted();
219: module.verifyLocalTransactionNotRolledBack();
220: try {
221: module.verifyLocalTransactionNotCommitted();
222: fail();
223: } catch (VerifyFailedException exc) {
224: //should throw exception
225: }
226: mockFactory.getMockConnection().getLocalTransaction().begin();
227: mockFactory.getMockConnection().getLocalTransaction()
228: .rollback();
229: module.verifyLocalTransactionNotCommitted();
230: module.verifyLocalTransactionRolledBack();
231: try {
232: module.verifyLocalTransactionCommitted();
233: fail();
234: } catch (VerifyFailedException exc) {
235: //should throw exception
236: }
237: }
238: }
|