/portal-impl/test/com/liferay/portlet/documentlibrary/service/persistence/DLFileShortcutPersistenceTest.java

https://github.com/monicali/liferay-portal · Java · 301 lines · 190 code · 95 blank · 16 comment · 1 complexity · 1c7aeb442a6a47f2797ac909a484a4e9 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.documentlibrary.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.documentlibrary.NoSuchFileShortcutException;
  25. import com.liferay.portlet.documentlibrary.model.DLFileShortcut;
  26. import com.liferay.portlet.documentlibrary.model.impl.DLFileShortcutModelImpl;
  27. import java.util.List;
  28. /**
  29. * @author Brian Wing Shun Chan
  30. */
  31. public class DLFileShortcutPersistenceTest extends BasePersistenceTestCase {
  32. @Override
  33. public void setUp() throws Exception {
  34. super.setUp();
  35. _persistence = (DLFileShortcutPersistence)PortalBeanLocatorUtil.locate(DLFileShortcutPersistence.class.getName());
  36. }
  37. public void testCreate() throws Exception {
  38. long pk = nextLong();
  39. DLFileShortcut dlFileShortcut = _persistence.create(pk);
  40. assertNotNull(dlFileShortcut);
  41. assertEquals(dlFileShortcut.getPrimaryKey(), pk);
  42. }
  43. public void testRemove() throws Exception {
  44. DLFileShortcut newDLFileShortcut = addDLFileShortcut();
  45. _persistence.remove(newDLFileShortcut);
  46. DLFileShortcut existingDLFileShortcut = _persistence.fetchByPrimaryKey(newDLFileShortcut.getPrimaryKey());
  47. assertNull(existingDLFileShortcut);
  48. }
  49. public void testUpdateNew() throws Exception {
  50. addDLFileShortcut();
  51. }
  52. public void testUpdateExisting() throws Exception {
  53. long pk = nextLong();
  54. DLFileShortcut newDLFileShortcut = _persistence.create(pk);
  55. newDLFileShortcut.setUuid(randomString());
  56. newDLFileShortcut.setGroupId(nextLong());
  57. newDLFileShortcut.setCompanyId(nextLong());
  58. newDLFileShortcut.setUserId(nextLong());
  59. newDLFileShortcut.setUserName(randomString());
  60. newDLFileShortcut.setCreateDate(nextDate());
  61. newDLFileShortcut.setModifiedDate(nextDate());
  62. newDLFileShortcut.setRepositoryId(nextLong());
  63. newDLFileShortcut.setFolderId(nextLong());
  64. newDLFileShortcut.setToFileEntryId(nextLong());
  65. newDLFileShortcut.setStatus(nextInt());
  66. newDLFileShortcut.setStatusByUserId(nextLong());
  67. newDLFileShortcut.setStatusByUserName(randomString());
  68. newDLFileShortcut.setStatusDate(nextDate());
  69. _persistence.update(newDLFileShortcut, false);
  70. DLFileShortcut existingDLFileShortcut = _persistence.findByPrimaryKey(newDLFileShortcut.getPrimaryKey());
  71. assertEquals(existingDLFileShortcut.getUuid(),
  72. newDLFileShortcut.getUuid());
  73. assertEquals(existingDLFileShortcut.getFileShortcutId(),
  74. newDLFileShortcut.getFileShortcutId());
  75. assertEquals(existingDLFileShortcut.getGroupId(),
  76. newDLFileShortcut.getGroupId());
  77. assertEquals(existingDLFileShortcut.getCompanyId(),
  78. newDLFileShortcut.getCompanyId());
  79. assertEquals(existingDLFileShortcut.getUserId(),
  80. newDLFileShortcut.getUserId());
  81. assertEquals(existingDLFileShortcut.getUserName(),
  82. newDLFileShortcut.getUserName());
  83. assertEquals(Time.getShortTimestamp(
  84. existingDLFileShortcut.getCreateDate()),
  85. Time.getShortTimestamp(newDLFileShortcut.getCreateDate()));
  86. assertEquals(Time.getShortTimestamp(
  87. existingDLFileShortcut.getModifiedDate()),
  88. Time.getShortTimestamp(newDLFileShortcut.getModifiedDate()));
  89. assertEquals(existingDLFileShortcut.getRepositoryId(),
  90. newDLFileShortcut.getRepositoryId());
  91. assertEquals(existingDLFileShortcut.getFolderId(),
  92. newDLFileShortcut.getFolderId());
  93. assertEquals(existingDLFileShortcut.getToFileEntryId(),
  94. newDLFileShortcut.getToFileEntryId());
  95. assertEquals(existingDLFileShortcut.getStatus(),
  96. newDLFileShortcut.getStatus());
  97. assertEquals(existingDLFileShortcut.getStatusByUserId(),
  98. newDLFileShortcut.getStatusByUserId());
  99. assertEquals(existingDLFileShortcut.getStatusByUserName(),
  100. newDLFileShortcut.getStatusByUserName());
  101. assertEquals(Time.getShortTimestamp(
  102. existingDLFileShortcut.getStatusDate()),
  103. Time.getShortTimestamp(newDLFileShortcut.getStatusDate()));
  104. }
  105. public void testFindByPrimaryKeyExisting() throws Exception {
  106. DLFileShortcut newDLFileShortcut = addDLFileShortcut();
  107. DLFileShortcut existingDLFileShortcut = _persistence.findByPrimaryKey(newDLFileShortcut.getPrimaryKey());
  108. assertEquals(existingDLFileShortcut, newDLFileShortcut);
  109. }
  110. public void testFindByPrimaryKeyMissing() throws Exception {
  111. long pk = nextLong();
  112. try {
  113. _persistence.findByPrimaryKey(pk);
  114. fail("Missing entity did not throw NoSuchFileShortcutException");
  115. }
  116. catch (NoSuchFileShortcutException nsee) {
  117. }
  118. }
  119. public void testFetchByPrimaryKeyExisting() throws Exception {
  120. DLFileShortcut newDLFileShortcut = addDLFileShortcut();
  121. DLFileShortcut existingDLFileShortcut = _persistence.fetchByPrimaryKey(newDLFileShortcut.getPrimaryKey());
  122. assertEquals(existingDLFileShortcut, newDLFileShortcut);
  123. }
  124. public void testFetchByPrimaryKeyMissing() throws Exception {
  125. long pk = nextLong();
  126. DLFileShortcut missingDLFileShortcut = _persistence.fetchByPrimaryKey(pk);
  127. assertNull(missingDLFileShortcut);
  128. }
  129. public void testDynamicQueryByPrimaryKeyExisting()
  130. throws Exception {
  131. DLFileShortcut newDLFileShortcut = addDLFileShortcut();
  132. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(DLFileShortcut.class,
  133. DLFileShortcut.class.getClassLoader());
  134. dynamicQuery.add(RestrictionsFactoryUtil.eq("fileShortcutId",
  135. newDLFileShortcut.getFileShortcutId()));
  136. List<DLFileShortcut> result = _persistence.findWithDynamicQuery(dynamicQuery);
  137. assertEquals(1, result.size());
  138. DLFileShortcut existingDLFileShortcut = result.get(0);
  139. assertEquals(existingDLFileShortcut, newDLFileShortcut);
  140. }
  141. public void testDynamicQueryByPrimaryKeyMissing() throws Exception {
  142. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(DLFileShortcut.class,
  143. DLFileShortcut.class.getClassLoader());
  144. dynamicQuery.add(RestrictionsFactoryUtil.eq("fileShortcutId", nextLong()));
  145. List<DLFileShortcut> result = _persistence.findWithDynamicQuery(dynamicQuery);
  146. assertEquals(0, result.size());
  147. }
  148. public void testDynamicQueryByProjectionExisting()
  149. throws Exception {
  150. DLFileShortcut newDLFileShortcut = addDLFileShortcut();
  151. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(DLFileShortcut.class,
  152. DLFileShortcut.class.getClassLoader());
  153. dynamicQuery.setProjection(ProjectionFactoryUtil.property(
  154. "fileShortcutId"));
  155. Object newFileShortcutId = newDLFileShortcut.getFileShortcutId();
  156. dynamicQuery.add(RestrictionsFactoryUtil.in("fileShortcutId",
  157. new Object[] { newFileShortcutId }));
  158. List<Object> result = _persistence.findWithDynamicQuery(dynamicQuery);
  159. assertEquals(1, result.size());
  160. Object existingFileShortcutId = result.get(0);
  161. assertEquals(existingFileShortcutId, newFileShortcutId);
  162. }
  163. public void testDynamicQueryByProjectionMissing() throws Exception {
  164. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(DLFileShortcut.class,
  165. DLFileShortcut.class.getClassLoader());
  166. dynamicQuery.setProjection(ProjectionFactoryUtil.property(
  167. "fileShortcutId"));
  168. dynamicQuery.add(RestrictionsFactoryUtil.in("fileShortcutId",
  169. new Object[] { nextLong() }));
  170. List<Object> result = _persistence.findWithDynamicQuery(dynamicQuery);
  171. assertEquals(0, result.size());
  172. }
  173. public void testResetOriginalValues() throws Exception {
  174. if (!PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
  175. return;
  176. }
  177. DLFileShortcut newDLFileShortcut = addDLFileShortcut();
  178. _persistence.clearCache();
  179. DLFileShortcutModelImpl existingDLFileShortcutModelImpl = (DLFileShortcutModelImpl)_persistence.findByPrimaryKey(newDLFileShortcut.getPrimaryKey());
  180. assertTrue(Validator.equals(existingDLFileShortcutModelImpl.getUuid(),
  181. existingDLFileShortcutModelImpl.getOriginalUuid()));
  182. assertEquals(existingDLFileShortcutModelImpl.getGroupId(),
  183. existingDLFileShortcutModelImpl.getOriginalGroupId());
  184. }
  185. protected DLFileShortcut addDLFileShortcut() throws Exception {
  186. long pk = nextLong();
  187. DLFileShortcut dlFileShortcut = _persistence.create(pk);
  188. dlFileShortcut.setUuid(randomString());
  189. dlFileShortcut.setGroupId(nextLong());
  190. dlFileShortcut.setCompanyId(nextLong());
  191. dlFileShortcut.setUserId(nextLong());
  192. dlFileShortcut.setUserName(randomString());
  193. dlFileShortcut.setCreateDate(nextDate());
  194. dlFileShortcut.setModifiedDate(nextDate());
  195. dlFileShortcut.setRepositoryId(nextLong());
  196. dlFileShortcut.setFolderId(nextLong());
  197. dlFileShortcut.setToFileEntryId(nextLong());
  198. dlFileShortcut.setStatus(nextInt());
  199. dlFileShortcut.setStatusByUserId(nextLong());
  200. dlFileShortcut.setStatusByUserName(randomString());
  201. dlFileShortcut.setStatusDate(nextDate());
  202. _persistence.update(dlFileShortcut, false);
  203. return dlFileShortcut;
  204. }
  205. private DLFileShortcutPersistence _persistence;
  206. }