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

https://github.com/viktorkovacs/liferay-portal-trunk · Java · 262 lines · 177 code · 69 blank · 16 comment · 1 complexity · 7d2265a8288dc0f5b80a526e456718ce MD5 · raw file

  1. /**
  2. * Copyright (c) 2000-2011 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.persistence.BasePersistenceTestCase;
  23. import com.liferay.portal.util.PropsValues;
  24. import com.liferay.portlet.bookmarks.NoSuchEntryException;
  25. import com.liferay.portlet.bookmarks.model.BookmarksEntry;
  26. import com.liferay.portlet.bookmarks.model.impl.BookmarksEntryModelImpl;
  27. import java.util.List;
  28. /**
  29. * @author Brian Wing Shun Chan
  30. */
  31. public class BookmarksEntryPersistenceTest extends BasePersistenceTestCase {
  32. public void setUp() throws Exception {
  33. super.setUp();
  34. _persistence = (BookmarksEntryPersistence)PortalBeanLocatorUtil.locate(BookmarksEntryPersistence.class.getName());
  35. }
  36. public void testCreate() throws Exception {
  37. long pk = nextLong();
  38. BookmarksEntry bookmarksEntry = _persistence.create(pk);
  39. assertNotNull(bookmarksEntry);
  40. assertEquals(bookmarksEntry.getPrimaryKey(), pk);
  41. }
  42. public void testRemove() throws Exception {
  43. BookmarksEntry newBookmarksEntry = addBookmarksEntry();
  44. _persistence.remove(newBookmarksEntry);
  45. BookmarksEntry existingBookmarksEntry = _persistence.fetchByPrimaryKey(newBookmarksEntry.getPrimaryKey());
  46. assertNull(existingBookmarksEntry);
  47. }
  48. public void testUpdateNew() throws Exception {
  49. addBookmarksEntry();
  50. }
  51. public void testUpdateExisting() throws Exception {
  52. long pk = nextLong();
  53. BookmarksEntry newBookmarksEntry = _persistence.create(pk);
  54. newBookmarksEntry.setUuid(randomString());
  55. newBookmarksEntry.setGroupId(nextLong());
  56. newBookmarksEntry.setCompanyId(nextLong());
  57. newBookmarksEntry.setUserId(nextLong());
  58. newBookmarksEntry.setCreateDate(nextDate());
  59. newBookmarksEntry.setModifiedDate(nextDate());
  60. newBookmarksEntry.setFolderId(nextLong());
  61. newBookmarksEntry.setName(randomString());
  62. newBookmarksEntry.setUrl(randomString());
  63. newBookmarksEntry.setDescription(randomString());
  64. newBookmarksEntry.setVisits(nextInt());
  65. newBookmarksEntry.setPriority(nextInt());
  66. _persistence.update(newBookmarksEntry, false);
  67. BookmarksEntry existingBookmarksEntry = _persistence.findByPrimaryKey(newBookmarksEntry.getPrimaryKey());
  68. assertEquals(existingBookmarksEntry.getUuid(),
  69. newBookmarksEntry.getUuid());
  70. assertEquals(existingBookmarksEntry.getEntryId(),
  71. newBookmarksEntry.getEntryId());
  72. assertEquals(existingBookmarksEntry.getGroupId(),
  73. newBookmarksEntry.getGroupId());
  74. assertEquals(existingBookmarksEntry.getCompanyId(),
  75. newBookmarksEntry.getCompanyId());
  76. assertEquals(existingBookmarksEntry.getUserId(),
  77. newBookmarksEntry.getUserId());
  78. assertEquals(Time.getShortTimestamp(
  79. existingBookmarksEntry.getCreateDate()),
  80. Time.getShortTimestamp(newBookmarksEntry.getCreateDate()));
  81. assertEquals(Time.getShortTimestamp(
  82. existingBookmarksEntry.getModifiedDate()),
  83. Time.getShortTimestamp(newBookmarksEntry.getModifiedDate()));
  84. assertEquals(existingBookmarksEntry.getFolderId(),
  85. newBookmarksEntry.getFolderId());
  86. assertEquals(existingBookmarksEntry.getName(),
  87. newBookmarksEntry.getName());
  88. assertEquals(existingBookmarksEntry.getUrl(), newBookmarksEntry.getUrl());
  89. assertEquals(existingBookmarksEntry.getDescription(),
  90. newBookmarksEntry.getDescription());
  91. assertEquals(existingBookmarksEntry.getVisits(),
  92. newBookmarksEntry.getVisits());
  93. assertEquals(existingBookmarksEntry.getPriority(),
  94. newBookmarksEntry.getPriority());
  95. }
  96. public void testFindByPrimaryKeyExisting() throws Exception {
  97. BookmarksEntry newBookmarksEntry = addBookmarksEntry();
  98. BookmarksEntry existingBookmarksEntry = _persistence.findByPrimaryKey(newBookmarksEntry.getPrimaryKey());
  99. assertEquals(existingBookmarksEntry, newBookmarksEntry);
  100. }
  101. public void testFindByPrimaryKeyMissing() throws Exception {
  102. long pk = nextLong();
  103. try {
  104. _persistence.findByPrimaryKey(pk);
  105. fail("Missing entity did not throw NoSuchEntryException");
  106. }
  107. catch (NoSuchEntryException nsee) {
  108. }
  109. }
  110. public void testFetchByPrimaryKeyExisting() throws Exception {
  111. BookmarksEntry newBookmarksEntry = addBookmarksEntry();
  112. BookmarksEntry existingBookmarksEntry = _persistence.fetchByPrimaryKey(newBookmarksEntry.getPrimaryKey());
  113. assertEquals(existingBookmarksEntry, newBookmarksEntry);
  114. }
  115. public void testFetchByPrimaryKeyMissing() throws Exception {
  116. long pk = nextLong();
  117. BookmarksEntry missingBookmarksEntry = _persistence.fetchByPrimaryKey(pk);
  118. assertNull(missingBookmarksEntry);
  119. }
  120. public void testDynamicQueryByPrimaryKeyExisting()
  121. throws Exception {
  122. BookmarksEntry newBookmarksEntry = addBookmarksEntry();
  123. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(BookmarksEntry.class,
  124. BookmarksEntry.class.getClassLoader());
  125. dynamicQuery.add(RestrictionsFactoryUtil.eq("entryId",
  126. newBookmarksEntry.getEntryId()));
  127. List<BookmarksEntry> result = _persistence.findWithDynamicQuery(dynamicQuery);
  128. assertEquals(1, result.size());
  129. BookmarksEntry existingBookmarksEntry = result.get(0);
  130. assertEquals(existingBookmarksEntry, newBookmarksEntry);
  131. }
  132. public void testDynamicQueryByPrimaryKeyMissing() throws Exception {
  133. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(BookmarksEntry.class,
  134. BookmarksEntry.class.getClassLoader());
  135. dynamicQuery.add(RestrictionsFactoryUtil.eq("entryId", nextLong()));
  136. List<BookmarksEntry> result = _persistence.findWithDynamicQuery(dynamicQuery);
  137. assertEquals(0, result.size());
  138. }
  139. public void testDynamicQueryByProjectionExisting()
  140. throws Exception {
  141. BookmarksEntry newBookmarksEntry = addBookmarksEntry();
  142. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(BookmarksEntry.class,
  143. BookmarksEntry.class.getClassLoader());
  144. dynamicQuery.setProjection(ProjectionFactoryUtil.property("entryId"));
  145. Object newEntryId = newBookmarksEntry.getEntryId();
  146. dynamicQuery.add(RestrictionsFactoryUtil.in("entryId",
  147. new Object[] { newEntryId }));
  148. List<Object> result = _persistence.findWithDynamicQuery(dynamicQuery);
  149. assertEquals(1, result.size());
  150. Object existingEntryId = result.get(0);
  151. assertEquals(existingEntryId, newEntryId);
  152. }
  153. public void testDynamicQueryByProjectionMissing() throws Exception {
  154. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(BookmarksEntry.class,
  155. BookmarksEntry.class.getClassLoader());
  156. dynamicQuery.setProjection(ProjectionFactoryUtil.property("entryId"));
  157. dynamicQuery.add(RestrictionsFactoryUtil.in("entryId",
  158. new Object[] { nextLong() }));
  159. List<Object> result = _persistence.findWithDynamicQuery(dynamicQuery);
  160. assertEquals(0, result.size());
  161. }
  162. public void testResetOriginalValues() throws Exception {
  163. if (!PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
  164. return;
  165. }
  166. BookmarksEntry newBookmarksEntry = addBookmarksEntry();
  167. _persistence.clearCache();
  168. BookmarksEntryModelImpl existingBookmarksEntryModelImpl = (BookmarksEntryModelImpl)_persistence.findByPrimaryKey(newBookmarksEntry.getPrimaryKey());
  169. assertTrue(Validator.equals(existingBookmarksEntryModelImpl.getUuid(),
  170. existingBookmarksEntryModelImpl.getOriginalUuid()));
  171. assertEquals(existingBookmarksEntryModelImpl.getGroupId(),
  172. existingBookmarksEntryModelImpl.getOriginalGroupId());
  173. }
  174. protected BookmarksEntry addBookmarksEntry() throws Exception {
  175. long pk = nextLong();
  176. BookmarksEntry bookmarksEntry = _persistence.create(pk);
  177. bookmarksEntry.setUuid(randomString());
  178. bookmarksEntry.setGroupId(nextLong());
  179. bookmarksEntry.setCompanyId(nextLong());
  180. bookmarksEntry.setUserId(nextLong());
  181. bookmarksEntry.setCreateDate(nextDate());
  182. bookmarksEntry.setModifiedDate(nextDate());
  183. bookmarksEntry.setFolderId(nextLong());
  184. bookmarksEntry.setName(randomString());
  185. bookmarksEntry.setUrl(randomString());
  186. bookmarksEntry.setDescription(randomString());
  187. bookmarksEntry.setVisits(nextInt());
  188. bookmarksEntry.setPriority(nextInt());
  189. _persistence.update(bookmarksEntry, false);
  190. return bookmarksEntry;
  191. }
  192. private BookmarksEntryPersistence _persistence;
  193. }