PageRenderTime 52ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/portal-impl/test/integration/com/liferay/portlet/trash/service/persistence/TrashEntryPersistenceTest.java

https://github.com/lululiferay/liferay-portal
Java | 318 lines | 211 code | 91 blank | 16 comment | 3 complexity | c5c31d19dfe5c891bec6d64ca453728a 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.trash.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.log.Log;
  21. import com.liferay.portal.kernel.log.LogFactoryUtil;
  22. import com.liferay.portal.kernel.util.Time;
  23. import com.liferay.portal.service.ServiceTestUtil;
  24. import com.liferay.portal.service.persistence.BasePersistence;
  25. import com.liferay.portal.service.persistence.PersistenceExecutionTestListener;
  26. import com.liferay.portal.test.ExecutionTestListeners;
  27. import com.liferay.portal.test.LiferayPersistenceIntegrationJUnitTestRunner;
  28. import com.liferay.portal.test.persistence.TransactionalPersistenceAdvice;
  29. import com.liferay.portal.util.PropsValues;
  30. import com.liferay.portlet.trash.NoSuchEntryException;
  31. import com.liferay.portlet.trash.model.TrashEntry;
  32. import com.liferay.portlet.trash.model.impl.TrashEntryModelImpl;
  33. import org.junit.After;
  34. import org.junit.Assert;
  35. import org.junit.Test;
  36. import org.junit.runner.RunWith;
  37. import java.io.Serializable;
  38. import java.util.List;
  39. import java.util.Map;
  40. import java.util.Set;
  41. /**
  42. * @author Brian Wing Shun Chan
  43. */
  44. @ExecutionTestListeners(listeners = {
  45. PersistenceExecutionTestListener.class})
  46. @RunWith(LiferayPersistenceIntegrationJUnitTestRunner.class)
  47. public class TrashEntryPersistenceTest {
  48. @After
  49. public void tearDown() throws Exception {
  50. Map<Serializable, BasePersistence<?>> basePersistences = _transactionalPersistenceAdvice.getBasePersistences();
  51. Set<Serializable> primaryKeys = basePersistences.keySet();
  52. for (Serializable primaryKey : primaryKeys) {
  53. BasePersistence<?> basePersistence = basePersistences.get(primaryKey);
  54. try {
  55. basePersistence.remove(primaryKey);
  56. }
  57. catch (Exception e) {
  58. if (_log.isDebugEnabled()) {
  59. _log.debug("The model with primary key " + primaryKey +
  60. " was already deleted");
  61. }
  62. }
  63. }
  64. _transactionalPersistenceAdvice.reset();
  65. }
  66. @Test
  67. public void testCreate() throws Exception {
  68. long pk = ServiceTestUtil.nextLong();
  69. TrashEntry trashEntry = _persistence.create(pk);
  70. Assert.assertNotNull(trashEntry);
  71. Assert.assertEquals(trashEntry.getPrimaryKey(), pk);
  72. }
  73. @Test
  74. public void testRemove() throws Exception {
  75. TrashEntry newTrashEntry = addTrashEntry();
  76. _persistence.remove(newTrashEntry);
  77. TrashEntry existingTrashEntry = _persistence.fetchByPrimaryKey(newTrashEntry.getPrimaryKey());
  78. Assert.assertNull(existingTrashEntry);
  79. }
  80. @Test
  81. public void testUpdateNew() throws Exception {
  82. addTrashEntry();
  83. }
  84. @Test
  85. public void testUpdateExisting() throws Exception {
  86. long pk = ServiceTestUtil.nextLong();
  87. TrashEntry newTrashEntry = _persistence.create(pk);
  88. newTrashEntry.setGroupId(ServiceTestUtil.nextLong());
  89. newTrashEntry.setCompanyId(ServiceTestUtil.nextLong());
  90. newTrashEntry.setUserId(ServiceTestUtil.nextLong());
  91. newTrashEntry.setUserName(ServiceTestUtil.randomString());
  92. newTrashEntry.setCreateDate(ServiceTestUtil.nextDate());
  93. newTrashEntry.setClassNameId(ServiceTestUtil.nextLong());
  94. newTrashEntry.setClassPK(ServiceTestUtil.nextLong());
  95. newTrashEntry.setTypeSettings(ServiceTestUtil.randomString());
  96. newTrashEntry.setStatus(ServiceTestUtil.nextInt());
  97. _persistence.update(newTrashEntry, false);
  98. TrashEntry existingTrashEntry = _persistence.findByPrimaryKey(newTrashEntry.getPrimaryKey());
  99. Assert.assertEquals(existingTrashEntry.getEntryId(),
  100. newTrashEntry.getEntryId());
  101. Assert.assertEquals(existingTrashEntry.getGroupId(),
  102. newTrashEntry.getGroupId());
  103. Assert.assertEquals(existingTrashEntry.getCompanyId(),
  104. newTrashEntry.getCompanyId());
  105. Assert.assertEquals(existingTrashEntry.getUserId(),
  106. newTrashEntry.getUserId());
  107. Assert.assertEquals(existingTrashEntry.getUserName(),
  108. newTrashEntry.getUserName());
  109. Assert.assertEquals(Time.getShortTimestamp(
  110. existingTrashEntry.getCreateDate()),
  111. Time.getShortTimestamp(newTrashEntry.getCreateDate()));
  112. Assert.assertEquals(existingTrashEntry.getClassNameId(),
  113. newTrashEntry.getClassNameId());
  114. Assert.assertEquals(existingTrashEntry.getClassPK(),
  115. newTrashEntry.getClassPK());
  116. Assert.assertEquals(existingTrashEntry.getTypeSettings(),
  117. newTrashEntry.getTypeSettings());
  118. Assert.assertEquals(existingTrashEntry.getStatus(),
  119. newTrashEntry.getStatus());
  120. }
  121. @Test
  122. public void testFindByPrimaryKeyExisting() throws Exception {
  123. TrashEntry newTrashEntry = addTrashEntry();
  124. TrashEntry existingTrashEntry = _persistence.findByPrimaryKey(newTrashEntry.getPrimaryKey());
  125. Assert.assertEquals(existingTrashEntry, newTrashEntry);
  126. }
  127. @Test
  128. public void testFindByPrimaryKeyMissing() throws Exception {
  129. long pk = ServiceTestUtil.nextLong();
  130. try {
  131. _persistence.findByPrimaryKey(pk);
  132. Assert.fail("Missing entity did not throw NoSuchEntryException");
  133. }
  134. catch (NoSuchEntryException nsee) {
  135. }
  136. }
  137. @Test
  138. public void testFetchByPrimaryKeyExisting() throws Exception {
  139. TrashEntry newTrashEntry = addTrashEntry();
  140. TrashEntry existingTrashEntry = _persistence.fetchByPrimaryKey(newTrashEntry.getPrimaryKey());
  141. Assert.assertEquals(existingTrashEntry, newTrashEntry);
  142. }
  143. @Test
  144. public void testFetchByPrimaryKeyMissing() throws Exception {
  145. long pk = ServiceTestUtil.nextLong();
  146. TrashEntry missingTrashEntry = _persistence.fetchByPrimaryKey(pk);
  147. Assert.assertNull(missingTrashEntry);
  148. }
  149. @Test
  150. public void testDynamicQueryByPrimaryKeyExisting()
  151. throws Exception {
  152. TrashEntry newTrashEntry = addTrashEntry();
  153. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(TrashEntry.class,
  154. TrashEntry.class.getClassLoader());
  155. dynamicQuery.add(RestrictionsFactoryUtil.eq("entryId",
  156. newTrashEntry.getEntryId()));
  157. List<TrashEntry> result = _persistence.findWithDynamicQuery(dynamicQuery);
  158. Assert.assertEquals(1, result.size());
  159. TrashEntry existingTrashEntry = result.get(0);
  160. Assert.assertEquals(existingTrashEntry, newTrashEntry);
  161. }
  162. @Test
  163. public void testDynamicQueryByPrimaryKeyMissing() throws Exception {
  164. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(TrashEntry.class,
  165. TrashEntry.class.getClassLoader());
  166. dynamicQuery.add(RestrictionsFactoryUtil.eq("entryId",
  167. ServiceTestUtil.nextLong()));
  168. List<TrashEntry> result = _persistence.findWithDynamicQuery(dynamicQuery);
  169. Assert.assertEquals(0, result.size());
  170. }
  171. @Test
  172. public void testDynamicQueryByProjectionExisting()
  173. throws Exception {
  174. TrashEntry newTrashEntry = addTrashEntry();
  175. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(TrashEntry.class,
  176. TrashEntry.class.getClassLoader());
  177. dynamicQuery.setProjection(ProjectionFactoryUtil.property("entryId"));
  178. Object newEntryId = newTrashEntry.getEntryId();
  179. dynamicQuery.add(RestrictionsFactoryUtil.in("entryId",
  180. new Object[] { newEntryId }));
  181. List<Object> result = _persistence.findWithDynamicQuery(dynamicQuery);
  182. Assert.assertEquals(1, result.size());
  183. Object existingEntryId = result.get(0);
  184. Assert.assertEquals(existingEntryId, newEntryId);
  185. }
  186. @Test
  187. public void testDynamicQueryByProjectionMissing() throws Exception {
  188. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(TrashEntry.class,
  189. TrashEntry.class.getClassLoader());
  190. dynamicQuery.setProjection(ProjectionFactoryUtil.property("entryId"));
  191. dynamicQuery.add(RestrictionsFactoryUtil.in("entryId",
  192. new Object[] { ServiceTestUtil.nextLong() }));
  193. List<Object> result = _persistence.findWithDynamicQuery(dynamicQuery);
  194. Assert.assertEquals(0, result.size());
  195. }
  196. @Test
  197. public void testResetOriginalValues() throws Exception {
  198. if (!PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
  199. return;
  200. }
  201. TrashEntry newTrashEntry = addTrashEntry();
  202. _persistence.clearCache();
  203. TrashEntryModelImpl existingTrashEntryModelImpl = (TrashEntryModelImpl)_persistence.findByPrimaryKey(newTrashEntry.getPrimaryKey());
  204. Assert.assertEquals(existingTrashEntryModelImpl.getClassNameId(),
  205. existingTrashEntryModelImpl.getOriginalClassNameId());
  206. Assert.assertEquals(existingTrashEntryModelImpl.getClassPK(),
  207. existingTrashEntryModelImpl.getOriginalClassPK());
  208. }
  209. protected TrashEntry addTrashEntry() throws Exception {
  210. long pk = ServiceTestUtil.nextLong();
  211. TrashEntry trashEntry = _persistence.create(pk);
  212. trashEntry.setGroupId(ServiceTestUtil.nextLong());
  213. trashEntry.setCompanyId(ServiceTestUtil.nextLong());
  214. trashEntry.setUserId(ServiceTestUtil.nextLong());
  215. trashEntry.setUserName(ServiceTestUtil.randomString());
  216. trashEntry.setCreateDate(ServiceTestUtil.nextDate());
  217. trashEntry.setClassNameId(ServiceTestUtil.nextLong());
  218. trashEntry.setClassPK(ServiceTestUtil.nextLong());
  219. trashEntry.setTypeSettings(ServiceTestUtil.randomString());
  220. trashEntry.setStatus(ServiceTestUtil.nextInt());
  221. _persistence.update(trashEntry, false);
  222. return trashEntry;
  223. }
  224. private static Log _log = LogFactoryUtil.getLog(TrashEntryPersistenceTest.class);
  225. private TrashEntryPersistence _persistence = (TrashEntryPersistence)PortalBeanLocatorUtil.locate(TrashEntryPersistence.class.getName());
  226. private TransactionalPersistenceAdvice _transactionalPersistenceAdvice = (TransactionalPersistenceAdvice)PortalBeanLocatorUtil.locate(TransactionalPersistenceAdvice.class.getName());
  227. }