@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 } }
@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); }
@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 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); }
@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(); } }
@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); } }
/** * Default constructor. */ public GenericConversionService() { FormattingConversionServiceFactoryBean factoryBean = new FormattingConversionServiceFactoryBean(); factoryBean.afterPropertiesSet(); this.delegate = factoryBean.getObject(); }
/** * Default constructor. */ public GenericConversionService() { FormattingConversionServiceFactoryBean factoryBean = new FormattingConversionServiceFactoryBean(); factoryBean.afterPropertiesSet(); this.delegate = factoryBean.getObject(); }
converters.add(new StringToTimestampConverter()); factory.setConverters(converters); factory.afterPropertiesSet(); FormattingConversionService conv = factory.getObject(); conversionService = conv;