solution 02 updated to 1.5
authorjapod@localhost
Tue, 30 Sep 2008 11:47:02 +0200
changeset 162864c6d744c0
parent 15 a861bd02a1d2
child 17 37c9921c653e
solution 02 updated to 1.5
task1/solution02/src/org/apidesign/apifest08/currency/Convertor.java
task1/solution02/src/org/apidesign/apifest08/currency/ConvertorFactory.java
task1/solution02/src/org/apidesign/apifest08/currency/DefaultConvertor.java
task1/solution02/src/org/apidesign/apifest08/currency/Money.java
task1/solution02/src/org/apidesign/apifest08/currency/MoneyImpl.java
task1/solution02/test/org/apidesign/apifest08/test/ConvertorFactoryTest.java
task1/solution02/test/org/apidesign/apifest08/test/ConvertorTest.java
task1/solution02/test/org/apidesign/apifest08/test/MoneyTest.java
task1/solution02/test/org/apidesign/apifest08/test/Task1Test.java
     1.1 --- a/task1/solution02/src/org/apidesign/apifest08/currency/Convertor.java	Mon Sep 29 13:40:19 2008 +0200
     1.2 +++ b/task1/solution02/src/org/apidesign/apifest08/currency/Convertor.java	Tue Sep 30 11:47:02 2008 +0200
     1.3 @@ -4,35 +4,15 @@
     1.4  
     1.5  
     1.6  /** 
     1.7 - * Converts one currency to other. The conversion is unidirectional. 
     1.8 - * For example you can have convertor that converts USD (sourceCurrency) to CZK (destination currency). You can call the {@link Convertor#convert(Money)} method
     1.9 - * with amount in USD to get the equivalent in CZK. If you need convert CZK to USD you can call {@link Convertor#revert()} method to get CZK to USD 
    1.10 - * convertor. To create a convertor instance call {@link ConvertorFactory#createConvertor(Currency, Currency)}.
    1.11 + * Converts currencies. To create an instance call {@link ConvertorFactory#createConvertor(Money, Money)}.
    1.12   */
    1.13  public interface Convertor {
    1.14  	/**
    1.15 -	 * Converts amount in source currency to amount in destination currency. The result is rounded to two decimal places.
    1.16 -	 * @param money 
    1.17 +	 * Converts amount to its equivalent in the destination currency. 
    1.18 +	 * @param amount 
    1.19 +	 * @param destinationCurrency
    1.20  	 * @return
    1.21 -	 * @throws IllegalArgumentException if money.getCurrency is not equal to sourceCurrency.
    1.22 +	 * @throws IllegalArgumentException if currency of the amount is not supported or if it is not possible to convert it to the destination currency.
    1.23  	 */
    1.24 -	public Money convert(Money money);
    1.25 -
    1.26 -	/**
    1.27 -	 * Returns convertor that converts from destination currency to source currency with the same exchange rate. 
    1.28 -	 * @return
    1.29 -	 */
    1.30 -	public Convertor revert();
    1.31 -	
    1.32 -	/**
    1.33 -	 * Returns source currency.
    1.34 -	 * @return
    1.35 -	 */
    1.36 -	public Currency getSourceCurrency();
    1.37 -
    1.38 -	/**
    1.39 -	 * Returns destination currency.
    1.40 -	 * @return
    1.41 -	 */
    1.42 -	public Currency getDestinationCurrency();
    1.43 +	public Money convert(Money amount, Currency destinationCurrency) throws IllegalArgumentException;
    1.44  }
     2.1 --- a/task1/solution02/src/org/apidesign/apifest08/currency/ConvertorFactory.java	Mon Sep 29 13:40:19 2008 +0200
     2.2 +++ b/task1/solution02/src/org/apidesign/apifest08/currency/ConvertorFactory.java	Tue Sep 30 11:47:02 2008 +0200
     2.3 @@ -1,46 +1,27 @@
     2.4  package org.apidesign.apifest08.currency;
     2.5  
     2.6 -import java.util.Currency;
     2.7  
     2.8  
     2.9  /**
    2.10 - * Creates default {@link Convertor} implementations.
    2.11 + * Creates {@link Convertor} implementations.
    2.12   * @author lukas
    2.13   *
    2.14   */
    2.15  public class ConvertorFactory {
    2.16 -	private static final DefaultConvertorFactory DEFAULT_FACTORY = new DefaultConvertorFactory();
    2.17 -	
    2.18  	private ConvertorFactory()
    2.19  	{
    2.20  		//nothing
    2.21  	}
    2.22  		
    2.23  	/**
    2.24 -	 * Creates {@link Convertor} that converts from sourceCurrency to destinationCurrency with stored exchange rate.
    2.25 -	 * @param sourceCurrency
    2.26 -	 * @param destinationCurrency
    2.27 -	 * @return
    2.28 -	 * @throws UnsupportedConversionException when exchange rate between currencies is not known.
    2.29 -	 */
    2.30 -	/*
    2.31 -	 * Only one of the createConveror methods is needed. The assignment is not explicit where the exchange rate should be set.
    2.32 -	 */
    2.33 -	public static final Convertor createConvertor(Currency sourceCurrency, Currency destinationCurrency) throws UnsupportedConversionException
    2.34 -	{
    2.35 -		return DEFAULT_FACTORY.getConvertor(sourceCurrency, destinationCurrency);
    2.36 -	}
    2.37 -	/**
    2.38  	 * Creates {@link Convertor} that converts from sourceEquivalent.currency to destinationEquivalent.currency. 
    2.39  	 * Exchange rate is set as equivalents. It means if you want to create USD to CZK convertor where USD1 = CZK17 
    2.40 -     * call createConvertor(new MoneyImpl(1, USD), new MoneyImpl(17, CZK)).
    2.41 +     * call createConvertor(new MoneyImpl(1, USD), new MoneyImpl(17, CZK)). Convertor created by this method 
    2.42 +     * rounds the result to two decimal places.
    2.43  	 * @param sourceEquivalent
    2.44  	 * @param destinationEquivalent
    2.45  	 * @return
    2.46  	 */
    2.47 -	/*
    2.48 -	 * Only one of the createConveror methods is needed. The assignment is not explicit where the exchange rate should be set.
    2.49 -	 */
    2.50  	public static final Convertor createConvertor(Money sourceEquivalent, Money destinationEquivalent)
    2.51  	{
    2.52  		return new DefaultConvertor(sourceEquivalent.getAmount(), destinationEquivalent.getAmount(), sourceEquivalent.getCurrency(), destinationEquivalent.getCurrency());
     3.1 --- a/task1/solution02/src/org/apidesign/apifest08/currency/DefaultConvertor.java	Mon Sep 29 13:40:19 2008 +0200
     3.2 +++ b/task1/solution02/src/org/apidesign/apifest08/currency/DefaultConvertor.java	Tue Sep 30 11:47:02 2008 +0200
     3.3 @@ -31,26 +31,56 @@
     3.4  	
     3.5  	public DefaultConvertor(BigDecimal sourceEquivalent, BigDecimal destinationEquivalent, Currency sourceCurrency, Currency destinationCurrency) {
     3.6  		super();
     3.7 +		if (BigDecimal.ZERO.compareTo(sourceEquivalent)==0)
     3.8 +		{
     3.9 +			throw new IllegalArgumentException("Source equivalent amount can not be 0.");
    3.10 +		}
    3.11 +		if (BigDecimal.ZERO.compareTo(destinationEquivalent)==0)
    3.12 +		{
    3.13 +			throw new IllegalArgumentException("Destination equivalent amount can not be 0.");
    3.14 +		}
    3.15  		this.sourceEquivalent = sourceEquivalent;
    3.16  		this.destinationEquivalent = destinationEquivalent;
    3.17  		this.sourceCurrency = sourceCurrency;
    3.18  		this.destinationCurrency = destinationCurrency;
    3.19  	}
    3.20  	
    3.21 -	public Money convert(Money money) {
    3.22 -		if (money==null)
    3.23 +	public Money convert(Money amount, Currency destinationCurrency) {
    3.24 +		if (amount==null)
    3.25  		{
    3.26  			throw new NullPointerException("Money is null");
    3.27  		}
    3.28 -		if (!money.getCurrency().equals(getSourceCurrency()))
    3.29 +		if (destinationCurrency==null)
    3.30  		{
    3.31 -			throw new IllegalArgumentException("Can not convert from "+money.getCurrency()+". Converts "+getSourceCurrency()+" to "+getDestinationCurrency());
    3.32 +			throw new NullPointerException("destionationCurrency is null");
    3.33  		}
    3.34 -		BigDecimal sourceAmount = money.getAmount();
    3.35 -		BigDecimal destinationAmount = sourceAmount.multiply(destinationEquivalent).divide(sourceEquivalent, 2, RoundingMode.HALF_DOWN);
    3.36 +		if (isConversionInOpositeDirection(amount, destinationCurrency))
    3.37 +		{
    3.38 +			return revert().convert(amount, destinationCurrency);
    3.39 +		}
    3.40 +		if (!amount.getCurrency().equals(getSourceCurrency()))
    3.41 +		{
    3.42 +			throw new IllegalArgumentException("Can not convert from "+amount.getCurrency()+". Converts between "+getSourceCurrency()+" and "+getDestinationCurrency());
    3.43 +		}
    3.44 +		if (!getDestinationCurrency().equals(destinationCurrency))
    3.45 +		{
    3.46 +			throw new IllegalArgumentException("Can not convert to "+destinationCurrency+". Converts between "+getSourceCurrency()+" and "+getDestinationCurrency());
    3.47 +		}
    3.48 +		BigDecimal sourceAmount = amount.getAmount();
    3.49 +		BigDecimal destinationAmount = sourceAmount.multiply(destinationEquivalent).divide(sourceEquivalent, 2, RoundingMode.HALF_UP);
    3.50  		return new MoneyImpl(destinationAmount, getDestinationCurrency());
    3.51  	}
    3.52  
    3.53 +	/**
    3.54 +	 * Returns true, if the conversion is in oposit direction.
    3.55 +	 * @param amount
    3.56 +	 * @param destinationCurrency
    3.57 +	 * @return
    3.58 +	 */
    3.59 +	private boolean isConversionInOpositeDirection(Money amount,	Currency destinationCurrency) {
    3.60 +		return amount.getCurrency().equals(getDestinationCurrency()) && destinationCurrency.equals(getSourceCurrency());
    3.61 +	}
    3.62 +
    3.63  
    3.64  	public Convertor revert() {
    3.65  		return new DefaultConvertor(destinationEquivalent, sourceEquivalent, destinationCurrency, sourceCurrency);
     4.1 --- a/task1/solution02/src/org/apidesign/apifest08/currency/Money.java	Mon Sep 29 13:40:19 2008 +0200
     4.2 +++ b/task1/solution02/src/org/apidesign/apifest08/currency/Money.java	Tue Sep 30 11:47:02 2008 +0200
     4.3 @@ -10,7 +10,7 @@
     4.4   *
     4.5   */
     4.6  /*
     4.7 - * Whether we need such interface depends on context. I can imagine than in a desktop application this interface 
     4.8 + * Whether we need such interface depends on the context. I can imagine than in a desktop application this interface 
     4.9   * would be useless, Money could be a class. In J2EE environment it can be useful.
    4.10   */
    4.11  public interface Money {
     5.1 --- a/task1/solution02/src/org/apidesign/apifest08/currency/MoneyImpl.java	Mon Sep 29 13:40:19 2008 +0200
     5.2 +++ b/task1/solution02/src/org/apidesign/apifest08/currency/MoneyImpl.java	Tue Sep 30 11:47:02 2008 +0200
     5.3 @@ -19,7 +19,7 @@
     5.4  	public MoneyImpl(BigDecimal amount, Currency currency) {
     5.5  		if (amount==null) throw new NullPointerException("Amount is null");
     5.6  		if (currency==null) throw new NullPointerException("Currency is null"+currency);
     5.7 -		this.amount = amount.setScale(2);
     5.8 +		this.amount = amount;
     5.9  		this.currency = currency;
    5.10  	}
    5.11  	
    5.12 @@ -31,15 +31,16 @@
    5.13  		this(BigDecimal.valueOf(amount), currency);
    5.14  	}
    5.15  
    5.16 -	/* (non-Javadoc)
    5.17 -	 * @see org.apidesign.apifest08.currency.Money#getAmount()
    5.18 +	/**
    5.19 +	 * Returns amount.
    5.20 +	 * @return
    5.21  	 */
    5.22  	public BigDecimal getAmount() {
    5.23  		return amount;
    5.24  	}
    5.25  	
    5.26 -	/* (non-Javadoc)
    5.27 -	 * @see org.apidesign.apifest08.currency.Money#getCurrency()
    5.28 +	/**
    5.29 +	 * Returns currency.
    5.30  	 */
    5.31  	public Currency getCurrency() {
    5.32  		return currency;
    5.33 @@ -67,7 +68,7 @@
    5.34  		if (amount == null) {
    5.35  			if (other.amount != null)
    5.36  				return false;
    5.37 -		} else if (!amount.equals(other.amount))
    5.38 +		} else if (amount.compareTo(other.amount)!=0)
    5.39  			return false;
    5.40  		if (currency == null) {
    5.41  			if (other.currency != null)
     6.1 --- a/task1/solution02/test/org/apidesign/apifest08/test/ConvertorFactoryTest.java	Mon Sep 29 13:40:19 2008 +0200
     6.2 +++ b/task1/solution02/test/org/apidesign/apifest08/test/ConvertorFactoryTest.java	Tue Sep 30 11:47:02 2008 +0200
     6.3 @@ -1,14 +1,15 @@
     6.4  package org.apidesign.apifest08.test;
     6.5  
     6.6 -import static junit.framework.Assert.assertEquals;
     6.7  import static junit.framework.Assert.assertNotNull;
     6.8  import static org.apidesign.apifest08.test.Task1Test.CZK;
     6.9  import static org.apidesign.apifest08.test.Task1Test.USD;
    6.10 +import static org.junit.Assert.assertEquals;
    6.11 +import static org.junit.Assert.assertTrue;
    6.12 +import static org.junit.Assert.fail;
    6.13  
    6.14 -import java.util.Currency;
    6.15 -
    6.16 +import org.apidesign.apifest08.currency.Convertor;
    6.17  import org.apidesign.apifest08.currency.ConvertorFactory;
    6.18 -import org.apidesign.apifest08.currency.UnsupportedConversionException;
    6.19 +import org.apidesign.apifest08.currency.MoneyImpl;
    6.20  import org.junit.Test;
    6.21  
    6.22  
    6.23 @@ -16,31 +17,36 @@
    6.24  	@Test(expected=NullPointerException.class)
    6.25  	public void testNullSource()
    6.26  	{
    6.27 -		ConvertorFactory.createConvertor(null, USD);
    6.28 +		ConvertorFactory.createConvertor(null, new MoneyImpl(1,USD));
    6.29  	}
    6.30  	@Test(expected=NullPointerException.class)
    6.31  	public void testNullDestination()
    6.32  	{
    6.33 -		ConvertorFactory.createConvertor(CZK, null);
    6.34 -	}
    6.35 -	@Test(expected=IllegalArgumentException.class)
    6.36 -	public void testShortSource()
    6.37 -	{
    6.38 -		ConvertorFactory.createConvertor(Currency.getInstance("CZ"), USD);
    6.39 -	}
    6.40 -	@Test(expected=UnsupportedConversionException.class)
    6.41 -	public void testUnknownCombination()
    6.42 -	{
    6.43 -		ConvertorFactory.createConvertor(CZK, Currency.getInstance("ZAR"));
    6.44 +		ConvertorFactory.createConvertor(new MoneyImpl(17,CZK), null);
    6.45  	}
    6.46  	@Test
    6.47  	public void testOk()
    6.48  	{
    6.49 -		assertNotNull(ConvertorFactory.createConvertor(CZK, USD));
    6.50 +		assertNotNull(ConvertorFactory.createConvertor(new MoneyImpl(17,CZK),  new MoneyImpl(1,USD)));
    6.51  	}
    6.52  	@Test
    6.53 -	public void testReverted()
    6.54 +	public void testOkDecimalRate()
    6.55  	{
    6.56 -		assertEquals(ConvertorFactory.createConvertor(CZK, USD).revert(), ConvertorFactory.createConvertor(USD, CZK));
    6.57 +		Convertor c = ConvertorFactory.createConvertor(new MoneyImpl(1,CZK),  new MoneyImpl(1d/17d,USD));
    6.58 +		assertNotNull(c);
    6.59 +		assertEquals(new MoneyImpl(17,CZK),c.convert(new MoneyImpl(1,USD), CZK));
    6.60 +	}
    6.61 +	@Test
    6.62 +	public void testZeroEquivalentRate()
    6.63 +	{
    6.64 +		try
    6.65 +		{
    6.66 +			ConvertorFactory.createConvertor(new MoneyImpl(1,CZK),  new MoneyImpl(0,USD));
    6.67 +			fail("Exception expected");
    6.68 +		}
    6.69 +		catch(IllegalArgumentException e)
    6.70 +		{
    6.71 +			assertTrue("OK",true);
    6.72 +		}
    6.73  	}
    6.74  }
     7.1 --- a/task1/solution02/test/org/apidesign/apifest08/test/ConvertorTest.java	Mon Sep 29 13:40:19 2008 +0200
     7.2 +++ b/task1/solution02/test/org/apidesign/apifest08/test/ConvertorTest.java	Tue Sep 30 11:47:02 2008 +0200
     7.3 @@ -6,6 +6,7 @@
     7.4  
     7.5  import java.math.BigDecimal;
     7.6  
     7.7 +import org.apidesign.apifest08.currency.Convertor;
     7.8  import org.apidesign.apifest08.currency.ConvertorFactory;
     7.9  import org.apidesign.apifest08.currency.Money;
    7.10  import org.apidesign.apifest08.currency.MoneyImpl;
    7.11 @@ -14,17 +15,18 @@
    7.12  
    7.13  public class ConvertorTest {
    7.14  
    7.15 +	private static final Convertor CZK_TO_USD_CONVERTOR = ConvertorFactory.createConvertor(new MoneyImpl(17,CZK), new MoneyImpl(1,USD));
    7.16  	@Test
    7.17  	public void testConvertSmall()
    7.18  	{
    7.19 -		Money converted = ConvertorFactory.createConvertor(CZK, USD).convert(new MoneyImpl(0.17,CZK));
    7.20 +		Money converted = CZK_TO_USD_CONVERTOR.convert(new MoneyImpl(0.17,CZK),USD);
    7.21  		assertEquals(new MoneyImpl(new BigDecimal("0.01"),USD),converted);
    7.22  		assertEquals(USD,converted.getCurrency());
    7.23  	}
    7.24  	@Test
    7.25  	public void testConvertSmallReverse()
    7.26  	{
    7.27 -		Money converted = ConvertorFactory.createConvertor(USD, CZK).convert(new MoneyImpl(0.01,USD));
    7.28 +		Money converted = CZK_TO_USD_CONVERTOR.convert(new MoneyImpl(0.01,USD),CZK);
    7.29  		assertEquals(new MoneyImpl(new BigDecimal("0.17"),CZK),converted);
    7.30  	}
    7.31  }
     8.1 --- a/task1/solution02/test/org/apidesign/apifest08/test/MoneyTest.java	Mon Sep 29 13:40:19 2008 +0200
     8.2 +++ b/task1/solution02/test/org/apidesign/apifest08/test/MoneyTest.java	Tue Sep 30 11:47:02 2008 +0200
     8.3 @@ -1,12 +1,12 @@
     8.4  package org.apidesign.apifest08.test;
     8.5  
     8.6  import static junit.framework.Assert.assertEquals;
     8.7 +import static org.apidesign.apifest08.test.Task1Test.CZK;
     8.8  
     8.9  import java.math.BigDecimal;
    8.10  
    8.11  import org.apidesign.apifest08.currency.MoneyImpl;
    8.12  import org.junit.Test;
    8.13 -import static org.apidesign.apifest08.test.Task1Test.*;
    8.14  
    8.15  public class MoneyTest {
    8.16  	@Test(expected=NullPointerException.class)
    8.17 @@ -19,6 +19,6 @@
    8.18  	}
    8.19  	@Test
    8.20  	public void testOk(){
    8.21 -		assertEquals(new BigDecimal("123.00"),new MoneyImpl(123,CZK).getAmount());
    8.22 +		assertEquals(0,new MoneyImpl(123,CZK).getAmount().compareTo(new BigDecimal("123")));
    8.23  	}
    8.24  }
     9.1 --- a/task1/solution02/test/org/apidesign/apifest08/test/Task1Test.java	Mon Sep 29 13:40:19 2008 +0200
     9.2 +++ b/task1/solution02/test/org/apidesign/apifest08/test/Task1Test.java	Tue Sep 30 11:47:02 2008 +0200
     9.3 @@ -15,9 +15,11 @@
     9.4   * shall run without any runtime permissions.
     9.5   */
     9.6  public class Task1Test extends TestCase {
     9.7 +	
     9.8  	public static final Currency USD = Currency.getInstance("USD");
     9.9  	public static final Currency CZK = Currency.getInstance("CZK");
    9.10  	public static final Currency SKK = Currency.getInstance("SKK");
    9.11 +	
    9.12      public Task1Test(String testName) {
    9.13          super(testName);
    9.14      }
    9.15 @@ -30,8 +32,19 @@
    9.16      protected void tearDown() throws Exception {
    9.17      }
    9.18  
    9.19 +    //
    9.20 +    // Imagine that there are three parts of the whole system:
    9.21 +    // 1. there is someone who knows the current exchange rate
    9.22 +    // 2. there is someone who wants to do the conversion
    9.23 +    // 3. there is the API between 1. and 2. which allows them to communicate
    9.24 +    // Please design such API
    9.25 +    //
    9.26 +
    9.27      /** Create convertor that understands two currencies, CZK and
    9.28 -     *  USD. Make 1 USD == 17 CZK.
    9.29 +     *  USD. Make 1 USD == 17 CZK. This is a method provided for #1 group -
    9.30 +     *  e.g. those that know the exchange rate. They somehow need to create
    9.31 +     *  the objects from the API and tell them the exchange rate. The API itself
    9.32 +     *  knows nothing about any rates, before the createCZKtoUSD method is called.
    9.33       *
    9.34       * Creation of the convertor shall not require subclassing of any class
    9.35       * or interface on the client side.
    9.36 @@ -39,14 +52,15 @@
    9.37       * @return prepared convertor ready for converting USD to CZK and CZK to USD
    9.38       */
    9.39      public static Convertor createCZKtoUSD() {
    9.40 -      //You can use both variants. I wrote the first one but than I realized that maybe
    9.41 -      //you want me to write the second one. So I have written both. 
    9.42 -      return ConvertorFactory.createConvertor(CZK, USD);
    9.43 -      //return ConvertorFactory.createConvertor(new MoneyImpl(17,CZK), new MoneyImpl(1,USD));
    9.44 +    	return ConvertorFactory.createConvertor(new MoneyImpl(17,CZK), new MoneyImpl(1,USD));
    9.45      }
    9.46  
    9.47      /** Create convertor that understands two currencies, CZK and
    9.48 -     *  SKK. Make 100 SKK == 80 CZK.
    9.49 +     *  SKK. Make 100 SKK == 80 CZK. Again this is method for the #1 group -
    9.50 +     *  it knows the exchange rate, and needs to use the API to create objects
    9.51 +     *  with the exchange rate. Anyone shall be ready to call this method without
    9.52 +     *  any other method being called previously. The API itself shall know
    9.53 +     *  nothing about any rates, before this method is called.
    9.54       *
    9.55       * Creation of the convertor shall not require subclassing of any class
    9.56       * or interface on the client side.
    9.57 @@ -54,38 +68,93 @@
    9.58       * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
    9.59       */
    9.60      public static Convertor createSKKtoCZK() {
    9.61 -    	//You can use both variants. I wrote the first one but than I realized that maybe
    9.62 -        //you want me to write the second one. So I have written both.
    9.63 -        //return ConvertorFactory.createConvertor(SKK, CZK);
    9.64 -        return ConvertorFactory.createConvertor(new MoneyImpl(100,SKK), new MoneyImpl(80,CZK));
    9.65 +    	return ConvertorFactory.createConvertor(new MoneyImpl(100,SKK), new MoneyImpl(80,CZK));
    9.66      }
    9.67 +
    9.68 +    //
    9.69 +    // now the methods for group #2 follow:
    9.70 +    // this group knows nothing about exchange rates, but knows how to use
    9.71 +    // the API to do conversions. It somehow (by calling one of the factory
    9.72 +    // methods) gets objects from the API and uses them to do the conversions.
    9.73 +    //
    9.74      
    9.75      /** Use the convertor from <code>createCZKtoUSD</code> method and do few conversions
    9.76       * with it.
    9.77       */
    9.78      public void testCurrencyCZKUSD() throws Exception {
    9.79 -        Convertor czkToUsdConvertor = createCZKtoUSD();
    9.80 +    	Convertor c = createCZKtoUSD();
    9.81          // convert $5 to CZK using c:
    9.82 -        Convertor usdToCzkConvertor = czkToUsdConvertor.revert();
    9.83 -		assertEquals("Result is 85 CZK",new MoneyImpl(85,CZK), usdToCzkConvertor.convert(new MoneyImpl(5,USD)));
    9.84 +        assertEquals("Result is 85 CZK",new MoneyImpl(85,CZK), c.convert(new MoneyImpl(5,USD),CZK));
    9.85  
    9.86          // convert $8 to CZK
    9.87 -        assertEquals("Result is 136 CZK",new MoneyImpl(136,CZK), usdToCzkConvertor.convert(new MoneyImpl(8,USD)));
    9.88 +        assertEquals("Result is 136 CZK",new MoneyImpl(136,CZK), c.convert(new MoneyImpl(8,USD),CZK));
    9.89  
    9.90          // convert 1003CZK to USD
    9.91 -        assertEquals("Result is 59 USD", new MoneyImpl(59,USD), czkToUsdConvertor.convert(new MoneyImpl(1003,CZK)));
    9.92 +        assertEquals("Result is 59 USD", new MoneyImpl(59,USD), c.convert(new MoneyImpl(1003,CZK),USD));
    9.93 +        
    9.94      }
    9.95  
    9.96      /** Use the convertor from <code>createSKKtoCZK</code> method and do few conversions
    9.97       * with it.
    9.98       */
    9.99      public void testCurrencySKKCZK() throws Exception {
   9.100 -        Convertor skkToCzkConvertor = createSKKtoCZK();
   9.101 +        Convertor c = createSKKtoCZK();
   9.102          // convert 16CZK using c:
   9.103 -        assertEquals("Result is 20 SKK", new MoneyImpl(20,SKK), skkToCzkConvertor.revert().convert(new MoneyImpl(16,CZK)));
   9.104 +        assertEquals("Result is 20 SKK", new MoneyImpl(20,SKK), c.convert(new MoneyImpl(16,CZK),SKK));
   9.105  
   9.106          // convert 500SKK to CZK
   9.107 -        assertEquals("Result is 400 CZK", new MoneyImpl(400,CZK), skkToCzkConvertor.convert(new MoneyImpl(500,SKK)));
   9.108 +        assertEquals("Result is 400 CZK", new MoneyImpl(400,CZK), c.convert(new MoneyImpl(500,SKK),CZK));
   9.109 +    }
   9.110 +
   9.111 +    /** Verify that the CZK to USD convertor knows nothing about SKK.
   9.112 +     */
   9.113 +    public void testCannotConvertToSKKwithCZKUSDConvertor() throws Exception {
   9.114 +        Convertor c = createCZKtoUSD();
   9.115 +        // convert $5 to SKK, the API shall say this is not possible
   9.116 +        try
   9.117 +        {
   9.118 +        	c.convert(new MoneyImpl(5, USD), SKK);
   9.119 +        	fail("Exception expected");
   9.120 +        }
   9.121 +        catch(IllegalArgumentException e)
   9.122 +        {
   9.123 +        	assertTrue("Ok",true);
   9.124 +        }
   9.125 +        // convert 500 SKK to CZK, the API shall say this is not possible
   9.126 +        try
   9.127 +        {
   9.128 +        	c.convert(new MoneyImpl(500, SKK), CZK);
   9.129 +        	fail("Exception expected");
   9.130 +        }
   9.131 +        catch(IllegalArgumentException e)
   9.132 +        {
   9.133 +        	assertTrue("Ok",true);
   9.134 +        }
   9.135 +    }
   9.136 +
   9.137 +    /** Verify that the CZK to SKK convertor knows nothing about USD.
   9.138 +     */
   9.139 +    public void testCannotConvertToUSDwithCZKSKKConvertor() throws Exception {
   9.140 +        Convertor c = createSKKtoCZK();
   9.141 +        // convert $5 to SKK, the API shall say this is not possible
   9.142 +        try
   9.143 +        {
   9.144 +        	c.convert(new MoneyImpl(5, USD), SKK);
   9.145 +        	fail("Exception expected");
   9.146 +        }
   9.147 +        catch(IllegalArgumentException e)
   9.148 +        {
   9.149 +        	assertTrue("Ok",true);
   9.150 +        }
   9.151 +        // convert 500 CZK to USD, the API shall say this is not possible
   9.152 +        try
   9.153 +        {
   9.154 +        	c.convert(new MoneyImpl(500, CZK), USD);
   9.155 +        	fail("Exception expected");
   9.156 +        }
   9.157 +        catch(IllegalArgumentException e)
   9.158 +        {
   9.159 +        	assertTrue("Ok",true);
   9.160 +        }
   9.161      }
   9.162  }
   9.163 -