View Javadoc

1   package com.github.smokestack.jca.cci;
2   
3   import static org.hamcrest.MatcherAssert.assertThat;
4   
5   import javax.resource.ResourceException;
6   import javax.resource.cci.Connection;
7   import javax.resource.cci.ConnectionMetaData;
8   import javax.resource.cci.ConnectionSpec;
9   import javax.resource.cci.Interaction;
10  import javax.resource.cci.LocalTransaction;
11  import javax.resource.cci.ResultSetInfo;
12  
13  import org.apache.commons.lang.builder.ReflectionToStringBuilder;
14  import org.apache.commons.lang.builder.ToStringStyle;
15  import org.hamcrest.core.Is;
16  
17  public class MockConnection implements Connection {
18  
19  	public enum ConnectionState {NEW, CLOSE};
20  	
21  	protected ConnectionState connectionState=ConnectionState.NEW;
22  	
23  	protected MockInteraction mockInteraction;
24  	protected MockLocalTransaction mockTransaction;
25  	protected ConnectionSpec connectionSpec;
26  	protected MockResultSetInfo mockResultSetInfo;
27  	protected MockConnectionMetaData mockConnectionMetaData;
28  	
29  	public MockConnection(ConnectionSpec connectionSpec) {
30  		this.connectionSpec=connectionSpec;
31  	}
32  
33  	public MockConnection() {
34  	}
35  
36  	/* (non-Javadoc)
37  	 * @see javax.resource.cci.Connection#close()
38  	 */
39  	public void close() throws ResourceException {
40  		assertThat("connectionState", connectionState, Is.is(ConnectionState.NEW));
41  		connectionState=ConnectionState.CLOSE;
42  		_close();
43  	}
44  
45  	public void _close() throws ResourceException {
46  	}
47  
48  	/* (non-Javadoc)
49  	 * @see javax.resource.cci.Connection#createInteraction()
50  	 */
51  	public Interaction createInteraction() throws ResourceException {
52  		assertThat("connectionState", connectionState, Is.is(ConnectionState.NEW));
53  		_createInteraction();
54  		if (mockInteraction==null){
55  			mockInteraction=new MockInteraction(this);
56  		}
57  		return mockInteraction;
58  	}
59  
60  	public MockInteraction _createInteraction() throws ResourceException {
61  		return null;
62  	}
63  
64  	/* (non-Javadoc)
65  	 * @see javax.resource.cci.Connection#getLocalTransaction()
66  	 */
67  	public LocalTransaction getLocalTransaction() throws ResourceException {
68  		assertThat("connectionState", connectionState, Is.is(ConnectionState.NEW));
69  		_getLocalTransaction();
70  		if (mockTransaction==null){
71  			mockTransaction=new MockLocalTransaction();
72  		}
73  		return mockTransaction;
74  	}
75  
76  	public MockLocalTransaction _getLocalTransaction() throws ResourceException {
77  		return null;
78  	}
79  
80  	/* (non-Javadoc)
81  	 * @see javax.resource.cci.Connection#getMetaData()
82  	 */
83  	public ConnectionMetaData getMetaData() throws ResourceException {
84  		assertThat("connectionState", connectionState, Is.is(ConnectionState.NEW));
85  		_getMetaData();
86  		if (mockConnectionMetaData==null){
87  			mockConnectionMetaData=new MockConnectionMetaData();
88  		}
89  		return mockConnectionMetaData;
90  	}
91  
92  	public MockConnectionMetaData _getMetaData() throws ResourceException {
93  		return null;
94  	}
95  
96  	/* (non-Javadoc)
97  	 * @see javax.resource.cci.Connection#getResultSetInfo()
98  	 */
99  	public ResultSetInfo getResultSetInfo() throws ResourceException {
100 		assertThat("connectionState", connectionState, Is.is(ConnectionState.NEW));
101 		_getResultSetInfo();
102 		if (mockResultSetInfo==null){
103 			mockResultSetInfo=new MockResultSetInfo();
104 		}
105 		return mockResultSetInfo;
106 	}
107 	
108 	public MockResultSetInfo _getResultSetInfo() throws ResourceException {
109 		return null;
110 	}
111 	
112 	public void validateMockComplete(){
113 		assertThat("connectionState", connectionState, Is.is(ConnectionState.CLOSE));
114 		if (mockTransaction!=null){
115 			mockTransaction.assertMockComplete();			
116 		}
117 		if (mockInteraction!=null){
118 			mockInteraction.validateMockComplete();			
119 		}		
120 	}
121 	
122 	public void validateMockCommit(){
123 		assertThat("connectionState", connectionState, Is.is(ConnectionState.CLOSE));
124 		if (mockTransaction!=null){
125 			mockTransaction.assertMockCommit();			
126 		}
127 		if (mockInteraction!=null){
128 			mockInteraction.validateMockComplete();			
129 		}		
130 	}
131 	
132 	public void validateMockRollback(){
133 		assertThat("connectionState", connectionState, Is.is(ConnectionState.CLOSE));
134 		if (mockTransaction!=null){
135 			mockTransaction.validateMockRollback();			
136 		}
137 		if (mockInteraction!=null){
138 			mockInteraction.validateMockComplete();			
139 		}		
140 	}
141 	
142 	@Override
143 	public String toString(){
144 		return ReflectionToStringBuilder.toString(this, ToStringStyle.MULTI_LINE_STYLE);
145 	}
146 }