PageRenderTime 56ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/apps/mail-reader/mail-reader-test/src/testIntegration/java/com/liferay/mail/reader/service/persistence/test/FolderPersistenceTest.java

http://github.com/liferay/liferay-portal
Java | 452 lines | 306 code | 130 blank | 16 comment | 1 complexity | 726fde0e914496f6e52e899a4e6fc1ef MD5 | raw file
Possible License(s): LGPL-2.0
  1. /**
  2. * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 2.1 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. */
  14. package com.liferay.mail.reader.service.persistence.test;
  15. import com.liferay.arquillian.extension.junit.bridge.junit.Arquillian;
  16. import com.liferay.mail.reader.exception.NoSuchFolderException;
  17. import com.liferay.mail.reader.model.Folder;
  18. import com.liferay.mail.reader.service.FolderLocalServiceUtil;
  19. import com.liferay.mail.reader.service.persistence.FolderPersistence;
  20. import com.liferay.mail.reader.service.persistence.FolderUtil;
  21. import com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery;
  22. import com.liferay.portal.kernel.dao.orm.DynamicQuery;
  23. import com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil;
  24. import com.liferay.portal.kernel.dao.orm.ProjectionFactoryUtil;
  25. import com.liferay.portal.kernel.dao.orm.QueryUtil;
  26. import com.liferay.portal.kernel.dao.orm.RestrictionsFactoryUtil;
  27. import com.liferay.portal.kernel.test.ReflectionTestUtil;
  28. import com.liferay.portal.kernel.test.rule.AggregateTestRule;
  29. import com.liferay.portal.kernel.test.rule.TransactionalTestRule;
  30. import com.liferay.portal.kernel.test.util.RandomTestUtil;
  31. import com.liferay.portal.kernel.transaction.Propagation;
  32. import com.liferay.portal.kernel.util.IntegerWrapper;
  33. import com.liferay.portal.kernel.util.OrderByComparator;
  34. import com.liferay.portal.kernel.util.OrderByComparatorFactoryUtil;
  35. import com.liferay.portal.kernel.util.StringPool;
  36. import com.liferay.portal.kernel.util.Time;
  37. import com.liferay.portal.test.rule.LiferayIntegrationTestRule;
  38. import com.liferay.portal.test.rule.PersistenceTestRule;
  39. import org.junit.After;
  40. import org.junit.Assert;
  41. import org.junit.Before;
  42. import org.junit.ClassRule;
  43. import org.junit.Rule;
  44. import org.junit.Test;
  45. import org.junit.runner.RunWith;
  46. import java.io.Serializable;
  47. import java.util.ArrayList;
  48. import java.util.HashSet;
  49. import java.util.Iterator;
  50. import java.util.List;
  51. import java.util.Map;
  52. import java.util.Objects;
  53. import java.util.Set;
  54. /**
  55. * @generated
  56. */
  57. @RunWith(Arquillian.class)
  58. public class FolderPersistenceTest {
  59. @ClassRule
  60. @Rule
  61. public static final AggregateTestRule aggregateTestRule = new AggregateTestRule(new LiferayIntegrationTestRule(),
  62. PersistenceTestRule.INSTANCE,
  63. new TransactionalTestRule(Propagation.REQUIRED));
  64. @Before
  65. public void setUp() {
  66. _persistence = FolderUtil.getPersistence();
  67. Class<?> clazz = _persistence.getClass();
  68. _dynamicQueryClassLoader = clazz.getClassLoader();
  69. }
  70. @After
  71. public void tearDown() throws Exception {
  72. Iterator<Folder> iterator = _folders.iterator();
  73. while (iterator.hasNext()) {
  74. _persistence.remove(iterator.next());
  75. iterator.remove();
  76. }
  77. }
  78. @Test
  79. public void testCreate() throws Exception {
  80. long pk = RandomTestUtil.nextLong();
  81. Folder folder = _persistence.create(pk);
  82. Assert.assertNotNull(folder);
  83. Assert.assertEquals(folder.getPrimaryKey(), pk);
  84. }
  85. @Test
  86. public void testRemove() throws Exception {
  87. Folder newFolder = addFolder();
  88. _persistence.remove(newFolder);
  89. Folder existingFolder = _persistence.fetchByPrimaryKey(newFolder.getPrimaryKey());
  90. Assert.assertNull(existingFolder);
  91. }
  92. @Test
  93. public void testUpdateNew() throws Exception {
  94. addFolder();
  95. }
  96. @Test
  97. public void testUpdateExisting() throws Exception {
  98. long pk = RandomTestUtil.nextLong();
  99. Folder newFolder = _persistence.create(pk);
  100. newFolder.setCompanyId(RandomTestUtil.nextLong());
  101. newFolder.setUserId(RandomTestUtil.nextLong());
  102. newFolder.setUserName(RandomTestUtil.randomString());
  103. newFolder.setCreateDate(RandomTestUtil.nextDate());
  104. newFolder.setModifiedDate(RandomTestUtil.nextDate());
  105. newFolder.setAccountId(RandomTestUtil.nextLong());
  106. newFolder.setFullName(RandomTestUtil.randomString());
  107. newFolder.setDisplayName(RandomTestUtil.randomString());
  108. newFolder.setRemoteMessageCount(RandomTestUtil.nextInt());
  109. _folders.add(_persistence.update(newFolder));
  110. Folder existingFolder = _persistence.findByPrimaryKey(newFolder.getPrimaryKey());
  111. Assert.assertEquals(existingFolder.getFolderId(),
  112. newFolder.getFolderId());
  113. Assert.assertEquals(existingFolder.getCompanyId(),
  114. newFolder.getCompanyId());
  115. Assert.assertEquals(existingFolder.getUserId(), newFolder.getUserId());
  116. Assert.assertEquals(existingFolder.getUserName(),
  117. newFolder.getUserName());
  118. Assert.assertEquals(Time.getShortTimestamp(
  119. existingFolder.getCreateDate()),
  120. Time.getShortTimestamp(newFolder.getCreateDate()));
  121. Assert.assertEquals(Time.getShortTimestamp(
  122. existingFolder.getModifiedDate()),
  123. Time.getShortTimestamp(newFolder.getModifiedDate()));
  124. Assert.assertEquals(existingFolder.getAccountId(),
  125. newFolder.getAccountId());
  126. Assert.assertEquals(existingFolder.getFullName(),
  127. newFolder.getFullName());
  128. Assert.assertEquals(existingFolder.getDisplayName(),
  129. newFolder.getDisplayName());
  130. Assert.assertEquals(existingFolder.getRemoteMessageCount(),
  131. newFolder.getRemoteMessageCount());
  132. }
  133. @Test
  134. public void testCountByAccountId() throws Exception {
  135. _persistence.countByAccountId(RandomTestUtil.nextLong());
  136. _persistence.countByAccountId(0L);
  137. }
  138. @Test
  139. public void testCountByA_F() throws Exception {
  140. _persistence.countByA_F(RandomTestUtil.nextLong(), StringPool.BLANK);
  141. _persistence.countByA_F(0L, StringPool.NULL);
  142. _persistence.countByA_F(0L, (String)null);
  143. }
  144. @Test
  145. public void testFindByPrimaryKeyExisting() throws Exception {
  146. Folder newFolder = addFolder();
  147. Folder existingFolder = _persistence.findByPrimaryKey(newFolder.getPrimaryKey());
  148. Assert.assertEquals(existingFolder, newFolder);
  149. }
  150. @Test(expected = NoSuchFolderException.class)
  151. public void testFindByPrimaryKeyMissing() throws Exception {
  152. long pk = RandomTestUtil.nextLong();
  153. _persistence.findByPrimaryKey(pk);
  154. }
  155. @Test
  156. public void testFindAll() throws Exception {
  157. _persistence.findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS,
  158. getOrderByComparator());
  159. }
  160. protected OrderByComparator<Folder> getOrderByComparator() {
  161. return OrderByComparatorFactoryUtil.create("Mail_Folder", "folderId",
  162. true, "companyId", true, "userId", true, "userName", true,
  163. "createDate", true, "modifiedDate", true, "accountId", true,
  164. "fullName", true, "displayName", true, "remoteMessageCount", true);
  165. }
  166. @Test
  167. public void testFetchByPrimaryKeyExisting() throws Exception {
  168. Folder newFolder = addFolder();
  169. Folder existingFolder = _persistence.fetchByPrimaryKey(newFolder.getPrimaryKey());
  170. Assert.assertEquals(existingFolder, newFolder);
  171. }
  172. @Test
  173. public void testFetchByPrimaryKeyMissing() throws Exception {
  174. long pk = RandomTestUtil.nextLong();
  175. Folder missingFolder = _persistence.fetchByPrimaryKey(pk);
  176. Assert.assertNull(missingFolder);
  177. }
  178. @Test
  179. public void testFetchByPrimaryKeysWithMultiplePrimaryKeysWhereAllPrimaryKeysExist()
  180. throws Exception {
  181. Folder newFolder1 = addFolder();
  182. Folder newFolder2 = addFolder();
  183. Set<Serializable> primaryKeys = new HashSet<Serializable>();
  184. primaryKeys.add(newFolder1.getPrimaryKey());
  185. primaryKeys.add(newFolder2.getPrimaryKey());
  186. Map<Serializable, Folder> folders = _persistence.fetchByPrimaryKeys(primaryKeys);
  187. Assert.assertEquals(2, folders.size());
  188. Assert.assertEquals(newFolder1, folders.get(newFolder1.getPrimaryKey()));
  189. Assert.assertEquals(newFolder2, folders.get(newFolder2.getPrimaryKey()));
  190. }
  191. @Test
  192. public void testFetchByPrimaryKeysWithMultiplePrimaryKeysWhereNoPrimaryKeysExist()
  193. throws Exception {
  194. long pk1 = RandomTestUtil.nextLong();
  195. long pk2 = RandomTestUtil.nextLong();
  196. Set<Serializable> primaryKeys = new HashSet<Serializable>();
  197. primaryKeys.add(pk1);
  198. primaryKeys.add(pk2);
  199. Map<Serializable, Folder> folders = _persistence.fetchByPrimaryKeys(primaryKeys);
  200. Assert.assertTrue(folders.isEmpty());
  201. }
  202. @Test
  203. public void testFetchByPrimaryKeysWithMultiplePrimaryKeysWhereSomePrimaryKeysExist()
  204. throws Exception {
  205. Folder newFolder = addFolder();
  206. long pk = RandomTestUtil.nextLong();
  207. Set<Serializable> primaryKeys = new HashSet<Serializable>();
  208. primaryKeys.add(newFolder.getPrimaryKey());
  209. primaryKeys.add(pk);
  210. Map<Serializable, Folder> folders = _persistence.fetchByPrimaryKeys(primaryKeys);
  211. Assert.assertEquals(1, folders.size());
  212. Assert.assertEquals(newFolder, folders.get(newFolder.getPrimaryKey()));
  213. }
  214. @Test
  215. public void testFetchByPrimaryKeysWithNoPrimaryKeys()
  216. throws Exception {
  217. Set<Serializable> primaryKeys = new HashSet<Serializable>();
  218. Map<Serializable, Folder> folders = _persistence.fetchByPrimaryKeys(primaryKeys);
  219. Assert.assertTrue(folders.isEmpty());
  220. }
  221. @Test
  222. public void testFetchByPrimaryKeysWithOnePrimaryKey()
  223. throws Exception {
  224. Folder newFolder = addFolder();
  225. Set<Serializable> primaryKeys = new HashSet<Serializable>();
  226. primaryKeys.add(newFolder.getPrimaryKey());
  227. Map<Serializable, Folder> folders = _persistence.fetchByPrimaryKeys(primaryKeys);
  228. Assert.assertEquals(1, folders.size());
  229. Assert.assertEquals(newFolder, folders.get(newFolder.getPrimaryKey()));
  230. }
  231. @Test
  232. public void testActionableDynamicQuery() throws Exception {
  233. final IntegerWrapper count = new IntegerWrapper();
  234. ActionableDynamicQuery actionableDynamicQuery = FolderLocalServiceUtil.getActionableDynamicQuery();
  235. actionableDynamicQuery.setPerformActionMethod(new ActionableDynamicQuery.PerformActionMethod<Folder>() {
  236. @Override
  237. public void performAction(Folder folder) {
  238. Assert.assertNotNull(folder);
  239. count.increment();
  240. }
  241. });
  242. actionableDynamicQuery.performActions();
  243. Assert.assertEquals(count.getValue(), _persistence.countAll());
  244. }
  245. @Test
  246. public void testDynamicQueryByPrimaryKeyExisting()
  247. throws Exception {
  248. Folder newFolder = addFolder();
  249. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(Folder.class,
  250. _dynamicQueryClassLoader);
  251. dynamicQuery.add(RestrictionsFactoryUtil.eq("folderId",
  252. newFolder.getFolderId()));
  253. List<Folder> result = _persistence.findWithDynamicQuery(dynamicQuery);
  254. Assert.assertEquals(1, result.size());
  255. Folder existingFolder = result.get(0);
  256. Assert.assertEquals(existingFolder, newFolder);
  257. }
  258. @Test
  259. public void testDynamicQueryByPrimaryKeyMissing() throws Exception {
  260. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(Folder.class,
  261. _dynamicQueryClassLoader);
  262. dynamicQuery.add(RestrictionsFactoryUtil.eq("folderId",
  263. RandomTestUtil.nextLong()));
  264. List<Folder> result = _persistence.findWithDynamicQuery(dynamicQuery);
  265. Assert.assertEquals(0, result.size());
  266. }
  267. @Test
  268. public void testDynamicQueryByProjectionExisting()
  269. throws Exception {
  270. Folder newFolder = addFolder();
  271. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(Folder.class,
  272. _dynamicQueryClassLoader);
  273. dynamicQuery.setProjection(ProjectionFactoryUtil.property("folderId"));
  274. Object newFolderId = newFolder.getFolderId();
  275. dynamicQuery.add(RestrictionsFactoryUtil.in("folderId",
  276. new Object[] { newFolderId }));
  277. List<Object> result = _persistence.findWithDynamicQuery(dynamicQuery);
  278. Assert.assertEquals(1, result.size());
  279. Object existingFolderId = result.get(0);
  280. Assert.assertEquals(existingFolderId, newFolderId);
  281. }
  282. @Test
  283. public void testDynamicQueryByProjectionMissing() throws Exception {
  284. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(Folder.class,
  285. _dynamicQueryClassLoader);
  286. dynamicQuery.setProjection(ProjectionFactoryUtil.property("folderId"));
  287. dynamicQuery.add(RestrictionsFactoryUtil.in("folderId",
  288. new Object[] { RandomTestUtil.nextLong() }));
  289. List<Object> result = _persistence.findWithDynamicQuery(dynamicQuery);
  290. Assert.assertEquals(0, result.size());
  291. }
  292. @Test
  293. public void testResetOriginalValues() throws Exception {
  294. Folder newFolder = addFolder();
  295. _persistence.clearCache();
  296. Folder existingFolder = _persistence.findByPrimaryKey(newFolder.getPrimaryKey());
  297. Assert.assertEquals(Long.valueOf(existingFolder.getAccountId()),
  298. ReflectionTestUtil.<Long>invoke(existingFolder,
  299. "getOriginalAccountId", new Class<?>[0]));
  300. Assert.assertTrue(Objects.equals(existingFolder.getFullName(),
  301. ReflectionTestUtil.invoke(existingFolder,
  302. "getOriginalFullName", new Class<?>[0])));
  303. }
  304. protected Folder addFolder() throws Exception {
  305. long pk = RandomTestUtil.nextLong();
  306. Folder folder = _persistence.create(pk);
  307. folder.setCompanyId(RandomTestUtil.nextLong());
  308. folder.setUserId(RandomTestUtil.nextLong());
  309. folder.setUserName(RandomTestUtil.randomString());
  310. folder.setCreateDate(RandomTestUtil.nextDate());
  311. folder.setModifiedDate(RandomTestUtil.nextDate());
  312. folder.setAccountId(RandomTestUtil.nextLong());
  313. folder.setFullName(RandomTestUtil.randomString());
  314. folder.setDisplayName(RandomTestUtil.randomString());
  315. folder.setRemoteMessageCount(RandomTestUtil.nextInt());
  316. _folders.add(_persistence.update(folder));
  317. return folder;
  318. }
  319. private List<Folder> _folders = new ArrayList<Folder>();
  320. private FolderPersistence _persistence;
  321. private ClassLoader _dynamicQueryClassLoader;
  322. }