PageRenderTime 27ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

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

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