/portal-impl/test/integration/com/liferay/portlet/bookmarks/service/persistence/BookmarksFolderPersistenceTest.java

https://github.com/azzazzel/liferay-portal · Java · 305 lines · 199 code · 90 blank · 16 comment · 1 complexity · 618ec8698c31cc1f95ddff8aace5b1d3 MD5 · raw file

  1. /**
  2. * Copyright (c) 2000-2012 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.portlet.bookmarks.service.persistence;
  15. import com.liferay.portal.kernel.bean.PortalBeanLocatorUtil;
  16. import com.liferay.portal.kernel.dao.orm.DynamicQuery;
  17. import com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil;
  18. import com.liferay.portal.kernel.dao.orm.ProjectionFactoryUtil;
  19. import com.liferay.portal.kernel.dao.orm.RestrictionsFactoryUtil;
  20. import com.liferay.portal.kernel.util.Time;
  21. import com.liferay.portal.kernel.util.Validator;
  22. import com.liferay.portal.service.ServiceTestUtil;
  23. import com.liferay.portal.service.persistence.PersistenceExecutionTestListener;
  24. import com.liferay.portal.test.ExecutionTestListeners;
  25. import com.liferay.portal.test.LiferayIntegrationJUnitTestRunner;
  26. import com.liferay.portal.util.PropsValues;
  27. import com.liferay.portlet.bookmarks.NoSuchFolderException;
  28. import com.liferay.portlet.bookmarks.model.BookmarksFolder;
  29. import com.liferay.portlet.bookmarks.model.impl.BookmarksFolderModelImpl;
  30. import org.junit.Assert;
  31. import org.junit.Before;
  32. import org.junit.Test;
  33. import org.junit.runner.RunWith;
  34. import java.util.List;
  35. /**
  36. * @author Brian Wing Shun Chan
  37. */
  38. @ExecutionTestListeners(listeners = {
  39. PersistenceExecutionTestListener.class})
  40. @RunWith(LiferayIntegrationJUnitTestRunner.class)
  41. public class BookmarksFolderPersistenceTest {
  42. @Before
  43. public void setUp() throws Exception {
  44. _persistence = (BookmarksFolderPersistence)PortalBeanLocatorUtil.locate(BookmarksFolderPersistence.class.getName());
  45. }
  46. @Test
  47. public void testCreate() throws Exception {
  48. long pk = ServiceTestUtil.nextLong();
  49. BookmarksFolder bookmarksFolder = _persistence.create(pk);
  50. Assert.assertNotNull(bookmarksFolder);
  51. Assert.assertEquals(bookmarksFolder.getPrimaryKey(), pk);
  52. }
  53. @Test
  54. public void testRemove() throws Exception {
  55. BookmarksFolder newBookmarksFolder = addBookmarksFolder();
  56. _persistence.remove(newBookmarksFolder);
  57. BookmarksFolder existingBookmarksFolder = _persistence.fetchByPrimaryKey(newBookmarksFolder.getPrimaryKey());
  58. Assert.assertNull(existingBookmarksFolder);
  59. }
  60. @Test
  61. public void testUpdateNew() throws Exception {
  62. addBookmarksFolder();
  63. }
  64. @Test
  65. public void testUpdateExisting() throws Exception {
  66. long pk = ServiceTestUtil.nextLong();
  67. BookmarksFolder newBookmarksFolder = _persistence.create(pk);
  68. newBookmarksFolder.setUuid(ServiceTestUtil.randomString());
  69. newBookmarksFolder.setGroupId(ServiceTestUtil.nextLong());
  70. newBookmarksFolder.setCompanyId(ServiceTestUtil.nextLong());
  71. newBookmarksFolder.setUserId(ServiceTestUtil.nextLong());
  72. newBookmarksFolder.setUserName(ServiceTestUtil.randomString());
  73. newBookmarksFolder.setCreateDate(ServiceTestUtil.nextDate());
  74. newBookmarksFolder.setModifiedDate(ServiceTestUtil.nextDate());
  75. newBookmarksFolder.setResourceBlockId(ServiceTestUtil.nextLong());
  76. newBookmarksFolder.setParentFolderId(ServiceTestUtil.nextLong());
  77. newBookmarksFolder.setName(ServiceTestUtil.randomString());
  78. newBookmarksFolder.setDescription(ServiceTestUtil.randomString());
  79. _persistence.update(newBookmarksFolder, false);
  80. BookmarksFolder existingBookmarksFolder = _persistence.findByPrimaryKey(newBookmarksFolder.getPrimaryKey());
  81. Assert.assertEquals(existingBookmarksFolder.getUuid(),
  82. newBookmarksFolder.getUuid());
  83. Assert.assertEquals(existingBookmarksFolder.getFolderId(),
  84. newBookmarksFolder.getFolderId());
  85. Assert.assertEquals(existingBookmarksFolder.getGroupId(),
  86. newBookmarksFolder.getGroupId());
  87. Assert.assertEquals(existingBookmarksFolder.getCompanyId(),
  88. newBookmarksFolder.getCompanyId());
  89. Assert.assertEquals(existingBookmarksFolder.getUserId(),
  90. newBookmarksFolder.getUserId());
  91. Assert.assertEquals(existingBookmarksFolder.getUserName(),
  92. newBookmarksFolder.getUserName());
  93. Assert.assertEquals(Time.getShortTimestamp(
  94. existingBookmarksFolder.getCreateDate()),
  95. Time.getShortTimestamp(newBookmarksFolder.getCreateDate()));
  96. Assert.assertEquals(Time.getShortTimestamp(
  97. existingBookmarksFolder.getModifiedDate()),
  98. Time.getShortTimestamp(newBookmarksFolder.getModifiedDate()));
  99. Assert.assertEquals(existingBookmarksFolder.getResourceBlockId(),
  100. newBookmarksFolder.getResourceBlockId());
  101. Assert.assertEquals(existingBookmarksFolder.getParentFolderId(),
  102. newBookmarksFolder.getParentFolderId());
  103. Assert.assertEquals(existingBookmarksFolder.getName(),
  104. newBookmarksFolder.getName());
  105. Assert.assertEquals(existingBookmarksFolder.getDescription(),
  106. newBookmarksFolder.getDescription());
  107. }
  108. @Test
  109. public void testFindByPrimaryKeyExisting() throws Exception {
  110. BookmarksFolder newBookmarksFolder = addBookmarksFolder();
  111. BookmarksFolder existingBookmarksFolder = _persistence.findByPrimaryKey(newBookmarksFolder.getPrimaryKey());
  112. Assert.assertEquals(existingBookmarksFolder, newBookmarksFolder);
  113. }
  114. @Test
  115. public void testFindByPrimaryKeyMissing() throws Exception {
  116. long pk = ServiceTestUtil.nextLong();
  117. try {
  118. _persistence.findByPrimaryKey(pk);
  119. Assert.fail("Missing entity did not throw NoSuchFolderException");
  120. }
  121. catch (NoSuchFolderException nsee) {
  122. }
  123. }
  124. @Test
  125. public void testFetchByPrimaryKeyExisting() throws Exception {
  126. BookmarksFolder newBookmarksFolder = addBookmarksFolder();
  127. BookmarksFolder existingBookmarksFolder = _persistence.fetchByPrimaryKey(newBookmarksFolder.getPrimaryKey());
  128. Assert.assertEquals(existingBookmarksFolder, newBookmarksFolder);
  129. }
  130. @Test
  131. public void testFetchByPrimaryKeyMissing() throws Exception {
  132. long pk = ServiceTestUtil.nextLong();
  133. BookmarksFolder missingBookmarksFolder = _persistence.fetchByPrimaryKey(pk);
  134. Assert.assertNull(missingBookmarksFolder);
  135. }
  136. @Test
  137. public void testDynamicQueryByPrimaryKeyExisting()
  138. throws Exception {
  139. BookmarksFolder newBookmarksFolder = addBookmarksFolder();
  140. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(BookmarksFolder.class,
  141. BookmarksFolder.class.getClassLoader());
  142. dynamicQuery.add(RestrictionsFactoryUtil.eq("folderId",
  143. newBookmarksFolder.getFolderId()));
  144. List<BookmarksFolder> result = _persistence.findWithDynamicQuery(dynamicQuery);
  145. Assert.assertEquals(1, result.size());
  146. BookmarksFolder existingBookmarksFolder = result.get(0);
  147. Assert.assertEquals(existingBookmarksFolder, newBookmarksFolder);
  148. }
  149. @Test
  150. public void testDynamicQueryByPrimaryKeyMissing() throws Exception {
  151. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(BookmarksFolder.class,
  152. BookmarksFolder.class.getClassLoader());
  153. dynamicQuery.add(RestrictionsFactoryUtil.eq("folderId",
  154. ServiceTestUtil.nextLong()));
  155. List<BookmarksFolder> result = _persistence.findWithDynamicQuery(dynamicQuery);
  156. Assert.assertEquals(0, result.size());
  157. }
  158. @Test
  159. public void testDynamicQueryByProjectionExisting()
  160. throws Exception {
  161. BookmarksFolder newBookmarksFolder = addBookmarksFolder();
  162. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(BookmarksFolder.class,
  163. BookmarksFolder.class.getClassLoader());
  164. dynamicQuery.setProjection(ProjectionFactoryUtil.property("folderId"));
  165. Object newFolderId = newBookmarksFolder.getFolderId();
  166. dynamicQuery.add(RestrictionsFactoryUtil.in("folderId",
  167. new Object[] { newFolderId }));
  168. List<Object> result = _persistence.findWithDynamicQuery(dynamicQuery);
  169. Assert.assertEquals(1, result.size());
  170. Object existingFolderId = result.get(0);
  171. Assert.assertEquals(existingFolderId, newFolderId);
  172. }
  173. @Test
  174. public void testDynamicQueryByProjectionMissing() throws Exception {
  175. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(BookmarksFolder.class,
  176. BookmarksFolder.class.getClassLoader());
  177. dynamicQuery.setProjection(ProjectionFactoryUtil.property("folderId"));
  178. dynamicQuery.add(RestrictionsFactoryUtil.in("folderId",
  179. new Object[] { ServiceTestUtil.nextLong() }));
  180. List<Object> result = _persistence.findWithDynamicQuery(dynamicQuery);
  181. Assert.assertEquals(0, result.size());
  182. }
  183. @Test
  184. public void testResetOriginalValues() throws Exception {
  185. if (!PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
  186. return;
  187. }
  188. BookmarksFolder newBookmarksFolder = addBookmarksFolder();
  189. _persistence.clearCache();
  190. BookmarksFolderModelImpl existingBookmarksFolderModelImpl = (BookmarksFolderModelImpl)_persistence.findByPrimaryKey(newBookmarksFolder.getPrimaryKey());
  191. Assert.assertTrue(Validator.equals(
  192. existingBookmarksFolderModelImpl.getUuid(),
  193. existingBookmarksFolderModelImpl.getOriginalUuid()));
  194. Assert.assertEquals(existingBookmarksFolderModelImpl.getGroupId(),
  195. existingBookmarksFolderModelImpl.getOriginalGroupId());
  196. }
  197. protected BookmarksFolder addBookmarksFolder() throws Exception {
  198. long pk = ServiceTestUtil.nextLong();
  199. BookmarksFolder bookmarksFolder = _persistence.create(pk);
  200. bookmarksFolder.setUuid(ServiceTestUtil.randomString());
  201. bookmarksFolder.setGroupId(ServiceTestUtil.nextLong());
  202. bookmarksFolder.setCompanyId(ServiceTestUtil.nextLong());
  203. bookmarksFolder.setUserId(ServiceTestUtil.nextLong());
  204. bookmarksFolder.setUserName(ServiceTestUtil.randomString());
  205. bookmarksFolder.setCreateDate(ServiceTestUtil.nextDate());
  206. bookmarksFolder.setModifiedDate(ServiceTestUtil.nextDate());
  207. bookmarksFolder.setResourceBlockId(ServiceTestUtil.nextLong());
  208. bookmarksFolder.setParentFolderId(ServiceTestUtil.nextLong());
  209. bookmarksFolder.setName(ServiceTestUtil.randomString());
  210. bookmarksFolder.setDescription(ServiceTestUtil.randomString());
  211. _persistence.update(bookmarksFolder, false);
  212. return bookmarksFolder;
  213. }
  214. private BookmarksFolderPersistence _persistence;
  215. }