private void registerFormatters(FormattingConversionService conversionService) { if (this.formatters != null) { for (Object formatter : this.formatters) { if (formatter instanceof Formatter<?>) { conversionService.addFormatter((Formatter<?>) formatter); } else if (formatter instanceof AnnotationFormatterFactory<?>) { conversionService.addFormatterForFieldAnnotation((AnnotationFormatterFactory<?>) formatter); } else { throw new IllegalArgumentException( "Custom formatters must be implementations of Formatter or AnnotationFormatterFactory"); } } } if (this.formatterRegistrars != null) { for (FormatterRegistrar registrar : this.formatterRegistrars) { registrar.registerFormatters(conversionService); } } }
private void registerFormatters(FormattingConversionService conversionService) { if (this.formatters != null) { for (Object formatter : this.formatters) { if (formatter instanceof Formatter<?>) { conversionService.addFormatter((Formatter<?>) formatter); } else if (formatter instanceof AnnotationFormatterFactory<?>) { conversionService.addFormatterForFieldAnnotation((AnnotationFormatterFactory<?>) formatter); } else { throw new IllegalArgumentException( "Custom formatters must be implementations of Formatter or AnnotationFormatterFactory"); } } } if (this.formatterRegistrars != null) { for (FormatterRegistrar registrar : this.formatterRegistrars) { registrar.registerFormatters(conversionService); } } }
@Test public void formatFieldForAnnotationWithDirectFieldAccess() throws Exception { formattingService.addFormatterForFieldAnnotation(new JodaDateTimeFormatAnnotationFormatterFactory()); doTestFormatFieldForAnnotation(Model.class, true); }
@Test public void formatFieldForAnnotation() throws Exception { formattingService.addFormatterForFieldAnnotation(new JodaDateTimeFormatAnnotationFormatterFactory()); doTestFormatFieldForAnnotation(Model.class, false); }
@Test @SuppressWarnings("resource") public void formatFieldForAnnotationWithPlaceholders() throws Exception { GenericApplicationContext context = new GenericApplicationContext(); PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); Properties props = new Properties(); props.setProperty("dateStyle", "S-"); props.setProperty("datePattern", "M-d-yy"); ppc.setProperties(props); context.getBeanFactory().registerSingleton("ppc", ppc); context.refresh(); context.getBeanFactory().initializeBean(formattingService, "formattingService"); formattingService.addFormatterForFieldAnnotation(new JodaDateTimeFormatAnnotationFormatterFactory()); doTestFormatFieldForAnnotation(ModelWithPlaceholders.class, false); }
@Test public void formatFieldForAnnotationWithSubclassAsFieldType() throws Exception { formattingService.addFormatterForFieldAnnotation(new JodaDateTimeFormatAnnotationFormatterFactory() { @Override public Printer<?> getPrinter(org.springframework.format.annotation.DateTimeFormat annotation, Class<?> fieldType) { assertEquals(MyDate.class, fieldType); return super.getPrinter(annotation, fieldType); } }); formattingService.addConverter(new Converter<MyDate, Long>() { @Override public Long convert(MyDate source) { return source.getTime(); } }); formattingService.addConverter(new Converter<MyDate, Date>() { @Override public Date convert(MyDate source) { return source; } }); formattingService.convert(new MyDate(), new TypeDescriptor(ModelWithSubclassField.class.getField("date")), TypeDescriptor.valueOf(String.class)); }
@Before public void setUp() { DefaultConversionService.addDefaultConverters(conversionService); conversionService.setEmbeddedValueResolver(new StringValueResolver() { @Override public String resolveStringValue(String strVal) { if ("${pattern}".equals(strVal)) { return "#,##.00"; } else { return strVal; } } }); conversionService.addFormatterForFieldType(Number.class, new NumberStyleFormatter()); conversionService.addFormatterForFieldAnnotation(new NumberFormatAnnotationFormatterFactory()); LocaleContextHolder.setLocale(Locale.US); binder = new DataBinder(new TestBean()); binder.setConversionService(conversionService); }
FormattingConversionService conversionService = new FormattingConversionService(); conversionService.addFormatterForFieldAnnotation(new ZonedDateTimeAnnotationFormatterFactory()); this.mockMvc = MockMvcBuilders.standaloneSetup(indexResource).setConversionService(conversionService).build();
private void registerFormatters(FormattingConversionService conversionService) { if (this.formatters != null) { for (Object formatter : this.formatters) { if (formatter instanceof Formatter<?>) { conversionService.addFormatter((Formatter<?>) formatter); } else if (formatter instanceof AnnotationFormatterFactory<?>) { conversionService.addFormatterForFieldAnnotation((AnnotationFormatterFactory<?>) formatter); } else { throw new IllegalArgumentException( "Custom formatters must be implementations of Formatter or AnnotationFormatterFactory"); } } } if (this.formatterRegistrars != null) { for (FormatterRegistrar registrar : this.formatterRegistrars) { registrar.registerFormatters(conversionService); } } }