@ -20,6 +20,7 @@ import static org.hamcrest.CoreMatchers.equalTo;
@@ -20,6 +20,7 @@ import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.is ;
import static org.hamcrest.CoreMatchers.nullValue ;
import static org.junit.Assert.assertThat ;
import static org.junit.Assert.assertTrue ;
import static org.junit.Assert.fail ;
import java.util.HashMap ;
@ -28,6 +29,7 @@ import java.util.Properties;
@@ -28,6 +29,7 @@ import java.util.Properties;
import org.junit.Before ;
import org.junit.Test ;
import org.springframework.core.convert.ConversionException ;
import org.springframework.mock.env.MockPropertySource ;
/ * *
@ -238,4 +240,70 @@ public class PropertySourcesPropertyResolverTests {
@@ -238,4 +240,70 @@ public class PropertySourcesPropertyResolverTests {
new PropertySourcesPropertyResolver ( new MutablePropertySources ( ) ) . resolveRequiredPlaceholders ( null ) ;
}
@Test
public void getPropertyAsClass ( ) throws ClassNotFoundException , LinkageError {
MutablePropertySources propertySources = new MutablePropertySources ( ) ;
propertySources . addFirst ( new MockPropertySource ( ) . withProperty ( "some.class" , SpecificType . class . getName ( ) ) ) ;
PropertyResolver resolver = new PropertySourcesPropertyResolver ( propertySources ) ;
assertTrue ( resolver . getPropertyAsClass ( "some.class" , SomeType . class ) . equals ( SpecificType . class ) ) ;
}
@Test
public void getPropertyAsClass_withInterfaceAsTarget ( ) throws ClassNotFoundException , LinkageError {
MutablePropertySources propertySources = new MutablePropertySources ( ) ;
propertySources . addFirst ( new MockPropertySource ( ) . withProperty ( "some.class" , SomeType . class . getName ( ) ) ) ;
PropertyResolver resolver = new PropertySourcesPropertyResolver ( propertySources ) ;
assertTrue ( resolver . getPropertyAsClass ( "some.class" , SomeType . class ) . equals ( SomeType . class ) ) ;
}
@Test ( expected = ConversionException . class )
public void getPropertyAsClass_withMismatchedTypeForValue ( ) {
MutablePropertySources propertySources = new MutablePropertySources ( ) ;
propertySources . addFirst ( new MockPropertySource ( ) . withProperty ( "some.class" , "java.lang.String" ) ) ;
PropertyResolver resolver = new PropertySourcesPropertyResolver ( propertySources ) ;
resolver . getPropertyAsClass ( "some.class" , SomeType . class ) ;
}
@Test ( expected = ConversionException . class )
public void getPropertyAsClass_withNonExistentClassForValue ( ) {
MutablePropertySources propertySources = new MutablePropertySources ( ) ;
propertySources . addFirst ( new MockPropertySource ( ) . withProperty ( "some.class" , "some.bogus.Class" ) ) ;
PropertyResolver resolver = new PropertySourcesPropertyResolver ( propertySources ) ;
resolver . getPropertyAsClass ( "some.class" , SomeType . class ) ;
}
@Test
public void getPropertyAsClass_withObjectForValue ( ) {
MutablePropertySources propertySources = new MutablePropertySources ( ) ;
propertySources . addFirst ( new MockPropertySource ( ) . withProperty ( "some.class" , new SpecificType ( ) ) ) ;
PropertyResolver resolver = new PropertySourcesPropertyResolver ( propertySources ) ;
assertTrue ( resolver . getPropertyAsClass ( "some.class" , SomeType . class ) . equals ( SpecificType . class ) ) ;
}
@Test ( expected = ConversionException . class )
public void getPropertyAsClass_withMismatchedObjectForValue ( ) {
MutablePropertySources propertySources = new MutablePropertySources ( ) ;
propertySources . addFirst ( new MockPropertySource ( ) . withProperty ( "some.class" , new Integer ( 42 ) ) ) ;
PropertyResolver resolver = new PropertySourcesPropertyResolver ( propertySources ) ;
resolver . getPropertyAsClass ( "some.class" , SomeType . class ) ;
}
@Test
public void getPropertyAsClass_withRealClassForValue ( ) {
MutablePropertySources propertySources = new MutablePropertySources ( ) ;
propertySources . addFirst ( new MockPropertySource ( ) . withProperty ( "some.class" , SpecificType . class ) ) ;
PropertyResolver resolver = new PropertySourcesPropertyResolver ( propertySources ) ;
assertTrue ( resolver . getPropertyAsClass ( "some.class" , SomeType . class ) . equals ( SpecificType . class ) ) ;
}
@Test ( expected = ConversionException . class )
public void getPropertyAsClass_withMismatchedRealClassForValue ( ) {
MutablePropertySources propertySources = new MutablePropertySources ( ) ;
propertySources . addFirst ( new MockPropertySource ( ) . withProperty ( "some.class" , Integer . class ) ) ;
PropertyResolver resolver = new PropertySourcesPropertyResolver ( propertySources ) ;
resolver . getPropertyAsClass ( "some.class" , SomeType . class ) ;
}
static interface SomeType { }
static class SpecificType implements SomeType { }
}