task4/solution07/test/org/apidesign/apifest08/test/ContractImposingDelegatingConvertor.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 11 Oct 2008 23:38:46 +0200
changeset 61 58ec6da75f6f
parent 45 task3/solution07/test/org/apidesign/apifest08/test/ContractImposingDelegatingConvertor.java@251d0ed461fb
permissions -rw-r--r--
Copying structure for task4
     1 /*
     2  * To change this template, choose Tools | Templates
     3  * and open the template in the editor.
     4  */
     5 
     6 package org.apidesign.apifest08.test;
     7 
     8 import java.math.BigDecimal;
     9 import java.util.Currency;
    10 import junit.framework.Assert;
    11 import org.apidesign.apifest08.currency.Convertor;
    12 import org.apidesign.apifest08.currency.DelegatingConvertor;
    13 import org.apidesign.apifest08.currency.MonetaryAmount;
    14 
    15 /**
    16  * A delegating convertor that checks preconditions and postconditions.
    17  * Useful for testing.
    18  * @author jdvorak
    19  */
    20 public class ContractImposingDelegatingConvertor extends DelegatingConvertor {
    21 
    22     public ContractImposingDelegatingConvertor( final Convertor underlyingConvertor ) {
    23         super( underlyingConvertor );
    24     }
    25 
    26     @Override
    27     public ConversionResult convert( final ConversionRequest req ) {
    28         Assert.assertNotNull( "The request", req );
    29         final ConversionResult result = super.convert( req );
    30         Assert.assertNotNull( "Result of the convert() call", result );
    31         final MonetaryAmount netAmount = result.getNetAmount();
    32         if ( netAmount != null ) {
    33             Assert.assertEquals( "Converted to a different currency than specified in the request", req.getTgtCurrency(), netAmount.getCurrency() );
    34         }
    35         return result;
    36     }
    37 
    38     /**
    39      * Do some tests on our own.
    40      * @return this
    41      */
    42     public Convertor test() {
    43         try {
    44             final Currency aCurrency = Currency.getInstance( "EUR" );
    45             new ConversionRequest( new MonetaryAmount( BigDecimal.ONE, aCurrency ), aCurrency );
    46             Assert.fail( "Should have thrown an IllegalArgumentException" );
    47         } catch ( final IllegalArgumentException e ) {
    48             Assert.assertEquals( "Cannot request conversion from EUR to EUR", e.getMessage() );
    49         }
    50         return this;
    51     }
    52 
    53 }