PageRenderTime 45ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

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

http://github.com/liferay/liferay-portal
Java | 627 lines | 437 code | 174 blank | 16 comment | 1 complexity | 2dfcfe83868c2feddfe34ae4aa1bad68 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.NoSuchRoleException;
  23. import com.liferay.portal.kernel.model.Role;
  24. import com.liferay.portal.kernel.service.RoleLocalServiceUtil;
  25. import com.liferay.portal.kernel.service.persistence.RolePersistence;
  26. import com.liferay.portal.kernel.service.persistence.RoleUtil;
  27. import com.liferay.portal.kernel.test.ReflectionTestUtil;
  28. import com.liferay.portal.kernel.test.rule.AggregateTestRule;
  29. import com.liferay.portal.kernel.test.util.RandomTestUtil;
  30. import com.liferay.portal.kernel.transaction.Propagation;
  31. import com.liferay.portal.kernel.util.IntegerWrapper;
  32. import com.liferay.portal.kernel.util.OrderByComparator;
  33. import com.liferay.portal.kernel.util.OrderByComparatorFactoryUtil;
  34. import com.liferay.portal.kernel.util.Time;
  35. import com.liferay.portal.test.rule.LiferayIntegrationTestRule;
  36. import com.liferay.portal.test.rule.PersistenceTestRule;
  37. import com.liferay.portal.test.rule.TransactionalTestRule;
  38. import java.io.Serializable;
  39. import java.util.ArrayList;
  40. import java.util.HashSet;
  41. import java.util.Iterator;
  42. import java.util.List;
  43. import java.util.Map;
  44. import java.util.Objects;
  45. import java.util.Set;
  46. import org.junit.After;
  47. import org.junit.Assert;
  48. import org.junit.Before;
  49. import org.junit.ClassRule;
  50. import org.junit.Rule;
  51. import org.junit.Test;
  52. import org.junit.runner.RunWith;
  53. /**
  54. * @generated
  55. */
  56. @RunWith(Arquillian.class)
  57. public class RolePersistenceTest {
  58. @ClassRule
  59. @Rule
  60. public static final AggregateTestRule aggregateTestRule =
  61. new AggregateTestRule(
  62. new LiferayIntegrationTestRule(), PersistenceTestRule.INSTANCE,
  63. new TransactionalTestRule(Propagation.REQUIRED));
  64. @Before
  65. public void setUp() {
  66. _persistence = RoleUtil.getPersistence();
  67. Class<?> clazz = _persistence.getClass();
  68. _dynamicQueryClassLoader = clazz.getClassLoader();
  69. }
  70. @After
  71. public void tearDown() throws Exception {
  72. Iterator<Role> iterator = _roles.iterator();
  73. while (iterator.hasNext()) {
  74. _persistence.remove(iterator.next());
  75. iterator.remove();
  76. }
  77. }
  78. @Test
  79. public void testCreate() throws Exception {
  80. long pk = RandomTestUtil.nextLong();
  81. Role role = _persistence.create(pk);
  82. Assert.assertNotNull(role);
  83. Assert.assertEquals(role.getPrimaryKey(), pk);
  84. }
  85. @Test
  86. public void testRemove() throws Exception {
  87. Role newRole = addRole();
  88. _persistence.remove(newRole);
  89. Role existingRole = _persistence.fetchByPrimaryKey(
  90. newRole.getPrimaryKey());
  91. Assert.assertNull(existingRole);
  92. }
  93. @Test
  94. public void testUpdateNew() throws Exception {
  95. addRole();
  96. }
  97. @Test
  98. public void testUpdateExisting() throws Exception {
  99. long pk = RandomTestUtil.nextLong();
  100. Role newRole = _persistence.create(pk);
  101. newRole.setMvccVersion(RandomTestUtil.nextLong());
  102. newRole.setCtCollectionId(RandomTestUtil.nextLong());
  103. newRole.setUuid(RandomTestUtil.randomString());
  104. newRole.setCompanyId(RandomTestUtil.nextLong());
  105. newRole.setUserId(RandomTestUtil.nextLong());
  106. newRole.setUserName(RandomTestUtil.randomString());
  107. newRole.setCreateDate(RandomTestUtil.nextDate());
  108. newRole.setModifiedDate(RandomTestUtil.nextDate());
  109. newRole.setClassNameId(RandomTestUtil.nextLong());
  110. newRole.setClassPK(RandomTestUtil.nextLong());
  111. newRole.setName(RandomTestUtil.randomString());
  112. newRole.setTitle(RandomTestUtil.randomString());
  113. newRole.setDescription(RandomTestUtil.randomString());
  114. newRole.setType(RandomTestUtil.nextInt());
  115. newRole.setSubtype(RandomTestUtil.randomString());
  116. _roles.add(_persistence.update(newRole));
  117. Role existingRole = _persistence.findByPrimaryKey(
  118. newRole.getPrimaryKey());
  119. Assert.assertEquals(
  120. existingRole.getMvccVersion(), newRole.getMvccVersion());
  121. Assert.assertEquals(
  122. existingRole.getCtCollectionId(), newRole.getCtCollectionId());
  123. Assert.assertEquals(existingRole.getUuid(), newRole.getUuid());
  124. Assert.assertEquals(existingRole.getRoleId(), newRole.getRoleId());
  125. Assert.assertEquals(
  126. existingRole.getCompanyId(), newRole.getCompanyId());
  127. Assert.assertEquals(existingRole.getUserId(), newRole.getUserId());
  128. Assert.assertEquals(existingRole.getUserName(), newRole.getUserName());
  129. Assert.assertEquals(
  130. Time.getShortTimestamp(existingRole.getCreateDate()),
  131. Time.getShortTimestamp(newRole.getCreateDate()));
  132. Assert.assertEquals(
  133. Time.getShortTimestamp(existingRole.getModifiedDate()),
  134. Time.getShortTimestamp(newRole.getModifiedDate()));
  135. Assert.assertEquals(
  136. existingRole.getClassNameId(), newRole.getClassNameId());
  137. Assert.assertEquals(existingRole.getClassPK(), newRole.getClassPK());
  138. Assert.assertEquals(existingRole.getName(), newRole.getName());
  139. Assert.assertEquals(existingRole.getTitle(), newRole.getTitle());
  140. Assert.assertEquals(
  141. existingRole.getDescription(), newRole.getDescription());
  142. Assert.assertEquals(existingRole.getType(), newRole.getType());
  143. Assert.assertEquals(existingRole.getSubtype(), newRole.getSubtype());
  144. }
  145. @Test
  146. public void testCountByUuid() throws Exception {
  147. _persistence.countByUuid("");
  148. _persistence.countByUuid("null");
  149. _persistence.countByUuid((String)null);
  150. }
  151. @Test
  152. public void testCountByUuid_C() throws Exception {
  153. _persistence.countByUuid_C("", RandomTestUtil.nextLong());
  154. _persistence.countByUuid_C("null", 0L);
  155. _persistence.countByUuid_C((String)null, 0L);
  156. }
  157. @Test
  158. public void testCountByCompanyId() throws Exception {
  159. _persistence.countByCompanyId(RandomTestUtil.nextLong());
  160. _persistence.countByCompanyId(0L);
  161. }
  162. @Test
  163. public void testCountByName() throws Exception {
  164. _persistence.countByName("");
  165. _persistence.countByName("null");
  166. _persistence.countByName((String)null);
  167. }
  168. @Test
  169. public void testCountByType() throws Exception {
  170. _persistence.countByType(RandomTestUtil.nextInt());
  171. _persistence.countByType(0);
  172. }
  173. @Test
  174. public void testCountBySubtype() throws Exception {
  175. _persistence.countBySubtype("");
  176. _persistence.countBySubtype("null");
  177. _persistence.countBySubtype((String)null);
  178. }
  179. @Test
  180. public void testCountByC_N() throws Exception {
  181. _persistence.countByC_N(RandomTestUtil.nextLong(), "");
  182. _persistence.countByC_N(0L, "null");
  183. _persistence.countByC_N(0L, (String)null);
  184. }
  185. @Test
  186. public void testCountByC_T() throws Exception {
  187. _persistence.countByC_T(
  188. RandomTestUtil.nextLong(), RandomTestUtil.nextInt());
  189. _persistence.countByC_T(0L, 0);
  190. }
  191. @Test
  192. public void testCountByC_TArrayable() throws Exception {
  193. _persistence.countByC_T(
  194. RandomTestUtil.nextLong(), new int[] {RandomTestUtil.nextInt(), 0});
  195. }
  196. @Test
  197. public void testCountByT_S() throws Exception {
  198. _persistence.countByT_S(RandomTestUtil.nextInt(), "");
  199. _persistence.countByT_S(0, "null");
  200. _persistence.countByT_S(0, (String)null);
  201. }
  202. @Test
  203. public void testCountByC_C_C() throws Exception {
  204. _persistence.countByC_C_C(
  205. RandomTestUtil.nextLong(), RandomTestUtil.nextLong(),
  206. RandomTestUtil.nextLong());
  207. _persistence.countByC_C_C(0L, 0L, 0L);
  208. }
  209. @Test
  210. public void testCountByC_C_CArrayable() throws Exception {
  211. _persistence.countByC_C_C(
  212. RandomTestUtil.nextLong(), RandomTestUtil.nextLong(),
  213. new long[] {RandomTestUtil.nextLong(), 0L});
  214. }
  215. @Test
  216. public void testCountByC_C_C_T() throws Exception {
  217. _persistence.countByC_C_C_T(
  218. RandomTestUtil.nextLong(), RandomTestUtil.nextLong(),
  219. RandomTestUtil.nextLong(), RandomTestUtil.nextInt());
  220. _persistence.countByC_C_C_T(0L, 0L, 0L, 0);
  221. }
  222. @Test
  223. public void testCountByC_C_C_TArrayable() throws Exception {
  224. _persistence.countByC_C_C_T(
  225. RandomTestUtil.nextLong(), RandomTestUtil.nextLong(),
  226. new long[] {RandomTestUtil.nextLong(), 0L},
  227. RandomTestUtil.nextInt());
  228. }
  229. @Test
  230. public void testFindByPrimaryKeyExisting() throws Exception {
  231. Role newRole = addRole();
  232. Role existingRole = _persistence.findByPrimaryKey(
  233. newRole.getPrimaryKey());
  234. Assert.assertEquals(existingRole, newRole);
  235. }
  236. @Test(expected = NoSuchRoleException.class)
  237. public void testFindByPrimaryKeyMissing() throws Exception {
  238. long pk = RandomTestUtil.nextLong();
  239. _persistence.findByPrimaryKey(pk);
  240. }
  241. @Test
  242. public void testFindAll() throws Exception {
  243. _persistence.findAll(
  244. QueryUtil.ALL_POS, QueryUtil.ALL_POS, getOrderByComparator());
  245. }
  246. protected OrderByComparator<Role> getOrderByComparator() {
  247. return OrderByComparatorFactoryUtil.create(
  248. "Role_", "mvccVersion", true, "ctCollectionId", true, "uuid", true,
  249. "roleId", true, "companyId", true, "userId", true, "userName", true,
  250. "createDate", true, "modifiedDate", true, "classNameId", true,
  251. "classPK", true, "name", true, "title", true, "description", true,
  252. "type", true, "subtype", true);
  253. }
  254. @Test
  255. public void testFetchByPrimaryKeyExisting() throws Exception {
  256. Role newRole = addRole();
  257. Role existingRole = _persistence.fetchByPrimaryKey(
  258. newRole.getPrimaryKey());
  259. Assert.assertEquals(existingRole, newRole);
  260. }
  261. @Test
  262. public void testFetchByPrimaryKeyMissing() throws Exception {
  263. long pk = RandomTestUtil.nextLong();
  264. Role missingRole = _persistence.fetchByPrimaryKey(pk);
  265. Assert.assertNull(missingRole);
  266. }
  267. @Test
  268. public void testFetchByPrimaryKeysWithMultiplePrimaryKeysWhereAllPrimaryKeysExist()
  269. throws Exception {
  270. Role newRole1 = addRole();
  271. Role newRole2 = addRole();
  272. Set<Serializable> primaryKeys = new HashSet<Serializable>();
  273. primaryKeys.add(newRole1.getPrimaryKey());
  274. primaryKeys.add(newRole2.getPrimaryKey());
  275. Map<Serializable, Role> roles = _persistence.fetchByPrimaryKeys(
  276. primaryKeys);
  277. Assert.assertEquals(2, roles.size());
  278. Assert.assertEquals(newRole1, roles.get(newRole1.getPrimaryKey()));
  279. Assert.assertEquals(newRole2, roles.get(newRole2.getPrimaryKey()));
  280. }
  281. @Test
  282. public void testFetchByPrimaryKeysWithMultiplePrimaryKeysWhereNoPrimaryKeysExist()
  283. throws Exception {
  284. long pk1 = RandomTestUtil.nextLong();
  285. long pk2 = RandomTestUtil.nextLong();
  286. Set<Serializable> primaryKeys = new HashSet<Serializable>();
  287. primaryKeys.add(pk1);
  288. primaryKeys.add(pk2);
  289. Map<Serializable, Role> roles = _persistence.fetchByPrimaryKeys(
  290. primaryKeys);
  291. Assert.assertTrue(roles.isEmpty());
  292. }
  293. @Test
  294. public void testFetchByPrimaryKeysWithMultiplePrimaryKeysWhereSomePrimaryKeysExist()
  295. throws Exception {
  296. Role newRole = addRole();
  297. long pk = RandomTestUtil.nextLong();
  298. Set<Serializable> primaryKeys = new HashSet<Serializable>();
  299. primaryKeys.add(newRole.getPrimaryKey());
  300. primaryKeys.add(pk);
  301. Map<Serializable, Role> roles = _persistence.fetchByPrimaryKeys(
  302. primaryKeys);
  303. Assert.assertEquals(1, roles.size());
  304. Assert.assertEquals(newRole, roles.get(newRole.getPrimaryKey()));
  305. }
  306. @Test
  307. public void testFetchByPrimaryKeysWithNoPrimaryKeys() throws Exception {
  308. Set<Serializable> primaryKeys = new HashSet<Serializable>();
  309. Map<Serializable, Role> roles = _persistence.fetchByPrimaryKeys(
  310. primaryKeys);
  311. Assert.assertTrue(roles.isEmpty());
  312. }
  313. @Test
  314. public void testFetchByPrimaryKeysWithOnePrimaryKey() throws Exception {
  315. Role newRole = addRole();
  316. Set<Serializable> primaryKeys = new HashSet<Serializable>();
  317. primaryKeys.add(newRole.getPrimaryKey());
  318. Map<Serializable, Role> roles = _persistence.fetchByPrimaryKeys(
  319. primaryKeys);
  320. Assert.assertEquals(1, roles.size());
  321. Assert.assertEquals(newRole, roles.get(newRole.getPrimaryKey()));
  322. }
  323. @Test
  324. public void testActionableDynamicQuery() throws Exception {
  325. final IntegerWrapper count = new IntegerWrapper();
  326. ActionableDynamicQuery actionableDynamicQuery =
  327. RoleLocalServiceUtil.getActionableDynamicQuery();
  328. actionableDynamicQuery.setPerformActionMethod(
  329. new ActionableDynamicQuery.PerformActionMethod<Role>() {
  330. @Override
  331. public void performAction(Role role) {
  332. Assert.assertNotNull(role);
  333. count.increment();
  334. }
  335. });
  336. actionableDynamicQuery.performActions();
  337. Assert.assertEquals(count.getValue(), _persistence.countAll());
  338. }
  339. @Test
  340. public void testDynamicQueryByPrimaryKeyExisting() throws Exception {
  341. Role newRole = addRole();
  342. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
  343. Role.class, _dynamicQueryClassLoader);
  344. dynamicQuery.add(
  345. RestrictionsFactoryUtil.eq("roleId", newRole.getRoleId()));
  346. List<Role> result = _persistence.findWithDynamicQuery(dynamicQuery);
  347. Assert.assertEquals(1, result.size());
  348. Role existingRole = result.get(0);
  349. Assert.assertEquals(existingRole, newRole);
  350. }
  351. @Test
  352. public void testDynamicQueryByPrimaryKeyMissing() throws Exception {
  353. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
  354. Role.class, _dynamicQueryClassLoader);
  355. dynamicQuery.add(
  356. RestrictionsFactoryUtil.eq("roleId", RandomTestUtil.nextLong()));
  357. List<Role> result = _persistence.findWithDynamicQuery(dynamicQuery);
  358. Assert.assertEquals(0, result.size());
  359. }
  360. @Test
  361. public void testDynamicQueryByProjectionExisting() throws Exception {
  362. Role newRole = addRole();
  363. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
  364. Role.class, _dynamicQueryClassLoader);
  365. dynamicQuery.setProjection(ProjectionFactoryUtil.property("roleId"));
  366. Object newRoleId = newRole.getRoleId();
  367. dynamicQuery.add(
  368. RestrictionsFactoryUtil.in("roleId", new Object[] {newRoleId}));
  369. List<Object> result = _persistence.findWithDynamicQuery(dynamicQuery);
  370. Assert.assertEquals(1, result.size());
  371. Object existingRoleId = result.get(0);
  372. Assert.assertEquals(existingRoleId, newRoleId);
  373. }
  374. @Test
  375. public void testDynamicQueryByProjectionMissing() throws Exception {
  376. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
  377. Role.class, _dynamicQueryClassLoader);
  378. dynamicQuery.setProjection(ProjectionFactoryUtil.property("roleId"));
  379. dynamicQuery.add(
  380. RestrictionsFactoryUtil.in(
  381. "roleId", new Object[] {RandomTestUtil.nextLong()}));
  382. List<Object> result = _persistence.findWithDynamicQuery(dynamicQuery);
  383. Assert.assertEquals(0, result.size());
  384. }
  385. @Test
  386. public void testResetOriginalValues() throws Exception {
  387. Role newRole = addRole();
  388. _persistence.clearCache();
  389. Role existingRole = _persistence.findByPrimaryKey(
  390. newRole.getPrimaryKey());
  391. Assert.assertEquals(
  392. Long.valueOf(existingRole.getCompanyId()),
  393. ReflectionTestUtil.<Long>invoke(
  394. existingRole, "getOriginalCompanyId", new Class<?>[0]));
  395. Assert.assertTrue(
  396. Objects.equals(
  397. existingRole.getName(),
  398. ReflectionTestUtil.invoke(
  399. existingRole, "getOriginalName", new Class<?>[0])));
  400. Assert.assertEquals(
  401. Long.valueOf(existingRole.getCompanyId()),
  402. ReflectionTestUtil.<Long>invoke(
  403. existingRole, "getOriginalCompanyId", new Class<?>[0]));
  404. Assert.assertEquals(
  405. Long.valueOf(existingRole.getClassNameId()),
  406. ReflectionTestUtil.<Long>invoke(
  407. existingRole, "getOriginalClassNameId", new Class<?>[0]));
  408. Assert.assertEquals(
  409. Long.valueOf(existingRole.getClassPK()),
  410. ReflectionTestUtil.<Long>invoke(
  411. existingRole, "getOriginalClassPK", new Class<?>[0]));
  412. Assert.assertEquals(
  413. Long.valueOf(existingRole.getCompanyId()),
  414. ReflectionTestUtil.<Long>invoke(
  415. existingRole, "getOriginalCompanyId", new Class<?>[0]));
  416. Assert.assertEquals(
  417. Long.valueOf(existingRole.getClassNameId()),
  418. ReflectionTestUtil.<Long>invoke(
  419. existingRole, "getOriginalClassNameId", new Class<?>[0]));
  420. Assert.assertEquals(
  421. Long.valueOf(existingRole.getClassPK()),
  422. ReflectionTestUtil.<Long>invoke(
  423. existingRole, "getOriginalClassPK", new Class<?>[0]));
  424. Assert.assertEquals(
  425. Integer.valueOf(existingRole.getType()),
  426. ReflectionTestUtil.<Integer>invoke(
  427. existingRole, "getOriginalType", new Class<?>[0]));
  428. }
  429. protected Role addRole() throws Exception {
  430. long pk = RandomTestUtil.nextLong();
  431. Role role = _persistence.create(pk);
  432. role.setMvccVersion(RandomTestUtil.nextLong());
  433. role.setCtCollectionId(RandomTestUtil.nextLong());
  434. role.setUuid(RandomTestUtil.randomString());
  435. role.setCompanyId(RandomTestUtil.nextLong());
  436. role.setUserId(RandomTestUtil.nextLong());
  437. role.setUserName(RandomTestUtil.randomString());
  438. role.setCreateDate(RandomTestUtil.nextDate());
  439. role.setModifiedDate(RandomTestUtil.nextDate());
  440. role.setClassNameId(RandomTestUtil.nextLong());
  441. role.setClassPK(RandomTestUtil.nextLong());
  442. role.setName(RandomTestUtil.randomString());
  443. role.setTitle(RandomTestUtil.randomString());
  444. role.setDescription(RandomTestUtil.randomString());
  445. role.setType(RandomTestUtil.nextInt());
  446. role.setSubtype(RandomTestUtil.randomString());
  447. _roles.add(_persistence.update(role));
  448. return role;
  449. }
  450. private List<Role> _roles = new ArrayList<Role>();
  451. private RolePersistence _persistence;
  452. private ClassLoader _dynamicQueryClassLoader;
  453. }