task4/solution07/test/org/apidesign/apifest08/test/Task4Test.java
changeset 65 8482e36a7ad2
parent 62 f711ecd374f3
     1.1 --- a/task4/solution07/test/org/apidesign/apifest08/test/Task4Test.java	Sat Oct 11 23:46:05 2008 +0200
     1.2 +++ b/task4/solution07/test/org/apidesign/apifest08/test/Task4Test.java	Fri Oct 17 17:33:32 2008 +0200
     1.3 @@ -1,8 +1,18 @@
     1.4  package org.apidesign.apifest08.test;
     1.5  
     1.6 +import org.apidesign.apifest08.currency.TimeRangeSpecificConvertor;
     1.7 +import java.text.DateFormat;
     1.8 +import java.text.ParseException;
     1.9 +import java.text.SimpleDateFormat;
    1.10 +import java.util.Currency;
    1.11  import java.util.Date;
    1.12  import junit.framework.TestCase;
    1.13 +import org.apidesign.apifest08.currency.ConversionRate;
    1.14  import org.apidesign.apifest08.currency.Convertor;
    1.15 +import org.apidesign.apifest08.currency.Convertor.ConversionResult;
    1.16 +import org.apidesign.apifest08.currency.MonetaryAmount;
    1.17 +import org.apidesign.apifest08.currency.TableConvertor;
    1.18 +import org.apidesign.apifest08.currency.TimeRangeSpecificConvertor.TimeSpecificConversionRequest;
    1.19  
    1.20  /** The exchange rates are not always the same. They are changing. However
    1.21   * as in order to predict the future, one needs to understand own past. That is
    1.22 @@ -30,6 +40,10 @@
    1.23      protected void tearDown() throws Exception {
    1.24      }
    1.25  
    1.26 +    protected static final Currency CZK = Currency.getInstance( "CZK" );
    1.27 +    protected static final Currency SKK = Currency.getInstance( "SKK" );
    1.28 +    protected static final Currency USD = Currency.getInstance( "USD" );
    1.29 +
    1.30      // Backward compatibly enhance your existing API to support following
    1.31      // usecases:
    1.32      //
    1.33 @@ -40,62 +54,110 @@
    1.34       * shall call some real one in the API.
    1.35       * 
    1.36       * @param old existing convertor
    1.37 -     * @param from initial date (inclusive)
    1.38 -     * @param till final date (exclusive)
    1.39 +     * @param from initial date (inclusive); null means since the Big Bang
    1.40 +     * @param till final date (exclusive); null means until the End of Universe
    1.41       * @return new convertor
    1.42       */
    1.43 -    public static Convertor limitTo(Convertor old, Date from, Date till) {
    1.44 -        return null;
    1.45 +    public static Convertor limitTo( final Convertor old, final Date from, final Date till ) {
    1.46 +        return new TimeRangeSpecificConvertor( old, from, till );
    1.47 +    }
    1.48 +        
    1.49 +    private static final DateFormat DATE_FORMAT = new SimpleDateFormat( "yyyy-MM-dd HH:mm Z" );
    1.50 +    
    1.51 +    protected static Date parseGmtDate( final String string ) {
    1.52 +        final String zonedString = string + " GMT+0:00";
    1.53 +        try {
    1.54 +            return DATE_FORMAT.parse( zonedString );
    1.55 +        } catch ( final ParseException ex ) {
    1.56 +            throw new IllegalArgumentException( "Cannot parse " + zonedString, ex );
    1.57 +        }
    1.58      }
    1.59  
    1.60 -
    1.61      public void testCompositionOfLimitedConvertors() throws Exception {
    1.62 -        if (Boolean.getBoolean("ignore.failing")) {
    1.63 -            // implement me! then delete this if statement
    1.64 -            return;
    1.65 -        }
    1.66 -
    1.67 -        Date d1 = null; // 2008-10-01 0:00 GMT
    1.68 -        Date d2 = null; // 2008-10-02 0:00 GMT
    1.69 -        Date d3 = null; // 2008-10-03 0:00 GMT
    1.70 +        final Date d1 = parseGmtDate( "2008-10-01 0:00" );
    1.71 +        final Date d2 = parseGmtDate( "2008-10-02 0:00" );
    1.72 +        final Date d3 = parseGmtDate( "2008-10-03 0:00" );
    1.73          
    1.74 -        Convertor c = Task2Test.merge(
    1.75 +        final Convertor c = Task2Test.merge(
    1.76              limitTo(Task1Test.createCZKtoUSD(), d1, d2),
    1.77              limitTo(Task1Test.createSKKtoCZK(), d2, d3)
    1.78          );
    1.79  
    1.80          // convert $5 to CZK using c:
    1.81 +        final ConversionResult r1 = c.convert( new TimeSpecificConversionRequest( new MonetaryAmount( 5, USD ), CZK ) );
    1.82 +        final MonetaryAmount a1 = r1.getNetAmount();
    1.83          // cannot convert as no rate is applicable to current date
    1.84 +        assertNull( a1 );
    1.85  
    1.86          // convert $8 to CZK using c:
    1.87 +        final ConversionResult r2 = c.convert( new TimeSpecificConversionRequest( new MonetaryAmount( 8, USD ), CZK ) );
    1.88 +        final MonetaryAmount a2 = r2.getNetAmount();
    1.89          // cannot convert as no rate is applicable to current date
    1.90 +        assertNull( a2 );
    1.91  
    1.92          // convert 1003CZK to USD using c:
    1.93 +        final ConversionResult r3 = c.convert( new TimeSpecificConversionRequest( new MonetaryAmount( 1003, CZK ), USD ) );
    1.94 +        final MonetaryAmount a3 = r3.getNetAmount();
    1.95          // cannot convert as no rate is applicable to current date
    1.96 +        assertNull( a3 );
    1.97  
    1.98          // convert 16CZK using c:
    1.99 +        final ConversionResult r4 = c.convert( new TimeSpecificConversionRequest( new MonetaryAmount( 16, CZK ), USD ) );
   1.100 +        final MonetaryAmount a4 = r4.getNetAmount();
   1.101          // cannot convert as no rate is applicable to current date
   1.102 +        assertNull( a4 );
   1.103  
   1.104          // convert 500SKK to CZK using c:
   1.105 +        final ConversionResult r5 = c.convert( new TimeSpecificConversionRequest( new MonetaryAmount( 500, SKK ), CZK ) );
   1.106 +        final MonetaryAmount a5 = r5.getNetAmount();
   1.107          // cannot convert as no rate is applicable to current date
   1.108 +        assertNull( a5 );
   1.109  
   1.110          // convert $5 to CZK using c at 2008-10-01 6:00 GMT:
   1.111 +        final ConversionResult r6 = c.convert( new TimeSpecificConversionRequest( new MonetaryAmount( 5, USD ), CZK, parseGmtDate( "2008-10-01 6:00" ) ) );
   1.112 +        final MonetaryAmount a6 = r6.getNetAmount();
   1.113          // assertEquals("Result is 85 CZK");
   1.114 +        assertNotNull( a6 );
   1.115 +        assertEquals( 85.0, a6.getAmount().doubleValue() );
   1.116 +        assertEquals( CZK, a6.getCurrency() );
   1.117  
   1.118          // convert $8 to CZK using c at 2008-10-01 6:00 GMT:
   1.119 +        final ConversionResult r7 = c.convert( new TimeSpecificConversionRequest( new MonetaryAmount( 8, USD ), CZK, parseGmtDate( "2008-10-01 6:00" ) ) );
   1.120 +        final MonetaryAmount a7 = r7.getNetAmount();
   1.121          // assertEquals("Result is 136 CZK");
   1.122 +        assertNotNull( a7 );
   1.123 +        assertEquals( 136.0, a7.getAmount().doubleValue() );
   1.124 +        assertEquals( CZK, a7.getCurrency() );
   1.125  
   1.126          // convert 1003CZK to USD using c at 2008-10-01 6:00 GMT:
   1.127 +        final ConversionResult r8 = c.convert( new TimeSpecificConversionRequest( new MonetaryAmount( 1003, CZK ), USD, parseGmtDate( "2008-10-01 6:00" ) ) );
   1.128 +        final MonetaryAmount a8 = r8.getNetAmount();
   1.129          // assertEquals("Result is 59 USD");
   1.130 +        assertNotNull( a8 );
   1.131 +        assertEquals( 59.0, a8.getAmount().doubleValue() );
   1.132 +        assertEquals( USD, a8.getCurrency() );
   1.133  
   1.134          // convert 16CZK using c at 2008-10-02 9:00 GMT:
   1.135 +        final ConversionResult r9 = c.convert( new TimeSpecificConversionRequest( new MonetaryAmount( 16, CZK ), SKK, parseGmtDate( "2008-10-02 9:00" ) ) );
   1.136 +        final MonetaryAmount a9 = r9.getNetAmount();
   1.137          // assertEquals("Result is 20 SKK");
   1.138 +        assertNotNull( a9 );
   1.139 +        assertEquals( 20.0, a9.getAmount().doubleValue() );
   1.140 +        assertEquals( SKK, a9.getCurrency() );
   1.141  
   1.142          // convert 500SKK to CZK using c at 2008-10-02 9:00 GMT:
   1.143 +        final ConversionResult r10 = c.convert( new TimeSpecificConversionRequest( new MonetaryAmount( 500, SKK ), CZK, parseGmtDate( "2008-10-02 9:00" ) ) );
   1.144 +        final MonetaryAmount a10 = r10.getNetAmount();
   1.145          // assertEquals("Result is 400 CZK");
   1.146 +        assertNotNull( a10 );
   1.147 +        assertEquals( 400.0, a10.getAmount().doubleValue() );
   1.148 +        assertEquals( CZK, a10.getCurrency() );
   1.149  
   1.150          // convert 500SKK to CZK using c at 2008-10-01 6:00 GMT:
   1.151 +        final ConversionResult r11 = c.convert( new TimeSpecificConversionRequest( new MonetaryAmount( 500, SKK ), CZK, parseGmtDate( "2008-10-01 6:00" ) ) );
   1.152 +        final MonetaryAmount a11 = r11.getNetAmount();
   1.153          // cannot convert as no rate is applicable to current date
   1.154 +        assertNull( a11 );
   1.155      }
   1.156  
   1.157      /** Create convertor that understands two currencies, CZK and
   1.158 @@ -104,29 +166,39 @@
   1.159       * @return prepared convertor ready for converting SKK to CZK and CZK to SKK
   1.160       */
   1.161      public static Convertor createSKKtoCZK2() {
   1.162 -        return null;
   1.163 +        final TableConvertor convertor = new TableConvertor();
   1.164 +        final MonetaryAmount amountInSKK = new MonetaryAmount( 100, SKK );
   1.165 +        final MonetaryAmount amountInCZK = new MonetaryAmount( 90, CZK );
   1.166 +        convertor.putIntoTable( new ConversionRate( amountInSKK, amountInCZK ) );
   1.167 +        convertor.putIntoTable( new ConversionRate( amountInCZK, amountInSKK ) );
   1.168 +        return new ContractImposingDelegatingConvertor( convertor ).test();
   1.169      }
   1.170  
   1.171      public void testDateConvetorWithTwoDifferentRates() throws Exception {
   1.172 -        if (Boolean.getBoolean("ignore.failing")) {
   1.173 -            // implement me! then delete this if statement
   1.174 -            return;
   1.175 -        }
   1.176 +        final Date d1 = parseGmtDate( "2008-10-01 0:00" );
   1.177 +        final Date d2 = parseGmtDate( "2008-10-02 0:00" );
   1.178 +        final Date d3 = parseGmtDate( "2008-10-03 0:00" );
   1.179  
   1.180 -        Date d1 = null; // 2008-10-01 0:00 GMT
   1.181 -        Date d2 = null; // 2008-10-02 0:00 GMT
   1.182 -        Date d3 = null; // 2008-10-03 0:00 GMT
   1.183 -
   1.184 -        Convertor c = Task2Test.merge(
   1.185 +        final Convertor c = Task2Test.merge(
   1.186              limitTo(createSKKtoCZK2(), d1, d2),
   1.187              limitTo(Task1Test.createSKKtoCZK(), d2, d3)
   1.188          );
   1.189  
   1.190          // convert 500SKK to CZK using c at 2008-10-02 9:00 GMT:
   1.191 +        final ConversionResult r1 = c.convert( new TimeSpecificConversionRequest( new MonetaryAmount( 500, SKK ), CZK, parseGmtDate( "2008-10-02 9:00" ) ) );
   1.192 +        final MonetaryAmount a1 = r1.getNetAmount();
   1.193          // assertEquals("Result is 400 CZK");
   1.194 +        assertNotNull( a1 );
   1.195 +        assertEquals( 400.0, a1.getAmount().doubleValue() );
   1.196 +        assertEquals( CZK, a1.getCurrency() );
   1.197  
   1.198          // convert 500SKK to CZK using c at 2008-10-01 6:00 GMT:
   1.199 +        final ConversionResult r2 = c.convert( new TimeSpecificConversionRequest( new MonetaryAmount( 500, SKK ), CZK, parseGmtDate( "2008-10-01 6:00" ) ) );
   1.200 +        final MonetaryAmount a2 = r2.getNetAmount();
   1.201          // assertEquals("Result is 450 CZK");
   1.202 +        assertNotNull( a2 );
   1.203 +        assertEquals( 450.0, a2.getAmount().doubleValue() );
   1.204 +        assertEquals( CZK, a2.getCurrency() );        
   1.205      }
   1.206  
   1.207  }