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

https://github.com/azzazzel/liferay-portal · Java · 285 lines · 185 code · 84 blank · 16 comment · 1 complexity · 1ec1b07febf0139c5fcca2b0f8bd3e23 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.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.ServiceTestUtil;
  23. import com.liferay.portal.service.persistence.PersistenceExecutionTestListener;
  24. import com.liferay.portal.test.ExecutionTestListeners;
  25. import com.liferay.portal.test.LiferayIntegrationJUnitTestRunner;
  26. import com.liferay.portal.util.PropsValues;
  27. import com.liferay.portlet.asset.NoSuchTagException;
  28. import com.liferay.portlet.asset.model.AssetTag;
  29. import com.liferay.portlet.asset.model.impl.AssetTagModelImpl;
  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 AssetTagPersistenceTest {
  42. @Before
  43. public void setUp() throws Exception {
  44. _persistence = (AssetTagPersistence)PortalBeanLocatorUtil.locate(AssetTagPersistence.class.getName());
  45. }
  46. @Test
  47. public void testCreate() throws Exception {
  48. long pk = ServiceTestUtil.nextLong();
  49. AssetTag assetTag = _persistence.create(pk);
  50. Assert.assertNotNull(assetTag);
  51. Assert.assertEquals(assetTag.getPrimaryKey(), pk);
  52. }
  53. @Test
  54. public void testRemove() throws Exception {
  55. AssetTag newAssetTag = addAssetTag();
  56. _persistence.remove(newAssetTag);
  57. AssetTag existingAssetTag = _persistence.fetchByPrimaryKey(newAssetTag.getPrimaryKey());
  58. Assert.assertNull(existingAssetTag);
  59. }
  60. @Test
  61. public void testUpdateNew() throws Exception {
  62. addAssetTag();
  63. }
  64. @Test
  65. public void testUpdateExisting() throws Exception {
  66. long pk = ServiceTestUtil.nextLong();
  67. AssetTag newAssetTag = _persistence.create(pk);
  68. newAssetTag.setGroupId(ServiceTestUtil.nextLong());
  69. newAssetTag.setCompanyId(ServiceTestUtil.nextLong());
  70. newAssetTag.setUserId(ServiceTestUtil.nextLong());
  71. newAssetTag.setUserName(ServiceTestUtil.randomString());
  72. newAssetTag.setCreateDate(ServiceTestUtil.nextDate());
  73. newAssetTag.setModifiedDate(ServiceTestUtil.nextDate());
  74. newAssetTag.setName(ServiceTestUtil.randomString());
  75. newAssetTag.setAssetCount(ServiceTestUtil.nextInt());
  76. _persistence.update(newAssetTag, false);
  77. AssetTag existingAssetTag = _persistence.findByPrimaryKey(newAssetTag.getPrimaryKey());
  78. Assert.assertEquals(existingAssetTag.getTagId(), newAssetTag.getTagId());
  79. Assert.assertEquals(existingAssetTag.getGroupId(),
  80. newAssetTag.getGroupId());
  81. Assert.assertEquals(existingAssetTag.getCompanyId(),
  82. newAssetTag.getCompanyId());
  83. Assert.assertEquals(existingAssetTag.getUserId(),
  84. newAssetTag.getUserId());
  85. Assert.assertEquals(existingAssetTag.getUserName(),
  86. newAssetTag.getUserName());
  87. Assert.assertEquals(Time.getShortTimestamp(
  88. existingAssetTag.getCreateDate()),
  89. Time.getShortTimestamp(newAssetTag.getCreateDate()));
  90. Assert.assertEquals(Time.getShortTimestamp(
  91. existingAssetTag.getModifiedDate()),
  92. Time.getShortTimestamp(newAssetTag.getModifiedDate()));
  93. Assert.assertEquals(existingAssetTag.getName(), newAssetTag.getName());
  94. Assert.assertEquals(existingAssetTag.getAssetCount(),
  95. newAssetTag.getAssetCount());
  96. }
  97. @Test
  98. public void testFindByPrimaryKeyExisting() throws Exception {
  99. AssetTag newAssetTag = addAssetTag();
  100. AssetTag existingAssetTag = _persistence.findByPrimaryKey(newAssetTag.getPrimaryKey());
  101. Assert.assertEquals(existingAssetTag, newAssetTag);
  102. }
  103. @Test
  104. public void testFindByPrimaryKeyMissing() throws Exception {
  105. long pk = ServiceTestUtil.nextLong();
  106. try {
  107. _persistence.findByPrimaryKey(pk);
  108. Assert.fail("Missing entity did not throw NoSuchTagException");
  109. }
  110. catch (NoSuchTagException nsee) {
  111. }
  112. }
  113. @Test
  114. public void testFetchByPrimaryKeyExisting() throws Exception {
  115. AssetTag newAssetTag = addAssetTag();
  116. AssetTag existingAssetTag = _persistence.fetchByPrimaryKey(newAssetTag.getPrimaryKey());
  117. Assert.assertEquals(existingAssetTag, newAssetTag);
  118. }
  119. @Test
  120. public void testFetchByPrimaryKeyMissing() throws Exception {
  121. long pk = ServiceTestUtil.nextLong();
  122. AssetTag missingAssetTag = _persistence.fetchByPrimaryKey(pk);
  123. Assert.assertNull(missingAssetTag);
  124. }
  125. @Test
  126. public void testDynamicQueryByPrimaryKeyExisting()
  127. throws Exception {
  128. AssetTag newAssetTag = addAssetTag();
  129. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(AssetTag.class,
  130. AssetTag.class.getClassLoader());
  131. dynamicQuery.add(RestrictionsFactoryUtil.eq("tagId",
  132. newAssetTag.getTagId()));
  133. List<AssetTag> result = _persistence.findWithDynamicQuery(dynamicQuery);
  134. Assert.assertEquals(1, result.size());
  135. AssetTag existingAssetTag = result.get(0);
  136. Assert.assertEquals(existingAssetTag, newAssetTag);
  137. }
  138. @Test
  139. public void testDynamicQueryByPrimaryKeyMissing() throws Exception {
  140. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(AssetTag.class,
  141. AssetTag.class.getClassLoader());
  142. dynamicQuery.add(RestrictionsFactoryUtil.eq("tagId",
  143. ServiceTestUtil.nextLong()));
  144. List<AssetTag> result = _persistence.findWithDynamicQuery(dynamicQuery);
  145. Assert.assertEquals(0, result.size());
  146. }
  147. @Test
  148. public void testDynamicQueryByProjectionExisting()
  149. throws Exception {
  150. AssetTag newAssetTag = addAssetTag();
  151. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(AssetTag.class,
  152. AssetTag.class.getClassLoader());
  153. dynamicQuery.setProjection(ProjectionFactoryUtil.property("tagId"));
  154. Object newTagId = newAssetTag.getTagId();
  155. dynamicQuery.add(RestrictionsFactoryUtil.in("tagId",
  156. new Object[] { newTagId }));
  157. List<Object> result = _persistence.findWithDynamicQuery(dynamicQuery);
  158. Assert.assertEquals(1, result.size());
  159. Object existingTagId = result.get(0);
  160. Assert.assertEquals(existingTagId, newTagId);
  161. }
  162. @Test
  163. public void testDynamicQueryByProjectionMissing() throws Exception {
  164. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(AssetTag.class,
  165. AssetTag.class.getClassLoader());
  166. dynamicQuery.setProjection(ProjectionFactoryUtil.property("tagId"));
  167. dynamicQuery.add(RestrictionsFactoryUtil.in("tagId",
  168. new Object[] { ServiceTestUtil.nextLong() }));
  169. List<Object> result = _persistence.findWithDynamicQuery(dynamicQuery);
  170. Assert.assertEquals(0, result.size());
  171. }
  172. @Test
  173. public void testResetOriginalValues() throws Exception {
  174. if (!PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
  175. return;
  176. }
  177. AssetTag newAssetTag = addAssetTag();
  178. _persistence.clearCache();
  179. AssetTagModelImpl existingAssetTagModelImpl = (AssetTagModelImpl)_persistence.findByPrimaryKey(newAssetTag.getPrimaryKey());
  180. Assert.assertEquals(existingAssetTagModelImpl.getGroupId(),
  181. existingAssetTagModelImpl.getOriginalGroupId());
  182. Assert.assertTrue(Validator.equals(
  183. existingAssetTagModelImpl.getName(),
  184. existingAssetTagModelImpl.getOriginalName()));
  185. }
  186. protected AssetTag addAssetTag() throws Exception {
  187. long pk = ServiceTestUtil.nextLong();
  188. AssetTag assetTag = _persistence.create(pk);
  189. assetTag.setGroupId(ServiceTestUtil.nextLong());
  190. assetTag.setCompanyId(ServiceTestUtil.nextLong());
  191. assetTag.setUserId(ServiceTestUtil.nextLong());
  192. assetTag.setUserName(ServiceTestUtil.randomString());
  193. assetTag.setCreateDate(ServiceTestUtil.nextDate());
  194. assetTag.setModifiedDate(ServiceTestUtil.nextDate());
  195. assetTag.setName(ServiceTestUtil.randomString());
  196. assetTag.setAssetCount(ServiceTestUtil.nextInt());
  197. _persistence.update(assetTag, false);
  198. return assetTag;
  199. }
  200. private AssetTagPersistence _persistence;
  201. }