task1/solution07/test/org/apidesign/apifest08/test/ContractImposingDelegatingConvertor.java
2 * To change this template, choose Tools | Templates
3 * and open the template in the editor.
6 package org.apidesign.apifest08.test;
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;
16 * A delegating convertor that checks preconditions and postconditions.
20 public class ContractImposingDelegatingConvertor extends DelegatingConvertor {
22 public ContractImposingDelegatingConvertor( final Convertor underlyingConvertor ) {
23 super( underlyingConvertor );
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() );
39 * Do some tests on our own.
42 public Convertor test() {
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() );