PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

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

http://github.com/liferay/liferay-portal
Java | 567 lines | 395 code | 156 blank | 16 comment | 1 complexity | 9e7fb05299e62223693f0291f89db5fd 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.NoSuchRepositoryException;
  23. import com.liferay.portal.kernel.model.Repository;
  24. import com.liferay.portal.kernel.service.RepositoryLocalServiceUtil;
  25. import com.liferay.portal.kernel.service.persistence.RepositoryPersistence;
  26. import com.liferay.portal.kernel.service.persistence.RepositoryUtil;
  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 RepositoryPersistenceTest {
  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 = RepositoryUtil.getPersistence();
  67. Class<?> clazz = _persistence.getClass();
  68. _dynamicQueryClassLoader = clazz.getClassLoader();
  69. }
  70. @After
  71. public void tearDown() throws Exception {
  72. Iterator<Repository> iterator = _repositories.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. Repository repository = _persistence.create(pk);
  82. Assert.assertNotNull(repository);
  83. Assert.assertEquals(repository.getPrimaryKey(), pk);
  84. }
  85. @Test
  86. public void testRemove() throws Exception {
  87. Repository newRepository = addRepository();
  88. _persistence.remove(newRepository);
  89. Repository existingRepository = _persistence.fetchByPrimaryKey(
  90. newRepository.getPrimaryKey());
  91. Assert.assertNull(existingRepository);
  92. }
  93. @Test
  94. public void testUpdateNew() throws Exception {
  95. addRepository();
  96. }
  97. @Test
  98. public void testUpdateExisting() throws Exception {
  99. long pk = RandomTestUtil.nextLong();
  100. Repository newRepository = _persistence.create(pk);
  101. newRepository.setMvccVersion(RandomTestUtil.nextLong());
  102. newRepository.setUuid(RandomTestUtil.randomString());
  103. newRepository.setGroupId(RandomTestUtil.nextLong());
  104. newRepository.setCompanyId(RandomTestUtil.nextLong());
  105. newRepository.setUserId(RandomTestUtil.nextLong());
  106. newRepository.setUserName(RandomTestUtil.randomString());
  107. newRepository.setCreateDate(RandomTestUtil.nextDate());
  108. newRepository.setModifiedDate(RandomTestUtil.nextDate());
  109. newRepository.setClassNameId(RandomTestUtil.nextLong());
  110. newRepository.setName(RandomTestUtil.randomString());
  111. newRepository.setDescription(RandomTestUtil.randomString());
  112. newRepository.setPortletId(RandomTestUtil.randomString());
  113. newRepository.setTypeSettings(RandomTestUtil.randomString());
  114. newRepository.setDlFolderId(RandomTestUtil.nextLong());
  115. newRepository.setLastPublishDate(RandomTestUtil.nextDate());
  116. _repositories.add(_persistence.update(newRepository));
  117. Repository existingRepository = _persistence.findByPrimaryKey(
  118. newRepository.getPrimaryKey());
  119. Assert.assertEquals(
  120. existingRepository.getMvccVersion(),
  121. newRepository.getMvccVersion());
  122. Assert.assertEquals(
  123. existingRepository.getUuid(), newRepository.getUuid());
  124. Assert.assertEquals(
  125. existingRepository.getRepositoryId(),
  126. newRepository.getRepositoryId());
  127. Assert.assertEquals(
  128. existingRepository.getGroupId(), newRepository.getGroupId());
  129. Assert.assertEquals(
  130. existingRepository.getCompanyId(), newRepository.getCompanyId());
  131. Assert.assertEquals(
  132. existingRepository.getUserId(), newRepository.getUserId());
  133. Assert.assertEquals(
  134. existingRepository.getUserName(), newRepository.getUserName());
  135. Assert.assertEquals(
  136. Time.getShortTimestamp(existingRepository.getCreateDate()),
  137. Time.getShortTimestamp(newRepository.getCreateDate()));
  138. Assert.assertEquals(
  139. Time.getShortTimestamp(existingRepository.getModifiedDate()),
  140. Time.getShortTimestamp(newRepository.getModifiedDate()));
  141. Assert.assertEquals(
  142. existingRepository.getClassNameId(),
  143. newRepository.getClassNameId());
  144. Assert.assertEquals(
  145. existingRepository.getName(), newRepository.getName());
  146. Assert.assertEquals(
  147. existingRepository.getDescription(),
  148. newRepository.getDescription());
  149. Assert.assertEquals(
  150. existingRepository.getPortletId(), newRepository.getPortletId());
  151. Assert.assertEquals(
  152. existingRepository.getTypeSettings(),
  153. newRepository.getTypeSettings());
  154. Assert.assertEquals(
  155. existingRepository.getDlFolderId(), newRepository.getDlFolderId());
  156. Assert.assertEquals(
  157. Time.getShortTimestamp(existingRepository.getLastPublishDate()),
  158. Time.getShortTimestamp(newRepository.getLastPublishDate()));
  159. }
  160. @Test
  161. public void testCountByUuid() throws Exception {
  162. _persistence.countByUuid("");
  163. _persistence.countByUuid("null");
  164. _persistence.countByUuid((String)null);
  165. }
  166. @Test
  167. public void testCountByUUID_G() throws Exception {
  168. _persistence.countByUUID_G("", RandomTestUtil.nextLong());
  169. _persistence.countByUUID_G("null", 0L);
  170. _persistence.countByUUID_G((String)null, 0L);
  171. }
  172. @Test
  173. public void testCountByUuid_C() throws Exception {
  174. _persistence.countByUuid_C("", RandomTestUtil.nextLong());
  175. _persistence.countByUuid_C("null", 0L);
  176. _persistence.countByUuid_C((String)null, 0L);
  177. }
  178. @Test
  179. public void testCountByGroupId() throws Exception {
  180. _persistence.countByGroupId(RandomTestUtil.nextLong());
  181. _persistence.countByGroupId(0L);
  182. }
  183. @Test
  184. public void testCountByG_N_P() throws Exception {
  185. _persistence.countByG_N_P(RandomTestUtil.nextLong(), "", "");
  186. _persistence.countByG_N_P(0L, "null", "null");
  187. _persistence.countByG_N_P(0L, (String)null, (String)null);
  188. }
  189. @Test
  190. public void testFindByPrimaryKeyExisting() throws Exception {
  191. Repository newRepository = addRepository();
  192. Repository existingRepository = _persistence.findByPrimaryKey(
  193. newRepository.getPrimaryKey());
  194. Assert.assertEquals(existingRepository, newRepository);
  195. }
  196. @Test(expected = NoSuchRepositoryException.class)
  197. public void testFindByPrimaryKeyMissing() throws Exception {
  198. long pk = RandomTestUtil.nextLong();
  199. _persistence.findByPrimaryKey(pk);
  200. }
  201. @Test
  202. public void testFindAll() throws Exception {
  203. _persistence.findAll(
  204. QueryUtil.ALL_POS, QueryUtil.ALL_POS, getOrderByComparator());
  205. }
  206. protected OrderByComparator<Repository> getOrderByComparator() {
  207. return OrderByComparatorFactoryUtil.create(
  208. "Repository", "mvccVersion", true, "uuid", true, "repositoryId",
  209. true, "groupId", true, "companyId", true, "userId", true,
  210. "userName", true, "createDate", true, "modifiedDate", true,
  211. "classNameId", true, "name", true, "description", true, "portletId",
  212. true, "dlFolderId", true, "lastPublishDate", true);
  213. }
  214. @Test
  215. public void testFetchByPrimaryKeyExisting() throws Exception {
  216. Repository newRepository = addRepository();
  217. Repository existingRepository = _persistence.fetchByPrimaryKey(
  218. newRepository.getPrimaryKey());
  219. Assert.assertEquals(existingRepository, newRepository);
  220. }
  221. @Test
  222. public void testFetchByPrimaryKeyMissing() throws Exception {
  223. long pk = RandomTestUtil.nextLong();
  224. Repository missingRepository = _persistence.fetchByPrimaryKey(pk);
  225. Assert.assertNull(missingRepository);
  226. }
  227. @Test
  228. public void testFetchByPrimaryKeysWithMultiplePrimaryKeysWhereAllPrimaryKeysExist()
  229. throws Exception {
  230. Repository newRepository1 = addRepository();
  231. Repository newRepository2 = addRepository();
  232. Set<Serializable> primaryKeys = new HashSet<Serializable>();
  233. primaryKeys.add(newRepository1.getPrimaryKey());
  234. primaryKeys.add(newRepository2.getPrimaryKey());
  235. Map<Serializable, Repository> repositories =
  236. _persistence.fetchByPrimaryKeys(primaryKeys);
  237. Assert.assertEquals(2, repositories.size());
  238. Assert.assertEquals(
  239. newRepository1, repositories.get(newRepository1.getPrimaryKey()));
  240. Assert.assertEquals(
  241. newRepository2, repositories.get(newRepository2.getPrimaryKey()));
  242. }
  243. @Test
  244. public void testFetchByPrimaryKeysWithMultiplePrimaryKeysWhereNoPrimaryKeysExist()
  245. throws Exception {
  246. long pk1 = RandomTestUtil.nextLong();
  247. long pk2 = RandomTestUtil.nextLong();
  248. Set<Serializable> primaryKeys = new HashSet<Serializable>();
  249. primaryKeys.add(pk1);
  250. primaryKeys.add(pk2);
  251. Map<Serializable, Repository> repositories =
  252. _persistence.fetchByPrimaryKeys(primaryKeys);
  253. Assert.assertTrue(repositories.isEmpty());
  254. }
  255. @Test
  256. public void testFetchByPrimaryKeysWithMultiplePrimaryKeysWhereSomePrimaryKeysExist()
  257. throws Exception {
  258. Repository newRepository = addRepository();
  259. long pk = RandomTestUtil.nextLong();
  260. Set<Serializable> primaryKeys = new HashSet<Serializable>();
  261. primaryKeys.add(newRepository.getPrimaryKey());
  262. primaryKeys.add(pk);
  263. Map<Serializable, Repository> repositories =
  264. _persistence.fetchByPrimaryKeys(primaryKeys);
  265. Assert.assertEquals(1, repositories.size());
  266. Assert.assertEquals(
  267. newRepository, repositories.get(newRepository.getPrimaryKey()));
  268. }
  269. @Test
  270. public void testFetchByPrimaryKeysWithNoPrimaryKeys() throws Exception {
  271. Set<Serializable> primaryKeys = new HashSet<Serializable>();
  272. Map<Serializable, Repository> repositories =
  273. _persistence.fetchByPrimaryKeys(primaryKeys);
  274. Assert.assertTrue(repositories.isEmpty());
  275. }
  276. @Test
  277. public void testFetchByPrimaryKeysWithOnePrimaryKey() throws Exception {
  278. Repository newRepository = addRepository();
  279. Set<Serializable> primaryKeys = new HashSet<Serializable>();
  280. primaryKeys.add(newRepository.getPrimaryKey());
  281. Map<Serializable, Repository> repositories =
  282. _persistence.fetchByPrimaryKeys(primaryKeys);
  283. Assert.assertEquals(1, repositories.size());
  284. Assert.assertEquals(
  285. newRepository, repositories.get(newRepository.getPrimaryKey()));
  286. }
  287. @Test
  288. public void testActionableDynamicQuery() throws Exception {
  289. final IntegerWrapper count = new IntegerWrapper();
  290. ActionableDynamicQuery actionableDynamicQuery =
  291. RepositoryLocalServiceUtil.getActionableDynamicQuery();
  292. actionableDynamicQuery.setPerformActionMethod(
  293. new ActionableDynamicQuery.PerformActionMethod<Repository>() {
  294. @Override
  295. public void performAction(Repository repository) {
  296. Assert.assertNotNull(repository);
  297. count.increment();
  298. }
  299. });
  300. actionableDynamicQuery.performActions();
  301. Assert.assertEquals(count.getValue(), _persistence.countAll());
  302. }
  303. @Test
  304. public void testDynamicQueryByPrimaryKeyExisting() throws Exception {
  305. Repository newRepository = addRepository();
  306. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
  307. Repository.class, _dynamicQueryClassLoader);
  308. dynamicQuery.add(
  309. RestrictionsFactoryUtil.eq(
  310. "repositoryId", newRepository.getRepositoryId()));
  311. List<Repository> result = _persistence.findWithDynamicQuery(
  312. dynamicQuery);
  313. Assert.assertEquals(1, result.size());
  314. Repository existingRepository = result.get(0);
  315. Assert.assertEquals(existingRepository, newRepository);
  316. }
  317. @Test
  318. public void testDynamicQueryByPrimaryKeyMissing() throws Exception {
  319. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
  320. Repository.class, _dynamicQueryClassLoader);
  321. dynamicQuery.add(
  322. RestrictionsFactoryUtil.eq(
  323. "repositoryId", RandomTestUtil.nextLong()));
  324. List<Repository> result = _persistence.findWithDynamicQuery(
  325. dynamicQuery);
  326. Assert.assertEquals(0, result.size());
  327. }
  328. @Test
  329. public void testDynamicQueryByProjectionExisting() throws Exception {
  330. Repository newRepository = addRepository();
  331. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
  332. Repository.class, _dynamicQueryClassLoader);
  333. dynamicQuery.setProjection(
  334. ProjectionFactoryUtil.property("repositoryId"));
  335. Object newRepositoryId = newRepository.getRepositoryId();
  336. dynamicQuery.add(
  337. RestrictionsFactoryUtil.in(
  338. "repositoryId", new Object[] {newRepositoryId}));
  339. List<Object> result = _persistence.findWithDynamicQuery(dynamicQuery);
  340. Assert.assertEquals(1, result.size());
  341. Object existingRepositoryId = result.get(0);
  342. Assert.assertEquals(existingRepositoryId, newRepositoryId);
  343. }
  344. @Test
  345. public void testDynamicQueryByProjectionMissing() throws Exception {
  346. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
  347. Repository.class, _dynamicQueryClassLoader);
  348. dynamicQuery.setProjection(
  349. ProjectionFactoryUtil.property("repositoryId"));
  350. dynamicQuery.add(
  351. RestrictionsFactoryUtil.in(
  352. "repositoryId", new Object[] {RandomTestUtil.nextLong()}));
  353. List<Object> result = _persistence.findWithDynamicQuery(dynamicQuery);
  354. Assert.assertEquals(0, result.size());
  355. }
  356. @Test
  357. public void testResetOriginalValues() throws Exception {
  358. Repository newRepository = addRepository();
  359. _persistence.clearCache();
  360. Repository existingRepository = _persistence.findByPrimaryKey(
  361. newRepository.getPrimaryKey());
  362. Assert.assertTrue(
  363. Objects.equals(
  364. existingRepository.getUuid(),
  365. ReflectionTestUtil.invoke(
  366. existingRepository, "getOriginalUuid", new Class<?>[0])));
  367. Assert.assertEquals(
  368. Long.valueOf(existingRepository.getGroupId()),
  369. ReflectionTestUtil.<Long>invoke(
  370. existingRepository, "getOriginalGroupId", new Class<?>[0]));
  371. Assert.assertEquals(
  372. Long.valueOf(existingRepository.getGroupId()),
  373. ReflectionTestUtil.<Long>invoke(
  374. existingRepository, "getOriginalGroupId", new Class<?>[0]));
  375. Assert.assertTrue(
  376. Objects.equals(
  377. existingRepository.getName(),
  378. ReflectionTestUtil.invoke(
  379. existingRepository, "getOriginalName", new Class<?>[0])));
  380. Assert.assertTrue(
  381. Objects.equals(
  382. existingRepository.getPortletId(),
  383. ReflectionTestUtil.invoke(
  384. existingRepository, "getOriginalPortletId",
  385. new Class<?>[0])));
  386. }
  387. protected Repository addRepository() throws Exception {
  388. long pk = RandomTestUtil.nextLong();
  389. Repository repository = _persistence.create(pk);
  390. repository.setMvccVersion(RandomTestUtil.nextLong());
  391. repository.setUuid(RandomTestUtil.randomString());
  392. repository.setGroupId(RandomTestUtil.nextLong());
  393. repository.setCompanyId(RandomTestUtil.nextLong());
  394. repository.setUserId(RandomTestUtil.nextLong());
  395. repository.setUserName(RandomTestUtil.randomString());
  396. repository.setCreateDate(RandomTestUtil.nextDate());
  397. repository.setModifiedDate(RandomTestUtil.nextDate());
  398. repository.setClassNameId(RandomTestUtil.nextLong());
  399. repository.setName(RandomTestUtil.randomString());
  400. repository.setDescription(RandomTestUtil.randomString());
  401. repository.setPortletId(RandomTestUtil.randomString());
  402. repository.setTypeSettings(RandomTestUtil.randomString());
  403. repository.setDlFolderId(RandomTestUtil.nextLong());
  404. repository.setLastPublishDate(RandomTestUtil.nextDate());
  405. _repositories.add(_persistence.update(repository));
  406. return repository;
  407. }
  408. private List<Repository> _repositories = new ArrayList<Repository>();
  409. private RepositoryPersistence _persistence;
  410. private ClassLoader _dynamicQueryClassLoader;
  411. }