PageRenderTime 39ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

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

http://github.com/liferay/liferay-portal
Java | 500 lines | 349 code | 135 blank | 16 comment | 1 complexity | 76dd92036985814a77ff4cb738f233db 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.NoSuchUserGroupRoleException;
  23. import com.liferay.portal.kernel.model.UserGroupRole;
  24. import com.liferay.portal.kernel.service.UserGroupRoleLocalServiceUtil;
  25. import com.liferay.portal.kernel.service.persistence.UserGroupRolePersistence;
  26. import com.liferay.portal.kernel.service.persistence.UserGroupRoleUtil;
  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.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 UserGroupRolePersistenceTest {
  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 = UserGroupRoleUtil.getPersistence();
  65. Class<?> clazz = _persistence.getClass();
  66. _dynamicQueryClassLoader = clazz.getClassLoader();
  67. }
  68. @After
  69. public void tearDown() throws Exception {
  70. Iterator<UserGroupRole> iterator = _userGroupRoles.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. UserGroupRole userGroupRole = _persistence.create(pk);
  80. Assert.assertNotNull(userGroupRole);
  81. Assert.assertEquals(userGroupRole.getPrimaryKey(), pk);
  82. }
  83. @Test
  84. public void testRemove() throws Exception {
  85. UserGroupRole newUserGroupRole = addUserGroupRole();
  86. _persistence.remove(newUserGroupRole);
  87. UserGroupRole existingUserGroupRole = _persistence.fetchByPrimaryKey(
  88. newUserGroupRole.getPrimaryKey());
  89. Assert.assertNull(existingUserGroupRole);
  90. }
  91. @Test
  92. public void testUpdateNew() throws Exception {
  93. addUserGroupRole();
  94. }
  95. @Test
  96. public void testUpdateExisting() throws Exception {
  97. long pk = RandomTestUtil.nextLong();
  98. UserGroupRole newUserGroupRole = _persistence.create(pk);
  99. newUserGroupRole.setMvccVersion(RandomTestUtil.nextLong());
  100. newUserGroupRole.setCtCollectionId(RandomTestUtil.nextLong());
  101. newUserGroupRole.setCompanyId(RandomTestUtil.nextLong());
  102. newUserGroupRole.setUserId(RandomTestUtil.nextLong());
  103. newUserGroupRole.setGroupId(RandomTestUtil.nextLong());
  104. newUserGroupRole.setRoleId(RandomTestUtil.nextLong());
  105. _userGroupRoles.add(_persistence.update(newUserGroupRole));
  106. UserGroupRole existingUserGroupRole = _persistence.findByPrimaryKey(
  107. newUserGroupRole.getPrimaryKey());
  108. Assert.assertEquals(
  109. existingUserGroupRole.getMvccVersion(),
  110. newUserGroupRole.getMvccVersion());
  111. Assert.assertEquals(
  112. existingUserGroupRole.getCtCollectionId(),
  113. newUserGroupRole.getCtCollectionId());
  114. Assert.assertEquals(
  115. existingUserGroupRole.getUserGroupRoleId(),
  116. newUserGroupRole.getUserGroupRoleId());
  117. Assert.assertEquals(
  118. existingUserGroupRole.getCompanyId(),
  119. newUserGroupRole.getCompanyId());
  120. Assert.assertEquals(
  121. existingUserGroupRole.getUserId(), newUserGroupRole.getUserId());
  122. Assert.assertEquals(
  123. existingUserGroupRole.getGroupId(), newUserGroupRole.getGroupId());
  124. Assert.assertEquals(
  125. existingUserGroupRole.getRoleId(), newUserGroupRole.getRoleId());
  126. }
  127. @Test
  128. public void testCountByUserId() throws Exception {
  129. _persistence.countByUserId(RandomTestUtil.nextLong());
  130. _persistence.countByUserId(0L);
  131. }
  132. @Test
  133. public void testCountByGroupId() throws Exception {
  134. _persistence.countByGroupId(RandomTestUtil.nextLong());
  135. _persistence.countByGroupId(0L);
  136. }
  137. @Test
  138. public void testCountByRoleId() throws Exception {
  139. _persistence.countByRoleId(RandomTestUtil.nextLong());
  140. _persistence.countByRoleId(0L);
  141. }
  142. @Test
  143. public void testCountByU_G() throws Exception {
  144. _persistence.countByU_G(
  145. RandomTestUtil.nextLong(), RandomTestUtil.nextLong());
  146. _persistence.countByU_G(0L, 0L);
  147. }
  148. @Test
  149. public void testCountByG_R() throws Exception {
  150. _persistence.countByG_R(
  151. RandomTestUtil.nextLong(), RandomTestUtil.nextLong());
  152. _persistence.countByG_R(0L, 0L);
  153. }
  154. @Test
  155. public void testCountByU_G_R() throws Exception {
  156. _persistence.countByU_G_R(
  157. RandomTestUtil.nextLong(), RandomTestUtil.nextLong(),
  158. RandomTestUtil.nextLong());
  159. _persistence.countByU_G_R(0L, 0L, 0L);
  160. }
  161. @Test
  162. public void testFindByPrimaryKeyExisting() throws Exception {
  163. UserGroupRole newUserGroupRole = addUserGroupRole();
  164. UserGroupRole existingUserGroupRole = _persistence.findByPrimaryKey(
  165. newUserGroupRole.getPrimaryKey());
  166. Assert.assertEquals(existingUserGroupRole, newUserGroupRole);
  167. }
  168. @Test(expected = NoSuchUserGroupRoleException.class)
  169. public void testFindByPrimaryKeyMissing() throws Exception {
  170. long pk = RandomTestUtil.nextLong();
  171. _persistence.findByPrimaryKey(pk);
  172. }
  173. @Test
  174. public void testFindAll() throws Exception {
  175. _persistence.findAll(
  176. QueryUtil.ALL_POS, QueryUtil.ALL_POS, getOrderByComparator());
  177. }
  178. protected OrderByComparator<UserGroupRole> getOrderByComparator() {
  179. return OrderByComparatorFactoryUtil.create(
  180. "UserGroupRole", "mvccVersion", true, "ctCollectionId", true,
  181. "userGroupRoleId", true, "companyId", true, "userId", true,
  182. "groupId", true, "roleId", true);
  183. }
  184. @Test
  185. public void testFetchByPrimaryKeyExisting() throws Exception {
  186. UserGroupRole newUserGroupRole = addUserGroupRole();
  187. UserGroupRole existingUserGroupRole = _persistence.fetchByPrimaryKey(
  188. newUserGroupRole.getPrimaryKey());
  189. Assert.assertEquals(existingUserGroupRole, newUserGroupRole);
  190. }
  191. @Test
  192. public void testFetchByPrimaryKeyMissing() throws Exception {
  193. long pk = RandomTestUtil.nextLong();
  194. UserGroupRole missingUserGroupRole = _persistence.fetchByPrimaryKey(pk);
  195. Assert.assertNull(missingUserGroupRole);
  196. }
  197. @Test
  198. public void testFetchByPrimaryKeysWithMultiplePrimaryKeysWhereAllPrimaryKeysExist()
  199. throws Exception {
  200. UserGroupRole newUserGroupRole1 = addUserGroupRole();
  201. UserGroupRole newUserGroupRole2 = addUserGroupRole();
  202. Set<Serializable> primaryKeys = new HashSet<Serializable>();
  203. primaryKeys.add(newUserGroupRole1.getPrimaryKey());
  204. primaryKeys.add(newUserGroupRole2.getPrimaryKey());
  205. Map<Serializable, UserGroupRole> userGroupRoles =
  206. _persistence.fetchByPrimaryKeys(primaryKeys);
  207. Assert.assertEquals(2, userGroupRoles.size());
  208. Assert.assertEquals(
  209. newUserGroupRole1,
  210. userGroupRoles.get(newUserGroupRole1.getPrimaryKey()));
  211. Assert.assertEquals(
  212. newUserGroupRole2,
  213. userGroupRoles.get(newUserGroupRole2.getPrimaryKey()));
  214. }
  215. @Test
  216. public void testFetchByPrimaryKeysWithMultiplePrimaryKeysWhereNoPrimaryKeysExist()
  217. throws Exception {
  218. long pk1 = RandomTestUtil.nextLong();
  219. long pk2 = RandomTestUtil.nextLong();
  220. Set<Serializable> primaryKeys = new HashSet<Serializable>();
  221. primaryKeys.add(pk1);
  222. primaryKeys.add(pk2);
  223. Map<Serializable, UserGroupRole> userGroupRoles =
  224. _persistence.fetchByPrimaryKeys(primaryKeys);
  225. Assert.assertTrue(userGroupRoles.isEmpty());
  226. }
  227. @Test
  228. public void testFetchByPrimaryKeysWithMultiplePrimaryKeysWhereSomePrimaryKeysExist()
  229. throws Exception {
  230. UserGroupRole newUserGroupRole = addUserGroupRole();
  231. long pk = RandomTestUtil.nextLong();
  232. Set<Serializable> primaryKeys = new HashSet<Serializable>();
  233. primaryKeys.add(newUserGroupRole.getPrimaryKey());
  234. primaryKeys.add(pk);
  235. Map<Serializable, UserGroupRole> userGroupRoles =
  236. _persistence.fetchByPrimaryKeys(primaryKeys);
  237. Assert.assertEquals(1, userGroupRoles.size());
  238. Assert.assertEquals(
  239. newUserGroupRole,
  240. userGroupRoles.get(newUserGroupRole.getPrimaryKey()));
  241. }
  242. @Test
  243. public void testFetchByPrimaryKeysWithNoPrimaryKeys() throws Exception {
  244. Set<Serializable> primaryKeys = new HashSet<Serializable>();
  245. Map<Serializable, UserGroupRole> userGroupRoles =
  246. _persistence.fetchByPrimaryKeys(primaryKeys);
  247. Assert.assertTrue(userGroupRoles.isEmpty());
  248. }
  249. @Test
  250. public void testFetchByPrimaryKeysWithOnePrimaryKey() throws Exception {
  251. UserGroupRole newUserGroupRole = addUserGroupRole();
  252. Set<Serializable> primaryKeys = new HashSet<Serializable>();
  253. primaryKeys.add(newUserGroupRole.getPrimaryKey());
  254. Map<Serializable, UserGroupRole> userGroupRoles =
  255. _persistence.fetchByPrimaryKeys(primaryKeys);
  256. Assert.assertEquals(1, userGroupRoles.size());
  257. Assert.assertEquals(
  258. newUserGroupRole,
  259. userGroupRoles.get(newUserGroupRole.getPrimaryKey()));
  260. }
  261. @Test
  262. public void testActionableDynamicQuery() throws Exception {
  263. final IntegerWrapper count = new IntegerWrapper();
  264. ActionableDynamicQuery actionableDynamicQuery =
  265. UserGroupRoleLocalServiceUtil.getActionableDynamicQuery();
  266. actionableDynamicQuery.setPerformActionMethod(
  267. new ActionableDynamicQuery.PerformActionMethod<UserGroupRole>() {
  268. @Override
  269. public void performAction(UserGroupRole userGroupRole) {
  270. Assert.assertNotNull(userGroupRole);
  271. count.increment();
  272. }
  273. });
  274. actionableDynamicQuery.performActions();
  275. Assert.assertEquals(count.getValue(), _persistence.countAll());
  276. }
  277. @Test
  278. public void testDynamicQueryByPrimaryKeyExisting() throws Exception {
  279. UserGroupRole newUserGroupRole = addUserGroupRole();
  280. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
  281. UserGroupRole.class, _dynamicQueryClassLoader);
  282. dynamicQuery.add(
  283. RestrictionsFactoryUtil.eq(
  284. "userGroupRoleId", newUserGroupRole.getUserGroupRoleId()));
  285. List<UserGroupRole> result = _persistence.findWithDynamicQuery(
  286. dynamicQuery);
  287. Assert.assertEquals(1, result.size());
  288. UserGroupRole existingUserGroupRole = result.get(0);
  289. Assert.assertEquals(existingUserGroupRole, newUserGroupRole);
  290. }
  291. @Test
  292. public void testDynamicQueryByPrimaryKeyMissing() throws Exception {
  293. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
  294. UserGroupRole.class, _dynamicQueryClassLoader);
  295. dynamicQuery.add(
  296. RestrictionsFactoryUtil.eq(
  297. "userGroupRoleId", RandomTestUtil.nextLong()));
  298. List<UserGroupRole> result = _persistence.findWithDynamicQuery(
  299. dynamicQuery);
  300. Assert.assertEquals(0, result.size());
  301. }
  302. @Test
  303. public void testDynamicQueryByProjectionExisting() throws Exception {
  304. UserGroupRole newUserGroupRole = addUserGroupRole();
  305. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
  306. UserGroupRole.class, _dynamicQueryClassLoader);
  307. dynamicQuery.setProjection(
  308. ProjectionFactoryUtil.property("userGroupRoleId"));
  309. Object newUserGroupRoleId = newUserGroupRole.getUserGroupRoleId();
  310. dynamicQuery.add(
  311. RestrictionsFactoryUtil.in(
  312. "userGroupRoleId", new Object[] {newUserGroupRoleId}));
  313. List<Object> result = _persistence.findWithDynamicQuery(dynamicQuery);
  314. Assert.assertEquals(1, result.size());
  315. Object existingUserGroupRoleId = result.get(0);
  316. Assert.assertEquals(existingUserGroupRoleId, newUserGroupRoleId);
  317. }
  318. @Test
  319. public void testDynamicQueryByProjectionMissing() throws Exception {
  320. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
  321. UserGroupRole.class, _dynamicQueryClassLoader);
  322. dynamicQuery.setProjection(
  323. ProjectionFactoryUtil.property("userGroupRoleId"));
  324. dynamicQuery.add(
  325. RestrictionsFactoryUtil.in(
  326. "userGroupRoleId", new Object[] {RandomTestUtil.nextLong()}));
  327. List<Object> result = _persistence.findWithDynamicQuery(dynamicQuery);
  328. Assert.assertEquals(0, result.size());
  329. }
  330. @Test
  331. public void testResetOriginalValues() throws Exception {
  332. UserGroupRole newUserGroupRole = addUserGroupRole();
  333. _persistence.clearCache();
  334. UserGroupRole existingUserGroupRole = _persistence.findByPrimaryKey(
  335. newUserGroupRole.getPrimaryKey());
  336. Assert.assertEquals(
  337. Long.valueOf(existingUserGroupRole.getUserId()),
  338. ReflectionTestUtil.<Long>invoke(
  339. existingUserGroupRole, "getOriginalUserId", new Class<?>[0]));
  340. Assert.assertEquals(
  341. Long.valueOf(existingUserGroupRole.getGroupId()),
  342. ReflectionTestUtil.<Long>invoke(
  343. existingUserGroupRole, "getOriginalGroupId", new Class<?>[0]));
  344. Assert.assertEquals(
  345. Long.valueOf(existingUserGroupRole.getRoleId()),
  346. ReflectionTestUtil.<Long>invoke(
  347. existingUserGroupRole, "getOriginalRoleId", new Class<?>[0]));
  348. }
  349. protected UserGroupRole addUserGroupRole() throws Exception {
  350. long pk = RandomTestUtil.nextLong();
  351. UserGroupRole userGroupRole = _persistence.create(pk);
  352. userGroupRole.setMvccVersion(RandomTestUtil.nextLong());
  353. userGroupRole.setCtCollectionId(RandomTestUtil.nextLong());
  354. userGroupRole.setCompanyId(RandomTestUtil.nextLong());
  355. userGroupRole.setUserId(RandomTestUtil.nextLong());
  356. userGroupRole.setGroupId(RandomTestUtil.nextLong());
  357. userGroupRole.setRoleId(RandomTestUtil.nextLong());
  358. _userGroupRoles.add(_persistence.update(userGroupRole));
  359. return userGroupRole;
  360. }
  361. private List<UserGroupRole> _userGroupRoles =
  362. new ArrayList<UserGroupRole>();
  363. private UserGroupRolePersistence _persistence;
  364. private ClassLoader _dynamicQueryClassLoader;
  365. }