/portal-impl/test/com/liferay/portlet/asset/service/persistence/AssetCategoryPersistenceTest.java

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