PageRenderTime 48ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/modules/test/persistence-test/src/testIntegration/java/com/liferay/portal/service/persistence/test/ContactPersistenceTest.java

http://github.com/liferay/liferay-portal
Java | 581 lines | 396 code | 169 blank | 16 comment | 1 complexity | 5b877764908b10c0ab22c72e57886271 MD5 | raw file
Possible License(s): LGPL-2.0
  1. /**
  2. * Copyright (c) 2000-present 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.test;
  15. import com.liferay.arquillian.extension.junit.bridge.junit.Arquillian;
  16. import com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery;
  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.QueryUtil;
  21. import com.liferay.portal.kernel.dao.orm.RestrictionsFactoryUtil;
  22. import com.liferay.portal.kernel.exception.NoSuchContactException;
  23. import com.liferay.portal.kernel.model.Contact;
  24. import com.liferay.portal.kernel.service.ContactLocalServiceUtil;
  25. import com.liferay.portal.kernel.service.persistence.ContactPersistence;
  26. import com.liferay.portal.kernel.service.persistence.ContactUtil;
  27. import com.liferay.portal.kernel.test.rule.AggregateTestRule;
  28. import com.liferay.portal.kernel.test.util.RandomTestUtil;
  29. import com.liferay.portal.kernel.transaction.Propagation;
  30. import com.liferay.portal.kernel.util.IntegerWrapper;
  31. import com.liferay.portal.kernel.util.OrderByComparator;
  32. import com.liferay.portal.kernel.util.OrderByComparatorFactoryUtil;
  33. import com.liferay.portal.kernel.util.Time;
  34. import com.liferay.portal.test.rule.LiferayIntegrationTestRule;
  35. import com.liferay.portal.test.rule.PersistenceTestRule;
  36. import com.liferay.portal.test.rule.TransactionalTestRule;
  37. import java.io.Serializable;
  38. import java.util.ArrayList;
  39. import java.util.HashSet;
  40. import java.util.Iterator;
  41. import java.util.List;
  42. import java.util.Map;
  43. import java.util.Set;
  44. import org.junit.After;
  45. import org.junit.Assert;
  46. import org.junit.Before;
  47. import org.junit.ClassRule;
  48. import org.junit.Rule;
  49. import org.junit.Test;
  50. import org.junit.runner.RunWith;
  51. /**
  52. * @generated
  53. */
  54. @RunWith(Arquillian.class)
  55. public class ContactPersistenceTest {
  56. @ClassRule
  57. @Rule
  58. public static final AggregateTestRule aggregateTestRule =
  59. new AggregateTestRule(
  60. new LiferayIntegrationTestRule(), PersistenceTestRule.INSTANCE,
  61. new TransactionalTestRule(Propagation.REQUIRED));
  62. @Before
  63. public void setUp() {
  64. _persistence = ContactUtil.getPersistence();
  65. Class<?> clazz = _persistence.getClass();
  66. _dynamicQueryClassLoader = clazz.getClassLoader();
  67. }
  68. @After
  69. public void tearDown() throws Exception {
  70. Iterator<Contact> iterator = _contacts.iterator();
  71. while (iterator.hasNext()) {
  72. _persistence.remove(iterator.next());
  73. iterator.remove();
  74. }
  75. }
  76. @Test
  77. public void testCreate() throws Exception {
  78. long pk = RandomTestUtil.nextLong();
  79. Contact contact = _persistence.create(pk);
  80. Assert.assertNotNull(contact);
  81. Assert.assertEquals(contact.getPrimaryKey(), pk);
  82. }
  83. @Test
  84. public void testRemove() throws Exception {
  85. Contact newContact = addContact();
  86. _persistence.remove(newContact);
  87. Contact existingContact = _persistence.fetchByPrimaryKey(
  88. newContact.getPrimaryKey());
  89. Assert.assertNull(existingContact);
  90. }
  91. @Test
  92. public void testUpdateNew() throws Exception {
  93. addContact();
  94. }
  95. @Test
  96. public void testUpdateExisting() throws Exception {
  97. long pk = RandomTestUtil.nextLong();
  98. Contact newContact = _persistence.create(pk);
  99. newContact.setMvccVersion(RandomTestUtil.nextLong());
  100. newContact.setCompanyId(RandomTestUtil.nextLong());
  101. newContact.setUserId(RandomTestUtil.nextLong());
  102. newContact.setUserName(RandomTestUtil.randomString());
  103. newContact.setCreateDate(RandomTestUtil.nextDate());
  104. newContact.setModifiedDate(RandomTestUtil.nextDate());
  105. newContact.setClassNameId(RandomTestUtil.nextLong());
  106. newContact.setClassPK(RandomTestUtil.nextLong());
  107. newContact.setAccountId(RandomTestUtil.nextLong());
  108. newContact.setParentContactId(RandomTestUtil.nextLong());
  109. newContact.setEmailAddress(RandomTestUtil.randomString());
  110. newContact.setFirstName(RandomTestUtil.randomString());
  111. newContact.setMiddleName(RandomTestUtil.randomString());
  112. newContact.setLastName(RandomTestUtil.randomString());
  113. newContact.setPrefixId(RandomTestUtil.nextLong());
  114. newContact.setSuffixId(RandomTestUtil.nextLong());
  115. newContact.setMale(RandomTestUtil.randomBoolean());
  116. newContact.setBirthday(RandomTestUtil.nextDate());
  117. newContact.setSmsSn(RandomTestUtil.randomString());
  118. newContact.setFacebookSn(RandomTestUtil.randomString());
  119. newContact.setJabberSn(RandomTestUtil.randomString());
  120. newContact.setSkypeSn(RandomTestUtil.randomString());
  121. newContact.setTwitterSn(RandomTestUtil.randomString());
  122. newContact.setEmployeeStatusId(RandomTestUtil.randomString());
  123. newContact.setEmployeeNumber(RandomTestUtil.randomString());
  124. newContact.setJobTitle(RandomTestUtil.randomString());
  125. newContact.setJobClass(RandomTestUtil.randomString());
  126. newContact.setHoursOfOperation(RandomTestUtil.randomString());
  127. _contacts.add(_persistence.update(newContact));
  128. Contact existingContact = _persistence.findByPrimaryKey(
  129. newContact.getPrimaryKey());
  130. Assert.assertEquals(
  131. existingContact.getMvccVersion(), newContact.getMvccVersion());
  132. Assert.assertEquals(
  133. existingContact.getContactId(), newContact.getContactId());
  134. Assert.assertEquals(
  135. existingContact.getCompanyId(), newContact.getCompanyId());
  136. Assert.assertEquals(
  137. existingContact.getUserId(), newContact.getUserId());
  138. Assert.assertEquals(
  139. existingContact.getUserName(), newContact.getUserName());
  140. Assert.assertEquals(
  141. Time.getShortTimestamp(existingContact.getCreateDate()),
  142. Time.getShortTimestamp(newContact.getCreateDate()));
  143. Assert.assertEquals(
  144. Time.getShortTimestamp(existingContact.getModifiedDate()),
  145. Time.getShortTimestamp(newContact.getModifiedDate()));
  146. Assert.assertEquals(
  147. existingContact.getClassNameId(), newContact.getClassNameId());
  148. Assert.assertEquals(
  149. existingContact.getClassPK(), newContact.getClassPK());
  150. Assert.assertEquals(
  151. existingContact.getAccountId(), newContact.getAccountId());
  152. Assert.assertEquals(
  153. existingContact.getParentContactId(),
  154. newContact.getParentContactId());
  155. Assert.assertEquals(
  156. existingContact.getEmailAddress(), newContact.getEmailAddress());
  157. Assert.assertEquals(
  158. existingContact.getFirstName(), newContact.getFirstName());
  159. Assert.assertEquals(
  160. existingContact.getMiddleName(), newContact.getMiddleName());
  161. Assert.assertEquals(
  162. existingContact.getLastName(), newContact.getLastName());
  163. Assert.assertEquals(
  164. existingContact.getPrefixId(), newContact.getPrefixId());
  165. Assert.assertEquals(
  166. existingContact.getSuffixId(), newContact.getSuffixId());
  167. Assert.assertEquals(existingContact.isMale(), newContact.isMale());
  168. Assert.assertEquals(
  169. Time.getShortTimestamp(existingContact.getBirthday()),
  170. Time.getShortTimestamp(newContact.getBirthday()));
  171. Assert.assertEquals(existingContact.getSmsSn(), newContact.getSmsSn());
  172. Assert.assertEquals(
  173. existingContact.getFacebookSn(), newContact.getFacebookSn());
  174. Assert.assertEquals(
  175. existingContact.getJabberSn(), newContact.getJabberSn());
  176. Assert.assertEquals(
  177. existingContact.getSkypeSn(), newContact.getSkypeSn());
  178. Assert.assertEquals(
  179. existingContact.getTwitterSn(), newContact.getTwitterSn());
  180. Assert.assertEquals(
  181. existingContact.getEmployeeStatusId(),
  182. newContact.getEmployeeStatusId());
  183. Assert.assertEquals(
  184. existingContact.getEmployeeNumber(),
  185. newContact.getEmployeeNumber());
  186. Assert.assertEquals(
  187. existingContact.getJobTitle(), newContact.getJobTitle());
  188. Assert.assertEquals(
  189. existingContact.getJobClass(), newContact.getJobClass());
  190. Assert.assertEquals(
  191. existingContact.getHoursOfOperation(),
  192. newContact.getHoursOfOperation());
  193. }
  194. @Test
  195. public void testCountByCompanyId() throws Exception {
  196. _persistence.countByCompanyId(RandomTestUtil.nextLong());
  197. _persistence.countByCompanyId(0L);
  198. }
  199. @Test
  200. public void testCountByAccountId() throws Exception {
  201. _persistence.countByAccountId(RandomTestUtil.nextLong());
  202. _persistence.countByAccountId(0L);
  203. }
  204. @Test
  205. public void testCountByC_C() throws Exception {
  206. _persistence.countByC_C(
  207. RandomTestUtil.nextLong(), RandomTestUtil.nextLong());
  208. _persistence.countByC_C(0L, 0L);
  209. }
  210. @Test
  211. public void testFindByPrimaryKeyExisting() throws Exception {
  212. Contact newContact = addContact();
  213. Contact existingContact = _persistence.findByPrimaryKey(
  214. newContact.getPrimaryKey());
  215. Assert.assertEquals(existingContact, newContact);
  216. }
  217. @Test(expected = NoSuchContactException.class)
  218. public void testFindByPrimaryKeyMissing() throws Exception {
  219. long pk = RandomTestUtil.nextLong();
  220. _persistence.findByPrimaryKey(pk);
  221. }
  222. @Test
  223. public void testFindAll() throws Exception {
  224. _persistence.findAll(
  225. QueryUtil.ALL_POS, QueryUtil.ALL_POS, getOrderByComparator());
  226. }
  227. protected OrderByComparator<Contact> getOrderByComparator() {
  228. return OrderByComparatorFactoryUtil.create(
  229. "Contact_", "mvccVersion", true, "contactId", true, "companyId",
  230. true, "userId", true, "userName", true, "createDate", true,
  231. "modifiedDate", true, "classNameId", true, "classPK", true,
  232. "accountId", true, "parentContactId", true, "emailAddress", true,
  233. "firstName", true, "middleName", true, "lastName", true, "prefixId",
  234. true, "suffixId", true, "male", true, "birthday", true, "smsSn",
  235. true, "facebookSn", true, "jabberSn", true, "skypeSn", true,
  236. "twitterSn", true, "employeeStatusId", true, "employeeNumber", true,
  237. "jobTitle", true, "jobClass", true, "hoursOfOperation", true);
  238. }
  239. @Test
  240. public void testFetchByPrimaryKeyExisting() throws Exception {
  241. Contact newContact = addContact();
  242. Contact existingContact = _persistence.fetchByPrimaryKey(
  243. newContact.getPrimaryKey());
  244. Assert.assertEquals(existingContact, newContact);
  245. }
  246. @Test
  247. public void testFetchByPrimaryKeyMissing() throws Exception {
  248. long pk = RandomTestUtil.nextLong();
  249. Contact missingContact = _persistence.fetchByPrimaryKey(pk);
  250. Assert.assertNull(missingContact);
  251. }
  252. @Test
  253. public void testFetchByPrimaryKeysWithMultiplePrimaryKeysWhereAllPrimaryKeysExist()
  254. throws Exception {
  255. Contact newContact1 = addContact();
  256. Contact newContact2 = addContact();
  257. Set<Serializable> primaryKeys = new HashSet<Serializable>();
  258. primaryKeys.add(newContact1.getPrimaryKey());
  259. primaryKeys.add(newContact2.getPrimaryKey());
  260. Map<Serializable, Contact> contacts = _persistence.fetchByPrimaryKeys(
  261. primaryKeys);
  262. Assert.assertEquals(2, contacts.size());
  263. Assert.assertEquals(
  264. newContact1, contacts.get(newContact1.getPrimaryKey()));
  265. Assert.assertEquals(
  266. newContact2, contacts.get(newContact2.getPrimaryKey()));
  267. }
  268. @Test
  269. public void testFetchByPrimaryKeysWithMultiplePrimaryKeysWhereNoPrimaryKeysExist()
  270. throws Exception {
  271. long pk1 = RandomTestUtil.nextLong();
  272. long pk2 = RandomTestUtil.nextLong();
  273. Set<Serializable> primaryKeys = new HashSet<Serializable>();
  274. primaryKeys.add(pk1);
  275. primaryKeys.add(pk2);
  276. Map<Serializable, Contact> contacts = _persistence.fetchByPrimaryKeys(
  277. primaryKeys);
  278. Assert.assertTrue(contacts.isEmpty());
  279. }
  280. @Test
  281. public void testFetchByPrimaryKeysWithMultiplePrimaryKeysWhereSomePrimaryKeysExist()
  282. throws Exception {
  283. Contact newContact = addContact();
  284. long pk = RandomTestUtil.nextLong();
  285. Set<Serializable> primaryKeys = new HashSet<Serializable>();
  286. primaryKeys.add(newContact.getPrimaryKey());
  287. primaryKeys.add(pk);
  288. Map<Serializable, Contact> contacts = _persistence.fetchByPrimaryKeys(
  289. primaryKeys);
  290. Assert.assertEquals(1, contacts.size());
  291. Assert.assertEquals(
  292. newContact, contacts.get(newContact.getPrimaryKey()));
  293. }
  294. @Test
  295. public void testFetchByPrimaryKeysWithNoPrimaryKeys() throws Exception {
  296. Set<Serializable> primaryKeys = new HashSet<Serializable>();
  297. Map<Serializable, Contact> contacts = _persistence.fetchByPrimaryKeys(
  298. primaryKeys);
  299. Assert.assertTrue(contacts.isEmpty());
  300. }
  301. @Test
  302. public void testFetchByPrimaryKeysWithOnePrimaryKey() throws Exception {
  303. Contact newContact = addContact();
  304. Set<Serializable> primaryKeys = new HashSet<Serializable>();
  305. primaryKeys.add(newContact.getPrimaryKey());
  306. Map<Serializable, Contact> contacts = _persistence.fetchByPrimaryKeys(
  307. primaryKeys);
  308. Assert.assertEquals(1, contacts.size());
  309. Assert.assertEquals(
  310. newContact, contacts.get(newContact.getPrimaryKey()));
  311. }
  312. @Test
  313. public void testActionableDynamicQuery() throws Exception {
  314. final IntegerWrapper count = new IntegerWrapper();
  315. ActionableDynamicQuery actionableDynamicQuery =
  316. ContactLocalServiceUtil.getActionableDynamicQuery();
  317. actionableDynamicQuery.setPerformActionMethod(
  318. new ActionableDynamicQuery.PerformActionMethod<Contact>() {
  319. @Override
  320. public void performAction(Contact contact) {
  321. Assert.assertNotNull(contact);
  322. count.increment();
  323. }
  324. });
  325. actionableDynamicQuery.performActions();
  326. Assert.assertEquals(count.getValue(), _persistence.countAll());
  327. }
  328. @Test
  329. public void testDynamicQueryByPrimaryKeyExisting() throws Exception {
  330. Contact newContact = addContact();
  331. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
  332. Contact.class, _dynamicQueryClassLoader);
  333. dynamicQuery.add(
  334. RestrictionsFactoryUtil.eq("contactId", newContact.getContactId()));
  335. List<Contact> result = _persistence.findWithDynamicQuery(dynamicQuery);
  336. Assert.assertEquals(1, result.size());
  337. Contact existingContact = result.get(0);
  338. Assert.assertEquals(existingContact, newContact);
  339. }
  340. @Test
  341. public void testDynamicQueryByPrimaryKeyMissing() throws Exception {
  342. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
  343. Contact.class, _dynamicQueryClassLoader);
  344. dynamicQuery.add(
  345. RestrictionsFactoryUtil.eq("contactId", RandomTestUtil.nextLong()));
  346. List<Contact> result = _persistence.findWithDynamicQuery(dynamicQuery);
  347. Assert.assertEquals(0, result.size());
  348. }
  349. @Test
  350. public void testDynamicQueryByProjectionExisting() throws Exception {
  351. Contact newContact = addContact();
  352. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
  353. Contact.class, _dynamicQueryClassLoader);
  354. dynamicQuery.setProjection(ProjectionFactoryUtil.property("contactId"));
  355. Object newContactId = newContact.getContactId();
  356. dynamicQuery.add(
  357. RestrictionsFactoryUtil.in(
  358. "contactId", new Object[] {newContactId}));
  359. List<Object> result = _persistence.findWithDynamicQuery(dynamicQuery);
  360. Assert.assertEquals(1, result.size());
  361. Object existingContactId = result.get(0);
  362. Assert.assertEquals(existingContactId, newContactId);
  363. }
  364. @Test
  365. public void testDynamicQueryByProjectionMissing() throws Exception {
  366. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
  367. Contact.class, _dynamicQueryClassLoader);
  368. dynamicQuery.setProjection(ProjectionFactoryUtil.property("contactId"));
  369. dynamicQuery.add(
  370. RestrictionsFactoryUtil.in(
  371. "contactId", new Object[] {RandomTestUtil.nextLong()}));
  372. List<Object> result = _persistence.findWithDynamicQuery(dynamicQuery);
  373. Assert.assertEquals(0, result.size());
  374. }
  375. protected Contact addContact() throws Exception {
  376. long pk = RandomTestUtil.nextLong();
  377. Contact contact = _persistence.create(pk);
  378. contact.setMvccVersion(RandomTestUtil.nextLong());
  379. contact.setCompanyId(RandomTestUtil.nextLong());
  380. contact.setUserId(RandomTestUtil.nextLong());
  381. contact.setUserName(RandomTestUtil.randomString());
  382. contact.setCreateDate(RandomTestUtil.nextDate());
  383. contact.setModifiedDate(RandomTestUtil.nextDate());
  384. contact.setClassNameId(RandomTestUtil.nextLong());
  385. contact.setClassPK(RandomTestUtil.nextLong());
  386. contact.setAccountId(RandomTestUtil.nextLong());
  387. contact.setParentContactId(RandomTestUtil.nextLong());
  388. contact.setEmailAddress(RandomTestUtil.randomString());
  389. contact.setFirstName(RandomTestUtil.randomString());
  390. contact.setMiddleName(RandomTestUtil.randomString());
  391. contact.setLastName(RandomTestUtil.randomString());
  392. contact.setPrefixId(RandomTestUtil.nextLong());
  393. contact.setSuffixId(RandomTestUtil.nextLong());
  394. contact.setMale(RandomTestUtil.randomBoolean());
  395. contact.setBirthday(RandomTestUtil.nextDate());
  396. contact.setSmsSn(RandomTestUtil.randomString());
  397. contact.setFacebookSn(RandomTestUtil.randomString());
  398. contact.setJabberSn(RandomTestUtil.randomString());
  399. contact.setSkypeSn(RandomTestUtil.randomString());
  400. contact.setTwitterSn(RandomTestUtil.randomString());
  401. contact.setEmployeeStatusId(RandomTestUtil.randomString());
  402. contact.setEmployeeNumber(RandomTestUtil.randomString());
  403. contact.setJobTitle(RandomTestUtil.randomString());
  404. contact.setJobClass(RandomTestUtil.randomString());
  405. contact.setHoursOfOperation(RandomTestUtil.randomString());
  406. _contacts.add(_persistence.update(contact));
  407. return contact;
  408. }
  409. private List<Contact> _contacts = new ArrayList<Contact>();
  410. private ContactPersistence _persistence;
  411. private ClassLoader _dynamicQueryClassLoader;
  412. }