View Javadoc

1   /**
2    * 
3    */
4   package com.github.smokestack.jpa;
5   
6   import javax.persistence.EntityTransaction;
7   
8   import static org.hamcrest.MatcherAssert.assertThat;
9   
10  import org.apache.commons.lang.builder.ReflectionToStringBuilder;
11  import org.apache.commons.lang.builder.ToStringStyle;
12  import org.hamcrest.Matchers;
13  import org.hamcrest.core.AnyOf;
14  import org.hamcrest.core.Is;
15  import org.hamcrest.core.IsNot;
16  
17  /**
18   * @author gliptak
19   *
20   */
21  public class MockEntityTransaction implements EntityTransaction {
22  
23  	public enum EntityTransactionState {NEW, BEGIN, COMMIT, ROLLBACK};
24  	
25  	protected EntityTransactionState mockState=EntityTransactionState.NEW;
26  	
27  	protected boolean isRollbackOnly=false;
28  
29  	/* (non-Javadoc)
30  	 * @see javax.persistence.EntityTransaction#begin()
31  	 */
32  	public void begin() {
33  		assertThat("mockState", mockState, IsNot.not(EntityTransactionState.BEGIN));
34  		_begin();
35  		mockState=EntityTransactionState.BEGIN;
36  	}
37  
38  	public void _begin() {
39  	}
40  	/* (non-Javadoc)
41  	 * @see javax.persistence.EntityTransaction#commit()
42  	 */
43  	public void commit() {
44  		assertThat("mockState", mockState, Is.is(EntityTransactionState.BEGIN));
45  		assertThat("isRollbackOnly", isRollbackOnly, Is.is(false));
46  		_commit();
47  		mockState=EntityTransactionState.COMMIT;
48  	}
49  
50  	public void _commit() {
51  	}
52  	/* (non-Javadoc)
53  	 * @see javax.persistence.EntityTransaction#getRollbackOnly()
54  	 */
55  	public boolean getRollbackOnly() {
56  		_getRollbackOnly();
57  		return isRollbackOnly;
58  	}
59  
60  	public boolean _getRollbackOnly() {
61  		return false;
62  	}
63  	/* (non-Javadoc)
64  	 * @see javax.persistence.EntityTransaction#isActive()
65  	 */
66  	public boolean isActive() {
67  		_isActive();
68  		return EntityTransactionState.BEGIN==mockState;
69  	}
70  
71  	public boolean _isActive() {
72  		return false;
73  	}
74  	/* (non-Javadoc)
75  	 * @see javax.persistence.EntityTransaction#rollback()
76  	 */
77  	public void rollback() {
78  		assertThat("mockState", mockState, Is.is(EntityTransactionState.BEGIN));
79  		_rollback();
80  		mockState=EntityTransactionState.ROLLBACK;
81  	}
82  
83  	public void _rollback() {
84  	}
85  	/* (non-Javadoc)
86  	 * @see javax.persistence.EntityTransaction#setRollbackOnly()
87  	 */
88  	public void setRollbackOnly() {
89  		_setRollbackOnly();
90  		isRollbackOnly=true;
91  	}
92  
93  	public void _setRollbackOnly() {
94  	}
95  	/* (non-Javadoc)
96  	 * @see java.lang.Object#toString()
97  	 */
98  	public String toString(){
99  		return ReflectionToStringBuilder.toString(this, ToStringStyle.MULTI_LINE_STYLE);
100 	}
101 
102 	/**
103 	 * Validation
104 	 */
105 	public void assertMockComplete() {
106 		assertThat("mockState", mockState, AnyOf.anyOf(Matchers.equalTo(EntityTransactionState.COMMIT), Matchers.equalTo(EntityTransactionState.ROLLBACK)));	
107 	}
108 	
109 	/**
110 	 * Validation
111 	 */
112 	public void assertMockCommit() {
113 		assertThat("mockState", mockState, Is.is(EntityTransactionState.COMMIT));
114 	}
115 	
116 	/**
117 	 * Validation
118 	 */
119 	public void assertMockRollback() {
120 		assertThat("mockState", mockState, Is.is(EntityTransactionState.ROLLBACK));
121 	}
122 
123 	public EntityTransactionState getMockState() {
124 		return mockState;
125 	}
126 }