@Before public void setup() throws Exception { context = createPageContext(); FormattingConversionServiceFactoryBean factory = new FormattingConversionServiceFactoryBean(); factory.afterPropertiesSet(); context.getRequest().setAttribute("org.springframework.core.convert.ConversionService", factory.getObject()); context.getRequest().setAttribute("bean", new Bean()); tag = new EvalTag(); tag.setPageContext(context); }
@Override public void afterPropertiesSet() { this.conversionService = new DefaultFormattingConversionService(this.embeddedValueResolver, this.registerDefaultFormatters); ConversionServiceFactory.registerConverters(this.converters, this.conversionService); registerFormatters(this.conversionService); }
@Test public void testFormatterRegistrar() throws Exception { FormattingConversionServiceFactoryBean factory = new FormattingConversionServiceFactoryBean(); Set<FormatterRegistrar> registrars = new HashSet<>(); registrars.add(new TestFormatterRegistrar()); factory.setFormatterRegistrars(registrars); factory.afterPropertiesSet(); FormattingConversionService fcs = factory.getObject(); TestBean testBean = fcs.convert("5", TestBean.class); assertEquals(5, testBean.getSpecialInt()); assertEquals("5", fcs.convert(testBean, String.class)); }
@Test public void testInvalidFormatter() throws Exception { FormattingConversionServiceFactoryBean factory = new FormattingConversionServiceFactoryBean(); Set<Object> formatters = new HashSet<>(); formatters.add(new Object()); factory.setFormatters(formatters); try { factory.afterPropertiesSet(); fail("Expected formatter to be rejected"); } catch (IllegalArgumentException ex) { // expected } }
@Test public void testDefaultFormattersOff() throws Exception { FormattingConversionServiceFactoryBean factory = new FormattingConversionServiceFactoryBean(); factory.setRegisterDefaultFormatters(false); factory.afterPropertiesSet(); FormattingConversionService fcs = factory.getObject(); TypeDescriptor descriptor = new TypeDescriptor(TestBean.class.getDeclaredField("pattern")); try { fcs.convert("15,00", TypeDescriptor.valueOf(String.class), descriptor); fail("This format should not be parseable"); } catch (ConversionFailedException ex) { assertTrue(ex.getCause() instanceof NumberFormatException); } }
@Test public void testCustomFormatter() throws Exception { FormattingConversionServiceFactoryBean factory = new FormattingConversionServiceFactoryBean(); Set<Object> formatters = new HashSet<>(); formatters.add(new TestBeanFormatter()); formatters.add(new SpecialIntAnnotationFormatterFactory()); factory.setFormatters(formatters); factory.afterPropertiesSet(); FormattingConversionService fcs = factory.getObject(); TestBean testBean = fcs.convert("5", TestBean.class); assertEquals(5, testBean.getSpecialInt()); assertEquals("5", fcs.convert(testBean, String.class)); TypeDescriptor descriptor = new TypeDescriptor(TestBean.class.getDeclaredField("specialInt")); Object value = fcs.convert(":5", TypeDescriptor.valueOf(String.class), descriptor); assertEquals(5, value); value = fcs.convert(5, descriptor, TypeDescriptor.valueOf(String.class)); assertEquals(":5", value); }
FormattingConversionServiceFactoryBean factory = new FormattingConversionServiceFactoryBean(); Set<Object> converters = new HashSet<>(); converters.add(new NumberToDateConverter()); converters.add(new StringToDateConverter()); converters.add(new StringToTimestampConverter()); factory.setConverters(converters); factory.afterPropertiesSet(); FormattingConversionService conv = factory.getObject(); conversionService = conv;
@Bean FormattingConversionServiceFactoryBean conversionService() { FormattingConversionServiceFactoryBean bean = new FormattingConversionServiceFactoryBean(); Set<FormatterRegistrar> set = new HashSet<FormatterRegistrar>(); JodaTimeFormatterRegistrar registrar = new JodaTimeFormatterRegistrar(); registrar.setUseIsoFormat(true); set.add(registrar); bean.setFormatterRegistrars(set); return bean; } }
@Bean("conversionService") public FactoryBean<FormattingConversionService> conversionService() { FormattingConversionServiceFactoryBean factory = new FormattingConversionServiceFactoryBean(); Set<Object> converters = new HashSet<>(); converters.add(new NumberToDateConverter()); converters.add(new NumberToTimestampConverter()); converters.add(new StringToDateConverter()); converters.add(new StringToTimestampConverter()); factory.setConverters(converters); return factory; }
FormattingConversionServiceFactoryBean fcs = new FormattingConversionServiceFactoryBean(); Set<Formatter> fmts = new HashSet<>(); fmts.add(this.webMvcConfig.dateFormatter()); fmts.add(this.webMvcConfig.employeeFormatter()); fcs.setFormatters(fmts); return fcs; } }
@Bean public MethodInvokingFactoryBean methodInvokingFactoryBean(@Qualifier("conversionService") FormattingConversionServiceFactoryBean conversionService){ MethodInvokingFactoryBean methodInvokingFactoryBean = new MethodInvokingFactoryBean(); methodInvokingFactoryBean.setStaticMethod("cn.jeeweb.core.query.utils.QueryableConvertUtils.setConversionService"); methodInvokingFactoryBean.setArguments(conversionService.getObject()); return methodInvokingFactoryBean; }
@Bean(name = "conversionService") public FormattingConversionServiceFactoryBean conversionService(){ return new FormattingConversionServiceFactoryBean(); }
FormattingConversionService cs = conversionService.getObject(); Mockito.reset(testDAO);
@Test public void testDefaultFormattersOn() throws Exception { FormattingConversionServiceFactoryBean factory = new FormattingConversionServiceFactoryBean(); factory.afterPropertiesSet(); FormattingConversionService fcs = factory.getObject(); TypeDescriptor descriptor = new TypeDescriptor(TestBean.class.getDeclaredField("pattern")); LocaleContextHolder.setLocale(Locale.GERMAN); try { Object value = fcs.convert("15,00", TypeDescriptor.valueOf(String.class), descriptor); assertEquals(15.0, value); value = fcs.convert(15.0, descriptor, TypeDescriptor.valueOf(String.class)); assertEquals("15", value); } finally { LocaleContextHolder.resetLocaleContext(); } }
@Override public void afterPropertiesSet() { this.conversionService = new DefaultFormattingConversionService(this.embeddedValueResolver, this.registerDefaultFormatters); ConversionServiceFactory.registerConverters(this.converters, this.conversionService); registerFormatters(this.conversionService); }
/** * Default constructor. */ public GenericConversionService() { FormattingConversionServiceFactoryBean factoryBean = new FormattingConversionServiceFactoryBean(); factoryBean.afterPropertiesSet(); this.delegate = factoryBean.getObject(); }
@Override public void afterPropertiesSet() { this.conversionService = new DefaultFormattingConversionService(this.embeddedValueResolver, this.registerDefaultFormatters); ConversionServiceFactory.registerConverters(this.converters, this.conversionService); registerFormatters(this.conversionService); }
/** * Default constructor. */ public GenericConversionService() { FormattingConversionServiceFactoryBean factoryBean = new FormattingConversionServiceFactoryBean(); factoryBean.afterPropertiesSet(); this.delegate = factoryBean.getObject(); }