use of cn.taketoday.beans.factory.config.TypedStringValue in project today-infrastructure by TAKETODAY.
the class StandardDependenciesBeanPostProcessorTests method testGenericsBasedFieldInjectionWithSimpleMatchAndMockito.
@Test
public void testGenericsBasedFieldInjectionWithSimpleMatchAndMockito() {
RootBeanDefinition bd = new RootBeanDefinition(RepositoryFieldInjectionBeanWithSimpleMatch.class);
bd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
bf.registerBeanDefinition("annotatedBean", bd);
RootBeanDefinition rbd = new RootBeanDefinition();
rbd.setBeanClassName(Mockito.class.getName());
rbd.setFactoryMethodName("mock");
// TypedStringValue used to be equivalent to an XML-defined argument String
rbd.getConstructorArgumentValues().addGenericArgumentValue(new TypedStringValue(Repository.class.getName()));
bf.registerBeanDefinition("repo", rbd);
RepositoryFieldInjectionBeanWithSimpleMatch bean = (RepositoryFieldInjectionBeanWithSimpleMatch) bf.getBean("annotatedBean");
Repository<?> repo = bf.getBean("repo", Repository.class);
assertThat(bean.repository).isSameAs(repo);
assertThat(bean.stringRepository).isSameAs(repo);
assertThat(bean.repositoryArray.length).isSameAs(1);
assertThat(bean.stringRepositoryArray.length).isSameAs(1);
assertThat(bean.repositoryArray[0]).isSameAs(repo);
assertThat(bean.stringRepositoryArray[0]).isSameAs(repo);
assertThat(bean.repositoryList.size()).isSameAs(1);
assertThat(bean.stringRepositoryList.size()).isSameAs(1);
assertThat(bean.repositoryList.get(0)).isSameAs(repo);
assertThat(bean.stringRepositoryList.get(0)).isSameAs(repo);
assertThat(bean.repositoryMap.size()).isSameAs(1);
assertThat(bean.stringRepositoryMap.size()).isSameAs(1);
assertThat(bean.repositoryMap.get("repo")).isSameAs(repo);
assertThat(bean.stringRepositoryMap.get("repo")).isSameAs(repo);
}
use of cn.taketoday.beans.factory.config.TypedStringValue in project today-infrastructure by TAKETODAY.
the class DatabasePopulatorConfigUtils method createDatabasePopulator.
private static BeanDefinition createDatabasePopulator(Element element, List<Element> scripts, String execution) {
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(CompositeDatabasePopulator.class);
boolean ignoreFailedDrops = element.getAttribute("ignore-failures").equals("DROPS");
boolean continueOnError = element.getAttribute("ignore-failures").equals("ALL");
ManagedList<BeanMetadataElement> delegates = new ManagedList<>();
for (Element scriptElement : scripts) {
String executionAttr = scriptElement.getAttribute("execution");
if (!StringUtils.hasText(executionAttr)) {
executionAttr = "INIT";
}
if (!execution.equals(executionAttr)) {
continue;
}
BeanDefinitionBuilder delegate = BeanDefinitionBuilder.genericBeanDefinition(ResourceDatabasePopulator.class);
delegate.addPropertyValue("ignoreFailedDrops", ignoreFailedDrops);
delegate.addPropertyValue("continueOnError", continueOnError);
// Use a factory bean for the resources so they can be given an order if a pattern is used
BeanDefinitionBuilder resourcesFactory = BeanDefinitionBuilder.genericBeanDefinition(SortedResourcesFactoryBean.class);
resourcesFactory.addConstructorArgValue(new TypedStringValue(scriptElement.getAttribute("location")));
delegate.addPropertyValue("scripts", resourcesFactory.getBeanDefinition());
if (StringUtils.isNotEmpty(scriptElement.getAttribute("encoding"))) {
delegate.addPropertyValue("sqlScriptEncoding", new TypedStringValue(scriptElement.getAttribute("encoding")));
}
String separator = getSeparator(element, scriptElement);
if (separator != null) {
delegate.addPropertyValue("separator", new TypedStringValue(separator));
}
delegates.add(delegate.getBeanDefinition());
}
builder.addPropertyValue("populators", delegates);
return builder.getBeanDefinition();
}
use of cn.taketoday.beans.factory.config.TypedStringValue in project today-infrastructure by TAKETODAY.
the class EventPublicationTests method beanEventReceived.
@Test
public void beanEventReceived() throws Exception {
ComponentDefinition componentDefinition1 = this.eventListener.getComponentDefinition("testBean");
boolean condition3 = componentDefinition1 instanceof BeanComponentDefinition;
assertThat(condition3).isTrue();
assertThat(componentDefinition1.getBeanDefinitions().length).isEqualTo(1);
BeanDefinition beanDefinition1 = componentDefinition1.getBeanDefinitions()[0];
assertThat(beanDefinition1.getConstructorArgumentValues().getGenericArgumentValue(String.class).getValue()).isEqualTo(new TypedStringValue("Rob Harrop"));
assertThat(componentDefinition1.getBeanReferences().length).isEqualTo(1);
assertThat(componentDefinition1.getBeanReferences()[0].getBeanName()).isEqualTo("testBean2");
assertThat(componentDefinition1.getInnerBeanDefinitions().length).isEqualTo(1);
BeanDefinition innerBd1 = componentDefinition1.getInnerBeanDefinitions()[0];
assertThat(innerBd1.getConstructorArgumentValues().getGenericArgumentValue(String.class).getValue()).isEqualTo(new TypedStringValue("ACME"));
boolean condition2 = componentDefinition1.getSource() instanceof Element;
assertThat(condition2).isTrue();
ComponentDefinition componentDefinition2 = this.eventListener.getComponentDefinition("testBean2");
boolean condition1 = componentDefinition2 instanceof BeanComponentDefinition;
assertThat(condition1).isTrue();
assertThat(componentDefinition1.getBeanDefinitions().length).isEqualTo(1);
BeanDefinition beanDefinition2 = componentDefinition2.getBeanDefinitions()[0];
assertThat(beanDefinition2.getPropertyValues().getPropertyValue("name")).isEqualTo(new TypedStringValue("Juergen Hoeller"));
assertThat(componentDefinition2.getBeanReferences().length).isEqualTo(0);
assertThat(componentDefinition2.getInnerBeanDefinitions().length).isEqualTo(1);
BeanDefinition innerBd2 = componentDefinition2.getInnerBeanDefinitions()[0];
assertThat(innerBd2.getPropertyValues().getPropertyValue("name")).isEqualTo(new TypedStringValue("Eva Schallmeiner"));
boolean condition = componentDefinition2.getSource() instanceof Element;
assertThat(condition).isTrue();
}
use of cn.taketoday.beans.factory.config.TypedStringValue in project today-framework by TAKETODAY.
the class PropertyResourceConfigurerTests method doTestPropertyPlaceholderConfigurer.
private void doTestPropertyPlaceholderConfigurer(boolean parentChildSeparation) {
Map<String, String> singletonMap = Collections.singletonMap("myKey", "myValue");
if (parentChildSeparation) {
PropertyValues pvs1 = new PropertyValues();
pvs1.add("age", "${age}");
PropertyValues pvs2 = new PropertyValues();
pvs2.add("name", "name${var}${var}${");
pvs2.add("spouse", new RuntimeBeanReference("${ref}"));
pvs2.add("someMap", singletonMap);
RootBeanDefinition parent = new RootBeanDefinition(TestBean.class);
parent.setPropertyValues(pvs1);
ChildBeanDefinition bd = new ChildBeanDefinition("${parent}", pvs2);
factory.registerBeanDefinition("parent1", parent);
factory.registerBeanDefinition("tb1", bd);
} else {
PropertyValues pvs = new PropertyValues();
pvs.add("age", "${age}");
pvs.add("name", "name${var}${var}${");
pvs.add("spouse", new RuntimeBeanReference("${ref}"));
pvs.add("someMap", singletonMap);
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
bd.setPropertyValues(pvs);
factory.registerBeanDefinition("tb1", bd);
}
ConstructorArgumentValues cas = new ConstructorArgumentValues();
cas.addIndexedArgumentValue(1, "${age}");
cas.addGenericArgumentValue("${var}name${age}");
PropertyValues pvs = new PropertyValues();
pvs.add("stringArray", new String[] { "${os.name}", "${age}" });
List<Object> friends = ManagedList.of("na${age}me", new RuntimeBeanReference("${ref}"));
pvs.add("friends", friends);
Set<Object> someSet = ManagedSet.of("na${age}me", new RuntimeBeanReference("${ref}"), new TypedStringValue("${age}", Integer.class));
pvs.add("someSet", someSet);
Map<Object, Object> someMap = ManagedMap.ofEntries(Map.entry(new TypedStringValue("key${age}"), new TypedStringValue("${age}")), Map.entry(new TypedStringValue("key${age}ref"), new RuntimeBeanReference("${ref}")), Map.entry("key1", new RuntimeBeanReference("${ref}")), Map.entry("key2", "${age}name"));
PropertyValues innerPvs = new PropertyValues();
innerPvs.add("country", "${os.name}");
RootBeanDefinition innerBd = new RootBeanDefinition(TestBean.class);
innerBd.setPropertyValues(innerPvs);
someMap.put("key3", innerBd);
PropertyValues innerPvs2 = new PropertyValues(innerPvs);
someMap.put("${key4}", new BeanDefinitionHolder(new ChildBeanDefinition("tb1", innerPvs2), "child"));
pvs.add("someMap", someMap);
RootBeanDefinition bd = new RootBeanDefinition(TestBean.class, cas, pvs);
factory.registerBeanDefinition("tb2", bd);
PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
Properties props = new Properties();
props.setProperty("age", "98");
props.setProperty("var", "${m}var");
props.setProperty("ref", "tb2");
props.setProperty("m", "my");
props.setProperty("key4", "mykey4");
props.setProperty("parent", "parent1");
ppc.setProperties(props);
ppc.postProcessBeanFactory(factory);
TestBean tb1 = (TestBean) factory.getBean("tb1");
TestBean tb2 = (TestBean) factory.getBean("tb2");
assertThat(tb1.getAge()).isEqualTo(98);
assertThat(tb2.getAge()).isEqualTo(98);
assertThat(tb1.getName()).isEqualTo("namemyvarmyvar${");
assertThat(tb2.getName()).isEqualTo("myvarname98");
assertThat(tb1.getSpouse()).isEqualTo(tb2);
assertThat(tb1.getSomeMap().size()).isEqualTo(1);
assertThat(tb1.getSomeMap().get("myKey")).isEqualTo("myValue");
assertThat(tb2.getStringArray().length).isEqualTo(2);
assertThat(tb2.getStringArray()[0]).isEqualTo(System.getProperty("os.name"));
assertThat(tb2.getStringArray()[1]).isEqualTo("98");
assertThat(tb2.getFriends().size()).isEqualTo(2);
assertThat(tb2.getFriends().iterator().next()).isEqualTo("na98me");
assertThat(tb2.getFriends().toArray()[1]).isEqualTo(tb2);
assertThat(tb2.getSomeSet().size()).isEqualTo(3);
assertThat(tb2.getSomeSet().contains("na98me")).isTrue();
assertThat(tb2.getSomeSet().contains(tb2)).isTrue();
assertThat(tb2.getSomeSet().contains(98)).isTrue();
assertThat(tb2.getSomeMap().size()).isEqualTo(6);
assertThat(tb2.getSomeMap().get("key98")).isEqualTo("98");
assertThat(tb2.getSomeMap().get("key98ref")).isEqualTo(tb2);
assertThat(tb2.getSomeMap().get("key1")).isEqualTo(tb2);
assertThat(tb2.getSomeMap().get("key2")).isEqualTo("98name");
TestBean inner1 = (TestBean) tb2.getSomeMap().get("key3");
TestBean inner2 = (TestBean) tb2.getSomeMap().get("mykey4");
assertThat(inner1.getAge()).isEqualTo(0);
assertThat(inner1.getName()).isNull();
assertThat(inner1.getCountry()).isEqualTo(System.getProperty("os.name"));
assertThat(inner2.getAge()).isEqualTo(98);
assertThat(inner2.getName()).isEqualTo("namemyvarmyvar${");
assertThat(inner2.getCountry()).isEqualTo(System.getProperty("os.name"));
}
use of cn.taketoday.beans.factory.config.TypedStringValue in project today-framework by TAKETODAY.
the class CacheAdviceParser method parseDefinitionSource.
private RootBeanDefinition parseDefinitionSource(Element definition, ParserContext parserContext) {
Props prop = new Props(definition);
// add cacheable first
ManagedMap<TypedStringValue, Collection<CacheOperation>> cacheOpMap = new ManagedMap<>();
cacheOpMap.setSource(parserContext.extractSource(definition));
List<Element> cacheableCacheMethods = DomUtils.getChildElementsByTagName(definition, CACHEABLE_ELEMENT);
for (Element opElement : cacheableCacheMethods) {
String name = prop.merge(opElement, parserContext.getReaderContext());
TypedStringValue nameHolder = new TypedStringValue(name);
nameHolder.setSource(parserContext.extractSource(opElement));
CacheableOperation.Builder builder = prop.merge(opElement, parserContext.getReaderContext(), new CacheableOperation.Builder());
builder.setUnless(getAttributeValue(opElement, "unless", ""));
builder.setSync(Boolean.parseBoolean(getAttributeValue(opElement, "sync", "false")));
Collection<CacheOperation> col = cacheOpMap.computeIfAbsent(nameHolder, k -> new ArrayList<>(2));
col.add(builder.build());
}
List<Element> evictCacheMethods = DomUtils.getChildElementsByTagName(definition, CACHE_EVICT_ELEMENT);
for (Element opElement : evictCacheMethods) {
String name = prop.merge(opElement, parserContext.getReaderContext());
TypedStringValue nameHolder = new TypedStringValue(name);
nameHolder.setSource(parserContext.extractSource(opElement));
CacheEvictOperation.Builder builder = prop.merge(opElement, parserContext.getReaderContext(), new CacheEvictOperation.Builder());
String wide = opElement.getAttribute("all-entries");
if (StringUtils.hasText(wide)) {
builder.setCacheWide(Boolean.parseBoolean(wide.trim()));
}
String after = opElement.getAttribute("before-invocation");
if (StringUtils.hasText(after)) {
builder.setBeforeInvocation(Boolean.parseBoolean(after.trim()));
}
Collection<CacheOperation> col = cacheOpMap.computeIfAbsent(nameHolder, k -> new ArrayList<>(2));
col.add(builder.build());
}
List<Element> putCacheMethods = DomUtils.getChildElementsByTagName(definition, CACHE_PUT_ELEMENT);
for (Element opElement : putCacheMethods) {
String name = prop.merge(opElement, parserContext.getReaderContext());
TypedStringValue nameHolder = new TypedStringValue(name);
nameHolder.setSource(parserContext.extractSource(opElement));
CachePutOperation.Builder builder = prop.merge(opElement, parserContext.getReaderContext(), new CachePutOperation.Builder());
builder.setUnless(getAttributeValue(opElement, "unless", ""));
Collection<CacheOperation> col = cacheOpMap.computeIfAbsent(nameHolder, k -> new ArrayList<>(2));
col.add(builder.build());
}
RootBeanDefinition attributeSourceDefinition = new RootBeanDefinition(NameMatchCacheOperationSource.class);
attributeSourceDefinition.setSource(parserContext.extractSource(definition));
attributeSourceDefinition.getPropertyValues().add("nameMap", cacheOpMap);
return attributeSourceDefinition;
}
Aggregations