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