Search in sources :

Example 1 with ConfigurableConversionService

use of org.springframework.core.convert.support.ConfigurableConversionService in project spring-boot by spring-projects.

the class EnvironmentConverterTests method convertedEnvironmentHasSameConversionService.

@Test
void convertedEnvironmentHasSameConversionService() {
    AbstractEnvironment originalEnvironment = new MockEnvironment();
    ConfigurableConversionService conversionService = mock(ConfigurableConversionService.class);
    originalEnvironment.setConversionService(conversionService);
    StandardEnvironment convertedEnvironment = this.environmentConverter.convertEnvironmentIfNecessary(originalEnvironment, StandardEnvironment.class);
    assertThat(convertedEnvironment.getConversionService()).isEqualTo(conversionService);
}
Also used : MockEnvironment(org.springframework.mock.env.MockEnvironment) AbstractEnvironment(org.springframework.core.env.AbstractEnvironment) ConfigurableConversionService(org.springframework.core.convert.support.ConfigurableConversionService) StandardEnvironment(org.springframework.core.env.StandardEnvironment) Test(org.junit.jupiter.api.Test)

Example 2 with ConfigurableConversionService

use of org.springframework.core.convert.support.ConfigurableConversionService in project spring-boot by spring-projects.

the class ConfigTreePropertySourceTests method getPropertyViaEnvironmentSupportsConversion.

@Test
void getPropertyViaEnvironmentSupportsConversion() throws Exception {
    StandardEnvironment environment = new StandardEnvironment();
    ConversionService conversionService = ApplicationConversionService.getSharedInstance();
    environment.setConversionService((ConfigurableConversionService) conversionService);
    environment.getPropertySources().addFirst(getFlatPropertySource());
    assertThat(environment.getProperty("a")).isEqualTo("A");
    assertThat(environment.getProperty("b")).isEqualTo("B");
    assertThat(environment.getProperty("c", InputStreamSource.class).getInputStream()).hasContent("C");
    assertThat(environment.getProperty("c", byte[].class)).contains('C');
    assertThat(environment.getProperty("one", Integer.class)).isEqualTo(1);
}
Also used : InputStreamSource(org.springframework.core.io.InputStreamSource) ConfigurableConversionService(org.springframework.core.convert.support.ConfigurableConversionService) ApplicationConversionService(org.springframework.boot.convert.ApplicationConversionService) ConversionService(org.springframework.core.convert.ConversionService) StandardEnvironment(org.springframework.core.env.StandardEnvironment) Test(org.junit.jupiter.api.Test)

Example 3 with ConfigurableConversionService

use of org.springframework.core.convert.support.ConfigurableConversionService in project spring-boot by spring-projects.

the class LogbackLoggingSystemTests method setup.

@BeforeEach
void setup() {
    System.getProperties().remove(LoggingSystemProperties.CONSOLE_LOG_CHARSET);
    System.getProperties().remove(LoggingSystemProperties.FILE_LOG_CHARSET);
    this.systemPropertyNames = new HashSet<>(System.getProperties().keySet());
    this.loggingSystem.cleanUp();
    this.logger = ((LoggerContext) StaticLoggerBinder.getSingleton().getLoggerFactory()).getLogger(getClass());
    this.environment = new MockEnvironment();
    ConversionService conversionService = ApplicationConversionService.getSharedInstance();
    this.environment.setConversionService((ConfigurableConversionService) conversionService);
    this.initializationContext = new LoggingInitializationContext(this.environment);
}
Also used : LoggingInitializationContext(org.springframework.boot.logging.LoggingInitializationContext) MockEnvironment(org.springframework.mock.env.MockEnvironment) ConfigurableConversionService(org.springframework.core.convert.support.ConfigurableConversionService) ApplicationConversionService(org.springframework.boot.convert.ApplicationConversionService) ConversionService(org.springframework.core.convert.ConversionService) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 4 with ConfigurableConversionService

