use of cn.taketoday.core.env.PropertiesPropertySource in project today-infrastructure by TAKETODAY.
the class StandardEnvironmentTests method loadProperties.
@Test
void loadProperties() throws IOException {
ConfigurableEnvironment environment = new StandardEnvironment();
PropertySources propertySources = environment.getPropertySources();
Properties properties = PropertiesUtils.loadProperties(ResourceUtils.getResource("classpath:info.properties"));
propertySources.addLast(new PropertiesPropertySource("info", properties));
assert "https://taketoday.cn".equals(environment.getProperty("site.host"));
}
use of cn.taketoday.core.env.PropertiesPropertySource in project today-framework by TAKETODAY.
the class StandardEnvironmentTests method loadProperties.
@Test
void loadProperties() throws IOException {
ConfigurableEnvironment environment = new StandardEnvironment();
PropertySources propertySources = environment.getPropertySources();
Properties properties = PropertiesUtils.loadProperties(ResourceUtils.getResource("classpath:info.properties"));
propertySources.addLast(new PropertiesPropertySource("info", properties));
assert "https://taketoday.cn".equals(environment.getProperty("site.host"));
}
use of cn.taketoday.core.env.PropertiesPropertySource in project today-framework by TAKETODAY.
the class ContextUtilsTest method testResolveParameter.
@Test
@Disabled
@SuppressWarnings("unchecked")
public void testResolveParameter() throws Exception {
try (StandardApplicationContext applicationContext = new StandardApplicationContext("cn.taketoday.util")) {
final BeanFactory beanFactory = applicationContext.getBeanFactory();
final ConfigurableEnvironment environment = applicationContext.getEnvironment();
// placeHolder
PropertySources propertySources = environment.getPropertySources();
Properties properties = new Properties();
propertySources.addLast(new PropertiesPropertySource("properties", properties));
properties.setProperty("placeHolder", "12345");
final Constructor<Config>[] declaredConstructors = (Constructor<Config>[]) Config.class.getDeclaredConstructors();
Constructor<Config> constructor = null;
for (final Constructor<Config> declaredConstructor : declaredConstructors) {
if (declaredConstructor.getParameterCount() > 0) {
constructor = declaredConstructor;
}
}
// properties.list(System.err);
// System.err.println(properties.get("placeHolder"));
Object[] parameters = new DependencyInjector(beanFactory).resolveArguments(constructor);
Config newInstance = constructor.newInstance(parameters);
assert parameters.length == 14;
assert parameters[0] instanceof UserModel;
UserModel userModel = (UserModel) parameters[0];
assert userModel.getAge() == 21;
assert userModel.getUserId().equals("666");
assert userModel.getUserName().equals("TODAY");
assert parameters[1] instanceof Properties;
// collection > 5
final List<Object> objectList = applicationContext.getBeans(Object.class);
final int allSize = objectList.size();
final Object collectionParameter = parameters[5];
assert collectionParameter instanceof Collection;
final int size = ((Collection<?>) collectionParameter).size();
assert size == allSize;
final Object setParameter = parameters[6];
assert setParameter instanceof Set;
assert ((Set<?>) setParameter).size() == allSize;
final Object hashSetParameter = parameters[7];
assert hashSetParameter instanceof HashSet;
assert ((Set<?>) hashSetParameter).size() == allSize;
final Object listParameter = parameters[8];
assert listParameter instanceof List;
assert ((List<?>) listParameter).size() == allSize;
final Object arrayListParameter = parameters[9];
assert arrayListParameter instanceof ArrayList;
assert ((ArrayList<?>) arrayListParameter).size() == allSize;
final Object mapParameter = parameters[10];
assert mapParameter instanceof Map;
assert ((Map) mapParameter).size() == allSize;
final Object hashMapParameter = parameters[11];
assert hashMapParameter instanceof HashMap;
assert ((HashMap) hashMapParameter).size() == allSize;
}
}
use of cn.taketoday.core.env.PropertiesPropertySource in project today-infrastructure by TAKETODAY.
the class ContextUtilsTest method testResolveParameter.
@Test
@Disabled
@SuppressWarnings("unchecked")
public void testResolveParameter() throws Exception {
try (StandardApplicationContext applicationContext = new StandardApplicationContext("cn.taketoday.util")) {
final BeanFactory beanFactory = applicationContext.getBeanFactory();
final ConfigurableEnvironment environment = applicationContext.getEnvironment();
// placeHolder
PropertySources propertySources = environment.getPropertySources();
Properties properties = new Properties();
propertySources.addLast(new PropertiesPropertySource("properties", properties));
properties.setProperty("placeHolder", "12345");
final Constructor<Config>[] declaredConstructors = (Constructor<Config>[]) Config.class.getDeclaredConstructors();
Constructor<Config> constructor = null;
for (final Constructor<Config> declaredConstructor : declaredConstructors) {
if (declaredConstructor.getParameterCount() > 0) {
constructor = declaredConstructor;
}
}
// properties.list(System.err);
// System.err.println(properties.get("placeHolder"));
Object[] parameters = new DependencyInjector(beanFactory).resolveArguments(constructor);
Config newInstance = constructor.newInstance(parameters);
assert parameters.length == 14;
assert parameters[0] instanceof UserModel;
UserModel userModel = (UserModel) parameters[0];
assert userModel.getAge() == 21;
assert userModel.getUserId().equals("666");
assert userModel.getUserName().equals("TODAY");
assert parameters[1] instanceof Properties;
// collection > 5
final List<Object> objectList = applicationContext.getBeans(Object.class);
final int allSize = objectList.size();
final Object collectionParameter = parameters[5];
assert collectionParameter instanceof Collection;
final int size = ((Collection<?>) collectionParameter).size();
assert size == allSize;
final Object setParameter = parameters[6];
assert setParameter instanceof Set;
assert ((Set<?>) setParameter).size() == allSize;
final Object hashSetParameter = parameters[7];
assert hashSetParameter instanceof HashSet;
assert ((Set<?>) hashSetParameter).size() == allSize;
final Object listParameter = parameters[8];
assert listParameter instanceof List;
assert ((List<?>) listParameter).size() == allSize;
final Object arrayListParameter = parameters[9];
assert arrayListParameter instanceof ArrayList;
assert ((ArrayList<?>) arrayListParameter).size() == allSize;
final Object mapParameter = parameters[10];
assert mapParameter instanceof Map;
assert ((Map) mapParameter).size() == allSize;
final Object hashMapParameter = parameters[11];
assert hashMapParameter instanceof HashMap;
assert ((HashMap) hashMapParameter).size() == allSize;
}
}
Aggregations