PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/portal-impl/test/com/liferay/portal/service/persistence/PortletPersistenceTest.java

http://github.com/liferay/liferay-portal
Java | 227 lines | 137 code | 74 blank | 16 comment | 1 complexity | 826916dd21866ab929a11bcbedc5b7f1 MD5 | raw file
Possible License(s): LGPL-2.0
  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.NoSuchPortletException;
  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.Validator;
  22. import com.liferay.portal.model.Portlet;
  23. import com.liferay.portal.model.impl.PortletModelImpl;
  24. import com.liferay.portal.service.persistence.BasePersistenceTestCase;
  25. import com.liferay.portal.util.PropsValues;
  26. import java.util.List;
  27. /**
  28. * @author Brian Wing Shun Chan
  29. */
  30. public class PortletPersistenceTest extends BasePersistenceTestCase {
  31. @Override
  32. public void setUp() throws Exception {
  33. super.setUp();
  34. _persistence = (PortletPersistence)PortalBeanLocatorUtil.locate(PortletPersistence.class.getName());
  35. }
  36. public void testCreate() throws Exception {
  37. long pk = nextLong();
  38. Portlet portlet = _persistence.create(pk);
  39. assertNotNull(portlet);
  40. assertEquals(portlet.getPrimaryKey(), pk);
  41. }
  42. public void testRemove() throws Exception {
  43. Portlet newPortlet = addPortlet();
  44. _persistence.remove(newPortlet);
  45. Portlet existingPortlet = _persistence.fetchByPrimaryKey(newPortlet.getPrimaryKey());
  46. assertNull(existingPortlet);
  47. }
  48. public void testUpdateNew() throws Exception {
  49. addPortlet();
  50. }
  51. public void testUpdateExisting() throws Exception {
  52. long pk = nextLong();
  53. Portlet newPortlet = _persistence.create(pk);
  54. newPortlet.setCompanyId(nextLong());
  55. newPortlet.setPortletId(randomString());
  56. newPortlet.setRoles(randomString());
  57. newPortlet.setActive(randomBoolean());
  58. _persistence.update(newPortlet, false);
  59. Portlet existingPortlet = _persistence.findByPrimaryKey(newPortlet.getPrimaryKey());
  60. assertEquals(existingPortlet.getId(), newPortlet.getId());
  61. assertEquals(existingPortlet.getCompanyId(), newPortlet.getCompanyId());
  62. assertEquals(existingPortlet.getPortletId(), newPortlet.getPortletId());
  63. assertEquals(existingPortlet.getRoles(), newPortlet.getRoles());
  64. assertEquals(existingPortlet.getActive(), newPortlet.getActive());
  65. }
  66. public void testFindByPrimaryKeyExisting() throws Exception {
  67. Portlet newPortlet = addPortlet();
  68. Portlet existingPortlet = _persistence.findByPrimaryKey(newPortlet.getPrimaryKey());
  69. assertEquals(existingPortlet, newPortlet);
  70. }
  71. public void testFindByPrimaryKeyMissing() throws Exception {
  72. long pk = nextLong();
  73. try {
  74. _persistence.findByPrimaryKey(pk);
  75. fail("Missing entity did not throw NoSuchPortletException");
  76. }
  77. catch (NoSuchPortletException nsee) {
  78. }
  79. }
  80. public void testFetchByPrimaryKeyExisting() throws Exception {
  81. Portlet newPortlet = addPortlet();
  82. Portlet existingPortlet = _persistence.fetchByPrimaryKey(newPortlet.getPrimaryKey());
  83. assertEquals(existingPortlet, newPortlet);
  84. }
  85. public void testFetchByPrimaryKeyMissing() throws Exception {
  86. long pk = nextLong();
  87. Portlet missingPortlet = _persistence.fetchByPrimaryKey(pk);
  88. assertNull(missingPortlet);
  89. }
  90. public void testDynamicQueryByPrimaryKeyExisting()
  91. throws Exception {
  92. Portlet newPortlet = addPortlet();
  93. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(Portlet.class,
  94. Portlet.class.getClassLoader());
  95. dynamicQuery.add(RestrictionsFactoryUtil.eq("id", newPortlet.getId()));
  96. List<Portlet> result = _persistence.findWithDynamicQuery(dynamicQuery);
  97. assertEquals(1, result.size());
  98. Portlet existingPortlet = result.get(0);
  99. assertEquals(existingPortlet, newPortlet);
  100. }
  101. public void testDynamicQueryByPrimaryKeyMissing() throws Exception {
  102. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(Portlet.class,
  103. Portlet.class.getClassLoader());
  104. dynamicQuery.add(RestrictionsFactoryUtil.eq("id", nextLong()));
  105. List<Portlet> result = _persistence.findWithDynamicQuery(dynamicQuery);
  106. assertEquals(0, result.size());
  107. }
  108. public void testDynamicQueryByProjectionExisting()
  109. throws Exception {
  110. Portlet newPortlet = addPortlet();
  111. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(Portlet.class,
  112. Portlet.class.getClassLoader());
  113. dynamicQuery.setProjection(ProjectionFactoryUtil.property("id"));
  114. Object newId = newPortlet.getId();
  115. dynamicQuery.add(RestrictionsFactoryUtil.in("id", new Object[] { newId }));
  116. List<Object> result = _persistence.findWithDynamicQuery(dynamicQuery);
  117. assertEquals(1, result.size());
  118. Object existingId = result.get(0);
  119. assertEquals(existingId, newId);
  120. }
  121. public void testDynamicQueryByProjectionMissing() throws Exception {
  122. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(Portlet.class,
  123. Portlet.class.getClassLoader());
  124. dynamicQuery.setProjection(ProjectionFactoryUtil.property("id"));
  125. dynamicQuery.add(RestrictionsFactoryUtil.in("id",
  126. new Object[] { nextLong() }));
  127. List<Object> result = _persistence.findWithDynamicQuery(dynamicQuery);
  128. assertEquals(0, result.size());
  129. }
  130. public void testResetOriginalValues() throws Exception {
  131. if (!PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
  132. return;
  133. }
  134. Portlet newPortlet = addPortlet();
  135. _persistence.clearCache();
  136. PortletModelImpl existingPortletModelImpl = (PortletModelImpl)_persistence.findByPrimaryKey(newPortlet.getPrimaryKey());
  137. assertEquals(existingPortletModelImpl.getCompanyId(),
  138. existingPortletModelImpl.getOriginalCompanyId());
  139. assertTrue(Validator.equals(existingPortletModelImpl.getPortletId(),
  140. existingPortletModelImpl.getOriginalPortletId()));
  141. }
  142. protected Portlet addPortlet() throws Exception {
  143. long pk = nextLong();
  144. Portlet portlet = _persistence.create(pk);
  145. portlet.setCompanyId(nextLong());
  146. portlet.setPortletId(randomString());
  147. portlet.setRoles(randomString());
  148. portlet.setActive(randomBoolean());
  149. _persistence.update(portlet, false);
  150. return portlet;
  151. }
  152. private PortletPersistence _persistence;
  153. }