/portal-impl/test/integration/com/liferay/portal/service/persistence/RepositoryPersistenceTest.java

https://github.com/azzazzel/liferay-portal · Java · 319 lines · 210 code · 93 blank · 16 comment · 1 complexity · fd7abd5219093b088181b904ca1422f3 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.portal.service.persistence;
  15. import com.liferay.portal.NoSuchRepositoryException;
  16. import com.liferay.portal.kernel.bean.PortalBeanLocatorUtil;
  17. import com.liferay.portal.kernel.dao.orm.DynamicQuery;
  18. import com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil;
  19. import com.liferay.portal.kernel.dao.orm.ProjectionFactoryUtil;
  20. import com.liferay.portal.kernel.dao.orm.RestrictionsFactoryUtil;
  21. import com.liferay.portal.kernel.util.Time;
  22. import com.liferay.portal.kernel.util.Validator;
  23. import com.liferay.portal.model.Repository;
  24. import com.liferay.portal.model.impl.RepositoryModelImpl;
  25. import com.liferay.portal.service.ServiceTestUtil;
  26. import com.liferay.portal.service.persistence.PersistenceExecutionTestListener;
  27. import com.liferay.portal.test.ExecutionTestListeners;
  28. import com.liferay.portal.test.LiferayIntegrationJUnitTestRunner;
  29. import com.liferay.portal.util.PropsValues;
  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 RepositoryPersistenceTest {
  42. @Before
  43. public void setUp() throws Exception {
  44. _persistence = (RepositoryPersistence)PortalBeanLocatorUtil.locate(RepositoryPersistence.class.getName());
  45. }
  46. @Test
  47. public void testCreate() throws Exception {
  48. long pk = ServiceTestUtil.nextLong();
  49. Repository repository = _persistence.create(pk);
  50. Assert.assertNotNull(repository);
  51. Assert.assertEquals(repository.getPrimaryKey(), pk);
  52. }
  53. @Test
  54. public void testRemove() throws Exception {
  55. Repository newRepository = addRepository();
  56. _persistence.remove(newRepository);
  57. Repository existingRepository = _persistence.fetchByPrimaryKey(newRepository.getPrimaryKey());
  58. Assert.assertNull(existingRepository);
  59. }
  60. @Test
  61. public void testUpdateNew() throws Exception {
  62. addRepository();
  63. }
  64. @Test
  65. public void testUpdateExisting() throws Exception {
  66. long pk = ServiceTestUtil.nextLong();
  67. Repository newRepository = _persistence.create(pk);
  68. newRepository.setUuid(ServiceTestUtil.randomString());
  69. newRepository.setGroupId(ServiceTestUtil.nextLong());
  70. newRepository.setCompanyId(ServiceTestUtil.nextLong());
  71. newRepository.setUserId(ServiceTestUtil.nextLong());
  72. newRepository.setUserName(ServiceTestUtil.randomString());
  73. newRepository.setCreateDate(ServiceTestUtil.nextDate());
  74. newRepository.setModifiedDate(ServiceTestUtil.nextDate());
  75. newRepository.setClassNameId(ServiceTestUtil.nextLong());
  76. newRepository.setName(ServiceTestUtil.randomString());
  77. newRepository.setDescription(ServiceTestUtil.randomString());
  78. newRepository.setPortletId(ServiceTestUtil.randomString());
  79. newRepository.setTypeSettings(ServiceTestUtil.randomString());
  80. newRepository.setDlFolderId(ServiceTestUtil.nextLong());
  81. _persistence.update(newRepository, false);
  82. Repository existingRepository = _persistence.findByPrimaryKey(newRepository.getPrimaryKey());
  83. Assert.assertEquals(existingRepository.getUuid(),
  84. newRepository.getUuid());
  85. Assert.assertEquals(existingRepository.getRepositoryId(),
  86. newRepository.getRepositoryId());
  87. Assert.assertEquals(existingRepository.getGroupId(),
  88. newRepository.getGroupId());
  89. Assert.assertEquals(existingRepository.getCompanyId(),
  90. newRepository.getCompanyId());
  91. Assert.assertEquals(existingRepository.getUserId(),
  92. newRepository.getUserId());
  93. Assert.assertEquals(existingRepository.getUserName(),
  94. newRepository.getUserName());
  95. Assert.assertEquals(Time.getShortTimestamp(
  96. existingRepository.getCreateDate()),
  97. Time.getShortTimestamp(newRepository.getCreateDate()));
  98. Assert.assertEquals(Time.getShortTimestamp(
  99. existingRepository.getModifiedDate()),
  100. Time.getShortTimestamp(newRepository.getModifiedDate()));
  101. Assert.assertEquals(existingRepository.getClassNameId(),
  102. newRepository.getClassNameId());
  103. Assert.assertEquals(existingRepository.getName(),
  104. newRepository.getName());
  105. Assert.assertEquals(existingRepository.getDescription(),
  106. newRepository.getDescription());
  107. Assert.assertEquals(existingRepository.getPortletId(),
  108. newRepository.getPortletId());
  109. Assert.assertEquals(existingRepository.getTypeSettings(),
  110. newRepository.getTypeSettings());
  111. Assert.assertEquals(existingRepository.getDlFolderId(),
  112. newRepository.getDlFolderId());
  113. }
  114. @Test
  115. public void testFindByPrimaryKeyExisting() throws Exception {
  116. Repository newRepository = addRepository();
  117. Repository existingRepository = _persistence.findByPrimaryKey(newRepository.getPrimaryKey());
  118. Assert.assertEquals(existingRepository, newRepository);
  119. }
  120. @Test
  121. public void testFindByPrimaryKeyMissing() throws Exception {
  122. long pk = ServiceTestUtil.nextLong();
  123. try {
  124. _persistence.findByPrimaryKey(pk);
  125. Assert.fail(
  126. "Missing entity did not throw NoSuchRepositoryException");
  127. }
  128. catch (NoSuchRepositoryException nsee) {
  129. }
  130. }
  131. @Test
  132. public void testFetchByPrimaryKeyExisting() throws Exception {
  133. Repository newRepository = addRepository();
  134. Repository existingRepository = _persistence.fetchByPrimaryKey(newRepository.getPrimaryKey());
  135. Assert.assertEquals(existingRepository, newRepository);
  136. }
  137. @Test
  138. public void testFetchByPrimaryKeyMissing() throws Exception {
  139. long pk = ServiceTestUtil.nextLong();
  140. Repository missingRepository = _persistence.fetchByPrimaryKey(pk);
  141. Assert.assertNull(missingRepository);
  142. }
  143. @Test
  144. public void testDynamicQueryByPrimaryKeyExisting()
  145. throws Exception {
  146. Repository newRepository = addRepository();
  147. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(Repository.class,
  148. Repository.class.getClassLoader());
  149. dynamicQuery.add(RestrictionsFactoryUtil.eq("repositoryId",
  150. newRepository.getRepositoryId()));
  151. List<Repository> result = _persistence.findWithDynamicQuery(dynamicQuery);
  152. Assert.assertEquals(1, result.size());
  153. Repository existingRepository = result.get(0);
  154. Assert.assertEquals(existingRepository, newRepository);
  155. }
  156. @Test
  157. public void testDynamicQueryByPrimaryKeyMissing() throws Exception {
  158. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(Repository.class,
  159. Repository.class.getClassLoader());
  160. dynamicQuery.add(RestrictionsFactoryUtil.eq("repositoryId",
  161. ServiceTestUtil.nextLong()));
  162. List<Repository> result = _persistence.findWithDynamicQuery(dynamicQuery);
  163. Assert.assertEquals(0, result.size());
  164. }
  165. @Test
  166. public void testDynamicQueryByProjectionExisting()
  167. throws Exception {
  168. Repository newRepository = addRepository();
  169. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(Repository.class,
  170. Repository.class.getClassLoader());
  171. dynamicQuery.setProjection(ProjectionFactoryUtil.property(
  172. "repositoryId"));
  173. Object newRepositoryId = newRepository.getRepositoryId();
  174. dynamicQuery.add(RestrictionsFactoryUtil.in("repositoryId",
  175. new Object[] { newRepositoryId }));
  176. List<Object> result = _persistence.findWithDynamicQuery(dynamicQuery);
  177. Assert.assertEquals(1, result.size());
  178. Object existingRepositoryId = result.get(0);
  179. Assert.assertEquals(existingRepositoryId, newRepositoryId);
  180. }
  181. @Test
  182. public void testDynamicQueryByProjectionMissing() throws Exception {
  183. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(Repository.class,
  184. Repository.class.getClassLoader());
  185. dynamicQuery.setProjection(ProjectionFactoryUtil.property(
  186. "repositoryId"));
  187. dynamicQuery.add(RestrictionsFactoryUtil.in("repositoryId",
  188. new Object[] { ServiceTestUtil.nextLong() }));
  189. List<Object> result = _persistence.findWithDynamicQuery(dynamicQuery);
  190. Assert.assertEquals(0, result.size());
  191. }
  192. @Test
  193. public void testResetOriginalValues() throws Exception {
  194. if (!PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
  195. return;
  196. }
  197. Repository newRepository = addRepository();
  198. _persistence.clearCache();
  199. RepositoryModelImpl existingRepositoryModelImpl = (RepositoryModelImpl)_persistence.findByPrimaryKey(newRepository.getPrimaryKey());
  200. Assert.assertTrue(Validator.equals(
  201. existingRepositoryModelImpl.getUuid(),
  202. existingRepositoryModelImpl.getOriginalUuid()));
  203. Assert.assertEquals(existingRepositoryModelImpl.getGroupId(),
  204. existingRepositoryModelImpl.getOriginalGroupId());
  205. }
  206. protected Repository addRepository() throws Exception {
  207. long pk = ServiceTestUtil.nextLong();
  208. Repository repository = _persistence.create(pk);
  209. repository.setUuid(ServiceTestUtil.randomString());
  210. repository.setGroupId(ServiceTestUtil.nextLong());
  211. repository.setCompanyId(ServiceTestUtil.nextLong());
  212. repository.setUserId(ServiceTestUtil.nextLong());
  213. repository.setUserName(ServiceTestUtil.randomString());
  214. repository.setCreateDate(ServiceTestUtil.nextDate());
  215. repository.setModifiedDate(ServiceTestUtil.nextDate());
  216. repository.setClassNameId(ServiceTestUtil.nextLong());
  217. repository.setName(ServiceTestUtil.randomString());
  218. repository.setDescription(ServiceTestUtil.randomString());
  219. repository.setPortletId(ServiceTestUtil.randomString());
  220. repository.setTypeSettings(ServiceTestUtil.randomString());
  221. repository.setDlFolderId(ServiceTestUtil.nextLong());
  222. _persistence.update(repository, false);
  223. return repository;
  224. }
  225. private RepositoryPersistence _persistence;
  226. }