/portal-impl/test/integration/com/liferay/portlet/expando/service/persistence/ExpandoColumnPersistenceTest.java

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