Java tutorial
/** * Copyright (c) 2000-present Liferay, Inc. All rights reserved. * * This library is free software; you can redistribute it and/or modify it under * the terms of the GNU Lesser General Public License as published by the Free * Software Foundation; either version 2.1 of the License, or (at your option) * any later version. * * This library is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more * details. */ package com.bemis.portal.shipment.service.persistence.test; import com.bemis.portal.shipment.exception.NoSuchShipmentException; import com.bemis.portal.shipment.model.Shipment; import com.bemis.portal.shipment.service.ShipmentLocalServiceUtil; import com.bemis.portal.shipment.service.persistence.ShipmentPersistence; import com.bemis.portal.shipment.service.persistence.ShipmentUtil; import com.liferay.arquillian.extension.junit.bridge.junit.Arquillian; import com.liferay.portal.kernel.dao.orm.ActionableDynamicQuery; import com.liferay.portal.kernel.dao.orm.DynamicQuery; import com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil; import com.liferay.portal.kernel.dao.orm.ProjectionFactoryUtil; import com.liferay.portal.kernel.dao.orm.QueryUtil; import com.liferay.portal.kernel.dao.orm.RestrictionsFactoryUtil; import com.liferay.portal.kernel.test.ReflectionTestUtil; import com.liferay.portal.kernel.test.rule.AggregateTestRule; import com.liferay.portal.kernel.test.util.RandomTestUtil; import com.liferay.portal.kernel.transaction.Propagation; import com.liferay.portal.kernel.util.IntegerWrapper; import com.liferay.portal.kernel.util.OrderByComparator; import com.liferay.portal.kernel.util.OrderByComparatorFactoryUtil; import com.liferay.portal.kernel.util.StringPool; import com.liferay.portal.kernel.util.Time; import com.liferay.portal.test.rule.LiferayIntegrationTestRule; import com.liferay.portal.test.rule.PersistenceTestRule; import com.liferay.portal.test.rule.TransactionalTestRule; import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.ClassRule; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; import java.io.Serializable; import java.util.ArrayList; import java.util.HashSet; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Objects; import java.util.Set; /** * @generated */ @RunWith(Arquillian.class) public class ShipmentPersistenceTest { @ClassRule @Rule public static final AggregateTestRule aggregateTestRule = new AggregateTestRule( new LiferayIntegrationTestRule(), PersistenceTestRule.INSTANCE, new TransactionalTestRule(Propagation.REQUIRED)); @Before public void setUp() { _persistence = ShipmentUtil.getPersistence(); Class<?> clazz = _persistence.getClass(); _dynamicQueryClassLoader = clazz.getClassLoader(); } @After public void tearDown() throws Exception { Iterator<Shipment> iterator = _shipments.iterator(); while (iterator.hasNext()) { _persistence.remove(iterator.next()); iterator.remove(); } } @Test public void testCreate() throws Exception { long pk = RandomTestUtil.nextLong(); Shipment shipment = _persistence.create(pk); Assert.assertNotNull(shipment); Assert.assertEquals(shipment.getPrimaryKey(), pk); } @Test public void testRemove() throws Exception { Shipment newShipment = addShipment(); _persistence.remove(newShipment); Shipment existingShipment = _persistence.fetchByPrimaryKey(newShipment.getPrimaryKey()); Assert.assertNull(existingShipment); } @Test public void testUpdateNew() throws Exception { addShipment(); } @Test public void testUpdateExisting() throws Exception { long pk = RandomTestUtil.nextLong(); Shipment newShipment = _persistence.create(pk); newShipment.setGroupId(RandomTestUtil.nextLong()); newShipment.setCompanyId(RandomTestUtil.nextLong()); newShipment.setUserId(RandomTestUtil.nextLong()); newShipment.setUserName(RandomTestUtil.randomString()); newShipment.setCreateDate(RandomTestUtil.nextDate()); newShipment.setModifiedDate(RandomTestUtil.nextDate()); newShipment.setOrderId(RandomTestUtil.nextLong()); newShipment.setReleaseId(RandomTestUtil.nextLong()); newShipment.setShipmentDate(RandomTestUtil.nextDate()); newShipment.setShipmentQty(RandomTestUtil.nextInt()); newShipment.setShipmentUom(RandomTestUtil.randomString()); newShipment.setPalletQty(RandomTestUtil.nextInt()); newShipment.setInvoiceNo(RandomTestUtil.nextLong()); newShipment.setBolNo(RandomTestUtil.nextLong()); newShipment.setCarrierName(RandomTestUtil.randomString()); newShipment.setProNo(RandomTestUtil.randomString()); newShipment.setShipToId(RandomTestUtil.randomString()); newShipment.setDeliveryNbr(RandomTestUtil.randomString()); _shipments.add(_persistence.update(newShipment)); Shipment existingShipment = _persistence.findByPrimaryKey(newShipment.getPrimaryKey()); Assert.assertEquals(existingShipment.getShipmentId(), newShipment.getShipmentId()); Assert.assertEquals(existingShipment.getGroupId(), newShipment.getGroupId()); Assert.assertEquals(existingShipment.getCompanyId(), newShipment.getCompanyId()); Assert.assertEquals(existingShipment.getUserId(), newShipment.getUserId()); Assert.assertEquals(existingShipment.getUserName(), newShipment.getUserName()); Assert.assertEquals(Time.getShortTimestamp(existingShipment.getCreateDate()), Time.getShortTimestamp(newShipment.getCreateDate())); Assert.assertEquals(Time.getShortTimestamp(existingShipment.getModifiedDate()), Time.getShortTimestamp(newShipment.getModifiedDate())); Assert.assertEquals(existingShipment.getOrderId(), newShipment.getOrderId()); Assert.assertEquals(existingShipment.getReleaseId(), newShipment.getReleaseId()); Assert.assertEquals(Time.getShortTimestamp(existingShipment.getShipmentDate()), Time.getShortTimestamp(newShipment.getShipmentDate())); Assert.assertEquals(existingShipment.getShipmentQty(), newShipment.getShipmentQty()); Assert.assertEquals(existingShipment.getShipmentUom(), newShipment.getShipmentUom()); Assert.assertEquals(existingShipment.getPalletQty(), newShipment.getPalletQty()); Assert.assertEquals(existingShipment.getInvoiceNo(), newShipment.getInvoiceNo()); Assert.assertEquals(existingShipment.getBolNo(), newShipment.getBolNo()); Assert.assertEquals(existingShipment.getCarrierName(), newShipment.getCarrierName()); Assert.assertEquals(existingShipment.getProNo(), newShipment.getProNo()); Assert.assertEquals(existingShipment.getShipToId(), newShipment.getShipToId()); Assert.assertEquals(existingShipment.getDeliveryNbr(), newShipment.getDeliveryNbr()); } @Test public void testCountByShipToId() throws Exception { _persistence.countByShipToId(StringPool.BLANK); _persistence.countByShipToId(StringPool.NULL); _persistence.countByShipToId((String) null); } @Test public void testCountByOrderId() throws Exception { _persistence.countByOrderId(RandomTestUtil.nextLong()); _persistence.countByOrderId(0L); } @Test public void testCountByOrderIdArrayable() throws Exception { _persistence.countByOrderId(new long[] { RandomTestUtil.nextLong(), 0L }); } @Test public void testCountByReleaseId() throws Exception { _persistence.countByReleaseId(RandomTestUtil.nextLong()); _persistence.countByReleaseId(0L); } @Test public void testCountByDeliveryNbr() throws Exception { _persistence.countByDeliveryNbr(StringPool.BLANK); _persistence.countByDeliveryNbr(StringPool.NULL); _persistence.countByDeliveryNbr((String) null); } @Test public void testFindByPrimaryKeyExisting() throws Exception { Shipment newShipment = addShipment(); Shipment existingShipment = _persistence.findByPrimaryKey(newShipment.getPrimaryKey()); Assert.assertEquals(existingShipment, newShipment); } @Test(expected = NoSuchShipmentException.class) public void testFindByPrimaryKeyMissing() throws Exception { long pk = RandomTestUtil.nextLong(); _persistence.findByPrimaryKey(pk); } @Test public void testFindAll() throws Exception { _persistence.findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, getOrderByComparator()); } protected OrderByComparator<Shipment> getOrderByComparator() { return OrderByComparatorFactoryUtil.create("Bemis_Shipment", "shipmentId", true, "groupId", true, "companyId", true, "userId", true, "userName", true, "createDate", true, "modifiedDate", true, "orderId", true, "releaseId", true, "shipmentDate", true, "shipmentQty", true, "shipmentUom", true, "palletQty", true, "invoiceNo", true, "bolNo", true, "carrierName", true, "proNo", true, "shipToId", true, "deliveryNbr", true); } @Test public void testFetchByPrimaryKeyExisting() throws Exception { Shipment newShipment = addShipment(); Shipment existingShipment = _persistence.fetchByPrimaryKey(newShipment.getPrimaryKey()); Assert.assertEquals(existingShipment, newShipment); } @Test public void testFetchByPrimaryKeyMissing() throws Exception { long pk = RandomTestUtil.nextLong(); Shipment missingShipment = _persistence.fetchByPrimaryKey(pk); Assert.assertNull(missingShipment); } @Test public void testFetchByPrimaryKeysWithMultiplePrimaryKeysWhereAllPrimaryKeysExist() throws Exception { Shipment newShipment1 = addShipment(); Shipment newShipment2 = addShipment(); Set<Serializable> primaryKeys = new HashSet<Serializable>(); primaryKeys.add(newShipment1.getPrimaryKey()); primaryKeys.add(newShipment2.getPrimaryKey()); Map<Serializable, Shipment> shipments = _persistence.fetchByPrimaryKeys(primaryKeys); Assert.assertEquals(2, shipments.size()); Assert.assertEquals(newShipment1, shipments.get(newShipment1.getPrimaryKey())); Assert.assertEquals(newShipment2, shipments.get(newShipment2.getPrimaryKey())); } @Test public void testFetchByPrimaryKeysWithMultiplePrimaryKeysWhereNoPrimaryKeysExist() throws Exception { long pk1 = RandomTestUtil.nextLong(); long pk2 = RandomTestUtil.nextLong(); Set<Serializable> primaryKeys = new HashSet<Serializable>(); primaryKeys.add(pk1); primaryKeys.add(pk2); Map<Serializable, Shipment> shipments = _persistence.fetchByPrimaryKeys(primaryKeys); Assert.assertTrue(shipments.isEmpty()); } @Test public void testFetchByPrimaryKeysWithMultiplePrimaryKeysWhereSomePrimaryKeysExist() throws Exception { Shipment newShipment = addShipment(); long pk = RandomTestUtil.nextLong(); Set<Serializable> primaryKeys = new HashSet<Serializable>(); primaryKeys.add(newShipment.getPrimaryKey()); primaryKeys.add(pk); Map<Serializable, Shipment> shipments = _persistence.fetchByPrimaryKeys(primaryKeys); Assert.assertEquals(1, shipments.size()); Assert.assertEquals(newShipment, shipments.get(newShipment.getPrimaryKey())); } @Test public void testFetchByPrimaryKeysWithNoPrimaryKeys() throws Exception { Set<Serializable> primaryKeys = new HashSet<Serializable>(); Map<Serializable, Shipment> shipments = _persistence.fetchByPrimaryKeys(primaryKeys); Assert.assertTrue(shipments.isEmpty()); } @Test public void testFetchByPrimaryKeysWithOnePrimaryKey() throws Exception { Shipment newShipment = addShipment(); Set<Serializable> primaryKeys = new HashSet<Serializable>(); primaryKeys.add(newShipment.getPrimaryKey()); Map<Serializable, Shipment> shipments = _persistence.fetchByPrimaryKeys(primaryKeys); Assert.assertEquals(1, shipments.size()); Assert.assertEquals(newShipment, shipments.get(newShipment.getPrimaryKey())); } @Test public void testActionableDynamicQuery() throws Exception { final IntegerWrapper count = new IntegerWrapper(); ActionableDynamicQuery actionableDynamicQuery = ShipmentLocalServiceUtil.getActionableDynamicQuery(); actionableDynamicQuery.setPerformActionMethod(new ActionableDynamicQuery.PerformActionMethod<Shipment>() { @Override public void performAction(Shipment shipment) { Assert.assertNotNull(shipment); count.increment(); } }); actionableDynamicQuery.performActions(); Assert.assertEquals(count.getValue(), _persistence.countAll()); } @Test public void testDynamicQueryByPrimaryKeyExisting() throws Exception { Shipment newShipment = addShipment(); DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(Shipment.class, _dynamicQueryClassLoader); dynamicQuery.add(RestrictionsFactoryUtil.eq("shipmentId", newShipment.getShipmentId())); List<Shipment> result = _persistence.findWithDynamicQuery(dynamicQuery); Assert.assertEquals(1, result.size()); Shipment existingShipment = result.get(0); Assert.assertEquals(existingShipment, newShipment); } @Test public void testDynamicQueryByPrimaryKeyMissing() throws Exception { DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(Shipment.class, _dynamicQueryClassLoader); dynamicQuery.add(RestrictionsFactoryUtil.eq("shipmentId", RandomTestUtil.nextLong())); List<Shipment> result = _persistence.findWithDynamicQuery(dynamicQuery); Assert.assertEquals(0, result.size()); } @Test public void testDynamicQueryByProjectionExisting() throws Exception { Shipment newShipment = addShipment(); DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(Shipment.class, _dynamicQueryClassLoader); dynamicQuery.setProjection(ProjectionFactoryUtil.property("shipmentId")); Object newShipmentId = newShipment.getShipmentId(); dynamicQuery.add(RestrictionsFactoryUtil.in("shipmentId", new Object[] { newShipmentId })); List<Object> result = _persistence.findWithDynamicQuery(dynamicQuery); Assert.assertEquals(1, result.size()); Object existingShipmentId = result.get(0); Assert.assertEquals(existingShipmentId, newShipmentId); } @Test public void testDynamicQueryByProjectionMissing() throws Exception { DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(Shipment.class, _dynamicQueryClassLoader); dynamicQuery.setProjection(ProjectionFactoryUtil.property("shipmentId")); dynamicQuery.add(RestrictionsFactoryUtil.in("shipmentId", new Object[] { RandomTestUtil.nextLong() })); List<Object> result = _persistence.findWithDynamicQuery(dynamicQuery); Assert.assertEquals(0, result.size()); } @Test public void testResetOriginalValues() throws Exception { Shipment newShipment = addShipment(); _persistence.clearCache(); Shipment existingShipment = _persistence.findByPrimaryKey(newShipment.getPrimaryKey()); Assert.assertTrue(Objects.equals(existingShipment.getDeliveryNbr(), ReflectionTestUtil.invoke(existingShipment, "getOriginalDeliveryNbr", new Class<?>[0]))); } protected Shipment addShipment() throws Exception { long pk = RandomTestUtil.nextLong(); Shipment shipment = _persistence.create(pk); shipment.setGroupId(RandomTestUtil.nextLong()); shipment.setCompanyId(RandomTestUtil.nextLong()); shipment.setUserId(RandomTestUtil.nextLong()); shipment.setUserName(RandomTestUtil.randomString()); shipment.setCreateDate(RandomTestUtil.nextDate()); shipment.setModifiedDate(RandomTestUtil.nextDate()); shipment.setOrderId(RandomTestUtil.nextLong()); shipment.setReleaseId(RandomTestUtil.nextLong()); shipment.setShipmentDate(RandomTestUtil.nextDate()); shipment.setShipmentQty(RandomTestUtil.nextInt()); shipment.setShipmentUom(RandomTestUtil.randomString()); shipment.setPalletQty(RandomTestUtil.nextInt()); shipment.setInvoiceNo(RandomTestUtil.nextLong()); shipment.setBolNo(RandomTestUtil.nextLong()); shipment.setCarrierName(RandomTestUtil.randomString()); shipment.setProNo(RandomTestUtil.randomString()); shipment.setShipToId(RandomTestUtil.randomString()); shipment.setDeliveryNbr(RandomTestUtil.randomString()); _shipments.add(_persistence.update(shipment)); return shipment; } private List<Shipment> _shipments = new ArrayList<Shipment>(); private ShipmentPersistence _persistence; private ClassLoader _dynamicQueryClassLoader; }