PageRenderTime 58ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/liferay/liferay-portal
Java | 479 lines | 324 code | 139 blank | 16 comment | 1 complexity | 6600e5706df4070ddc42b791c947eb3f 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.NoSuchAccountException;
  23. import com.liferay.portal.kernel.model.Account;
  24. import com.liferay.portal.kernel.service.AccountLocalServiceUtil;
  25. import com.liferay.portal.kernel.service.persistence.AccountPersistence;
  26. import com.liferay.portal.kernel.service.persistence.AccountUtil;
  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 AccountPersistenceTest {
  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 = AccountUtil.getPersistence();
  65. Class<?> clazz = _persistence.getClass();
  66. _dynamicQueryClassLoader = clazz.getClassLoader();
  67. }
  68. @After
  69. public void tearDown() throws Exception {
  70. Iterator<Account> iterator = _accounts.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. Account account = _persistence.create(pk);
  80. Assert.assertNotNull(account);
  81. Assert.assertEquals(account.getPrimaryKey(), pk);
  82. }
  83. @Test
  84. public void testRemove() throws Exception {
  85. Account newAccount = addAccount();
  86. _persistence.remove(newAccount);
  87. Account existingAccount = _persistence.fetchByPrimaryKey(
  88. newAccount.getPrimaryKey());
  89. Assert.assertNull(existingAccount);
  90. }
  91. @Test
  92. public void testUpdateNew() throws Exception {
  93. addAccount();
  94. }
  95. @Test
  96. public void testUpdateExisting() throws Exception {
  97. long pk = RandomTestUtil.nextLong();
  98. Account newAccount = _persistence.create(pk);
  99. newAccount.setMvccVersion(RandomTestUtil.nextLong());
  100. newAccount.setCompanyId(RandomTestUtil.nextLong());
  101. newAccount.setUserId(RandomTestUtil.nextLong());
  102. newAccount.setUserName(RandomTestUtil.randomString());
  103. newAccount.setCreateDate(RandomTestUtil.nextDate());
  104. newAccount.setModifiedDate(RandomTestUtil.nextDate());
  105. newAccount.setParentAccountId(RandomTestUtil.nextLong());
  106. newAccount.setName(RandomTestUtil.randomString());
  107. newAccount.setLegalName(RandomTestUtil.randomString());
  108. newAccount.setLegalId(RandomTestUtil.randomString());
  109. newAccount.setLegalType(RandomTestUtil.randomString());
  110. newAccount.setSicCode(RandomTestUtil.randomString());
  111. newAccount.setTickerSymbol(RandomTestUtil.randomString());
  112. newAccount.setIndustry(RandomTestUtil.randomString());
  113. newAccount.setType(RandomTestUtil.randomString());
  114. newAccount.setSize(RandomTestUtil.randomString());
  115. _accounts.add(_persistence.update(newAccount));
  116. Account existingAccount = _persistence.findByPrimaryKey(
  117. newAccount.getPrimaryKey());
  118. Assert.assertEquals(
  119. existingAccount.getMvccVersion(), newAccount.getMvccVersion());
  120. Assert.assertEquals(
  121. existingAccount.getAccountId(), newAccount.getAccountId());
  122. Assert.assertEquals(
  123. existingAccount.getCompanyId(), newAccount.getCompanyId());
  124. Assert.assertEquals(
  125. existingAccount.getUserId(), newAccount.getUserId());
  126. Assert.assertEquals(
  127. existingAccount.getUserName(), newAccount.getUserName());
  128. Assert.assertEquals(
  129. Time.getShortTimestamp(existingAccount.getCreateDate()),
  130. Time.getShortTimestamp(newAccount.getCreateDate()));
  131. Assert.assertEquals(
  132. Time.getShortTimestamp(existingAccount.getModifiedDate()),
  133. Time.getShortTimestamp(newAccount.getModifiedDate()));
  134. Assert.assertEquals(
  135. existingAccount.getParentAccountId(),
  136. newAccount.getParentAccountId());
  137. Assert.assertEquals(existingAccount.getName(), newAccount.getName());
  138. Assert.assertEquals(
  139. existingAccount.getLegalName(), newAccount.getLegalName());
  140. Assert.assertEquals(
  141. existingAccount.getLegalId(), newAccount.getLegalId());
  142. Assert.assertEquals(
  143. existingAccount.getLegalType(), newAccount.getLegalType());
  144. Assert.assertEquals(
  145. existingAccount.getSicCode(), newAccount.getSicCode());
  146. Assert.assertEquals(
  147. existingAccount.getTickerSymbol(), newAccount.getTickerSymbol());
  148. Assert.assertEquals(
  149. existingAccount.getIndustry(), newAccount.getIndustry());
  150. Assert.assertEquals(existingAccount.getType(), newAccount.getType());
  151. Assert.assertEquals(existingAccount.getSize(), newAccount.getSize());
  152. }
  153. @Test
  154. public void testFindByPrimaryKeyExisting() throws Exception {
  155. Account newAccount = addAccount();
  156. Account existingAccount = _persistence.findByPrimaryKey(
  157. newAccount.getPrimaryKey());
  158. Assert.assertEquals(existingAccount, newAccount);
  159. }
  160. @Test(expected = NoSuchAccountException.class)
  161. public void testFindByPrimaryKeyMissing() throws Exception {
  162. long pk = RandomTestUtil.nextLong();
  163. _persistence.findByPrimaryKey(pk);
  164. }
  165. @Test
  166. public void testFindAll() throws Exception {
  167. _persistence.findAll(
  168. QueryUtil.ALL_POS, QueryUtil.ALL_POS, getOrderByComparator());
  169. }
  170. protected OrderByComparator<Account> getOrderByComparator() {
  171. return OrderByComparatorFactoryUtil.create(
  172. "Account_", "mvccVersion", true, "accountId", true, "companyId",
  173. true, "userId", true, "userName", true, "createDate", true,
  174. "modifiedDate", true, "parentAccountId", true, "name", true,
  175. "legalName", true, "legalId", true, "legalType", true, "sicCode",
  176. true, "tickerSymbol", true, "industry", true, "type", true, "size",
  177. true);
  178. }
  179. @Test
  180. public void testFetchByPrimaryKeyExisting() throws Exception {
  181. Account newAccount = addAccount();
  182. Account existingAccount = _persistence.fetchByPrimaryKey(
  183. newAccount.getPrimaryKey());
  184. Assert.assertEquals(existingAccount, newAccount);
  185. }
  186. @Test
  187. public void testFetchByPrimaryKeyMissing() throws Exception {
  188. long pk = RandomTestUtil.nextLong();
  189. Account missingAccount = _persistence.fetchByPrimaryKey(pk);
  190. Assert.assertNull(missingAccount);
  191. }
  192. @Test
  193. public void testFetchByPrimaryKeysWithMultiplePrimaryKeysWhereAllPrimaryKeysExist()
  194. throws Exception {
  195. Account newAccount1 = addAccount();
  196. Account newAccount2 = addAccount();
  197. Set<Serializable> primaryKeys = new HashSet<Serializable>();
  198. primaryKeys.add(newAccount1.getPrimaryKey());
  199. primaryKeys.add(newAccount2.getPrimaryKey());
  200. Map<Serializable, Account> accounts = _persistence.fetchByPrimaryKeys(
  201. primaryKeys);
  202. Assert.assertEquals(2, accounts.size());
  203. Assert.assertEquals(
  204. newAccount1, accounts.get(newAccount1.getPrimaryKey()));
  205. Assert.assertEquals(
  206. newAccount2, accounts.get(newAccount2.getPrimaryKey()));
  207. }
  208. @Test
  209. public void testFetchByPrimaryKeysWithMultiplePrimaryKeysWhereNoPrimaryKeysExist()
  210. throws Exception {
  211. long pk1 = RandomTestUtil.nextLong();
  212. long pk2 = RandomTestUtil.nextLong();
  213. Set<Serializable> primaryKeys = new HashSet<Serializable>();
  214. primaryKeys.add(pk1);
  215. primaryKeys.add(pk2);
  216. Map<Serializable, Account> accounts = _persistence.fetchByPrimaryKeys(
  217. primaryKeys);
  218. Assert.assertTrue(accounts.isEmpty());
  219. }
  220. @Test
  221. public void testFetchByPrimaryKeysWithMultiplePrimaryKeysWhereSomePrimaryKeysExist()
  222. throws Exception {
  223. Account newAccount = addAccount();
  224. long pk = RandomTestUtil.nextLong();
  225. Set<Serializable> primaryKeys = new HashSet<Serializable>();
  226. primaryKeys.add(newAccount.getPrimaryKey());
  227. primaryKeys.add(pk);
  228. Map<Serializable, Account> accounts = _persistence.fetchByPrimaryKeys(
  229. primaryKeys);
  230. Assert.assertEquals(1, accounts.size());
  231. Assert.assertEquals(
  232. newAccount, accounts.get(newAccount.getPrimaryKey()));
  233. }
  234. @Test
  235. public void testFetchByPrimaryKeysWithNoPrimaryKeys() throws Exception {
  236. Set<Serializable> primaryKeys = new HashSet<Serializable>();
  237. Map<Serializable, Account> accounts = _persistence.fetchByPrimaryKeys(
  238. primaryKeys);
  239. Assert.assertTrue(accounts.isEmpty());
  240. }
  241. @Test
  242. public void testFetchByPrimaryKeysWithOnePrimaryKey() throws Exception {
  243. Account newAccount = addAccount();
  244. Set<Serializable> primaryKeys = new HashSet<Serializable>();
  245. primaryKeys.add(newAccount.getPrimaryKey());
  246. Map<Serializable, Account> accounts = _persistence.fetchByPrimaryKeys(
  247. primaryKeys);
  248. Assert.assertEquals(1, accounts.size());
  249. Assert.assertEquals(
  250. newAccount, accounts.get(newAccount.getPrimaryKey()));
  251. }
  252. @Test
  253. public void testActionableDynamicQuery() throws Exception {
  254. final IntegerWrapper count = new IntegerWrapper();
  255. ActionableDynamicQuery actionableDynamicQuery =
  256. AccountLocalServiceUtil.getActionableDynamicQuery();
  257. actionableDynamicQuery.setPerformActionMethod(
  258. new ActionableDynamicQuery.PerformActionMethod<Account>() {
  259. @Override
  260. public void performAction(Account account) {
  261. Assert.assertNotNull(account);
  262. count.increment();
  263. }
  264. });
  265. actionableDynamicQuery.performActions();
  266. Assert.assertEquals(count.getValue(), _persistence.countAll());
  267. }
  268. @Test
  269. public void testDynamicQueryByPrimaryKeyExisting() throws Exception {
  270. Account newAccount = addAccount();
  271. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
  272. Account.class, _dynamicQueryClassLoader);
  273. dynamicQuery.add(
  274. RestrictionsFactoryUtil.eq("accountId", newAccount.getAccountId()));
  275. List<Account> result = _persistence.findWithDynamicQuery(dynamicQuery);
  276. Assert.assertEquals(1, result.size());
  277. Account existingAccount = result.get(0);
  278. Assert.assertEquals(existingAccount, newAccount);
  279. }
  280. @Test
  281. public void testDynamicQueryByPrimaryKeyMissing() throws Exception {
  282. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
  283. Account.class, _dynamicQueryClassLoader);
  284. dynamicQuery.add(
  285. RestrictionsFactoryUtil.eq("accountId", RandomTestUtil.nextLong()));
  286. List<Account> result = _persistence.findWithDynamicQuery(dynamicQuery);
  287. Assert.assertEquals(0, result.size());
  288. }
  289. @Test
  290. public void testDynamicQueryByProjectionExisting() throws Exception {
  291. Account newAccount = addAccount();
  292. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
  293. Account.class, _dynamicQueryClassLoader);
  294. dynamicQuery.setProjection(ProjectionFactoryUtil.property("accountId"));
  295. Object newAccountId = newAccount.getAccountId();
  296. dynamicQuery.add(
  297. RestrictionsFactoryUtil.in(
  298. "accountId", new Object[] {newAccountId}));
  299. List<Object> result = _persistence.findWithDynamicQuery(dynamicQuery);
  300. Assert.assertEquals(1, result.size());
  301. Object existingAccountId = result.get(0);
  302. Assert.assertEquals(existingAccountId, newAccountId);
  303. }
  304. @Test
  305. public void testDynamicQueryByProjectionMissing() throws Exception {
  306. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
  307. Account.class, _dynamicQueryClassLoader);
  308. dynamicQuery.setProjection(ProjectionFactoryUtil.property("accountId"));
  309. dynamicQuery.add(
  310. RestrictionsFactoryUtil.in(
  311. "accountId", new Object[] {RandomTestUtil.nextLong()}));
  312. List<Object> result = _persistence.findWithDynamicQuery(dynamicQuery);
  313. Assert.assertEquals(0, result.size());
  314. }
  315. protected Account addAccount() throws Exception {
  316. long pk = RandomTestUtil.nextLong();
  317. Account account = _persistence.create(pk);
  318. account.setMvccVersion(RandomTestUtil.nextLong());
  319. account.setCompanyId(RandomTestUtil.nextLong());
  320. account.setUserId(RandomTestUtil.nextLong());
  321. account.setUserName(RandomTestUtil.randomString());
  322. account.setCreateDate(RandomTestUtil.nextDate());
  323. account.setModifiedDate(RandomTestUtil.nextDate());
  324. account.setParentAccountId(RandomTestUtil.nextLong());
  325. account.setName(RandomTestUtil.randomString());
  326. account.setLegalName(RandomTestUtil.randomString());
  327. account.setLegalId(RandomTestUtil.randomString());
  328. account.setLegalType(RandomTestUtil.randomString());
  329. account.setSicCode(RandomTestUtil.randomString());
  330. account.setTickerSymbol(RandomTestUtil.randomString());
  331. account.setIndustry(RandomTestUtil.randomString());
  332. account.setType(RandomTestUtil.randomString());
  333. account.setSize(RandomTestUtil.randomString());
  334. _accounts.add(_persistence.update(account));
  335. return account;
  336. }
  337. private List<Account> _accounts = new ArrayList<Account>();
  338. private AccountPersistence _persistence;
  339. private ClassLoader _dynamicQueryClassLoader;
  340. }