PageRenderTime 43ms CodeModel.GetById 6ms RepoModel.GetById 0ms app.codeStats 1ms

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

http://github.com/liferay/liferay-portal
Java | 563 lines | 384 code | 163 blank | 16 comment | 1 complexity | 7729b51c6dae43562021c4c16ec896f2 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.NoSuchAddressException;
  23. import com.liferay.portal.kernel.model.Address;
  24. import com.liferay.portal.kernel.service.AddressLocalServiceUtil;
  25. import com.liferay.portal.kernel.service.persistence.AddressPersistence;
  26. import com.liferay.portal.kernel.service.persistence.AddressUtil;
  27. import com.liferay.portal.kernel.test.rule.AggregateTestRule;
  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.Time;
  34. import com.liferay.portal.test.rule.LiferayIntegrationTestRule;
  35. import com.liferay.portal.test.rule.PersistenceTestRule;
  36. import com.liferay.portal.test.rule.TransactionalTestRule;
  37. import java.io.Serializable;
  38. import java.util.ArrayList;
  39. import java.util.HashSet;
  40. import java.util.Iterator;
  41. import java.util.List;
  42. import java.util.Map;
  43. import java.util.Set;
  44. import org.junit.After;
  45. import org.junit.Assert;
  46. import org.junit.Before;
  47. import org.junit.ClassRule;
  48. import org.junit.Rule;
  49. import org.junit.Test;
  50. import org.junit.runner.RunWith;
  51. /**
  52. * @generated
  53. */
  54. @RunWith(Arquillian.class)
  55. public class AddressPersistenceTest {
  56. @ClassRule
  57. @Rule
  58. public static final AggregateTestRule aggregateTestRule =
  59. new AggregateTestRule(
  60. new LiferayIntegrationTestRule(), PersistenceTestRule.INSTANCE,
  61. new TransactionalTestRule(Propagation.REQUIRED));
  62. @Before
  63. public void setUp() {
  64. _persistence = AddressUtil.getPersistence();
  65. Class<?> clazz = _persistence.getClass();
  66. _dynamicQueryClassLoader = clazz.getClassLoader();
  67. }
  68. @After
  69. public void tearDown() throws Exception {
  70. Iterator<Address> iterator = _addresses.iterator();
  71. while (iterator.hasNext()) {
  72. _persistence.remove(iterator.next());
  73. iterator.remove();
  74. }
  75. }
  76. @Test
  77. public void testCreate() throws Exception {
  78. long pk = RandomTestUtil.nextLong();
  79. Address address = _persistence.create(pk);
  80. Assert.assertNotNull(address);
  81. Assert.assertEquals(address.getPrimaryKey(), pk);
  82. }
  83. @Test
  84. public void testRemove() throws Exception {
  85. Address newAddress = addAddress();
  86. _persistence.remove(newAddress);
  87. Address existingAddress = _persistence.fetchByPrimaryKey(
  88. newAddress.getPrimaryKey());
  89. Assert.assertNull(existingAddress);
  90. }
  91. @Test
  92. public void testUpdateNew() throws Exception {
  93. addAddress();
  94. }
  95. @Test
  96. public void testUpdateExisting() throws Exception {
  97. long pk = RandomTestUtil.nextLong();
  98. Address newAddress = _persistence.create(pk);
  99. newAddress.setMvccVersion(RandomTestUtil.nextLong());
  100. newAddress.setUuid(RandomTestUtil.randomString());
  101. newAddress.setCompanyId(RandomTestUtil.nextLong());
  102. newAddress.setUserId(RandomTestUtil.nextLong());
  103. newAddress.setUserName(RandomTestUtil.randomString());
  104. newAddress.setCreateDate(RandomTestUtil.nextDate());
  105. newAddress.setModifiedDate(RandomTestUtil.nextDate());
  106. newAddress.setClassNameId(RandomTestUtil.nextLong());
  107. newAddress.setClassPK(RandomTestUtil.nextLong());
  108. newAddress.setStreet1(RandomTestUtil.randomString());
  109. newAddress.setStreet2(RandomTestUtil.randomString());
  110. newAddress.setStreet3(RandomTestUtil.randomString());
  111. newAddress.setCity(RandomTestUtil.randomString());
  112. newAddress.setZip(RandomTestUtil.randomString());
  113. newAddress.setRegionId(RandomTestUtil.nextLong());
  114. newAddress.setCountryId(RandomTestUtil.nextLong());
  115. newAddress.setTypeId(RandomTestUtil.nextLong());
  116. newAddress.setMailing(RandomTestUtil.randomBoolean());
  117. newAddress.setPrimary(RandomTestUtil.randomBoolean());
  118. _addresses.add(_persistence.update(newAddress));
  119. Address existingAddress = _persistence.findByPrimaryKey(
  120. newAddress.getPrimaryKey());
  121. Assert.assertEquals(
  122. existingAddress.getMvccVersion(), newAddress.getMvccVersion());
  123. Assert.assertEquals(existingAddress.getUuid(), newAddress.getUuid());
  124. Assert.assertEquals(
  125. existingAddress.getAddressId(), newAddress.getAddressId());
  126. Assert.assertEquals(
  127. existingAddress.getCompanyId(), newAddress.getCompanyId());
  128. Assert.assertEquals(
  129. existingAddress.getUserId(), newAddress.getUserId());
  130. Assert.assertEquals(
  131. existingAddress.getUserName(), newAddress.getUserName());
  132. Assert.assertEquals(
  133. Time.getShortTimestamp(existingAddress.getCreateDate()),
  134. Time.getShortTimestamp(newAddress.getCreateDate()));
  135. Assert.assertEquals(
  136. Time.getShortTimestamp(existingAddress.getModifiedDate()),
  137. Time.getShortTimestamp(newAddress.getModifiedDate()));
  138. Assert.assertEquals(
  139. existingAddress.getClassNameId(), newAddress.getClassNameId());
  140. Assert.assertEquals(
  141. existingAddress.getClassPK(), newAddress.getClassPK());
  142. Assert.assertEquals(
  143. existingAddress.getStreet1(), newAddress.getStreet1());
  144. Assert.assertEquals(
  145. existingAddress.getStreet2(), newAddress.getStreet2());
  146. Assert.assertEquals(
  147. existingAddress.getStreet3(), newAddress.getStreet3());
  148. Assert.assertEquals(existingAddress.getCity(), newAddress.getCity());
  149. Assert.assertEquals(existingAddress.getZip(), newAddress.getZip());
  150. Assert.assertEquals(
  151. existingAddress.getRegionId(), newAddress.getRegionId());
  152. Assert.assertEquals(
  153. existingAddress.getCountryId(), newAddress.getCountryId());
  154. Assert.assertEquals(
  155. existingAddress.getTypeId(), newAddress.getTypeId());
  156. Assert.assertEquals(
  157. existingAddress.isMailing(), newAddress.isMailing());
  158. Assert.assertEquals(
  159. existingAddress.isPrimary(), newAddress.isPrimary());
  160. }
  161. @Test
  162. public void testCountByUuid() throws Exception {
  163. _persistence.countByUuid("");
  164. _persistence.countByUuid("null");
  165. _persistence.countByUuid((String)null);
  166. }
  167. @Test
  168. public void testCountByUuid_C() throws Exception {
  169. _persistence.countByUuid_C("", RandomTestUtil.nextLong());
  170. _persistence.countByUuid_C("null", 0L);
  171. _persistence.countByUuid_C((String)null, 0L);
  172. }
  173. @Test
  174. public void testCountByCompanyId() throws Exception {
  175. _persistence.countByCompanyId(RandomTestUtil.nextLong());
  176. _persistence.countByCompanyId(0L);
  177. }
  178. @Test
  179. public void testCountByUserId() throws Exception {
  180. _persistence.countByUserId(RandomTestUtil.nextLong());
  181. _persistence.countByUserId(0L);
  182. }
  183. @Test
  184. public void testCountByC_C() throws Exception {
  185. _persistence.countByC_C(
  186. RandomTestUtil.nextLong(), RandomTestUtil.nextLong());
  187. _persistence.countByC_C(0L, 0L);
  188. }
  189. @Test
  190. public void testCountByC_C_C() throws Exception {
  191. _persistence.countByC_C_C(
  192. RandomTestUtil.nextLong(), RandomTestUtil.nextLong(),
  193. RandomTestUtil.nextLong());
  194. _persistence.countByC_C_C(0L, 0L, 0L);
  195. }
  196. @Test
  197. public void testCountByC_C_C_M() throws Exception {
  198. _persistence.countByC_C_C_M(
  199. RandomTestUtil.nextLong(), RandomTestUtil.nextLong(),
  200. RandomTestUtil.nextLong(), RandomTestUtil.randomBoolean());
  201. _persistence.countByC_C_C_M(0L, 0L, 0L, RandomTestUtil.randomBoolean());
  202. }
  203. @Test
  204. public void testCountByC_C_C_P() throws Exception {
  205. _persistence.countByC_C_C_P(
  206. RandomTestUtil.nextLong(), RandomTestUtil.nextLong(),
  207. RandomTestUtil.nextLong(), RandomTestUtil.randomBoolean());
  208. _persistence.countByC_C_C_P(0L, 0L, 0L, RandomTestUtil.randomBoolean());
  209. }
  210. @Test
  211. public void testFindByPrimaryKeyExisting() throws Exception {
  212. Address newAddress = addAddress();
  213. Address existingAddress = _persistence.findByPrimaryKey(
  214. newAddress.getPrimaryKey());
  215. Assert.assertEquals(existingAddress, newAddress);
  216. }
  217. @Test(expected = NoSuchAddressException.class)
  218. public void testFindByPrimaryKeyMissing() throws Exception {
  219. long pk = RandomTestUtil.nextLong();
  220. _persistence.findByPrimaryKey(pk);
  221. }
  222. @Test
  223. public void testFindAll() throws Exception {
  224. _persistence.findAll(
  225. QueryUtil.ALL_POS, QueryUtil.ALL_POS, getOrderByComparator());
  226. }
  227. protected OrderByComparator<Address> getOrderByComparator() {
  228. return OrderByComparatorFactoryUtil.create(
  229. "Address", "mvccVersion", true, "uuid", true, "addressId", true,
  230. "companyId", true, "userId", true, "userName", true, "createDate",
  231. true, "modifiedDate", true, "classNameId", true, "classPK", true,
  232. "street1", true, "street2", true, "street3", true, "city", true,
  233. "zip", true, "regionId", true, "countryId", true, "typeId", true,
  234. "mailing", true, "primary", true);
  235. }
  236. @Test
  237. public void testFetchByPrimaryKeyExisting() throws Exception {
  238. Address newAddress = addAddress();
  239. Address existingAddress = _persistence.fetchByPrimaryKey(
  240. newAddress.getPrimaryKey());
  241. Assert.assertEquals(existingAddress, newAddress);
  242. }
  243. @Test
  244. public void testFetchByPrimaryKeyMissing() throws Exception {
  245. long pk = RandomTestUtil.nextLong();
  246. Address missingAddress = _persistence.fetchByPrimaryKey(pk);
  247. Assert.assertNull(missingAddress);
  248. }
  249. @Test
  250. public void testFetchByPrimaryKeysWithMultiplePrimaryKeysWhereAllPrimaryKeysExist()
  251. throws Exception {
  252. Address newAddress1 = addAddress();
  253. Address newAddress2 = addAddress();
  254. Set<Serializable> primaryKeys = new HashSet<Serializable>();
  255. primaryKeys.add(newAddress1.getPrimaryKey());
  256. primaryKeys.add(newAddress2.getPrimaryKey());
  257. Map<Serializable, Address> addresses = _persistence.fetchByPrimaryKeys(
  258. primaryKeys);
  259. Assert.assertEquals(2, addresses.size());
  260. Assert.assertEquals(
  261. newAddress1, addresses.get(newAddress1.getPrimaryKey()));
  262. Assert.assertEquals(
  263. newAddress2, addresses.get(newAddress2.getPrimaryKey()));
  264. }
  265. @Test
  266. public void testFetchByPrimaryKeysWithMultiplePrimaryKeysWhereNoPrimaryKeysExist()
  267. throws Exception {
  268. long pk1 = RandomTestUtil.nextLong();
  269. long pk2 = RandomTestUtil.nextLong();
  270. Set<Serializable> primaryKeys = new HashSet<Serializable>();
  271. primaryKeys.add(pk1);
  272. primaryKeys.add(pk2);
  273. Map<Serializable, Address> addresses = _persistence.fetchByPrimaryKeys(
  274. primaryKeys);
  275. Assert.assertTrue(addresses.isEmpty());
  276. }
  277. @Test
  278. public void testFetchByPrimaryKeysWithMultiplePrimaryKeysWhereSomePrimaryKeysExist()
  279. throws Exception {
  280. Address newAddress = addAddress();
  281. long pk = RandomTestUtil.nextLong();
  282. Set<Serializable> primaryKeys = new HashSet<Serializable>();
  283. primaryKeys.add(newAddress.getPrimaryKey());
  284. primaryKeys.add(pk);
  285. Map<Serializable, Address> addresses = _persistence.fetchByPrimaryKeys(
  286. primaryKeys);
  287. Assert.assertEquals(1, addresses.size());
  288. Assert.assertEquals(
  289. newAddress, addresses.get(newAddress.getPrimaryKey()));
  290. }
  291. @Test
  292. public void testFetchByPrimaryKeysWithNoPrimaryKeys() throws Exception {
  293. Set<Serializable> primaryKeys = new HashSet<Serializable>();
  294. Map<Serializable, Address> addresses = _persistence.fetchByPrimaryKeys(
  295. primaryKeys);
  296. Assert.assertTrue(addresses.isEmpty());
  297. }
  298. @Test
  299. public void testFetchByPrimaryKeysWithOnePrimaryKey() throws Exception {
  300. Address newAddress = addAddress();
  301. Set<Serializable> primaryKeys = new HashSet<Serializable>();
  302. primaryKeys.add(newAddress.getPrimaryKey());
  303. Map<Serializable, Address> addresses = _persistence.fetchByPrimaryKeys(
  304. primaryKeys);
  305. Assert.assertEquals(1, addresses.size());
  306. Assert.assertEquals(
  307. newAddress, addresses.get(newAddress.getPrimaryKey()));
  308. }
  309. @Test
  310. public void testActionableDynamicQuery() throws Exception {
  311. final IntegerWrapper count = new IntegerWrapper();
  312. ActionableDynamicQuery actionableDynamicQuery =
  313. AddressLocalServiceUtil.getActionableDynamicQuery();
  314. actionableDynamicQuery.setPerformActionMethod(
  315. new ActionableDynamicQuery.PerformActionMethod<Address>() {
  316. @Override
  317. public void performAction(Address address) {
  318. Assert.assertNotNull(address);
  319. count.increment();
  320. }
  321. });
  322. actionableDynamicQuery.performActions();
  323. Assert.assertEquals(count.getValue(), _persistence.countAll());
  324. }
  325. @Test
  326. public void testDynamicQueryByPrimaryKeyExisting() throws Exception {
  327. Address newAddress = addAddress();
  328. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
  329. Address.class, _dynamicQueryClassLoader);
  330. dynamicQuery.add(
  331. RestrictionsFactoryUtil.eq("addressId", newAddress.getAddressId()));
  332. List<Address> result = _persistence.findWithDynamicQuery(dynamicQuery);
  333. Assert.assertEquals(1, result.size());
  334. Address existingAddress = result.get(0);
  335. Assert.assertEquals(existingAddress, newAddress);
  336. }
  337. @Test
  338. public void testDynamicQueryByPrimaryKeyMissing() throws Exception {
  339. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
  340. Address.class, _dynamicQueryClassLoader);
  341. dynamicQuery.add(
  342. RestrictionsFactoryUtil.eq("addressId", RandomTestUtil.nextLong()));
  343. List<Address> result = _persistence.findWithDynamicQuery(dynamicQuery);
  344. Assert.assertEquals(0, result.size());
  345. }
  346. @Test
  347. public void testDynamicQueryByProjectionExisting() throws Exception {
  348. Address newAddress = addAddress();
  349. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
  350. Address.class, _dynamicQueryClassLoader);
  351. dynamicQuery.setProjection(ProjectionFactoryUtil.property("addressId"));
  352. Object newAddressId = newAddress.getAddressId();
  353. dynamicQuery.add(
  354. RestrictionsFactoryUtil.in(
  355. "addressId", new Object[] {newAddressId}));
  356. List<Object> result = _persistence.findWithDynamicQuery(dynamicQuery);
  357. Assert.assertEquals(1, result.size());
  358. Object existingAddressId = result.get(0);
  359. Assert.assertEquals(existingAddressId, newAddressId);
  360. }
  361. @Test
  362. public void testDynamicQueryByProjectionMissing() throws Exception {
  363. DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(
  364. Address.class, _dynamicQueryClassLoader);
  365. dynamicQuery.setProjection(ProjectionFactoryUtil.property("addressId"));
  366. dynamicQuery.add(
  367. RestrictionsFactoryUtil.in(
  368. "addressId", new Object[] {RandomTestUtil.nextLong()}));
  369. List<Object> result = _persistence.findWithDynamicQuery(dynamicQuery);
  370. Assert.assertEquals(0, result.size());
  371. }
  372. protected Address addAddress() throws Exception {
  373. long pk = RandomTestUtil.nextLong();
  374. Address address = _persistence.create(pk);
  375. address.setMvccVersion(RandomTestUtil.nextLong());
  376. address.setUuid(RandomTestUtil.randomString());
  377. address.setCompanyId(RandomTestUtil.nextLong());
  378. address.setUserId(RandomTestUtil.nextLong());
  379. address.setUserName(RandomTestUtil.randomString());
  380. address.setCreateDate(RandomTestUtil.nextDate());
  381. address.setModifiedDate(RandomTestUtil.nextDate());
  382. address.setClassNameId(RandomTestUtil.nextLong());
  383. address.setClassPK(RandomTestUtil.nextLong());
  384. address.setStreet1(RandomTestUtil.randomString());
  385. address.setStreet2(RandomTestUtil.randomString());
  386. address.setStreet3(RandomTestUtil.randomString());
  387. address.setCity(RandomTestUtil.randomString());
  388. address.setZip(RandomTestUtil.randomString());
  389. address.setRegionId(RandomTestUtil.nextLong());
  390. address.setCountryId(RandomTestUtil.nextLong());
  391. address.setTypeId(RandomTestUtil.nextLong());
  392. address.setMailing(RandomTestUtil.randomBoolean());
  393. address.setPrimary(RandomTestUtil.randomBoolean());
  394. _addresses.add(_persistence.update(address));
  395. return address;
  396. }
  397. private List<Address> _addresses = new ArrayList<Address>();
  398. private AddressPersistence _persistence;
  399. private ClassLoader _dynamicQueryClassLoader;
  400. }