use of org.springframework.core.convert.support.ConfigurableConversionService in project spring-data-commons by spring-projects.

the class CustomConversionsUnitTests method registersConverterFromConverterAware.

// DATACMNS-1034
@Test
public void registersConverterFromConverterAware() {
    ConverterAware converters = ConverterBuilder.reading(Left.class, Right.class, left -> new Right()).andWriting(right -> new Left());
    CustomConversions conversions = new CustomConversions(StoreConversions.NONE, Collections.singletonList(converters));
    assertThat(conversions.hasCustomWriteTarget(Right.class)).isTrue();
    assertThat(conversions.hasCustomReadTarget(Left.class, Right.class)).isTrue();
    ConfigurableConversionService conversionService = new GenericConversionService();
    conversions.registerConvertersIn(conversionService);
    assertThat(conversionService.canConvert(Left.class, Right.class)).isTrue();
    assertThat(conversionService.canConvert(Right.class, Left.class)).isTrue();
}
Also used : Converter(org.springframework.core.convert.converter.Converter) Arrays(java.util.Arrays) Format(java.text.Format) ConfigurableConversionService(org.springframework.core.convert.support.ConfigurableConversionService) Date(java.util.Date) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) DateTime(org.joda.time.DateTime) ConverterFactory(org.springframework.core.convert.converter.ConverterFactory) StoreConversions(org.springframework.data.convert.CustomConversions.StoreConversions) Matchers(org.hamcrest.Matchers) SimpleDateFormat(java.text.SimpleDateFormat) Test(org.junit.Test) GenericConversionService(org.springframework.core.convert.support.GenericConversionService) Assert.assertThat(org.junit.Assert.assertThat) Locale(java.util.Locale) ProxyFactory(org.springframework.aop.framework.ProxyFactory) LocalDateTime(org.threeten.bp.LocalDateTime) Collections(java.util.Collections) ConverterAware(org.springframework.data.convert.ConverterBuilder.ConverterAware) DateFormat(java.text.DateFormat) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) ConverterAware(org.springframework.data.convert.ConverterBuilder.ConverterAware) ConfigurableConversionService(org.springframework.core.convert.support.ConfigurableConversionService) GenericConversionService(org.springframework.core.convert.support.GenericConversionService) Test(org.junit.Test)

Example 5 with ConfigurableConversionService

use of org.springframework.core.convert.support.ConfigurableConversionService in project spring-framework by spring-projects.

the class AbstractPropertyResolver method getConversionService.

@Override
public ConfigurableConversionService getConversionService() {
    // Need to provide an independent DefaultConversionService, not the
    // shared DefaultConversionService used by PropertySourcesPropertyResolver.
    ConfigurableConversionService cs = this.conversionService;
    if (cs == null) {
        synchronized (this) {
            cs = this.conversionService;
            if (cs == null) {
                cs = new DefaultConversionService();
                this.conversionService = cs;
            }
        }
    }
    return cs;
}
Also used : ConfigurableConversionService(org.springframework.core.convert.support.ConfigurableConversionService) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService)

Aggregations

ConfigurableConversionService (org.springframework.core.convert.support.ConfigurableConversionService)7 DefaultConversionService (org.springframework.core.convert.support.DefaultConversionService)4 Test (org.junit.jupiter.api.Test)3 ConversionService (org.springframework.core.convert.ConversionService)3 ApplicationConversionService (org.springframework.boot.convert.ApplicationConversionService)2 StandardEnvironment (org.springframework.core.env.StandardEnvironment)2 MockEnvironment (org.springframework.mock.env.MockEnvironment)2 DateFormat (java.text.DateFormat)1 Format (java.text.Format)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 Date (java.util.Date)1 Locale (java.util.Locale)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Matchers (org.hamcrest.Matchers)1 DateTime (org.joda.time.DateTime)1 Assert.assertThat (org.junit.Assert.assertThat)1 Test (org.junit.Test)1 BeforeEach (org.junit.jupiter.api.BeforeEach)1