PageRenderTime 24ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/java/back/src/test/java/uk/mafu/loon/services/AdminServiceImplMockTest.java

https://github.com/binarytemple/looncms
Java | 439 lines | 232 code | 87 blank | 120 comment | 3 complexity | ef67fb8eb2e3e705130718b8751b2fda MD5 | raw file
  1. package uk.mafu.loon.services;
  2. import static org.junit.Assert.assertNotNull;
  3. import java.io.Serializable;
  4. import java.lang.reflect.Field;
  5. import java.util.Date;
  6. import javax.persistence.EntityManager;
  7. import org.easymock.IAnswer;
  8. import org.easymock.IArgumentMatcher;
  9. import org.easymock.classextension.EasyMock;
  10. import org.junit.After;
  11. import org.junit.AfterClass;
  12. import org.junit.Before;
  13. import org.junit.BeforeClass;
  14. import org.junit.Test;
  15. import org.springframework.orm.jpa.JpaTemplate;
  16. public class AdminServiceImplMockTest {
  17. public static class IsClass implements IArgumentMatcher, Serializable {
  18. private static final long serialVersionUID = 1094930851962278376L;
  19. private final Class<?> expected;
  20. public IsClass(Class<?> expected) {
  21. this.expected = expected;
  22. }
  23. public boolean matches(Object actual) {
  24. boolean instance = expected.isInstance((Class<?>) actual)
  25. || expected.equals((Class<?>) actual);
  26. System.err.println(instance);
  27. return instance;
  28. }
  29. public void appendTo(StringBuffer buffer) {
  30. buffer.append("same(");
  31. appendQuoting(buffer);
  32. buffer.append(expected);
  33. appendQuoting(buffer);
  34. buffer.append(")");
  35. }
  36. private void appendQuoting(StringBuffer buffer) {
  37. buffer.append("\"");
  38. }
  39. }
  40. private static AdminServiceImpl adminServiceImpl;
  41. @BeforeClass
  42. public static void setUpBeforeClass() throws Exception {
  43. // entityManager.persist(EasyMock.isA(Serializable.class));
  44. // org.easymock.EasyMock.expectLastCall().andAnswer(new
  45. // IAnswer<Object>() {
  46. // public Object answer() throws Throwable {
  47. // Object[] args = org.easymock.EasyMock.getCurrentArguments();
  48. // if (args[0] == null) {
  49. // throw new UnsupportedOperationException(
  50. // "asked to persist a null object");
  51. // }
  52. // return args[0];
  53. // }
  54. // }).anyTimes();
  55. // entityManager.find(org.easymock.EasyMock.isA(Class.class), EasyMock
  56. // .isNull());
  57. //
  58. // org.easymock.EasyMock.expectLastCall().andAnswer(new
  59. // IAnswer<Object>() {
  60. // public Object answer() throws Throwable {
  61. // Object[] args = org.easymock.EasyMock.getCurrentArguments();
  62. // if (args[0] == null) {
  63. // throw new UnsupportedOperationException(
  64. // "asked to persist a null object");
  65. // }
  66. // return args[0];
  67. // }
  68. // }).anyTimes();
  69. //
  70. // entityManager.find(EasyMock.isA(Class.class), EasyMock
  71. // .isA(String.class));
  72. // org.easymock.EasyMock.expectLastCall().andAnswer(new
  73. // IAnswer<Object>() {
  74. // public Object answer() throws Throwable {
  75. // Object[] args = org.easymock.EasyMock.getCurrentArguments();
  76. // if (args[0] == null) {
  77. // throw new UnsupportedOperationException(
  78. // "asked to persist a null object");
  79. // }
  80. // return args[0];
  81. // }
  82. // }).anyTimes();
  83. //
  84. // entityManager.find(EasyMock.isA(NumericEntity.class.getClass()),
  85. // EasyMock
  86. // .isA(String.class));
  87. // org.easymock.EasyMock.expectLastCall().andAnswer(new
  88. // IAnswer<Object>() {
  89. // public Object answer() throws Throwable {
  90. // Object[] args = org.easymock.EasyMock.getCurrentArguments();
  91. // if (args[0] == null) {
  92. // throw new UnsupportedOperationException(
  93. // "asked to persist a null object");
  94. // }
  95. // return args[0];
  96. // }
  97. // }).anyTimes();
  98. //
  99. // entityManager.find(EasyMock.isA(Class.class), EasyMock
  100. // .isA(Integer.class));
  101. // org.easymock.EasyMock.expectLastCall().andAnswer(new
  102. // IAnswer<Object>() {
  103. // public Object answer() throws Throwable {
  104. // Object[] args = org.easymock.EasyMock.getCurrentArguments();
  105. // if (args[0] == null) {
  106. // throw new UnsupportedOperationException(
  107. // "asked to persist a null object");
  108. // }
  109. // return args[0];
  110. // }
  111. // }).anyTimes();
  112. AdminServiceImplMockTest.adminServiceImpl = createMockedAdminService(createMockJpaTemplate(createMockEntityManager()));
  113. System.err.println();
  114. // EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
  115. // public Object answer() throws Throwable {
  116. // throw new RuntimeException("no fucking way");
  117. //
  118. // // Object[] args = org.easymock.EasyMock.getCurrentArguments();
  119. // // return ((Class)args[0]).newInstance();
  120. // }
  121. // }).anyTimes();
  122. // UUIDEntity simpleEntity = new UUIDEntity();
  123. // simpleEntity.setTitle("test entity");
  124. // entityManager.persist(simpleEntity);
  125. // adminServiceImpl.setEntityManager(entityManager);
  126. }
  127. private static AdminServiceImpl createMockedAdminService(
  128. JpaTemplate jpaTemplate) throws NoSuchMethodException {
  129. // Method method = AdminServiceImpl.class.getMethod(
  130. // "internalCreateJpaTemplate", EntityManagerFactory.class);
  131. // AdminServiceImpl adminServiceImpl = EasyMock.createMock(
  132. // AdminServiceImpl.class, method);
  133. // EasyMock.expect(
  134. // adminServiceImpl.internalCreateJpaTemplate(EasyMock
  135. // .isA(EntityManagerFactory.class))).andAnswer(
  136. // new IAnswer<? extends JpaTemplate>() {
  137. // public JpaTemplate answer() throws Throwable {
  138. // Object[] args = org.easymock.EasyMock
  139. // .getCurrentArguments();
  140. // // return ((Class)args[0]).newInstance();
  141. // throw new UnsupportedOperationException(
  142. // "fuck you !!!");
  143. // }
  144. // });
  145. // EasyMock.replay(adminServiceImpl);
  146. AdminServiceImpl i = new AdminServiceImpl();
  147. i.setJpaTemplate(jpaTemplate);
  148. return i;
  149. }
  150. private static JpaTemplate createMockJpaTemplate(EntityManager entityManager) {
  151. JpaTemplate jpaTemplate = new JpaTemplate();// EasyMock.createMock(JpaTemplate.class);
  152. jpaTemplate.setExposeNativeEntityManager(true);
  153. jpaTemplate.setEntityManager(entityManager);
  154. // jpaTemplate.execute(EasyMock.isA(JpaCallback.class));
  155. // EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
  156. // public Object answer() throws Throwable {
  157. // Object[] args = EasyMock.getCurrentArguments();
  158. // if (args[0] == null) {
  159. // throw new UnsupportedOperationException(
  160. // "asked to persist a null object");
  161. // }
  162. // return args[0];
  163. // }
  164. // }).anyTimes();
  165. // EasyMock.replay(jpaTemplate);
  166. return jpaTemplate;
  167. }
  168. private static EntityManager createMockEntityManager() {
  169. @SuppressWarnings("unused")
  170. Class<NumericEntity> ne = NumericEntity.class;
  171. EntityManager entityManager = EasyMock.createMock(EntityManager.class);
  172. EasyMock.expect(
  173. entityManager.find(isClass(Class.class), EasyMock.isNull()))
  174. .andAnswer(new IAnswer<Object>() {
  175. public Object answer() throws Throwable {
  176. throw new IllegalArgumentException(
  177. "id to load is required for loading");
  178. }
  179. }).anyTimes();
  180. EasyMock.expect(
  181. entityManager.find(isClass(NumericEntity.class), EasyMock
  182. .isA(Integer.class))).andAnswer(
  183. new IAnswer<NumericEntity>() {
  184. public NumericEntity answer() throws Throwable {
  185. Object[] args = org.easymock.EasyMock
  186. .getCurrentArguments();
  187. NumericEntity newInstance = (NumericEntity) ((Class<?>) args[0])
  188. .newInstance();
  189. newInstance.setPk((Integer) args[1]);
  190. newInstance.setTitle("random" + new Date().toString());
  191. return newInstance;
  192. }
  193. }).anyTimes();
  194. EasyMock.expect(
  195. entityManager.find(isClass(UUIDEntity.class), EasyMock
  196. .isA(String.class))).andAnswer(
  197. new IAnswer<UUIDEntity>() {
  198. public UUIDEntity answer() throws Throwable {
  199. Object[] args = org.easymock.EasyMock
  200. .getCurrentArguments();
  201. UUIDEntity newInstance = (UUIDEntity) ((Class<?>) args[0])
  202. .newInstance();
  203. newInstance.setPk((String) args[1]);
  204. newInstance.setTitle("random" + new Date().toString());
  205. return newInstance;
  206. }
  207. }).anyTimes();
  208. entityManager.persist(EasyMock.isA(Object.class));
  209. EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
  210. public Object answer() throws Throwable {
  211. Object[] args = org.easymock.EasyMock.getCurrentArguments();
  212. Class<? extends Object> class1 = args[0].getClass();
  213. Field field = class1.getDeclaredField("pk");
  214. field.setAccessible(true);
  215. Object object = field.get(args[0]);
  216. if (object == null) {
  217. throw new IllegalArgumentException("pk is not set");
  218. }
  219. // UUIDEntity newInstance = (UUIDEntity) ((Class<?>) args[0])
  220. // .newInstance();
  221. // newInstance.setPk((String) args[1]);
  222. // newInstance.setTitle("random" + new Date().toString());
  223. return null;
  224. }
  225. }).anyTimes();
  226. entityManager.merge(EasyMock.isA(UUIDEntity.class));
  227. EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {
  228. public Object answer() throws Throwable {
  229. @SuppressWarnings("unused")
  230. Object[] args = org.easymock.EasyMock.getCurrentArguments();
  231. // UUIDEntity newInstance = (UUIDEntity) ((Class<?>) args[0])
  232. // .newInstance();
  233. // newInstance.setPk((String) args[1]);
  234. // newInstance.setTitle("random" + new Date().toString());
  235. return null;
  236. }
  237. }).anyTimes();
  238. entityManager.flush();
  239. EasyMock.expectLastCall().anyTimes();
  240. entityManager.clear();
  241. org.easymock.EasyMock.expectLastCall().anyTimes();
  242. EasyMock.replay(entityManager);
  243. EasyMock.verify(entityManager);
  244. return entityManager;
  245. }
  246. private static Class<Object> isClass(Class<?> clazz) {
  247. AdminServiceImplMockTest.IsClass matcher = new IsClass(clazz);
  248. EasyMock.reportMatcher(matcher);
  249. return null;
  250. }
  251. @AfterClass
  252. public static void tearDownAfterClass() throws Exception {
  253. }
  254. @Before
  255. public void setUp() throws Exception {
  256. }
  257. @After
  258. public void tearDown() throws Exception {
  259. }
  260. @Test
  261. public void testLogin() {
  262. }
  263. @Test
  264. public void testNonce() {
  265. }
  266. @Test
  267. public void testRemove() {
  268. }
  269. @Test
  270. public void testLoad() {
  271. Object load = adminServiceImpl.load(NumericEntity.class.getName(), 14);
  272. assertNotNull(load);
  273. }
  274. @Test
  275. public void testLoadOneToOne() {
  276. // adminServiceImpl.loadOneToOne(uuid, child_clazz, relationship_name,
  277. // parentId, fields, loadOptions)
  278. }
  279. @Test
  280. public void testGetAll() {
  281. }
  282. @Test
  283. public void testSave() {
  284. UUIDEntity e = new UUIDEntity();
  285. e.setTitle("test");
  286. adminServiceImpl.save(e);
  287. UUIDEntity e2 = new UUIDEntity();
  288. e.setTitle("test2");
  289. e2.setPk("existing-key");
  290. adminServiceImpl.save(e2);
  291. }
  292. @Test
  293. public void testIsSimple() {
  294. }
  295. @Test
  296. public void testSaveOneToOne() {
  297. }
  298. @Test
  299. public void testLoadOneToMany() {
  300. }
  301. @Test
  302. public void testSaveOneToMany() {
  303. }
  304. @Test
  305. public void testSaveManyToMany() {
  306. }
  307. @Test
  308. public void testRemoveImage() {
  309. }
  310. @Test
  311. public void testRemovePdf() {
  312. }
  313. @Test
  314. public void testRemoveVideo() {
  315. }
  316. @Test
  317. public void testLoadImage() {
  318. }
  319. @Test
  320. public void testSaveSingleLink() {
  321. }
  322. @Test
  323. public void testDeleteSingleLink() {
  324. }
  325. @Test
  326. public void testSaveOneToManyImages() {
  327. }
  328. @Test
  329. public void testLoadPdf() {
  330. }
  331. @Test
  332. public void testLoadVideo() {
  333. }
  334. @Test
  335. public void testSaveOneToManyPdfs() {
  336. }
  337. @Test
  338. public void testSaveOneToManyVideos() {
  339. }
  340. @Test
  341. public void testUploadPdf() {
  342. }
  343. @Test
  344. public void testUploadVideo() {
  345. }
  346. @Test
  347. public void testLoadImageThumb() {
  348. }
  349. @Test
  350. public void testLoadManyToMany() {
  351. }
  352. }