Search in sources :

Example 1 with PropertyValue

use of cn.taketoday.beans.PropertyValue in project today-infrastructure by TAKETODAY.

the class MixinProxyTargetClassTrueConfig method testAspectsAndAdvisorAreAppliedEvenIfComingFromParentFactory.

@Test
public void testAspectsAndAdvisorAreAppliedEvenIfComingFromParentFactory() {
    ClassPathXmlApplicationContext ac = newContext("aspectsPlusAdvisor.xml");
    GenericApplicationContext childAc = new GenericApplicationContext(ac);
    // Create a child factory with a bean that should be woven
    RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
    bd.getPropertyValues().add(new PropertyValue("name", "Adrian")).add(new PropertyValue("age", 34));
    childAc.registerBeanDefinition("adrian2", bd);
    // Register the advisor auto proxy creator with subclass
    childAc.registerBeanDefinition(AnnotationAwareAspectJAutoProxyCreator.class.getName(), new RootBeanDefinition(AnnotationAwareAspectJAutoProxyCreator.class));
    childAc.refresh();
    ITestBean beanFromChildContextThatShouldBeWeaved = (ITestBean) childAc.getBean("adrian2");
    // testAspectsAndAdvisorAreApplied(childAc, (ITestBean) ac.getBean("adrian"));
    doTestAspectsAndAdvisorAreApplied(childAc, beanFromChildContextThatShouldBeWeaved);
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) GenericApplicationContext(cn.taketoday.context.support.GenericApplicationContext) ClassPathXmlApplicationContext(cn.taketoday.context.support.ClassPathXmlApplicationContext) AnnotationAwareAspectJAutoProxyCreator(cn.taketoday.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator) RootBeanDefinition(cn.taketoday.beans.factory.support.RootBeanDefinition) PropertyValue(cn.taketoday.beans.PropertyValue) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with PropertyValue

use of cn.taketoday.beans.PropertyValue in project today-infrastructure by TAKETODAY.

the class ViewResolverTests method beanNameViewResolver.

@Test
public void beanNameViewResolver() {
    PropertyValues pvs1 = new PropertyValues();
    pvs1.add(new PropertyValue("url", "/example1.jsp"));
    this.wac.registerSingleton("example1", InternalResourceView.class, pvs1);
    PropertyValues pvs2 = new PropertyValues();
    pvs2.add(new PropertyValue("url", "/example2.jsp"));
    this.wac.registerSingleton("example2", JstlView.class, pvs2);
    BeanNameViewResolver vr = new BeanNameViewResolver();
    vr.setApplicationContext(this.wac);
    this.wac.refresh();
    View view = vr.resolveViewName("example1", Locale.getDefault());
    assertThat(view.getClass()).as("Correct view class").isEqualTo(InternalResourceView.class);
    assertThat(((InternalResourceView) view).getUrl()).as("Correct URL").isEqualTo("/example1.jsp");
    view = vr.resolveViewName("example2", Locale.getDefault());
    assertThat(view).isInstanceOf(JstlView.class);
    assertThat(((JstlView) view).getUrl()).as("Correct URL").isEqualTo("/example2.jsp");
}
Also used : PropertyValues(cn.taketoday.beans.PropertyValues) PropertyValue(cn.taketoday.beans.PropertyValue) JstlView(cn.taketoday.web.servlet.view.JstlView) InternalResourceView(cn.taketoday.web.servlet.view.InternalResourceView) Test(org.junit.jupiter.api.Test)

Example 3 with PropertyValue

use of cn.taketoday.beans.PropertyValue in project today-infrastructure by TAKETODAY.

the class CustomEditorTests method testComplexObject.

@Test
void testComplexObject() {
    TestBean tb = new TestBean();
    String newName = "Rod";
    String tbString = "Kerry_34";
    BeanWrapper bw = new BeanWrapperImpl(tb);
    bw.registerCustomEditor(ITestBean.class, new TestBeanEditor());
    PropertyValues pvs = new PropertyValues();
    pvs.add(new PropertyValue("age", 55));
    pvs.add(new PropertyValue("name", newName));
    pvs.add(new PropertyValue("touchy", "valid"));
    pvs.add(new PropertyValue("spouse", tbString));
    bw.setPropertyValues(pvs);
    assertThat(tb.getSpouse()).as("spouse is non-null").isNotNull();
    assertThat(tb.getSpouse().getName().equals("Kerry") && tb.getSpouse().getAge() == 34).as("spouse name is Kerry and age is 34").isTrue();
}
Also used : BeanWrapper(cn.taketoday.beans.BeanWrapper) PropertyValues(cn.taketoday.beans.PropertyValues) BeanWrapperImpl(cn.taketoday.beans.BeanWrapperImpl) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) IndexedTestBean(cn.taketoday.beans.testfixture.beans.IndexedTestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) BooleanTestBean(cn.taketoday.beans.BooleanTestBean) NumberTestBean(cn.taketoday.beans.NumberTestBean) PropertyValue(cn.taketoday.beans.PropertyValue) Test(org.junit.jupiter.api.Test)

Example 4 with PropertyValue

