task1/solution06/test/org/apidesign/apifest08/test/Task1Test.java
changeset 21 61e4c4c120fd
parent 6 97662396c0fd
     1.1 --- a/task1/solution06/test/org/apidesign/apifest08/test/Task1Test.java	Sun Sep 28 14:12:38 2008 +0200
     1.2 +++ b/task1/solution06/test/org/apidesign/apifest08/test/Task1Test.java	Tue Sep 30 12:24:45 2008 +0200
     1.3 @@ -1,16 +1,16 @@
     1.4  package org.apidesign.apifest08.test;
     1.5  
     1.6 -import static org.apidesign.apifest08.currency.Currencies.CZK;
     1.7 -import static org.apidesign.apifest08.currency.Currencies.SKK;
     1.8 -import static org.apidesign.apifest08.currency.Currencies.USD;
     1.9 +import static org.apidesign.apifest08.test.Currencies.CZK;
    1.10 +import static org.apidesign.apifest08.test.Currencies.SKK;
    1.11 +import static org.apidesign.apifest08.test.Currencies.USD;
    1.12  
    1.13  import java.math.BigDecimal;
    1.14  
    1.15  import junit.framework.TestCase;
    1.16  
    1.17  import org.apidesign.apifest08.currency.Amount;
    1.18 +import org.apidesign.apifest08.currency.ConversionException;
    1.19  import org.apidesign.apifest08.currency.Convertor;
    1.20 -import org.apidesign.apifest08.currency.ConvertorFactory;
    1.21  import org.apidesign.apifest08.currency.UnsupportedConversionException;
    1.22  
    1.23  /** Finish the Convertor API, and then write bodies of methods inside
    1.24 @@ -41,7 +41,7 @@
    1.25       * @return prepared convertor ready for converting USD to CZK and CZK to USD
    1.26       */
    1.27      public static Convertor createCZKtoUSD() {
    1.28 -        return ConvertorFactory.newInstance();
    1.29 +        return new Convertor(new BigDecimal(17), USD, CZK);
    1.30      }
    1.31  
    1.32      /** Create convertor that understands two currencies, CZK and
    1.33 @@ -53,7 +53,7 @@
    1.34       * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
    1.35       */
    1.36      public static Convertor createSKKtoCZK() {
    1.37 -    	return ConvertorFactory.newInstance();
    1.38 +    	return new Convertor(new BigDecimal("0.8"), SKK, CZK);
    1.39      }
    1.40      
    1.41      /** Use the convertor from <code>createCZKtoUSD</code> method and do few conversions
    1.42 @@ -88,14 +88,49 @@
    1.43          assertEquals("Result is 400 CZK", 400, result.getValue().intValue());
    1.44      }
    1.45      
    1.46 -    public void testUnssuportedConversion(){
    1.47 -    	Convertor c = ConvertorFactory.newInstance();
    1.48 +    
    1.49 +    /**
    1.50 +     *  Verify that the CZK to USD convertor knows nothing about SKK.
    1.51 +     */
    1.52 +     public void testCannotConvertToSKKwithCZKUSDConvertor() throws Exception {
    1.53 +    	 Convertor c = createCZKtoUSD();
    1.54 +    	 // convert $5 to SKK, the API shall say this is not possible
    1.55 +    	 try {
    1.56 +    		 c.convert(new BigDecimal(5), USD, SKK);
    1.57 +    		 fail("convert $5 to SKK, the API shall say this is not possible");
    1.58 +    	 } catch (ConversionException e) {
    1.59 +    		 //expected
    1.60 +    	 }
    1.61 +    	 
    1.62 +    	 // convert 500 SKK to CZK, the API shall say this is not possible
    1.63 +    	 
    1.64 +    	 try {
    1.65 +    		 c.convert(new BigDecimal("500"), SKK, CZK);
    1.66 +    		 fail("convert 500 SKK to CZK, the API shall say this is not possible");
    1.67 +    	 } catch (ConversionException e) {
    1.68 +    		 //expected
    1.69 +    	 }
    1.70 +     }
    1.71 +    
    1.72 +    /** 
    1.73 +     * Verify that the CZK to SKK convertor knows nothing about USD.
    1.74 +     */
    1.75 +    public void testCannotConvertToSKKwithCZKSKKConvertor() throws Exception {
    1.76 +    	Convertor c = createSKKtoCZK();
    1.77 +    	// convert $5 to SKK, the API shall say this is not possible
    1.78      	try {
    1.79      		c.convert(new BigDecimal(5), USD, SKK);
    1.80 -    		fail();
    1.81 -    	} catch(UnsupportedConversionException e) {
    1.82 -    		//expected    		
    1.83 -    	}    	
    1.84 -    }
    1.85 +    		fail("convert $5 to SKK, the API shall say this is not possible");
    1.86 +    	} catch(ConversionException e) {
    1.87 +    		//expected
    1.88 +    	}
    1.89 +    	
    1.90 +    	try {
    1.91 +    		c.convert(new BigDecimal(500), CZK, USD);	
    1.92 +    		fail("convert 500 CZK to USD, the API shall say this is not possible");
    1.93 +    	} catch(ConversionException e) {
    1.94 +    		//expected
    1.95 +    	}
    1.96 +    } 
    1.97  }
    1.98