Java tutorial
/* **************************************************************************** * * * (c) Copyright 2008 ABM-utvikling * * * * This program is free software; you can redistribute it and/or modify it * * under the terms of the GNU General Public License as published by the * * Free Software Foundation; either version 2 of the License, or (at your * * option) any later version. * * * * This program 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 General * * Public License for more details. http://www.gnu.org/licenses/gpl.html * * * **************************************************************************** */ package no.abmu.lise.domain.fixtures; import java.math.BigDecimal; import java.util.Calendar; import java.util.Collection; import java.util.Date; import java.util.LinkedHashSet; import junit.framework.Assert; import no.abmu.common.domain.Entity; import no.abmu.lise.domain.Consortium; import no.abmu.lise.domain.LiseComment; import no.abmu.lise.domain.PaymentToSupplier; import no.abmu.test.domainmodels.AbstractDeepComparator; import no.abmu.test.domainmodels.DeepCompare; import no.abmu.test.domainmodels.TestFixture; import no.abmu.test.util.randomgenerator.RandomGeneratorUtil; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * PaymentToSupplierTestFixture. * * @author Erik Romson, erik@zenior.no * @author Erlend verby; erlend.overby@hypatia.no * @author $Author: jens $ * @version $Rev: 9360 $ * @date $Date: 2008-03-29 23:29:03 +0100 (Sat, 29 Mar 2008) $ * @copyright ABM-Utvikling */ public class PaymentToSupplierTestFixture implements TestFixture { public static final DeepCompare PAYMENT_COMPARATOR = new PaymentToSupplierTestFixture.PaymentToSupplierDeepComparator(); @SuppressWarnings("unused") private static final Log logger = (Log) LogFactory.getLog(PaymentToSupplierTestFixture.class); private PaymentToSupplier paymentToSupplierA; private PaymentToSupplier paymentToSupplierB; private PaymentToSupplier paymentToSupplierC; private ExtendedPaymentToSupplier paymentToSupplierD; private Collection<Entity> entitiesForDeepComparatorTesting; public PaymentToSupplierTestFixture(ConsortiumTestFixture consortiumTestFixture) { Calendar calendar = Calendar.getInstance(); Date today = calendar.getTime(); calendar.roll(Calendar.DAY_OF_YEAR, 1); Date tomorrow = calendar.getTime(); calendar.roll(Calendar.DAY_OF_YEAR, -2); Date yesterday = calendar.getTime(); Consortium consortiumA = consortiumTestFixture.getConsortiumA(); String subscriptionYearA = "subscriptionYearA"; LiseComment liseCommentA = new LiseComment("commentA"); liseCommentA.setCreated(yesterday); liseCommentA.setLastUpdated(today); Date dueDateA = today; Date invoiceDateA = yesterday; Long randomLongA = RandomGeneratorUtil.createRandomLong(); BigDecimal amountA = new BigDecimal(randomLongA); String invoiceNumberA = "invoiceNumberA"; paymentToSupplierA = new PaymentToSupplier(amountA, subscriptionYearA, invoiceNumberA, dueDateA); paymentToSupplierA.setConsortium(consortiumA); paymentToSupplierA.setInvoiceDate(invoiceDateA); paymentToSupplierA.setLiseComment(liseCommentA); paymentToSupplierB = new PaymentToSupplier(amountA, subscriptionYearA, invoiceNumberA, dueDateA); paymentToSupplierB.setConsortium(consortiumA); paymentToSupplierB.setInvoiceDate(invoiceDateA); paymentToSupplierB.setLiseComment(liseCommentA); Consortium consortiumC = consortiumTestFixture.getConsortiumC(); String subscriptionYearC = "subscriptionYearC"; LiseComment liseCommentC = new LiseComment("commentC"); liseCommentC.setCreated(today); liseCommentC.setLastUpdated(tomorrow); Date dueDateC = tomorrow; Date invoiceDateC = today; Long randomLongC = RandomGeneratorUtil.createRandomADifferentLong(randomLongA); BigDecimal amountC = new BigDecimal(randomLongC); String invoiceNumberC = "invoiceNumberC"; paymentToSupplierC = new PaymentToSupplier(amountC, subscriptionYearC, invoiceNumberC, dueDateC); paymentToSupplierC.setConsortium(consortiumC); paymentToSupplierC.setInvoiceDate(invoiceDateC); paymentToSupplierA.setLiseComment(liseCommentC); paymentToSupplierD = new ExtendedPaymentToSupplier(paymentToSupplierA); } public PaymentToSupplier getPaymentToSupplierA() { return paymentToSupplierA; } public PaymentToSupplier getPaymentToSupplierB() { return paymentToSupplierB; } public PaymentToSupplier getPaymentToSupplierC() { return paymentToSupplierC; } public ExtendedPaymentToSupplier getPaymentToSupplierD() { return paymentToSupplierD; } public Collection<Entity> getEntitiesForDeepComparatorTesting() { if (entitiesForDeepComparatorTesting == null) { entitiesForDeepComparatorTesting = new LinkedHashSet<Entity>(); entitiesForDeepComparatorTesting.add(paymentToSupplierA); entitiesForDeepComparatorTesting.add(paymentToSupplierB); entitiesForDeepComparatorTesting.add(paymentToSupplierC); } return entitiesForDeepComparatorTesting; } /** * ExtendedPaymentToSupplier. * */ @SuppressWarnings("serial") public class ExtendedPaymentToSupplier extends PaymentToSupplier { public ExtendedPaymentToSupplier(PaymentToSupplier p) { super.setConsortium(p.getConsortium()); super.setAmount(p.getAmount()); super.setSubscriptionYear(p.getSubscriptionYear()); super.setInvoiceNumber(p.getInvoiceNumber()); super.setInvoiceDate(p.getInvoiceDate()); super.setDueDate(p.getDueDate()); super.setLiseComment(p.getLiseComment()); } } /** * PaymentToSupplierDeepComparator. * */ public static class PaymentToSupplierDeepComparator extends AbstractDeepComparator { public boolean allowCompare(Entity retrieved, Entity loaded) { return (retrieved instanceof PaymentToSupplier) && (loaded instanceof PaymentToSupplier); } public void deepCompare(Entity retrievedEntity, Entity loadedEntity) { Assert.assertTrue( "Can only compare " + PaymentToSupplier.class.getName() + ". arg1 " + retrievedEntity.getClass().getName() + " arg2: " + loadedEntity.getClass().getName(), allowCompare(retrievedEntity, loadedEntity)); PaymentToSupplier retrieved = (PaymentToSupplier) retrievedEntity; PaymentToSupplier loaded = (PaymentToSupplier) loadedEntity; Assert.assertEquals(retrieved.getClass(), loaded.getClass()); Assert.assertEquals(retrieved.getAmount(), loaded.getAmount()); Assert.assertEquals(retrieved.getInvoiceNumber(), loaded.getInvoiceNumber()); Assert.assertEquals(retrieved.getSubscriptionYear(), loaded.getSubscriptionYear()); } } }