use of cn.taketoday.beans.PropertyValue in project today-infrastructure by TAKETODAY.

the class CustomEditorTests method testComplexObjectWithOldValueAccess.

@Test
void testComplexObjectWithOldValueAccess() {
    TestBean tb = new TestBean();
    String newName = "Rod";
    String tbString = "Kerry_34";
    BeanWrapper bw = new BeanWrapperImpl(tb);
    bw.setExtractOldValueForEditor(true);
    bw.registerCustomEditor(ITestBean.class, new OldValueAccessingTestBeanEditor());
    PropertyValues pvs = new PropertyValues();
    pvs.add(new PropertyValue("age", 55));
    pvs.add(new PropertyValue("name", newName));
    pvs.add(new PropertyValue("touchy", "valid"));
    pvs.add(new PropertyValue("spouse", tbString));
    bw.setPropertyValues(pvs);
    assertThat(tb.getSpouse()).as("spouse is non-null").isNotNull();
    assertThat(tb.getSpouse().getName().equals("Kerry") && tb.getSpouse().getAge() == 34).as("spouse name is Kerry and age is 34").isTrue();
    ITestBean spouse = tb.getSpouse();
    bw.setPropertyValues(pvs);
    assertThat(tb.getSpouse()).as("Should have remained same object").isSameAs(spouse);
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) BeanWrapper(cn.taketoday.beans.BeanWrapper) PropertyValues(cn.taketoday.beans.PropertyValues) BeanWrapperImpl(cn.taketoday.beans.BeanWrapperImpl) ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) IndexedTestBean(cn.taketoday.beans.testfixture.beans.IndexedTestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) BooleanTestBean(cn.taketoday.beans.BooleanTestBean) NumberTestBean(cn.taketoday.beans.NumberTestBean) PropertyValue(cn.taketoday.beans.PropertyValue) Test(org.junit.jupiter.api.Test)

Example 5 with PropertyValue

use of cn.taketoday.beans.PropertyValue in project today-infrastructure by TAKETODAY.

the class AnnotationJmxAttributeSource method getManagedResource.

@Override
@Nullable
public cn.taketoday.jmx.export.metadata.ManagedResource getManagedResource(Class<?> beanClass) throws InvalidMetadataException {
    MergedAnnotation<ManagedResource> ann = MergedAnnotations.from(beanClass, SearchStrategy.TYPE_HIERARCHY).get(ManagedResource.class).withNonMergedAttributes();
    if (!ann.isPresent()) {
        return null;
    }
    Class<?> declaringClass = (Class<?>) ann.getSource();
    Class<?> target = (declaringClass != null && !declaringClass.isInterface() ? declaringClass : beanClass);
    if (!Modifier.isPublic(target.getModifiers())) {
        throw new InvalidMetadataException("@ManagedResource class '" + target.getName() + "' must be public");
    }
    cn.taketoday.jmx.export.metadata.ManagedResource bean = new cn.taketoday.jmx.export.metadata.ManagedResource();
    Map<String, Object> map = ann.asMap();
    List<PropertyValue> list = new ArrayList<>(map.size());
    map.forEach((attrName, attrValue) -> {
        if (!"value".equals(attrName)) {
            Object value = attrValue;
            if (this.embeddedValueResolver != null && value instanceof String) {
                value = this.embeddedValueResolver.resolveStringValue((String) value);
            }
            list.add(new PropertyValue(attrName, value));
        }
    });
    BeanWrapper.forBeanPropertyAccess(bean).setPropertyValues(new PropertyValues(list));
    return bean;
}
Also used : PropertyValues(cn.taketoday.beans.PropertyValues) InvalidMetadataException(cn.taketoday.jmx.export.metadata.InvalidMetadataException) ArrayList(java.util.ArrayList) PropertyValue(cn.taketoday.beans.PropertyValue) Nullable(cn.taketoday.lang.Nullable)

Aggregations

PropertyValue (cn.taketoday.beans.PropertyValue)46 PropertyValues (cn.taketoday.beans.PropertyValues)28 Test (org.junit.jupiter.api.Test)22 ConfigurablePropertyAccessor (cn.taketoday.beans.ConfigurablePropertyAccessor)6 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)6 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)6 MultiValueMap (cn.taketoday.core.MultiValueMap)6 List (java.util.List)6 Map (java.util.Map)6 BeanMetadata (cn.taketoday.beans.BeanMetadata)4 BeanWrapper (cn.taketoday.beans.BeanWrapper)4 BeanWrapperImpl (cn.taketoday.beans.BeanWrapperImpl)4 BooleanTestBean (cn.taketoday.beans.BooleanTestBean)4 NumberTestBean (cn.taketoday.beans.NumberTestBean)4 BeanDefinition (cn.taketoday.beans.factory.config.BeanDefinition)4 IndexedTestBean (cn.taketoday.beans.testfixture.beans.IndexedTestBean)4 Nullable (cn.taketoday.lang.Nullable)4 RequestContextDataBinder (cn.taketoday.web.bind.RequestContextDataBinder)4 ArrayList (java.util.ArrayList)4 AnnotationAwareAspectJAutoProxyCreator (cn.taketoday.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator)2