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

/modules/apps/wiki/wiki-test/src/testIntegration/java/com/liferay/wiki/service/persistence/test/WikiPageResourcePersistenceTest.java

http://github.com/liferay/liferay-portal
Java | 546 lines | 381 code | 149 blank | 16 comment | 2 complexity | 9ec84c752e6137237a3d907b114ffe54 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.wiki.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.dao.orm.Session;
  23. import com.liferay.portal.kernel.test.ReflectionTestUtil;
  24. import com.liferay.portal.kernel.test.rule.AggregateTestRule;
  25. import com.liferay.portal.kernel.test.util.RandomTestUtil;
  26. import com.liferay.portal.kernel.transaction.Propagation;
  27. import com.liferay.portal.kernel.util.IntegerWrapper;
  28. import com.liferay.portal.kernel.util.OrderByComparator;
  29. import com.liferay.portal.kernel.util.OrderByComparatorFactoryUtil;
  30. import com.liferay.portal.test.rule.LiferayIntegrationTestRule;
  31. import com.liferay.portal.test.rule.PersistenceTestRule;
  32. import com.liferay.portal.test.rule.TransactionalTestRule;
  33. import com.liferay.wiki.exception.NoSuchPageResourceException;
  34. import com.liferay.wiki.model.WikiPageResource;
  35. import com.liferay.wiki.service.WikiPageResourceLocalServiceUtil;
  36. import com.liferay.wiki.service.persistence.WikiPageResourcePersistence;
  37. import com.liferay.wiki.service.persistence.WikiPageResourceUtil;
  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.Set;
  45. import org.junit.After;
  46. import org.junit.Assert;
  47. import org.junit.Before;
  48. import org.junit.ClassRule;
  49. import org.junit.Rule;
  50. import org.junit.Test;
  51. import org.junit.runner.RunWith;
  52. /**
  53. * @generated
  54. */
  55. @RunWith(Arquillian.class)
  56. public class WikiPageResourcePersistenceTest {
  57. @ClassRule
  58. @Rule
  59. public static final AggregateTestRule aggregateTestRule =
  60. new AggregateTestRule(
  61. new LiferayIntegrationTestRule(), PersistenceTestRule.INSTANCE,
  62. new TransactionalTestRule(
  63. Propagation.REQUIRED, "com.liferay.wiki.service"));
  64. @Before
  65. public void setUp() {
  66. _persistence = WikiPageResourceUtil.getPersistence();
  67. Class<?> clazz = _persistence.getClass();
  68. _dynamicQueryClassLoader = clazz.getClassLoader();
  69. }
  70. @After
  71. public void tearDown() throws Exception {
  72. Iterator<WikiPageResource> iterator = _wikiPageResources.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. WikiPageResource wikiPageResource = _persistence.create(pk);
  82. Assert.assertNotNull(wikiPageResource);
  83. Assert.assertEquals(wikiPageResource.getPrimaryKey(), pk);
  84. }
  85. @Test
  86. public void testRemove() throws Exception {
  87. WikiPageResource newWikiPageResource = addWikiPageResource();
  88. _persistence.remove(newWikiPageResource);
  89. WikiPageResource existingWikiPageResource =
  90. _persistence.fetchByPrimaryKey(newWikiPageResource.getPrimaryKey());
  91. Assert.assertNull(existingWikiPageResource);
  92. }
  93. @Test
  94. public void testUpdateNew() throws Exception {
  95. addWikiPageResource();
  96. }
  97. @Test
  98. public void testUpdateExisting() throws Exception {
  99. long pk = RandomTestUtil.nextLong();
  100. WikiPageResource newWikiPageResource = _persistence.create(pk);
  101. newWikiPageResource.setMvccVersion(RandomTestUtil.nextLong());
  102. newWikiPageResource.setUuid(RandomTestUtil.randomString());
  103. newWikiPageResource.setGroupId(RandomTestUtil.nextLong());
  104. newWikiPageResource.setCompanyId(RandomTestUtil.nextLong());
  105. newWikiPageResource.setNodeId(RandomTestUtil.nextLong());
  106. newWikiPageResource.setTitle(RandomTestUtil.randomString());
  107. _wikiPageResources.add(_persistence.update(newWikiPageResource));
  108. WikiPageResource existingWikiPageResource =
  109. _persistence.findByPrimaryKey(newWikiPageResource.getPrimaryKey());
  110. Assert.assertEquals(
  111. existingWikiPageResource.getMvccVersion(),
  112. newWikiPageResource.getMvccVersion());
  113. Assert.assertEquals(
  114. existingWikiPageResource.getUuid(), newWikiPageResource.getUuid());
  115. Assert.assertEquals(
  116. existingWikiPageResource.getResourcePrimKey(),
  117. newWikiPageResource.getResourcePrimKey());
  118. Assert.assertEquals(
  119. existingWikiPageResource.getGroupId(),
  120. newWikiPageResource.getGroupId());
  121. Assert.assertEquals(
  122. existingWikiPageResource.getCompanyId(),
  123. newWikiPageResource.getCompanyId());
  124. Assert.assertEquals(
  125. existingWikiPageResource.getNodeId(),
  126. newWikiPageResource.getNodeId());
  127. Assert.assertEquals(
  128. existingWikiPageResource.getTitle(),
  129. newWikiPageResource.getTitle());
  130. }
  131. @Test
  132. public void testCountByUuid() throws Exception {
  133. _persistence.countByUuid("");
  134. _persistence.countByUuid("null");
  135. _persistence.countByUuid((String)null);
  136. }
  137. @Test
  138. public void testCountByUUID_G() throws Exception {
  139. _persistence.countByUUID_G("", RandomTestUtil.nextLong());
  140. _persistence.countByUUID_G("null", 0L);
  141. _persistence.countByUUID_G((String)null, 0L);
  142. }
  143. @Test
  144. public void testCountByUuid_C() throws Exception {
  145. _persistence.countByUuid_C("", RandomTestUtil.nextLong());
  146. _persistence.countByUuid_C("null", 0L);
  147. _persistence.countByUuid_C((String)null, 0L);
  148. }
  149. @Test
  150. public void testCountByN_T() throws Exception {
  151. _persistence.countByN_T(RandomTestUtil.nextLong(), "");
  152. _persistence.countByN_T(0L, "null");
  153. _persistence.countByN_T(0L, (String)null);
  154. }
  155. @Test
  156. public void testFindByPrimaryKeyExisting() throws Exception {
  157. WikiPageResource newWikiPageResource = addWikiPageResource();
  158. WikiPageResource existingWikiPageResource =
  159. _persistence.findByPrimaryKey(newWikiPageResource.getPrimaryKey());
  160. Assert.assertEquals(existingWikiPageResource, newWikiPageResource);
  161. }
  162. @Test(expected = NoSuchPageResourceException.class)
  163. public void testFindByPrimaryKeyMissing() throws Exception {
  164. long pk = RandomTestUtil.nextLong();
  165. _persistence.findByPrimaryKey(pk);
  166. }
  167. @Test
  168. public void testFindAll() throws Exception {
  169. _persistence.findAll(
  170. QueryUtil.ALL_POS, QueryUtil.ALL_POS, getOrderByComparator());
  171. }
  172. protected OrderByComparator<WikiPageResource> getOrderByComparator() {
  173. return OrderByComparatorFactoryUtil.create(
  174. "WikiPageResource", "mvccVersion", true, "uuid", true,
  175. "resourcePrimKey", true, "groupId", true, "companyId", true,
  176. "nodeId", true, "title", true);
  177. }
  178. @Test
  179. public void testFetchByPrimaryKeyExisting() throws Exception {
  180. WikiPageResource newWikiPageResource = addWikiPageResource();
  181. WikiPageResource existingWikiPageResource =
  182. _persistence.fetchByPrimaryKey(newWikiPageResource.getPrimaryKey());
  183. Assert.assertEquals(existingWikiPageResource, newWikiPageResource);
  184. }
  185. @Test
  186. public void testFetchByPrimaryKeyMissing() throws Exception {
  187. long pk = RandomTestUtil.nextLong();
  188. WikiPageResource missingWikiPageResource =
  189. _persistence.fetchByPrimaryKey(pk);
  190. Assert.assertNull(missingWikiPageResource);
  191. }
  192. @Test
  193. public void testFetchByPrimaryKeysWithMultiplePrimaryKeysWhereAllPrimaryKeysExist()
  194. throws Exception {
  195. WikiPageResource newWikiPageResource1 = addWikiPageResource();
  196. WikiPageResource newWikiPageResource2 = addWikiPageResource();
  197. Set<Serializable> primaryKeys = new HashSet<Serializable>();
  198. primaryKeys.add(newWikiPageResource1.getPrimaryKey());
  199. primaryKeys.add(newWikiPageResource2.getPrimaryKey());
  200. Map<Serializable, WikiPageResource> wikiPageResources =
  201. _persistence.fetchByPrimaryKeys(primaryKeys);
  202. Assert.assertEquals(2, wikiPageResources.size());
  203. Assert.assertEquals(
  204. newWikiPageResource1,
  205. wikiPageResources.get(newWikiPageResource1.getPrimaryKey()));
  206. Assert.assertEquals(
  207. newWikiPageResource2,
  208. wikiPageResources.get(newWikiPageResource2.getPrimaryKey()));
  209. }
  210. @Test
  211. public void testFetchByPrimaryKeysWithMultiplePrimaryKeysWhereNoPrimaryKeysExist()
  212. throws Exception {
  213. long pk1 = RandomTestUtil.nextLong();
  214. long pk2 = RandomTestUtil.nextLong();
  215. Set<Serializable> primaryKeys = new HashSet<Serializable>();
  216. primaryKeys.add(pk1);
  217. primaryKeys.add(pk2);
  218. Map<Serializable, WikiPageResource> wikiPageResources =
  219. _persistence.fetchByPrimaryKeys(primaryKeys);
  220. Assert.assertTrue(wikiPageResources.isEmpty());
  221. }
  222. @Test
  223. public void testFetchByPrimaryKeysWithMultiplePrimaryKeysWhereSomePrimaryKeysExist()
  224. throws Exception {
  225. WikiPageResource newWikiPageResource = addWikiPageResource();
  226. long pk = RandomTestUtil.nextLong();
  227. Set<Serializable> primaryKeys = new HashSet<Serializable>();
  228. primaryKeys.add(newWikiPageResource.getPrimaryKey());
  229. primaryKeys.add(pk);
  230. Map<Serializable, WikiPageResource> wikiPageResources =
  231. _persistence.fetchByPrimaryKeys(primaryKeys);
  232. Assert.assertEquals(1, wikiPageResources.size());
  233. Assert.assertEquals(
  234. newWikiPageResource,
  235. wikiPageResources.get(newWikiPageResource.getPrimaryKey()));
  236. }
  237. @Test
  238. public void testFetchByPrimaryKeysWithNoPrimaryKeys() throws Exception {
  239. Set<Serializable> primaryKeys = new HashSet<Serializable>();
  240. Map<Serializable, WikiPageResource> wikiPageResources =
  241. _persistence.fetchByPrimaryKeys(primaryKeys);
  242. Assert.assertTrue(wikiPageResources.isEmpty());
  243. }
  244. @Test
  245. public void testFetchByPrimaryKeysWithOnePrimaryKey() throws Exception {
  246. WikiPageResource newWikiPageResource = addWikiPageResource();
  247. Set<Serializable> primaryKeys = new HashSet<Serializable>();
  248. primaryKeys.add(newWikiPageResource.getPrimaryKey());
  249. Map<Serializable, WikiPageResource> wikiPageResources =
  250. _persistence.fetchByPrimaryKeys(primaryKeys);
  251. Assert.assertEquals(1, wikiPageResources.size());
  252. Assert.assertEquals(
  253. newWikiPageResource,
  254. wikiPageResources.get(newWikiPageResource.getPrimaryKey()));
  255. }
  256. @Test
  257. public void testActionableDynamicQuery() throws Exception {
  258. final IntegerWrapper count = new IntegerWrapper();
  259. ActionableDynamicQuery actionableDynamicQuery =
  260. WikiPageResourceLocalServiceUtil.getActionableDynamicQuery();
  261. actionableDynamicQuery.setPerformActionMethod(
  262. new ActionableDynamicQuery.PerformActionMethod<WikiPageResource>() {
  263. @Override
  264. public void performAction(WikiPageResource wikiPageResource) {
  265. Assert.assertNotNull(wikiPageResource);
  266. count.increment();
  267. }
  268. });
  269. actionableDynamicQuery.performActions();
  270. Assert.assertEquals(count.getValue(), _persistence.countAll());
  271. }
  272. @Test
  273. public void testDynamicQueryByPrimaryKeyExisting() throws Exception {
  274. WikiPageResource newWikiPageResource = addWikiPageResource();
  275. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
  276. WikiPageResource.class, _dynamicQueryClassLoader);
  277. dynamicQuery.add(
  278. RestrictionsFactoryUtil.eq(
  279. "resourcePrimKey", newWikiPageResource.getResourcePrimKey()));
  280. List<WikiPageResource> result = _persistence.findWithDynamicQuery(
  281. dynamicQuery);
  282. Assert.assertEquals(1, result.size());
  283. WikiPageResource existingWikiPageResource = result.get(0);
  284. Assert.assertEquals(existingWikiPageResource, newWikiPageResource);
  285. }
  286. @Test
  287. public void testDynamicQueryByPrimaryKeyMissing() throws Exception {
  288. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
  289. WikiPageResource.class, _dynamicQueryClassLoader);
  290. dynamicQuery.add(
  291. RestrictionsFactoryUtil.eq(
  292. "resourcePrimKey", RandomTestUtil.nextLong()));
  293. List<WikiPageResource> result = _persistence.findWithDynamicQuery(
  294. dynamicQuery);
  295. Assert.assertEquals(0, result.size());
  296. }
  297. @Test
  298. public void testDynamicQueryByProjectionExisting() throws Exception {
  299. WikiPageResource newWikiPageResource = addWikiPageResource();
  300. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
  301. WikiPageResource.class, _dynamicQueryClassLoader);
  302. dynamicQuery.setProjection(
  303. ProjectionFactoryUtil.property("resourcePrimKey"));
  304. Object newResourcePrimKey = newWikiPageResource.getResourcePrimKey();
  305. dynamicQuery.add(
  306. RestrictionsFactoryUtil.in(
  307. "resourcePrimKey", new Object[] {newResourcePrimKey}));
  308. List<Object> result = _persistence.findWithDynamicQuery(dynamicQuery);
  309. Assert.assertEquals(1, result.size());
  310. Object existingResourcePrimKey = result.get(0);
  311. Assert.assertEquals(existingResourcePrimKey, newResourcePrimKey);
  312. }
  313. @Test
  314. public void testDynamicQueryByProjectionMissing() throws Exception {
  315. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
  316. WikiPageResource.class, _dynamicQueryClassLoader);
  317. dynamicQuery.setProjection(
  318. ProjectionFactoryUtil.property("resourcePrimKey"));
  319. dynamicQuery.add(
  320. RestrictionsFactoryUtil.in(
  321. "resourcePrimKey", new Object[] {RandomTestUtil.nextLong()}));
  322. List<Object> result = _persistence.findWithDynamicQuery(dynamicQuery);
  323. Assert.assertEquals(0, result.size());
  324. }
  325. @Test
  326. public void testResetOriginalValues() throws Exception {
  327. WikiPageResource newWikiPageResource = addWikiPageResource();
  328. _persistence.clearCache();
  329. _assertOriginalValues(
  330. _persistence.findByPrimaryKey(newWikiPageResource.getPrimaryKey()));
  331. }
  332. @Test
  333. public void testResetOriginalValuesWithDynamicQueryLoadFromDatabase()
  334. throws Exception {
  335. _testResetOriginalValuesWithDynamicQuery(true);
  336. }
  337. @Test
  338. public void testResetOriginalValuesWithDynamicQueryLoadFromSession()
  339. throws Exception {
  340. _testResetOriginalValuesWithDynamicQuery(false);
  341. }
  342. private void _testResetOriginalValuesWithDynamicQuery(boolean clearSession)
  343. throws Exception {
  344. WikiPageResource newWikiPageResource = addWikiPageResource();
  345. if (clearSession) {
  346. Session session = _persistence.openSession();
  347. session.flush();
  348. session.clear();
  349. }
  350. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
  351. WikiPageResource.class, _dynamicQueryClassLoader);
  352. dynamicQuery.add(
  353. RestrictionsFactoryUtil.eq(
  354. "resourcePrimKey", newWikiPageResource.getResourcePrimKey()));
  355. List<WikiPageResource> result = _persistence.findWithDynamicQuery(
  356. dynamicQuery);
  357. _assertOriginalValues(result.get(0));
  358. }
  359. private void _assertOriginalValues(WikiPageResource wikiPageResource) {
  360. Assert.assertEquals(
  361. wikiPageResource.getUuid(),
  362. ReflectionTestUtil.invoke(
  363. wikiPageResource, "getColumnOriginalValue",
  364. new Class<?>[] {String.class}, "uuid_"));
  365. Assert.assertEquals(
  366. Long.valueOf(wikiPageResource.getGroupId()),
  367. ReflectionTestUtil.<Long>invoke(
  368. wikiPageResource, "getColumnOriginalValue",
  369. new Class<?>[] {String.class}, "groupId"));
  370. Assert.assertEquals(
  371. Long.valueOf(wikiPageResource.getNodeId()),
  372. ReflectionTestUtil.<Long>invoke(
  373. wikiPageResource, "getColumnOriginalValue",
  374. new Class<?>[] {String.class}, "nodeId"));
  375. Assert.assertEquals(
  376. wikiPageResource.getTitle(),
  377. ReflectionTestUtil.invoke(
  378. wikiPageResource, "getColumnOriginalValue",
  379. new Class<?>[] {String.class}, "title"));
  380. }
  381. protected WikiPageResource addWikiPageResource() throws Exception {
  382. long pk = RandomTestUtil.nextLong();
  383. WikiPageResource wikiPageResource = _persistence.create(pk);
  384. wikiPageResource.setMvccVersion(RandomTestUtil.nextLong());
  385. wikiPageResource.setUuid(RandomTestUtil.randomString());
  386. wikiPageResource.setGroupId(RandomTestUtil.nextLong());
  387. wikiPageResource.setCompanyId(RandomTestUtil.nextLong());
  388. wikiPageResource.setNodeId(RandomTestUtil.nextLong());
  389. wikiPageResource.setTitle(RandomTestUtil.randomString());
  390. _wikiPageResources.add(_persistence.update(wikiPageResource));
  391. return wikiPageResource;
  392. }
  393. private List<WikiPageResource> _wikiPageResources =
  394. new ArrayList<WikiPageResource>();
  395. private WikiPageResourcePersistence _persistence;
  396. private ClassLoader _dynamicQueryClassLoader;
  397. }