View Javadoc

1   /**
2    * 
3    */
4   package com.github.smokestack.jpa;
5   
6   import static org.hamcrest.MatcherAssert.assertThat;
7   
8   import java.util.HashMap;
9   import java.util.Map;
10  
11  import javax.persistence.EntityManager;
12  import javax.persistence.EntityTransaction;
13  import javax.persistence.FlushModeType;
14  import javax.persistence.LockModeType;
15  import javax.persistence.Query;
16  
17  import org.apache.commons.lang.builder.ReflectionToStringBuilder;
18  import org.apache.commons.lang.builder.ToStringStyle;
19  import org.hamcrest.core.Is;
20  
21  import com.github.smokestack.exception.NeedsMockDefinitionException;
22  import com.github.smokestack.exception.NotYetImplementedException;
23  
24  /**
25   * @author gliptak
26   *
27   */
28  public class MockEntityManager implements EntityManager {
29  
30  	protected Map map=new HashMap();
31  
32  	protected FlushModeType flushModeType;
33  	
34  	protected MockEntityTransaction mockEntityTransaction;
35  
36  	public enum EntityManagerState {NEW, CLOSE};
37  	
38  	protected EntityManagerState mockState=EntityManagerState.NEW;
39  
40  	/**
41  	 * Constructor
42  	 */
43  	public MockEntityManager() {
44  	}
45  
46  	/**
47  	 * Constructor
48  	 */
49  	public MockEntityManager(Map map){
50  		this.map=map;
51  	}
52  	
53  	/* (non-Javadoc)
54  	 * @see javax.persistence.EntityManager#clear()
55  	 */
56  	public void clear() {
57  		assertThat("mockState", mockState, Is.is(EntityManagerState.NEW));
58  		throw new NotYetImplementedException();
59  	}
60  
61  	/* (non-Javadoc)
62  	 * @see javax.persistence.EntityManager#close()
63  	 */
64  	public void close() {
65  		assertThat("mockState", mockState, Is.is(EntityManagerState.NEW));
66  		_close();
67  		mockState=EntityManagerState.CLOSE;
68  	}
69  
70  	public void _close() {
71  	}
72  
73  	/* (non-Javadoc)
74  	 * @see javax.persistence.EntityManager#contains(java.lang.Object)
75  	 */
76  	public boolean contains(Object entity) {
77  		assertThat("mockState", mockState, Is.is(EntityManagerState.NEW));
78  		throw new NotYetImplementedException();
79  	}
80  
81  	/* (non-Javadoc)
82  	 * @see javax.persistence.EntityManager#createNamedQuery(java.lang.String)
83  	 */
84  	public Query createNamedQuery(String name) {
85  		assertThat("mockState", mockState, Is.is(EntityManagerState.NEW));
86  		_createNamedQuery(name);
87  		return new MockNamedQuery(name);
88  	}
89  
90  	public Query _createNamedQuery(String name) {
91  		return null;
92  	}
93  
94  	/* (non-Javadoc)
95  	 * @see javax.persistence.EntityManager#createNativeQuery(java.lang.String)
96  	 */
97  	public Query createNativeQuery(String sqlString) {
98  		assertThat("mockState", mockState, Is.is(EntityManagerState.NEW));
99  		_createNativeQuery(sqlString);
100 		return new MockNativeQuery(sqlString);
101 	}
102 
103 	public Query _createNativeQuery(String sqlString) {
104 		return null;
105 	}
106 
107 	/* (non-Javadoc)
108 	 * @see javax.persistence.EntityManager#createNativeQuery(java.lang.String, java.lang.Class)
109 	 */
110 	public Query createNativeQuery(String sqlString, Class resultClass) {
111 		assertThat("mockState", mockState, Is.is(EntityManagerState.NEW));
112 		_createNativeQuery(sqlString, resultClass);
113 		return new MockNativeQuery(sqlString, resultClass);
114 	}
115 
116 	public Query  _createNativeQuery(String sqlString, Class resultClass) {
117 		return null;
118 	}
119 
120 	/* (non-Javadoc)
121 	 * @see javax.persistence.EntityManager#createNativeQuery(java.lang.String, java.lang.String)
122 	 */
123 	public Query createNativeQuery(String sqlString, String resultSetMapping) {
124 		assertThat("mockState", mockState, Is.is(EntityManagerState.NEW));
125 		_createNativeQuery(sqlString, resultSetMapping);
126 		return new MockNativeQuery(sqlString, resultSetMapping);
127 	}
128 
129 	public Query _createNativeQuery(String sqlString, String resultSetMapping) {
130 		return null;
131 	}
132 
133 	/* (non-Javadoc)
134 	 * @see javax.persistence.EntityManager#createQuery(java.lang.String)
135 	 */
136 	public Query createQuery(String qlString) {
137 		assertThat("mockState", mockState, Is.is(EntityManagerState.NEW));
138 		_createQuery(qlString);
139 		return new MockQLQuery(qlString);
140 	}
141 
142 	public Query  _createQuery(String qlString) {
143 		return null;
144 	}
145 
146 	/* (non-Javadoc)
147 	 * @see javax.persistence.EntityManager#find(java.lang.Class, java.lang.Object)
148 	 */
149 	public <T> T find(Class<T> arg0, Object arg1) {
150 		assertThat("mockState", mockState, Is.is(EntityManagerState.NEW));
151 		return _find(arg0, arg1);
152 	}
153 	
154 	public <T> T _find(Class<T> arg0, Object arg1) {
155 		throw new NeedsMockDefinitionException();
156 	}
157 
158 	/* (non-Javadoc)
159 	 * @see javax.persistence.EntityManager#flush()
160 	 */
161 	public void flush() {
162 		assertThat("mockState", mockState, Is.is(EntityManagerState.NEW));
163 		throw new NotYetImplementedException();
164 	}
165 
166 	/* (non-Javadoc)
167 	 * @see javax.persistence.EntityManager#getDelegate()
168 	 */
169 	public Object getDelegate() {
170 		assertThat("mockState", mockState, Is.is(EntityManagerState.NEW));
171 		throw new NotYetImplementedException();
172 	}
173 
174 	/* (non-Javadoc)
175 	 * @see javax.persistence.EntityManager#getFlushMode()
176 	 */
177 	public FlushModeType getFlushMode() {
178 		assertThat("mockState", mockState, Is.is(EntityManagerState.NEW));
179 		_getFlushMode();
180 		return flushModeType;
181 	}
182 	
183 	public FlushModeType _getFlushMode(){
184 		return null;
185 		
186 	}
187 	/* (non-Javadoc)
188 	 * @see javax.persistence.EntityManager#getReference(java.lang.Class, java.lang.Object)
189 	 */
190 	public <T> T getReference(Class<T> arg0, Object arg1) {
191 		assertThat("mockState", mockState, Is.is(EntityManagerState.NEW));
192 		return _getReference(arg0, arg1);
193 	}
194 
195 	public <T> T _getReference(Class<T> arg0, Object arg1) {
196 		throw new NeedsMockDefinitionException();
197 	}
198 
199 	/* (non-Javadoc)
200 	 * @see javax.persistence.EntityManager#getTransaction()
201 	 */
202 	public EntityTransaction getTransaction() {
203 		assertThat("mockState", mockState, Is.is(EntityManagerState.NEW));
204 		_getTransaction();
205 		if (mockEntityTransaction==null){
206 			mockEntityTransaction=new MockEntityTransaction();
207 		}
208 		return mockEntityTransaction;
209 	}
210 
211 	public EntityTransaction _getTransaction() {
212 		return null;
213 	}
214 
215 	/* (non-Javadoc)
216 	 * @see javax.persistence.EntityManager#isOpen()
217 	 */
218 	public boolean isOpen() {
219 		_isOpen();
220 		return EntityManagerState.NEW==mockState;
221 	}
222 
223 	public boolean _isOpen() {
224 		return false;
225 	}
226 	/* (non-Javadoc)
227 	 * @see javax.persistence.EntityManager#joinTransaction()
228 	 */
229 	public void joinTransaction() {
230 		assertThat("mockState", mockState, Is.is(EntityManagerState.NEW));
231 		throw new NotYetImplementedException();
232 	}
233 
234 	/* (non-Javadoc)
235 	 * @see javax.persistence.EntityManager#lock(java.lang.Object, javax.persistence.LockModeType)
236 	 */
237 	public void lock(Object arg0, LockModeType arg1) {
238 		assertThat("mockState", mockState, Is.is(EntityManagerState.NEW));
239 		throw new NotYetImplementedException();
240 	}
241 
242 	/* (non-Javadoc)
243 	 * @see javax.persistence.EntityManager#merge(java.lang.Object)
244 	 */
245 	public <T> T merge(T arg0) {
246 		assertThat("mockState", mockState, Is.is(EntityManagerState.NEW));
247 		return _merge(arg0);
248 	}
249 
250 	public <T> T _merge(T arg0) {
251 		throw new NeedsMockDefinitionException();
252 	}
253 	/* (non-Javadoc)
254 	 * @see javax.persistence.EntityManager#persist(java.lang.Object)
255 	 */
256 	public void persist(Object arg0) {
257 		assertThat("mockState", mockState, Is.is(EntityManagerState.NEW));
258 		_persist(arg0);
259 	}
260 
261 	public void _persist(Object arg0) {
262 		throw new NeedsMockDefinitionException();
263 	}
264 	/* (non-Javadoc)
265 	 * @see javax.persistence.EntityManager#refresh(java.lang.Object)
266 	 */
267 	public void refresh(Object arg0) {
268 		assertThat("mockState", mockState, Is.is(EntityManagerState.NEW));
269 		_refresh(arg0);
270 	}
271 
272 	public void _refresh(Object arg0) {
273 		throw new NeedsMockDefinitionException();
274 	}
275 	/* (non-Javadoc)
276 	 * @see javax.persistence.EntityManager#remove(java.lang.Object)
277 	 */
278 	public void remove(Object arg0) {
279 		assertThat("mockState", mockState, Is.is(EntityManagerState.NEW));
280 		_remove(arg0);
281 	}
282 
283 	public void _remove(Object arg0) {
284 		throw new NeedsMockDefinitionException();
285 	}
286 	/* (non-Javadoc)
287 	 * @see javax.persistence.EntityManager#setFlushMode(javax.persistence.FlushModeType)
288 	 */
289 	public void setFlushMode(FlushModeType flushModeType) {
290 		assertThat("mockState", mockState, Is.is(EntityManagerState.NEW));
291 		_setFlushMode(flushModeType);
292 		this.flushModeType=flushModeType;
293 	}
294 	
295 	public void _setFlushMode(FlushModeType flushModeType) {
296 	}
297 	/* (non-Javadoc)
298 	 * @see java.lang.Object#toString()
299 	 */
300 	public String toString(){
301 		return ReflectionToStringBuilder.toString(this, ToStringStyle.MULTI_LINE_STYLE);
302 	}
303 
304 	/**
305 	 * Validation
306 	 */
307 	public void assertMockComplete() {
308 		assertThat("mockState", mockState, Is.is(EntityManagerState.CLOSE));
309 		if (mockEntityTransaction!=null){
310 			mockEntityTransaction.assertMockComplete();
311 		}
312 	}
313 
314 	/**
315 	 * Validation
316 	 */
317 	public void assertMockCommit() {
318 		assertThat("mockState", mockState, Is.is(EntityManagerState.CLOSE));
319 		if (mockEntityTransaction!=null){
320 			mockEntityTransaction.assertMockCommit();
321 		}
322 	}
323 
324 	/**
325 	 * Validation
326 	 */
327 	public void assertMockRollback() {
328 		assertThat("mockState", mockState, Is.is(EntityManagerState.CLOSE));
329 		if (mockEntityTransaction!=null){
330 			mockEntityTransaction.assertMockRollback();
331 		}
332 	}
333 
334 	public EntityManagerState getMockState() {
335 		return mockState;
336 	}
337 
338 	public MockEntityTransaction getMockEntityTransaction() {
339 		return mockEntityTransaction;
340 	}
341 }