PageRenderTime 28ms CodeModel.GetById 1ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/test/persistence-test/src/testIntegration/java/com/liferay/portlet/asset/service/persistence/test/AssetTagPersistenceTest.java

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