/portal-impl/test/integration/com/liferay/portal/service/persistence/ContactPersistenceTest.java

https://github.com/azzazzel/liferay-portal · Java · 384 lines · 248 code · 120 blank · 16 comment · 0 complexity · ce9004f332329b9913c8984c12bed03a 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.portal.service.persistence;
  15. import com.liferay.portal.NoSuchContactException;
  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.Time;
  22. import com.liferay.portal.model.Contact;
  23. import com.liferay.portal.service.ServiceTestUtil;
  24. import com.liferay.portal.service.persistence.PersistenceExecutionTestListener;
  25. import com.liferay.portal.test.ExecutionTestListeners;
  26. import com.liferay.portal.test.LiferayIntegrationJUnitTestRunner;
  27. import org.junit.Assert;
  28. import org.junit.Before;
  29. import org.junit.Test;
  30. import org.junit.runner.RunWith;
  31. import java.util.List;
  32. /**
  33. * @author Brian Wing Shun Chan
  34. */
  35. @ExecutionTestListeners(listeners = {
  36. PersistenceExecutionTestListener.class})
  37. @RunWith(LiferayIntegrationJUnitTestRunner.class)
  38. public class ContactPersistenceTest {
  39. @Before
  40. public void setUp() throws Exception {
  41. _persistence = (ContactPersistence)PortalBeanLocatorUtil.locate(ContactPersistence.class.getName());
  42. }
  43. @Test
  44. public void testCreate() throws Exception {
  45. long pk = ServiceTestUtil.nextLong();
  46. Contact contact = _persistence.create(pk);
  47. Assert.assertNotNull(contact);
  48. Assert.assertEquals(contact.getPrimaryKey(), pk);
  49. }
  50. @Test
  51. public void testRemove() throws Exception {
  52. Contact newContact = addContact();
  53. _persistence.remove(newContact);
  54. Contact existingContact = _persistence.fetchByPrimaryKey(newContact.getPrimaryKey());
  55. Assert.assertNull(existingContact);
  56. }
  57. @Test
  58. public void testUpdateNew() throws Exception {
  59. addContact();
  60. }
  61. @Test
  62. public void testUpdateExisting() throws Exception {
  63. long pk = ServiceTestUtil.nextLong();
  64. Contact newContact = _persistence.create(pk);
  65. newContact.setCompanyId(ServiceTestUtil.nextLong());
  66. newContact.setUserId(ServiceTestUtil.nextLong());
  67. newContact.setUserName(ServiceTestUtil.randomString());
  68. newContact.setCreateDate(ServiceTestUtil.nextDate());
  69. newContact.setModifiedDate(ServiceTestUtil.nextDate());
  70. newContact.setAccountId(ServiceTestUtil.nextLong());
  71. newContact.setParentContactId(ServiceTestUtil.nextLong());
  72. newContact.setFirstName(ServiceTestUtil.randomString());
  73. newContact.setMiddleName(ServiceTestUtil.randomString());
  74. newContact.setLastName(ServiceTestUtil.randomString());
  75. newContact.setPrefixId(ServiceTestUtil.nextInt());
  76. newContact.setSuffixId(ServiceTestUtil.nextInt());
  77. newContact.setMale(ServiceTestUtil.randomBoolean());
  78. newContact.setBirthday(ServiceTestUtil.nextDate());
  79. newContact.setSmsSn(ServiceTestUtil.randomString());
  80. newContact.setAimSn(ServiceTestUtil.randomString());
  81. newContact.setFacebookSn(ServiceTestUtil.randomString());
  82. newContact.setIcqSn(ServiceTestUtil.randomString());
  83. newContact.setJabberSn(ServiceTestUtil.randomString());
  84. newContact.setMsnSn(ServiceTestUtil.randomString());
  85. newContact.setMySpaceSn(ServiceTestUtil.randomString());
  86. newContact.setSkypeSn(ServiceTestUtil.randomString());
  87. newContact.setTwitterSn(ServiceTestUtil.randomString());
  88. newContact.setYmSn(ServiceTestUtil.randomString());
  89. newContact.setEmployeeStatusId(ServiceTestUtil.randomString());
  90. newContact.setEmployeeNumber(ServiceTestUtil.randomString());
  91. newContact.setJobTitle(ServiceTestUtil.randomString());
  92. newContact.setJobClass(ServiceTestUtil.randomString());
  93. newContact.setHoursOfOperation(ServiceTestUtil.randomString());
  94. _persistence.update(newContact, false);
  95. Contact existingContact = _persistence.findByPrimaryKey(newContact.getPrimaryKey());
  96. Assert.assertEquals(existingContact.getContactId(),
  97. newContact.getContactId());
  98. Assert.assertEquals(existingContact.getCompanyId(),
  99. newContact.getCompanyId());
  100. Assert.assertEquals(existingContact.getUserId(), newContact.getUserId());
  101. Assert.assertEquals(existingContact.getUserName(),
  102. newContact.getUserName());
  103. Assert.assertEquals(Time.getShortTimestamp(
  104. existingContact.getCreateDate()),
  105. Time.getShortTimestamp(newContact.getCreateDate()));
  106. Assert.assertEquals(Time.getShortTimestamp(
  107. existingContact.getModifiedDate()),
  108. Time.getShortTimestamp(newContact.getModifiedDate()));
  109. Assert.assertEquals(existingContact.getAccountId(),
  110. newContact.getAccountId());
  111. Assert.assertEquals(existingContact.getParentContactId(),
  112. newContact.getParentContactId());
  113. Assert.assertEquals(existingContact.getFirstName(),
  114. newContact.getFirstName());
  115. Assert.assertEquals(existingContact.getMiddleName(),
  116. newContact.getMiddleName());
  117. Assert.assertEquals(existingContact.getLastName(),
  118. newContact.getLastName());
  119. Assert.assertEquals(existingContact.getPrefixId(),
  120. newContact.getPrefixId());
  121. Assert.assertEquals(existingContact.getSuffixId(),
  122. newContact.getSuffixId());
  123. Assert.assertEquals(existingContact.getMale(), newContact.getMale());
  124. Assert.assertEquals(Time.getShortTimestamp(
  125. existingContact.getBirthday()),
  126. Time.getShortTimestamp(newContact.getBirthday()));
  127. Assert.assertEquals(existingContact.getSmsSn(), newContact.getSmsSn());
  128. Assert.assertEquals(existingContact.getAimSn(), newContact.getAimSn());
  129. Assert.assertEquals(existingContact.getFacebookSn(),
  130. newContact.getFacebookSn());
  131. Assert.assertEquals(existingContact.getIcqSn(), newContact.getIcqSn());
  132. Assert.assertEquals(existingContact.getJabberSn(),
  133. newContact.getJabberSn());
  134. Assert.assertEquals(existingContact.getMsnSn(), newContact.getMsnSn());
  135. Assert.assertEquals(existingContact.getMySpaceSn(),
  136. newContact.getMySpaceSn());
  137. Assert.assertEquals(existingContact.getSkypeSn(),
  138. newContact.getSkypeSn());
  139. Assert.assertEquals(existingContact.getTwitterSn(),
  140. newContact.getTwitterSn());
  141. Assert.assertEquals(existingContact.getYmSn(), newContact.getYmSn());
  142. Assert.assertEquals(existingContact.getEmployeeStatusId(),
  143. newContact.getEmployeeStatusId());
  144. Assert.assertEquals(existingContact.getEmployeeNumber(),
  145. newContact.getEmployeeNumber());
  146. Assert.assertEquals(existingContact.getJobTitle(),
  147. newContact.getJobTitle());
  148. Assert.assertEquals(existingContact.getJobClass(),
  149. newContact.getJobClass());
  150. Assert.assertEquals(existingContact.getHoursOfOperation(),
  151. newContact.getHoursOfOperation());
  152. }
  153. @Test
  154. public void testFindByPrimaryKeyExisting() throws Exception {
  155. Contact newContact = addContact();
  156. Contact existingContact = _persistence.findByPrimaryKey(newContact.getPrimaryKey());
  157. Assert.assertEquals(existingContact, newContact);
  158. }
  159. @Test
  160. public void testFindByPrimaryKeyMissing() throws Exception {
  161. long pk = ServiceTestUtil.nextLong();
  162. try {
  163. _persistence.findByPrimaryKey(pk);
  164. Assert.fail("Missing entity did not throw NoSuchContactException");
  165. }
  166. catch (NoSuchContactException nsee) {
  167. }
  168. }
  169. @Test
  170. public void testFetchByPrimaryKeyExisting() throws Exception {
  171. Contact newContact = addContact();
  172. Contact existingContact = _persistence.fetchByPrimaryKey(newContact.getPrimaryKey());
  173. Assert.assertEquals(existingContact, newContact);
  174. }
  175. @Test
  176. public void testFetchByPrimaryKeyMissing() throws Exception {
  177. long pk = ServiceTestUtil.nextLong();
  178. Contact missingContact = _persistence.fetchByPrimaryKey(pk);
  179. Assert.assertNull(missingContact);
  180. }
  181. @Test
  182. public void testDynamicQueryByPrimaryKeyExisting()
  183. throws Exception {
  184. Contact newContact = addContact();
  185. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(Contact.class,
  186. Contact.class.getClassLoader());
  187. dynamicQuery.add(RestrictionsFactoryUtil.eq("contactId",
  188. newContact.getContactId()));
  189. List<Contact> result = _persistence.findWithDynamicQuery(dynamicQuery);
  190. Assert.assertEquals(1, result.size());
  191. Contact existingContact = result.get(0);
  192. Assert.assertEquals(existingContact, newContact);
  193. }
  194. @Test
  195. public void testDynamicQueryByPrimaryKeyMissing() throws Exception {
  196. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(Contact.class,
  197. Contact.class.getClassLoader());
  198. dynamicQuery.add(RestrictionsFactoryUtil.eq("contactId",
  199. ServiceTestUtil.nextLong()));
  200. List<Contact> result = _persistence.findWithDynamicQuery(dynamicQuery);
  201. Assert.assertEquals(0, result.size());
  202. }
  203. @Test
  204. public void testDynamicQueryByProjectionExisting()
  205. throws Exception {
  206. Contact newContact = addContact();
  207. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(Contact.class,
  208. Contact.class.getClassLoader());
  209. dynamicQuery.setProjection(ProjectionFactoryUtil.property("contactId"));
  210. Object newContactId = newContact.getContactId();
  211. dynamicQuery.add(RestrictionsFactoryUtil.in("contactId",
  212. new Object[] { newContactId }));
  213. List<Object> result = _persistence.findWithDynamicQuery(dynamicQuery);
  214. Assert.assertEquals(1, result.size());
  215. Object existingContactId = result.get(0);
  216. Assert.assertEquals(existingContactId, newContactId);
  217. }
  218. @Test
  219. public void testDynamicQueryByProjectionMissing() throws Exception {
  220. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(Contact.class,
  221. Contact.class.getClassLoader());
  222. dynamicQuery.setProjection(ProjectionFactoryUtil.property("contactId"));
  223. dynamicQuery.add(RestrictionsFactoryUtil.in("contactId",
  224. new Object[] { ServiceTestUtil.nextLong() }));
  225. List<Object> result = _persistence.findWithDynamicQuery(dynamicQuery);
  226. Assert.assertEquals(0, result.size());
  227. }
  228. protected Contact addContact() throws Exception {
  229. long pk = ServiceTestUtil.nextLong();
  230. Contact contact = _persistence.create(pk);
  231. contact.setCompanyId(ServiceTestUtil.nextLong());
  232. contact.setUserId(ServiceTestUtil.nextLong());
  233. contact.setUserName(ServiceTestUtil.randomString());
  234. contact.setCreateDate(ServiceTestUtil.nextDate());
  235. contact.setModifiedDate(ServiceTestUtil.nextDate());
  236. contact.setAccountId(ServiceTestUtil.nextLong());
  237. contact.setParentContactId(ServiceTestUtil.nextLong());
  238. contact.setFirstName(ServiceTestUtil.randomString());
  239. contact.setMiddleName(ServiceTestUtil.randomString());
  240. contact.setLastName(ServiceTestUtil.randomString());
  241. contact.setPrefixId(ServiceTestUtil.nextInt());
  242. contact.setSuffixId(ServiceTestUtil.nextInt());
  243. contact.setMale(ServiceTestUtil.randomBoolean());
  244. contact.setBirthday(ServiceTestUtil.nextDate());
  245. contact.setSmsSn(ServiceTestUtil.randomString());
  246. contact.setAimSn(ServiceTestUtil.randomString());
  247. contact.setFacebookSn(ServiceTestUtil.randomString());
  248. contact.setIcqSn(ServiceTestUtil.randomString());
  249. contact.setJabberSn(ServiceTestUtil.randomString());
  250. contact.setMsnSn(ServiceTestUtil.randomString());
  251. contact.setMySpaceSn(ServiceTestUtil.randomString());
  252. contact.setSkypeSn(ServiceTestUtil.randomString());
  253. contact.setTwitterSn(ServiceTestUtil.randomString());
  254. contact.setYmSn(ServiceTestUtil.randomString());
  255. contact.setEmployeeStatusId(ServiceTestUtil.randomString());
  256. contact.setEmployeeNumber(ServiceTestUtil.randomString());
  257. contact.setJobTitle(ServiceTestUtil.randomString());
  258. contact.setJobClass(ServiceTestUtil.randomString());
  259. contact.setHoursOfOperation(ServiceTestUtil.randomString());
  260. _persistence.update(contact, false);
  261. return contact;
  262. }
  263. private ContactPersistence _persistence;
  264. }