PageRenderTime 69ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 1ms

/modules/test/persistence-test/src/testIntegration/java/com/liferay/portlet/ratings/service/persistence/test/RatingsEntryPersistenceTest.java

http://github.com/liferay/liferay-portal
Java | 522 lines | 364 code | 142 blank | 16 comment | 1 complexity | bf65aa4ff799954c66bc2d3457c9f445 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.portlet.ratings.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.test.AssertUtils;
  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.kernel.util.Time;
  31. import com.liferay.portal.test.rule.LiferayIntegrationTestRule;
  32. import com.liferay.portal.test.rule.PersistenceTestRule;
  33. import com.liferay.portal.test.rule.TransactionalTestRule;
  34. import com.liferay.ratings.kernel.exception.NoSuchEntryException;
  35. import com.liferay.ratings.kernel.model.RatingsEntry;
  36. import com.liferay.ratings.kernel.service.RatingsEntryLocalServiceUtil;
  37. import com.liferay.ratings.kernel.service.persistence.RatingsEntryPersistence;
  38. import com.liferay.ratings.kernel.service.persistence.RatingsEntryUtil;
  39. import java.io.Serializable;
  40. import java.util.ArrayList;
  41. import java.util.HashSet;
  42. import java.util.Iterator;
  43. import java.util.List;
  44. import java.util.Map;
  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 RatingsEntryPersistenceTest {
  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 = RatingsEntryUtil.getPersistence();
  67. Class<?> clazz = _persistence.getClass();
  68. _dynamicQueryClassLoader = clazz.getClassLoader();
  69. }
  70. @After
  71. public void tearDown() throws Exception {
  72. Iterator<RatingsEntry> iterator = _ratingsEntries.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. RatingsEntry ratingsEntry = _persistence.create(pk);
  82. Assert.assertNotNull(ratingsEntry);
  83. Assert.assertEquals(ratingsEntry.getPrimaryKey(), pk);
  84. }
  85. @Test
  86. public void testRemove() throws Exception {
  87. RatingsEntry newRatingsEntry = addRatingsEntry();
  88. _persistence.remove(newRatingsEntry);
  89. RatingsEntry existingRatingsEntry = _persistence.fetchByPrimaryKey(
  90. newRatingsEntry.getPrimaryKey());
  91. Assert.assertNull(existingRatingsEntry);
  92. }
  93. @Test
  94. public void testUpdateNew() throws Exception {
  95. addRatingsEntry();
  96. }
  97. @Test
  98. public void testUpdateExisting() throws Exception {
  99. long pk = RandomTestUtil.nextLong();
  100. RatingsEntry newRatingsEntry = _persistence.create(pk);
  101. newRatingsEntry.setUuid(RandomTestUtil.randomString());
  102. newRatingsEntry.setCompanyId(RandomTestUtil.nextLong());
  103. newRatingsEntry.setUserId(RandomTestUtil.nextLong());
  104. newRatingsEntry.setUserName(RandomTestUtil.randomString());
  105. newRatingsEntry.setCreateDate(RandomTestUtil.nextDate());
  106. newRatingsEntry.setModifiedDate(RandomTestUtil.nextDate());
  107. newRatingsEntry.setClassNameId(RandomTestUtil.nextLong());
  108. newRatingsEntry.setClassPK(RandomTestUtil.nextLong());
  109. newRatingsEntry.setScore(RandomTestUtil.nextDouble());
  110. _ratingsEntries.add(_persistence.update(newRatingsEntry));
  111. RatingsEntry existingRatingsEntry = _persistence.findByPrimaryKey(
  112. newRatingsEntry.getPrimaryKey());
  113. Assert.assertEquals(
  114. existingRatingsEntry.getUuid(), newRatingsEntry.getUuid());
  115. Assert.assertEquals(
  116. existingRatingsEntry.getEntryId(), newRatingsEntry.getEntryId());
  117. Assert.assertEquals(
  118. existingRatingsEntry.getCompanyId(),
  119. newRatingsEntry.getCompanyId());
  120. Assert.assertEquals(
  121. existingRatingsEntry.getUserId(), newRatingsEntry.getUserId());
  122. Assert.assertEquals(
  123. existingRatingsEntry.getUserName(), newRatingsEntry.getUserName());
  124. Assert.assertEquals(
  125. Time.getShortTimestamp(existingRatingsEntry.getCreateDate()),
  126. Time.getShortTimestamp(newRatingsEntry.getCreateDate()));
  127. Assert.assertEquals(
  128. Time.getShortTimestamp(existingRatingsEntry.getModifiedDate()),
  129. Time.getShortTimestamp(newRatingsEntry.getModifiedDate()));
  130. Assert.assertEquals(
  131. existingRatingsEntry.getClassNameId(),
  132. newRatingsEntry.getClassNameId());
  133. Assert.assertEquals(
  134. existingRatingsEntry.getClassPK(), newRatingsEntry.getClassPK());
  135. AssertUtils.assertEquals(
  136. existingRatingsEntry.getScore(), newRatingsEntry.getScore());
  137. }
  138. @Test
  139. public void testCountByUuid() throws Exception {
  140. _persistence.countByUuid("");
  141. _persistence.countByUuid("null");
  142. _persistence.countByUuid((String)null);
  143. }
  144. @Test
  145. public void testCountByUuid_C() throws Exception {
  146. _persistence.countByUuid_C("", RandomTestUtil.nextLong());
  147. _persistence.countByUuid_C("null", 0L);
  148. _persistence.countByUuid_C((String)null, 0L);
  149. }
  150. @Test
  151. public void testCountByC_C() throws Exception {
  152. _persistence.countByC_C(
  153. RandomTestUtil.nextLong(), RandomTestUtil.nextLong());
  154. _persistence.countByC_C(0L, 0L);
  155. }
  156. @Test
  157. public void testCountByU_C_C() throws Exception {
  158. _persistence.countByU_C_C(
  159. RandomTestUtil.nextLong(), RandomTestUtil.nextLong(),
  160. RandomTestUtil.nextLong());
  161. _persistence.countByU_C_C(0L, 0L, 0L);
  162. }
  163. @Test
  164. public void testCountByU_C_CArrayable() throws Exception {
  165. _persistence.countByU_C_C(
  166. RandomTestUtil.nextLong(), RandomTestUtil.nextLong(),
  167. new long[] {RandomTestUtil.nextLong(), 0L});
  168. }
  169. @Test
  170. public void testCountByC_C_S() throws Exception {
  171. _persistence.countByC_C_S(
  172. RandomTestUtil.nextLong(), RandomTestUtil.nextLong(),
  173. RandomTestUtil.nextDouble());
  174. _persistence.countByC_C_S(0L, 0L, 0D);
  175. }
  176. @Test
  177. public void testFindByPrimaryKeyExisting() throws Exception {
  178. RatingsEntry newRatingsEntry = addRatingsEntry();
  179. RatingsEntry existingRatingsEntry = _persistence.findByPrimaryKey(
  180. newRatingsEntry.getPrimaryKey());
  181. Assert.assertEquals(existingRatingsEntry, newRatingsEntry);
  182. }
  183. @Test(expected = NoSuchEntryException.class)
  184. public void testFindByPrimaryKeyMissing() throws Exception {
  185. long pk = RandomTestUtil.nextLong();
  186. _persistence.findByPrimaryKey(pk);
  187. }
  188. @Test
  189. public void testFindAll() throws Exception {
  190. _persistence.findAll(
  191. QueryUtil.ALL_POS, QueryUtil.ALL_POS, getOrderByComparator());
  192. }
  193. protected OrderByComparator<RatingsEntry> getOrderByComparator() {
  194. return OrderByComparatorFactoryUtil.create(
  195. "RatingsEntry", "uuid", true, "entryId", true, "companyId", true,
  196. "userId", true, "userName", true, "createDate", true,
  197. "modifiedDate", true, "classNameId", true, "classPK", true, "score",
  198. true);
  199. }
  200. @Test
  201. public void testFetchByPrimaryKeyExisting() throws Exception {
  202. RatingsEntry newRatingsEntry = addRatingsEntry();
  203. RatingsEntry existingRatingsEntry = _persistence.fetchByPrimaryKey(
  204. newRatingsEntry.getPrimaryKey());
  205. Assert.assertEquals(existingRatingsEntry, newRatingsEntry);
  206. }
  207. @Test
  208. public void testFetchByPrimaryKeyMissing() throws Exception {
  209. long pk = RandomTestUtil.nextLong();
  210. RatingsEntry missingRatingsEntry = _persistence.fetchByPrimaryKey(pk);
  211. Assert.assertNull(missingRatingsEntry);
  212. }
  213. @Test
  214. public void testFetchByPrimaryKeysWithMultiplePrimaryKeysWhereAllPrimaryKeysExist()
  215. throws Exception {
  216. RatingsEntry newRatingsEntry1 = addRatingsEntry();
  217. RatingsEntry newRatingsEntry2 = addRatingsEntry();
  218. Set<Serializable> primaryKeys = new HashSet<Serializable>();
  219. primaryKeys.add(newRatingsEntry1.getPrimaryKey());
  220. primaryKeys.add(newRatingsEntry2.getPrimaryKey());
  221. Map<Serializable, RatingsEntry> ratingsEntries =
  222. _persistence.fetchByPrimaryKeys(primaryKeys);
  223. Assert.assertEquals(2, ratingsEntries.size());
  224. Assert.assertEquals(
  225. newRatingsEntry1,
  226. ratingsEntries.get(newRatingsEntry1.getPrimaryKey()));
  227. Assert.assertEquals(
  228. newRatingsEntry2,
  229. ratingsEntries.get(newRatingsEntry2.getPrimaryKey()));
  230. }
  231. @Test
  232. public void testFetchByPrimaryKeysWithMultiplePrimaryKeysWhereNoPrimaryKeysExist()
  233. throws Exception {
  234. long pk1 = RandomTestUtil.nextLong();
  235. long pk2 = RandomTestUtil.nextLong();
  236. Set<Serializable> primaryKeys = new HashSet<Serializable>();
  237. primaryKeys.add(pk1);
  238. primaryKeys.add(pk2);
  239. Map<Serializable, RatingsEntry> ratingsEntries =
  240. _persistence.fetchByPrimaryKeys(primaryKeys);
  241. Assert.assertTrue(ratingsEntries.isEmpty());
  242. }
  243. @Test
  244. public void testFetchByPrimaryKeysWithMultiplePrimaryKeysWhereSomePrimaryKeysExist()
  245. throws Exception {
  246. RatingsEntry newRatingsEntry = addRatingsEntry();
  247. long pk = RandomTestUtil.nextLong();
  248. Set<Serializable> primaryKeys = new HashSet<Serializable>();
  249. primaryKeys.add(newRatingsEntry.getPrimaryKey());
  250. primaryKeys.add(pk);
  251. Map<Serializable, RatingsEntry> ratingsEntries =
  252. _persistence.fetchByPrimaryKeys(primaryKeys);
  253. Assert.assertEquals(1, ratingsEntries.size());
  254. Assert.assertEquals(
  255. newRatingsEntry,
  256. ratingsEntries.get(newRatingsEntry.getPrimaryKey()));
  257. }
  258. @Test
  259. public void testFetchByPrimaryKeysWithNoPrimaryKeys() throws Exception {
  260. Set<Serializable> primaryKeys = new HashSet<Serializable>();
  261. Map<Serializable, RatingsEntry> ratingsEntries =
  262. _persistence.fetchByPrimaryKeys(primaryKeys);
  263. Assert.assertTrue(ratingsEntries.isEmpty());
  264. }
  265. @Test
  266. public void testFetchByPrimaryKeysWithOnePrimaryKey() throws Exception {
  267. RatingsEntry newRatingsEntry = addRatingsEntry();
  268. Set<Serializable> primaryKeys = new HashSet<Serializable>();
  269. primaryKeys.add(newRatingsEntry.getPrimaryKey());
  270. Map<Serializable, RatingsEntry> ratingsEntries =
  271. _persistence.fetchByPrimaryKeys(primaryKeys);
  272. Assert.assertEquals(1, ratingsEntries.size());
  273. Assert.assertEquals(
  274. newRatingsEntry,
  275. ratingsEntries.get(newRatingsEntry.getPrimaryKey()));
  276. }
  277. @Test
  278. public void testActionableDynamicQuery() throws Exception {
  279. final IntegerWrapper count = new IntegerWrapper();
  280. ActionableDynamicQuery actionableDynamicQuery =
  281. RatingsEntryLocalServiceUtil.getActionableDynamicQuery();
  282. actionableDynamicQuery.setPerformActionMethod(
  283. new ActionableDynamicQuery.PerformActionMethod<RatingsEntry>() {
  284. @Override
  285. public void performAction(RatingsEntry ratingsEntry) {
  286. Assert.assertNotNull(ratingsEntry);
  287. count.increment();
  288. }
  289. });
  290. actionableDynamicQuery.performActions();
  291. Assert.assertEquals(count.getValue(), _persistence.countAll());
  292. }
  293. @Test
  294. public void testDynamicQueryByPrimaryKeyExisting() throws Exception {
  295. RatingsEntry newRatingsEntry = addRatingsEntry();
  296. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
  297. RatingsEntry.class, _dynamicQueryClassLoader);
  298. dynamicQuery.add(
  299. RestrictionsFactoryUtil.eq(
  300. "entryId", newRatingsEntry.getEntryId()));
  301. List<RatingsEntry> result = _persistence.findWithDynamicQuery(
  302. dynamicQuery);
  303. Assert.assertEquals(1, result.size());
  304. RatingsEntry existingRatingsEntry = result.get(0);
  305. Assert.assertEquals(existingRatingsEntry, newRatingsEntry);
  306. }
  307. @Test
  308. public void testDynamicQueryByPrimaryKeyMissing() throws Exception {
  309. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
  310. RatingsEntry.class, _dynamicQueryClassLoader);
  311. dynamicQuery.add(
  312. RestrictionsFactoryUtil.eq("entryId", RandomTestUtil.nextLong()));
  313. List<RatingsEntry> result = _persistence.findWithDynamicQuery(
  314. dynamicQuery);
  315. Assert.assertEquals(0, result.size());
  316. }
  317. @Test
  318. public void testDynamicQueryByProjectionExisting() throws Exception {
  319. RatingsEntry newRatingsEntry = addRatingsEntry();
  320. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
  321. RatingsEntry.class, _dynamicQueryClassLoader);
  322. dynamicQuery.setProjection(ProjectionFactoryUtil.property("entryId"));
  323. Object newEntryId = newRatingsEntry.getEntryId();
  324. dynamicQuery.add(
  325. RestrictionsFactoryUtil.in("entryId", new Object[] {newEntryId}));
  326. List<Object> result = _persistence.findWithDynamicQuery(dynamicQuery);
  327. Assert.assertEquals(1, result.size());
  328. Object existingEntryId = result.get(0);
  329. Assert.assertEquals(existingEntryId, newEntryId);
  330. }
  331. @Test
  332. public void testDynamicQueryByProjectionMissing() throws Exception {
  333. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
  334. RatingsEntry.class, _dynamicQueryClassLoader);
  335. dynamicQuery.setProjection(ProjectionFactoryUtil.property("entryId"));
  336. dynamicQuery.add(
  337. RestrictionsFactoryUtil.in(
  338. "entryId", new Object[] {RandomTestUtil.nextLong()}));
  339. List<Object> result = _persistence.findWithDynamicQuery(dynamicQuery);
  340. Assert.assertEquals(0, result.size());
  341. }
  342. @Test
  343. public void testResetOriginalValues() throws Exception {
  344. RatingsEntry newRatingsEntry = addRatingsEntry();
  345. _persistence.clearCache();
  346. RatingsEntry existingRatingsEntry = _persistence.findByPrimaryKey(
  347. newRatingsEntry.getPrimaryKey());
  348. Assert.assertEquals(
  349. Long.valueOf(existingRatingsEntry.getUserId()),
  350. ReflectionTestUtil.<Long>invoke(
  351. existingRatingsEntry, "getOriginalUserId", new Class<?>[0]));
  352. Assert.assertEquals(
  353. Long.valueOf(existingRatingsEntry.getClassNameId()),
  354. ReflectionTestUtil.<Long>invoke(
  355. existingRatingsEntry, "getOriginalClassNameId",
  356. new Class<?>[0]));
  357. Assert.assertEquals(
  358. Long.valueOf(existingRatingsEntry.getClassPK()),
  359. ReflectionTestUtil.<Long>invoke(
  360. existingRatingsEntry, "getOriginalClassPK", new Class<?>[0]));
  361. }
  362. protected RatingsEntry addRatingsEntry() throws Exception {
  363. long pk = RandomTestUtil.nextLong();
  364. RatingsEntry ratingsEntry = _persistence.create(pk);
  365. ratingsEntry.setUuid(RandomTestUtil.randomString());
  366. ratingsEntry.setCompanyId(RandomTestUtil.nextLong());
  367. ratingsEntry.setUserId(RandomTestUtil.nextLong());
  368. ratingsEntry.setUserName(RandomTestUtil.randomString());
  369. ratingsEntry.setCreateDate(RandomTestUtil.nextDate());
  370. ratingsEntry.setModifiedDate(RandomTestUtil.nextDate());
  371. ratingsEntry.setClassNameId(RandomTestUtil.nextLong());
  372. ratingsEntry.setClassPK(RandomTestUtil.nextLong());
  373. ratingsEntry.setScore(RandomTestUtil.nextDouble());
  374. _ratingsEntries.add(_persistence.update(ratingsEntry));
  375. return ratingsEntry;
  376. }
  377. private List<RatingsEntry> _ratingsEntries = new ArrayList<RatingsEntry>();
  378. private RatingsEntryPersistence _persistence;
  379. private ClassLoader _dynamicQueryClassLoader;
  380. }