Advertisement
tpeierls

Parsing dates in Guice TypeConverter

Jan 31st, 2012
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.47 KB | None | 0 0
  1. package com.example.dateprop;
  2.  
  3. import javax.inject.Inject;
  4. import javax.inject.Named;
  5.  
  6. import com.google.inject.*;
  7. import com.google.inject.matcher.*;
  8. import com.google.inject.name.*;
  9. import com.google.inject.spi.*;
  10.  
  11. import java.text.*;
  12. import java.util.*;
  13.  
  14. import org.junit.*;
  15. import static org.junit.Assert.*;
  16.  
  17. public class DatePropTest {
  18.  
  19.     /** The name of the property we want to bind as a constant Date. */
  20.     static final String DATE_PROP_KEY = "date.prop.key";
  21.  
  22.     @Test public void writeDateAndBindConstant() throws ParseException {
  23.  
  24.         // The DateFormat we will use for converting Dates to and from strings:
  25.  
  26.         final DateFormat fmt = new SimpleDateFormat("MM-dd-yyyy HH:mm:ss");
  27.  
  28.         // Matches java.util.Date only:
  29.  
  30.         final Matcher<TypeLiteral<?>> dateMatcher = new AbstractMatcher<TypeLiteral<?>>() {
  31.             public boolean matches(TypeLiteral<?> type) {
  32.                 return type.getRawType().equals(Date.class);
  33.             }
  34.         };
  35.  
  36.         // Parses strings as dates using the given DateFormat:
  37.  
  38.         final TypeConverter dateConverter = new TypeConverter() {
  39.             public Object convert(String stringValue, TypeLiteral<?> toType) {
  40.                 try {
  41.                     return fmt.parse(stringValue);
  42.                 } catch (ParseException ex) {
  43.                     throw new ProvisionException("can't parse date", ex);
  44.                 }
  45.             }
  46.         };
  47.  
  48.         // Create our test date.
  49.  
  50.         final Date date = fmt.parse("03-11-2012 13:05:23");
  51.  
  52.         // Store it in a Properties as a string using the given DateFormat.
  53.  
  54.         final Properties props = new Properties();
  55.         props.setProperty(DATE_PROP_KEY, fmt.format(date));
  56.  
  57.         // Bind the properties using the date converter on date constants,
  58.         // and inject an object that uses the named constant date.
  59.  
  60.         Dated dated = Guice.createInjector(new AbstractModule() {
  61.             protected void configure() {
  62.                 Names.bindProperties(binder(), props);
  63.                 convertToTypes(dateMatcher, dateConverter);
  64.             }
  65.         }).getInstance(Dated.class);
  66.  
  67.         // Assert that the injected date constant is the same as the
  68.         // one we put in the properties.
  69.  
  70.         assertEquals(date, dated.date);
  71.     }
  72.  
  73.     public static class Dated {
  74.         @Inject Dated(@Named(DATE_PROP_KEY) Date date) {
  75.             this.date = date;
  76.         }
  77.         final Date date;
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement