PageRenderTime 63ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/Manishkg/liferay-portal
Java | 348 lines | 221 code | 111 blank | 16 comment | 1 complexity | 059479ecae44683b57058c6988f66c6c 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.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.NoSuchFileEntryException;
  25. import com.liferay.portlet.documentlibrary.model.DLFileEntry;
  26. import com.liferay.portlet.documentlibrary.model.impl.DLFileEntryModelImpl;
  27. import java.util.List;
  28. /**
  29. * @author Brian Wing Shun Chan
  30. */
  31. public class DLFileEntryPersistenceTest extends BasePersistenceTestCase {
  32. @Override
  33. public void setUp() throws Exception {
  34. super.setUp();
  35. _persistence = (DLFileEntryPersistence)PortalBeanLocatorUtil.locate(DLFileEntryPersistence.class.getName());
  36. }
  37. public void testCreate() throws Exception {
  38. long pk = nextLong();
  39. DLFileEntry dlFileEntry = _persistence.create(pk);
  40. assertNotNull(dlFileEntry);
  41. assertEquals(dlFileEntry.getPrimaryKey(), pk);
  42. }
  43. public void testRemove() throws Exception {
  44. DLFileEntry newDLFileEntry = addDLFileEntry();
  45. _persistence.remove(newDLFileEntry);
  46. DLFileEntry existingDLFileEntry = _persistence.fetchByPrimaryKey(newDLFileEntry.getPrimaryKey());
  47. assertNull(existingDLFileEntry);
  48. }
  49. public void testUpdateNew() throws Exception {
  50. addDLFileEntry();
  51. }
  52. public void testUpdateExisting() throws Exception {
  53. long pk = nextLong();
  54. DLFileEntry newDLFileEntry = _persistence.create(pk);
  55. newDLFileEntry.setUuid(randomString());
  56. newDLFileEntry.setGroupId(nextLong());
  57. newDLFileEntry.setCompanyId(nextLong());
  58. newDLFileEntry.setUserId(nextLong());
  59. newDLFileEntry.setUserName(randomString());
  60. newDLFileEntry.setVersionUserId(nextLong());
  61. newDLFileEntry.setVersionUserName(randomString());
  62. newDLFileEntry.setCreateDate(nextDate());
  63. newDLFileEntry.setModifiedDate(nextDate());
  64. newDLFileEntry.setRepositoryId(nextLong());
  65. newDLFileEntry.setFolderId(nextLong());
  66. newDLFileEntry.setName(randomString());
  67. newDLFileEntry.setExtension(randomString());
  68. newDLFileEntry.setMimeType(randomString());
  69. newDLFileEntry.setTitle(randomString());
  70. newDLFileEntry.setDescription(randomString());
  71. newDLFileEntry.setExtraSettings(randomString());
  72. newDLFileEntry.setFileEntryTypeId(nextLong());
  73. newDLFileEntry.setVersion(randomString());
  74. newDLFileEntry.setSize(nextLong());
  75. newDLFileEntry.setReadCount(nextInt());
  76. _persistence.update(newDLFileEntry, false);
  77. DLFileEntry existingDLFileEntry = _persistence.findByPrimaryKey(newDLFileEntry.getPrimaryKey());
  78. assertEquals(existingDLFileEntry.getUuid(), newDLFileEntry.getUuid());
  79. assertEquals(existingDLFileEntry.getFileEntryId(),
  80. newDLFileEntry.getFileEntryId());
  81. assertEquals(existingDLFileEntry.getGroupId(),
  82. newDLFileEntry.getGroupId());
  83. assertEquals(existingDLFileEntry.getCompanyId(),
  84. newDLFileEntry.getCompanyId());
  85. assertEquals(existingDLFileEntry.getUserId(), newDLFileEntry.getUserId());
  86. assertEquals(existingDLFileEntry.getUserName(),
  87. newDLFileEntry.getUserName());
  88. assertEquals(existingDLFileEntry.getVersionUserId(),
  89. newDLFileEntry.getVersionUserId());
  90. assertEquals(existingDLFileEntry.getVersionUserName(),
  91. newDLFileEntry.getVersionUserName());
  92. assertEquals(Time.getShortTimestamp(existingDLFileEntry.getCreateDate()),
  93. Time.getShortTimestamp(newDLFileEntry.getCreateDate()));
  94. assertEquals(Time.getShortTimestamp(
  95. existingDLFileEntry.getModifiedDate()),
  96. Time.getShortTimestamp(newDLFileEntry.getModifiedDate()));
  97. assertEquals(existingDLFileEntry.getRepositoryId(),
  98. newDLFileEntry.getRepositoryId());
  99. assertEquals(existingDLFileEntry.getFolderId(),
  100. newDLFileEntry.getFolderId());
  101. assertEquals(existingDLFileEntry.getName(), newDLFileEntry.getName());
  102. assertEquals(existingDLFileEntry.getExtension(),
  103. newDLFileEntry.getExtension());
  104. assertEquals(existingDLFileEntry.getMimeType(),
  105. newDLFileEntry.getMimeType());
  106. assertEquals(existingDLFileEntry.getTitle(), newDLFileEntry.getTitle());
  107. assertEquals(existingDLFileEntry.getDescription(),
  108. newDLFileEntry.getDescription());
  109. assertEquals(existingDLFileEntry.getExtraSettings(),
  110. newDLFileEntry.getExtraSettings());
  111. assertEquals(existingDLFileEntry.getFileEntryTypeId(),
  112. newDLFileEntry.getFileEntryTypeId());
  113. assertEquals(existingDLFileEntry.getVersion(),
  114. newDLFileEntry.getVersion());
  115. assertEquals(existingDLFileEntry.getSize(), newDLFileEntry.getSize());
  116. assertEquals(existingDLFileEntry.getReadCount(),
  117. newDLFileEntry.getReadCount());
  118. }
  119. public void testFindByPrimaryKeyExisting() throws Exception {
  120. DLFileEntry newDLFileEntry = addDLFileEntry();
  121. DLFileEntry existingDLFileEntry = _persistence.findByPrimaryKey(newDLFileEntry.getPrimaryKey());
  122. assertEquals(existingDLFileEntry, newDLFileEntry);
  123. }
  124. public void testFindByPrimaryKeyMissing() throws Exception {
  125. long pk = nextLong();
  126. try {
  127. _persistence.findByPrimaryKey(pk);
  128. fail("Missing entity did not throw NoSuchFileEntryException");
  129. }
  130. catch (NoSuchFileEntryException nsee) {
  131. }
  132. }
  133. public void testFetchByPrimaryKeyExisting() throws Exception {
  134. DLFileEntry newDLFileEntry = addDLFileEntry();
  135. DLFileEntry existingDLFileEntry = _persistence.fetchByPrimaryKey(newDLFileEntry.getPrimaryKey());
  136. assertEquals(existingDLFileEntry, newDLFileEntry);
  137. }
  138. public void testFetchByPrimaryKeyMissing() throws Exception {
  139. long pk = nextLong();
  140. DLFileEntry missingDLFileEntry = _persistence.fetchByPrimaryKey(pk);
  141. assertNull(missingDLFileEntry);
  142. }
  143. public void testDynamicQueryByPrimaryKeyExisting()
  144. throws Exception {
  145. DLFileEntry newDLFileEntry = addDLFileEntry();
  146. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(DLFileEntry.class,
  147. DLFileEntry.class.getClassLoader());
  148. dynamicQuery.add(RestrictionsFactoryUtil.eq("fileEntryId",
  149. newDLFileEntry.getFileEntryId()));
  150. List<DLFileEntry> result = _persistence.findWithDynamicQuery(dynamicQuery);
  151. assertEquals(1, result.size());
  152. DLFileEntry existingDLFileEntry = result.get(0);
  153. assertEquals(existingDLFileEntry, newDLFileEntry);
  154. }
  155. public void testDynamicQueryByPrimaryKeyMissing() throws Exception {
  156. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(DLFileEntry.class,
  157. DLFileEntry.class.getClassLoader());
  158. dynamicQuery.add(RestrictionsFactoryUtil.eq("fileEntryId", nextLong()));
  159. List<DLFileEntry> result = _persistence.findWithDynamicQuery(dynamicQuery);
  160. assertEquals(0, result.size());
  161. }
  162. public void testDynamicQueryByProjectionExisting()
  163. throws Exception {
  164. DLFileEntry newDLFileEntry = addDLFileEntry();
  165. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(DLFileEntry.class,
  166. DLFileEntry.class.getClassLoader());
  167. dynamicQuery.setProjection(ProjectionFactoryUtil.property("fileEntryId"));
  168. Object newFileEntryId = newDLFileEntry.getFileEntryId();
  169. dynamicQuery.add(RestrictionsFactoryUtil.in("fileEntryId",
  170. new Object[] { newFileEntryId }));
  171. List<Object> result = _persistence.findWithDynamicQuery(dynamicQuery);
  172. assertEquals(1, result.size());
  173. Object existingFileEntryId = result.get(0);
  174. assertEquals(existingFileEntryId, newFileEntryId);
  175. }
  176. public void testDynamicQueryByProjectionMissing() throws Exception {
  177. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(DLFileEntry.class,
  178. DLFileEntry.class.getClassLoader());
  179. dynamicQuery.setProjection(ProjectionFactoryUtil.property("fileEntryId"));
  180. dynamicQuery.add(RestrictionsFactoryUtil.in("fileEntryId",
  181. new Object[] { nextLong() }));
  182. List<Object> result = _persistence.findWithDynamicQuery(dynamicQuery);
  183. assertEquals(0, result.size());
  184. }
  185. public void testResetOriginalValues() throws Exception {
  186. if (!PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
  187. return;
  188. }
  189. DLFileEntry newDLFileEntry = addDLFileEntry();
  190. _persistence.clearCache();
  191. DLFileEntryModelImpl existingDLFileEntryModelImpl = (DLFileEntryModelImpl)_persistence.findByPrimaryKey(newDLFileEntry.getPrimaryKey());
  192. assertTrue(Validator.equals(existingDLFileEntryModelImpl.getUuid(),
  193. existingDLFileEntryModelImpl.getOriginalUuid()));
  194. assertEquals(existingDLFileEntryModelImpl.getGroupId(),
  195. existingDLFileEntryModelImpl.getOriginalGroupId());
  196. assertEquals(existingDLFileEntryModelImpl.getGroupId(),
  197. existingDLFileEntryModelImpl.getOriginalGroupId());
  198. assertEquals(existingDLFileEntryModelImpl.getFolderId(),
  199. existingDLFileEntryModelImpl.getOriginalFolderId());
  200. assertTrue(Validator.equals(existingDLFileEntryModelImpl.getName(),
  201. existingDLFileEntryModelImpl.getOriginalName()));
  202. assertEquals(existingDLFileEntryModelImpl.getGroupId(),
  203. existingDLFileEntryModelImpl.getOriginalGroupId());
  204. assertEquals(existingDLFileEntryModelImpl.getFolderId(),
  205. existingDLFileEntryModelImpl.getOriginalFolderId());
  206. assertTrue(Validator.equals(existingDLFileEntryModelImpl.getTitle(),
  207. existingDLFileEntryModelImpl.getOriginalTitle()));
  208. }
  209. protected DLFileEntry addDLFileEntry() throws Exception {
  210. long pk = nextLong();
  211. DLFileEntry dlFileEntry = _persistence.create(pk);
  212. dlFileEntry.setUuid(randomString());
  213. dlFileEntry.setGroupId(nextLong());
  214. dlFileEntry.setCompanyId(nextLong());
  215. dlFileEntry.setUserId(nextLong());
  216. dlFileEntry.setUserName(randomString());
  217. dlFileEntry.setVersionUserId(nextLong());
  218. dlFileEntry.setVersionUserName(randomString());
  219. dlFileEntry.setCreateDate(nextDate());
  220. dlFileEntry.setModifiedDate(nextDate());
  221. dlFileEntry.setRepositoryId(nextLong());
  222. dlFileEntry.setFolderId(nextLong());
  223. dlFileEntry.setName(randomString());
  224. dlFileEntry.setExtension(randomString());
  225. dlFileEntry.setMimeType(randomString());
  226. dlFileEntry.setTitle(randomString());
  227. dlFileEntry.setDescription(randomString());
  228. dlFileEntry.setExtraSettings(randomString());
  229. dlFileEntry.setFileEntryTypeId(nextLong());
  230. dlFileEntry.setVersion(randomString());
  231. dlFileEntry.setSize(nextLong());
  232. dlFileEntry.setReadCount(nextInt());
  233. _persistence.update(dlFileEntry, false);
  234. return dlFileEntry;
  235. }
  236. private DLFileEntryPersistence _persistence;
  237. }