View Javadoc

1   package com.github.smokestack.jca.cci;
2   
3   import javax.resource.ResourceException;
4   import javax.resource.cci.LocalTransaction;
5   
6   import org.apache.commons.lang.builder.ReflectionToStringBuilder;
7   import org.apache.commons.lang.builder.ToStringStyle;
8   import org.hamcrest.Matchers;
9   import org.hamcrest.core.AnyOf;
10  import org.hamcrest.core.Is;
11  
12  import static org.hamcrest.MatcherAssert.assertThat;
13  
14  public class MockLocalTransaction implements LocalTransaction {
15  	
16  	public enum LocalTransactionState {NEW, BEGIN, COMMIT, ROLLBACK};
17  	
18  	protected LocalTransactionState localTransactionState=LocalTransactionState.NEW;
19  
20  	public MockLocalTransaction(){
21  	}
22  	
23  	/* (non-Javadoc)
24  	 * @see javax.resource.cci.LocalTransaction#begin()
25  	 */
26  	public void begin() throws ResourceException {
27  		assertThat("localTransactionState", localTransactionState, Is.is(LocalTransactionState.NEW));
28  		_begin();
29  		localTransactionState=LocalTransactionState.BEGIN;
30  	}
31  
32  	public void _begin() throws ResourceException {
33  	}
34  
35  	/* (non-Javadoc)
36  	 * @see javax.resource.cci.LocalTransaction#commit()
37  	 */
38  	public void commit() throws ResourceException {
39  		assertThat("localTransactionState", localTransactionState, Is.is(LocalTransactionState.BEGIN));
40  		_commit();
41  		localTransactionState=LocalTransactionState.COMMIT;
42  	}
43  
44  	public void _commit() throws ResourceException {
45  	}
46  
47  	/* (non-Javadoc)
48  	 * @see javax.resource.cci.LocalTransaction#rollback()
49  	 */
50  	public void rollback() throws ResourceException {
51  		assertThat("localTransactionState", localTransactionState, Is.is(LocalTransactionState.BEGIN));
52  		_rollback();
53  		localTransactionState=LocalTransactionState.ROLLBACK;
54  	}
55  
56  	public void _rollback() throws ResourceException {
57  	}
58  
59  	public LocalTransactionState getMockTransactionState() {
60  		_getMockTransactionState();
61  		return localTransactionState;
62  	}
63  
64  	public LocalTransactionState _getMockTransactionState() {
65  		return null;
66  	}
67  
68  	public void assertMockComplete(){
69  		assertThat("localTransactionState", localTransactionState, AnyOf.anyOf(Matchers.equalTo(LocalTransactionState.COMMIT), Matchers.equalTo(LocalTransactionState.ROLLBACK)));	
70  	}
71  	
72  	public void assertMockCommit(){
73  		assertThat("localTransactionState", localTransactionState, Is.is(LocalTransactionState.COMMIT));	
74  	}
75  
76  	public void validateMockRollback(){
77  		assertThat("localTransactionState", localTransactionState, Is.is(LocalTransactionState.ROLLBACK));	
78  	}
79  
80  	@Override
81  	public String toString(){
82  		return ReflectionToStringBuilder.toString(this, ToStringStyle.MULTI_LINE_STYLE);
83  	}
84  }