PageRenderTime 205ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/portal-impl/test/integration/com/liferay/portal/service/persistence/test/WebsitePersistenceTest.java

http://github.com/liferay/liferay-portal
Java | 494 lines | 336 code | 142 blank | 16 comment | 1 complexity | 605e94d61de75de8829c0d083d99a922 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.portal.kernel.dao.orm.ActionableDynamicQuery;
  16. import com.liferay.portal.kernel.dao.orm.DynamicQuery;
  17. import com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil;
  18. import com.liferay.portal.kernel.dao.orm.ProjectionFactoryUtil;
  19. import com.liferay.portal.kernel.dao.orm.QueryUtil;
  20. import com.liferay.portal.kernel.dao.orm.RestrictionsFactoryUtil;
  21. import com.liferay.portal.kernel.exception.NoSuchWebsiteException;
  22. import com.liferay.portal.kernel.model.Website;
  23. import com.liferay.portal.kernel.service.WebsiteLocalServiceUtil;
  24. import com.liferay.portal.kernel.service.persistence.WebsitePersistence;
  25. import com.liferay.portal.kernel.service.persistence.WebsiteUtil;
  26. import com.liferay.portal.kernel.test.rule.AggregateTestRule;
  27. import com.liferay.portal.kernel.test.rule.TransactionalTestRule;
  28. import com.liferay.portal.kernel.test.util.RandomTestUtil;
  29. import com.liferay.portal.kernel.transaction.Propagation;
  30. import com.liferay.portal.kernel.util.IntegerWrapper;
  31. import com.liferay.portal.kernel.util.OrderByComparator;
  32. import com.liferay.portal.kernel.util.OrderByComparatorFactoryUtil;
  33. import com.liferay.portal.kernel.util.StringPool;
  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 org.junit.After;
  38. import org.junit.Assert;
  39. import org.junit.Before;
  40. import org.junit.ClassRule;
  41. import org.junit.Rule;
  42. import org.junit.Test;
  43. import java.io.Serializable;
  44. import java.util.ArrayList;
  45. import java.util.HashSet;
  46. import java.util.Iterator;
  47. import java.util.List;
  48. import java.util.Map;
  49. import java.util.Set;
  50. /**
  51. * @generated
  52. */
  53. public class WebsitePersistenceTest {
  54. @ClassRule
  55. @Rule
  56. public static final AggregateTestRule aggregateTestRule = new AggregateTestRule(new LiferayIntegrationTestRule(),
  57. PersistenceTestRule.INSTANCE,
  58. new TransactionalTestRule(Propagation.REQUIRED));
  59. @Before
  60. public void setUp() {
  61. _persistence = WebsiteUtil.getPersistence();
  62. Class<?> clazz = _persistence.getClass();
  63. _dynamicQueryClassLoader = clazz.getClassLoader();
  64. }
  65. @After
  66. public void tearDown() throws Exception {
  67. Iterator<Website> iterator = _websites.iterator();
  68. while (iterator.hasNext()) {
  69. _persistence.remove(iterator.next());
  70. iterator.remove();
  71. }
  72. }
  73. @Test
  74. public void testCreate() throws Exception {
  75. long pk = RandomTestUtil.nextLong();
  76. Website website = _persistence.create(pk);
  77. Assert.assertNotNull(website);
  78. Assert.assertEquals(website.getPrimaryKey(), pk);
  79. }
  80. @Test
  81. public void testRemove() throws Exception {
  82. Website newWebsite = addWebsite();
  83. _persistence.remove(newWebsite);
  84. Website existingWebsite = _persistence.fetchByPrimaryKey(newWebsite.getPrimaryKey());
  85. Assert.assertNull(existingWebsite);
  86. }
  87. @Test
  88. public void testUpdateNew() throws Exception {
  89. addWebsite();
  90. }
  91. @Test
  92. public void testUpdateExisting() throws Exception {
  93. long pk = RandomTestUtil.nextLong();
  94. Website newWebsite = _persistence.create(pk);
  95. newWebsite.setMvccVersion(RandomTestUtil.nextLong());
  96. newWebsite.setUuid(RandomTestUtil.randomString());
  97. newWebsite.setCompanyId(RandomTestUtil.nextLong());
  98. newWebsite.setUserId(RandomTestUtil.nextLong());
  99. newWebsite.setUserName(RandomTestUtil.randomString());
  100. newWebsite.setCreateDate(RandomTestUtil.nextDate());
  101. newWebsite.setModifiedDate(RandomTestUtil.nextDate());
  102. newWebsite.setClassNameId(RandomTestUtil.nextLong());
  103. newWebsite.setClassPK(RandomTestUtil.nextLong());
  104. newWebsite.setUrl(RandomTestUtil.randomString());
  105. newWebsite.setTypeId(RandomTestUtil.nextLong());
  106. newWebsite.setPrimary(RandomTestUtil.randomBoolean());
  107. newWebsite.setLastPublishDate(RandomTestUtil.nextDate());
  108. _websites.add(_persistence.update(newWebsite));
  109. Website existingWebsite = _persistence.findByPrimaryKey(newWebsite.getPrimaryKey());
  110. Assert.assertEquals(existingWebsite.getMvccVersion(),
  111. newWebsite.getMvccVersion());
  112. Assert.assertEquals(existingWebsite.getUuid(), newWebsite.getUuid());
  113. Assert.assertEquals(existingWebsite.getWebsiteId(),
  114. newWebsite.getWebsiteId());
  115. Assert.assertEquals(existingWebsite.getCompanyId(),
  116. newWebsite.getCompanyId());
  117. Assert.assertEquals(existingWebsite.getUserId(), newWebsite.getUserId());
  118. Assert.assertEquals(existingWebsite.getUserName(),
  119. newWebsite.getUserName());
  120. Assert.assertEquals(Time.getShortTimestamp(
  121. existingWebsite.getCreateDate()),
  122. Time.getShortTimestamp(newWebsite.getCreateDate()));
  123. Assert.assertEquals(Time.getShortTimestamp(
  124. existingWebsite.getModifiedDate()),
  125. Time.getShortTimestamp(newWebsite.getModifiedDate()));
  126. Assert.assertEquals(existingWebsite.getClassNameId(),
  127. newWebsite.getClassNameId());
  128. Assert.assertEquals(existingWebsite.getClassPK(),
  129. newWebsite.getClassPK());
  130. Assert.assertEquals(existingWebsite.getUrl(), newWebsite.getUrl());
  131. Assert.assertEquals(existingWebsite.getTypeId(), newWebsite.getTypeId());
  132. Assert.assertEquals(existingWebsite.getPrimary(),
  133. newWebsite.getPrimary());
  134. Assert.assertEquals(Time.getShortTimestamp(
  135. existingWebsite.getLastPublishDate()),
  136. Time.getShortTimestamp(newWebsite.getLastPublishDate()));
  137. }
  138. @Test
  139. public void testCountByUuid() throws Exception {
  140. _persistence.countByUuid(StringPool.BLANK);
  141. _persistence.countByUuid(StringPool.NULL);
  142. _persistence.countByUuid((String)null);
  143. }
  144. @Test
  145. public void testCountByUuid_C() throws Exception {
  146. _persistence.countByUuid_C(StringPool.BLANK, RandomTestUtil.nextLong());
  147. _persistence.countByUuid_C(StringPool.NULL, 0L);
  148. _persistence.countByUuid_C((String)null, 0L);
  149. }
  150. @Test
  151. public void testCountByCompanyId() throws Exception {
  152. _persistence.countByCompanyId(RandomTestUtil.nextLong());
  153. _persistence.countByCompanyId(0L);
  154. }
  155. @Test
  156. public void testCountByUserId() throws Exception {
  157. _persistence.countByUserId(RandomTestUtil.nextLong());
  158. _persistence.countByUserId(0L);
  159. }
  160. @Test
  161. public void testCountByC_C() throws Exception {
  162. _persistence.countByC_C(RandomTestUtil.nextLong(),
  163. RandomTestUtil.nextLong());
  164. _persistence.countByC_C(0L, 0L);
  165. }
  166. @Test
  167. public void testCountByC_C_C() throws Exception {
  168. _persistence.countByC_C_C(RandomTestUtil.nextLong(),
  169. RandomTestUtil.nextLong(), RandomTestUtil.nextLong());
  170. _persistence.countByC_C_C(0L, 0L, 0L);
  171. }
  172. @Test
  173. public void testCountByC_C_C_P() throws Exception {
  174. _persistence.countByC_C_C_P(RandomTestUtil.nextLong(),
  175. RandomTestUtil.nextLong(), RandomTestUtil.nextLong(),
  176. RandomTestUtil.randomBoolean());
  177. _persistence.countByC_C_C_P(0L, 0L, 0L, RandomTestUtil.randomBoolean());
  178. }
  179. @Test
  180. public void testFindByPrimaryKeyExisting() throws Exception {
  181. Website newWebsite = addWebsite();
  182. Website existingWebsite = _persistence.findByPrimaryKey(newWebsite.getPrimaryKey());
  183. Assert.assertEquals(existingWebsite, newWebsite);
  184. }
  185. @Test(expected = NoSuchWebsiteException.class)
  186. public void testFindByPrimaryKeyMissing() throws Exception {
  187. long pk = RandomTestUtil.nextLong();
  188. _persistence.findByPrimaryKey(pk);
  189. }
  190. @Test
  191. public void testFindAll() throws Exception {
  192. _persistence.findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS,
  193. getOrderByComparator());
  194. }
  195. protected OrderByComparator<Website> getOrderByComparator() {
  196. return OrderByComparatorFactoryUtil.create("Website", "mvccVersion",
  197. true, "uuid", true, "websiteId", true, "companyId", true, "userId",
  198. true, "userName", true, "createDate", true, "modifiedDate", true,
  199. "classNameId", true, "classPK", true, "url", true, "typeId", true,
  200. "primary", true, "lastPublishDate", true);
  201. }
  202. @Test
  203. public void testFetchByPrimaryKeyExisting() throws Exception {
  204. Website newWebsite = addWebsite();
  205. Website existingWebsite = _persistence.fetchByPrimaryKey(newWebsite.getPrimaryKey());
  206. Assert.assertEquals(existingWebsite, newWebsite);
  207. }
  208. @Test
  209. public void testFetchByPrimaryKeyMissing() throws Exception {
  210. long pk = RandomTestUtil.nextLong();
  211. Website missingWebsite = _persistence.fetchByPrimaryKey(pk);
  212. Assert.assertNull(missingWebsite);
  213. }
  214. @Test
  215. public void testFetchByPrimaryKeysWithMultiplePrimaryKeysWhereAllPrimaryKeysExist()
  216. throws Exception {
  217. Website newWebsite1 = addWebsite();
  218. Website newWebsite2 = addWebsite();
  219. Set<Serializable> primaryKeys = new HashSet<Serializable>();
  220. primaryKeys.add(newWebsite1.getPrimaryKey());
  221. primaryKeys.add(newWebsite2.getPrimaryKey());
  222. Map<Serializable, Website> websites = _persistence.fetchByPrimaryKeys(primaryKeys);
  223. Assert.assertEquals(2, websites.size());
  224. Assert.assertEquals(newWebsite1,
  225. websites.get(newWebsite1.getPrimaryKey()));
  226. Assert.assertEquals(newWebsite2,
  227. websites.get(newWebsite2.getPrimaryKey()));
  228. }
  229. @Test
  230. public void testFetchByPrimaryKeysWithMultiplePrimaryKeysWhereNoPrimaryKeysExist()
  231. throws Exception {
  232. long pk1 = RandomTestUtil.nextLong();
  233. long pk2 = RandomTestUtil.nextLong();
  234. Set<Serializable> primaryKeys = new HashSet<Serializable>();
  235. primaryKeys.add(pk1);
  236. primaryKeys.add(pk2);
  237. Map<Serializable, Website> websites = _persistence.fetchByPrimaryKeys(primaryKeys);
  238. Assert.assertTrue(websites.isEmpty());
  239. }
  240. @Test
  241. public void testFetchByPrimaryKeysWithMultiplePrimaryKeysWhereSomePrimaryKeysExist()
  242. throws Exception {
  243. Website newWebsite = addWebsite();
  244. long pk = RandomTestUtil.nextLong();
  245. Set<Serializable> primaryKeys = new HashSet<Serializable>();
  246. primaryKeys.add(newWebsite.getPrimaryKey());
  247. primaryKeys.add(pk);
  248. Map<Serializable, Website> websites = _persistence.fetchByPrimaryKeys(primaryKeys);
  249. Assert.assertEquals(1, websites.size());
  250. Assert.assertEquals(newWebsite, websites.get(newWebsite.getPrimaryKey()));
  251. }
  252. @Test
  253. public void testFetchByPrimaryKeysWithNoPrimaryKeys()
  254. throws Exception {
  255. Set<Serializable> primaryKeys = new HashSet<Serializable>();
  256. Map<Serializable, Website> websites = _persistence.fetchByPrimaryKeys(primaryKeys);
  257. Assert.assertTrue(websites.isEmpty());
  258. }
  259. @Test
  260. public void testFetchByPrimaryKeysWithOnePrimaryKey()
  261. throws Exception {
  262. Website newWebsite = addWebsite();
  263. Set<Serializable> primaryKeys = new HashSet<Serializable>();
  264. primaryKeys.add(newWebsite.getPrimaryKey());
  265. Map<Serializable, Website> websites = _persistence.fetchByPrimaryKeys(primaryKeys);
  266. Assert.assertEquals(1, websites.size());
  267. Assert.assertEquals(newWebsite, websites.get(newWebsite.getPrimaryKey()));
  268. }
  269. @Test
  270. public void testActionableDynamicQuery() throws Exception {
  271. final IntegerWrapper count = new IntegerWrapper();
  272. ActionableDynamicQuery actionableDynamicQuery = WebsiteLocalServiceUtil.getActionableDynamicQuery();
  273. actionableDynamicQuery.setPerformActionMethod(new ActionableDynamicQuery.PerformActionMethod<Website>() {
  274. @Override
  275. public void performAction(Website website) {
  276. Assert.assertNotNull(website);
  277. count.increment();
  278. }
  279. });
  280. actionableDynamicQuery.performActions();
  281. Assert.assertEquals(count.getValue(), _persistence.countAll());
  282. }
  283. @Test
  284. public void testDynamicQueryByPrimaryKeyExisting()
  285. throws Exception {
  286. Website newWebsite = addWebsite();
  287. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(Website.class,
  288. _dynamicQueryClassLoader);
  289. dynamicQuery.add(RestrictionsFactoryUtil.eq("websiteId",
  290. newWebsite.getWebsiteId()));
  291. List<Website> result = _persistence.findWithDynamicQuery(dynamicQuery);
  292. Assert.assertEquals(1, result.size());
  293. Website existingWebsite = result.get(0);
  294. Assert.assertEquals(existingWebsite, newWebsite);
  295. }
  296. @Test
  297. public void testDynamicQueryByPrimaryKeyMissing() throws Exception {
  298. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(Website.class,
  299. _dynamicQueryClassLoader);
  300. dynamicQuery.add(RestrictionsFactoryUtil.eq("websiteId",
  301. RandomTestUtil.nextLong()));
  302. List<Website> result = _persistence.findWithDynamicQuery(dynamicQuery);
  303. Assert.assertEquals(0, result.size());
  304. }
  305. @Test
  306. public void testDynamicQueryByProjectionExisting()
  307. throws Exception {
  308. Website newWebsite = addWebsite();
  309. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(Website.class,
  310. _dynamicQueryClassLoader);
  311. dynamicQuery.setProjection(ProjectionFactoryUtil.property("websiteId"));
  312. Object newWebsiteId = newWebsite.getWebsiteId();
  313. dynamicQuery.add(RestrictionsFactoryUtil.in("websiteId",
  314. new Object[] { newWebsiteId }));
  315. List<Object> result = _persistence.findWithDynamicQuery(dynamicQuery);
  316. Assert.assertEquals(1, result.size());
  317. Object existingWebsiteId = result.get(0);
  318. Assert.assertEquals(existingWebsiteId, newWebsiteId);
  319. }
  320. @Test
  321. public void testDynamicQueryByProjectionMissing() throws Exception {
  322. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(Website.class,
  323. _dynamicQueryClassLoader);
  324. dynamicQuery.setProjection(ProjectionFactoryUtil.property("websiteId"));
  325. dynamicQuery.add(RestrictionsFactoryUtil.in("websiteId",
  326. new Object[] { RandomTestUtil.nextLong() }));
  327. List<Object> result = _persistence.findWithDynamicQuery(dynamicQuery);
  328. Assert.assertEquals(0, result.size());
  329. }
  330. protected Website addWebsite() throws Exception {
  331. long pk = RandomTestUtil.nextLong();
  332. Website website = _persistence.create(pk);
  333. website.setMvccVersion(RandomTestUtil.nextLong());
  334. website.setUuid(RandomTestUtil.randomString());
  335. website.setCompanyId(RandomTestUtil.nextLong());
  336. website.setUserId(RandomTestUtil.nextLong());
  337. website.setUserName(RandomTestUtil.randomString());
  338. website.setCreateDate(RandomTestUtil.nextDate());
  339. website.setModifiedDate(RandomTestUtil.nextDate());
  340. website.setClassNameId(RandomTestUtil.nextLong());
  341. website.setClassPK(RandomTestUtil.nextLong());
  342. website.setUrl(RandomTestUtil.randomString());
  343. website.setTypeId(RandomTestUtil.nextLong());
  344. website.setPrimary(RandomTestUtil.randomBoolean());
  345. website.setLastPublishDate(RandomTestUtil.nextDate());
  346. _websites.add(_persistence.update(website));
  347. return website;
  348. }
  349. private List<Website> _websites = new ArrayList<Website>();
  350. private WebsitePersistence _persistence;
  351. private ClassLoader _dynamicQueryClassLoader;
  352. }