/portal-impl/test/integration/com/liferay/portlet/shopping/service/persistence/ShoppingCategoryPersistenceTest.java

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