Search in sources :

Example 1 with DerivedTestBean

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

the class ConcurrencyThrottleInterceptorTests method testSerializable.

@Test
public void testSerializable() throws Exception {
    DerivedTestBean tb = new DerivedTestBean();
    ProxyFactory proxyFactory = new ProxyFactory();
    proxyFactory.setInterfaces(ITestBean.class);
    ConcurrencyThrottleInterceptor cti = new ConcurrencyThrottleInterceptor();
    proxyFactory.addAdvice(cti);
    proxyFactory.setTarget(tb);
    ITestBean proxy = (ITestBean) proxyFactory.getProxy();
    proxy.getAge();
    ITestBean serializedProxy = SerializationTestUtils.serializeAndDeserialize(proxy);
    Advised advised = (Advised) serializedProxy;
    ConcurrencyThrottleInterceptor serializedCti = (ConcurrencyThrottleInterceptor) advised.getAdvisors()[0].getAdvice();
    assertThat(serializedCti.getConcurrencyLimit()).isEqualTo(cti.getConcurrencyLimit());
    serializedProxy.getAge();
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory) Advised(cn.taketoday.aop.framework.Advised) DerivedTestBean(cn.taketoday.beans.testfixture.beans.DerivedTestBean) ConcurrencyThrottleInterceptor(cn.taketoday.aop.interceptor.ConcurrencyThrottleInterceptor) Test(org.junit.jupiter.api.Test)

Example 2 with DerivedTestBean

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

the class DataBinderTests method directBindingToEmptyIndexedFieldWithRegisteredGenericEditor.

@Test
void directBindingToEmptyIndexedFieldWithRegisteredGenericEditor() {
    IndexedTestBean tb = new IndexedTestBean();
    DataBinder binder = new DataBinder(tb, "tb");
    binder.registerCustomEditor(TestBean.class, "map", new PropertyEditorSupport() {

        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            DerivedTestBean tb = new DerivedTestBean();
            tb.setName("array" + text);
            setValue(tb);
        }

        @Override
        public String getAsText() {
            return ((TestBean) getValue()).getName();
        }
    });
    Errors errors = binder.getBindingResult();
    errors.rejectValue("map[key0]", "NOT_NULL", "should not be null");
    assertThat(errors.getFieldErrorCount("map[key0]")).isEqualTo(1);
    assertThat(errors.getFieldError("map[key0]").getCode()).isEqualTo("NOT_NULL");
    assertThat(errors.getFieldError("map[key0]").getCodes()[0]).isEqualTo("NOT_NULL.tb.map[key0]");
    assertThat(errors.getFieldError("map[key0]").getCodes()[1]).isEqualTo("NOT_NULL.tb.map");
    assertThat(errors.getFieldError("map[key0]").getCodes()[2]).isEqualTo("NOT_NULL.map[key0]");
    assertThat(errors.getFieldError("map[key0]").getCodes()[3]).isEqualTo("NOT_NULL.map");
    // This next code is only generated because of the registered editor, using the
    // registered type of the editor as guess for the content type of the collection.
    assertThat(errors.getFieldError("map[key0]").getCodes()[4]).isEqualTo("NOT_NULL.cn.taketoday.beans.testfixture.beans.TestBean");
    assertThat(errors.getFieldError("map[key0]").getCodes()[5]).isEqualTo("NOT_NULL");
}
Also used : IndexedTestBean(cn.taketoday.beans.testfixture.beans.IndexedTestBean) DerivedTestBean(cn.taketoday.beans.testfixture.beans.DerivedTestBean) PropertyEditorSupport(java.beans.PropertyEditorSupport) Test(org.junit.jupiter.api.Test)

Example 3 with DerivedTestBean

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

the class BeanUtilsTests method copyPropertiesWithDifferentTypes2.

@Test
void copyPropertiesWithDifferentTypes2() throws Exception {
    TestBean tb = new TestBean();
    tb.setName("rod");
    tb.setAge(32);
    tb.setTouchy("touchy");
    DerivedTestBean tb2 = new DerivedTestBean();
    assertThat(tb2.getName() == null).as("Name empty").isTrue();
    assertThat(tb2.getAge() == 0).as("Age empty").isTrue();
    assertThat(tb2.getTouchy() == null).as("Touchy empty").isTrue();
    BeanUtils.copyProperties(tb, tb2);
    assertThat(tb2.getName().equals(tb.getName())).as("Name copied").isTrue();
    assertThat(tb2.getAge() == tb.getAge()).as("Age copied").isTrue();
    assertThat(tb2.getTouchy().equals(tb.getTouchy())).as("Touchy copied").isTrue();
}
Also used : ITestBean(cn.taketoday.beans.testfixture.beans.ITestBean) DerivedTestBean(cn.taketoday.beans.testfixture.beans.DerivedTestBean) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) DerivedTestBean(cn.taketoday.beans.testfixture.beans.DerivedTestBean) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 4 with DerivedTestBean

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

the class SimpleTransactionScopeTests method getFromScope.

@Test
@SuppressWarnings("resource")
public void getFromScope() throws Exception {
    GenericApplicationContext context = new GenericApplicationContext();
    context.getBeanFactory().registerScope("tx", new SimpleTransactionScope());
    RootBeanDefinition bd1 = new RootBeanDefinition();
    bd1.setBeanClass(TestBean.class);
    bd1.setScope("tx");
    bd1.setPrimary(true);
    context.registerBeanDefinition("txScopedObject1", bd1);
    RootBeanDefinition bd2 = new RootBeanDefinition();
    bd2.setBeanClass(DerivedTestBean.class);
    bd2.setScope("tx");
    context.registerBeanDefinition("txScopedObject2", bd2);
    context.refresh();
    assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> context.getBean(TestBean.class)).withCauseInstanceOf(IllegalStateException.class);
    assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> context.getBean(DerivedTestBean.class)).withCauseInstanceOf(IllegalStateException.class);
    TestBean bean1 = null;
    DerivedTestBean bean2 = null;
    DerivedTestBean bean2a = null;
    DerivedTestBean bean2b = null;
    TransactionSynchronizationManager.initSynchronization();
    try {
        bean1 = context.getBean(TestBean.class);
        assertThat(context.getBean(TestBean.class)).isSameAs(bean1);
        bean2 = context.getBean(DerivedTestBean.class);
        assertThat(context.getBean(DerivedTestBean.class)).isSameAs(bean2);
        context.getBeanFactory().destroyScopedBean("txScopedObject2");
        assertThat(TransactionSynchronizationManager.hasResource("txScopedObject2")).isFalse();
        assertThat(bean2.wasDestroyed()).isTrue();
        bean2a = context.getBean(DerivedTestBean.class);
        assertThat(context.getBean(DerivedTestBean.class)).isSameAs(bean2a);
        assertThat(bean2a).isNotSameAs(bean2);
        context.getBeanFactory().getRegisteredScope("tx").remove("txScopedObject2");
        assertThat(TransactionSynchronizationManager.hasResource("txScopedObject2")).isFalse();
        assertThat(bean2a.wasDestroyed()).isFalse();
        bean2b = context.getBean(DerivedTestBean.class);
        assertThat(context.getBean(DerivedTestBean.class)).isSameAs(bean2b);
        assertThat(bean2b).isNotSameAs(bean2);
        assertThat(bean2b).isNotSameAs(bean2a);
    } finally {
        TransactionSynchronizationUtils.triggerAfterCompletion(TransactionSynchronization.STATUS_COMMITTED);
        TransactionSynchronizationManager.clearSynchronization();
    }
    assertThat(bean2a.wasDestroyed()).isFalse();
    assertThat(bean2b.wasDestroyed()).isTrue();
    assertThat(TransactionSynchronizationManager.getResourceMap().isEmpty()).isTrue();
    assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> context.getBean(TestBean.class)).withCauseInstanceOf(IllegalStateException.class);
    assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() -> context.getBean(DerivedTestBean.class)).withCauseInstanceOf(IllegalStateException.class);
}
Also used : GenericApplicationContext(cn.taketoday.context.support.GenericApplicationContext) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) DerivedTestBean(cn.taketoday.beans.testfixture.beans.DerivedTestBean) RootBeanDefinition(cn.taketoday.beans.factory.support.RootBeanDefinition) DerivedTestBean(cn.taketoday.beans.testfixture.beans.DerivedTestBean) Test(org.junit.jupiter.api.Test)

Example 5 with DerivedTestBean

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

the class SimpleTransactionScopeTests method getWithTransactionManager.

@Test
public void getWithTransactionManager() throws Exception {
    try (GenericApplicationContext context = new GenericApplicationContext()) {
        context.getBeanFactory().registerScope("tx", new SimpleTransactionScope());
        RootBeanDefinition bd1 = new RootBeanDefinition();
        bd1.setBeanClass(TestBean.class);
        bd1.setScope("tx");
        bd1.setPrimary(true);
        context.registerBeanDefinition("txScopedObject1", bd1);
        RootBeanDefinition bd2 = new RootBeanDefinition();
        bd2.setBeanClass(DerivedTestBean.class);
        bd2.setScope("tx");
        context.registerBeanDefinition("txScopedObject2", bd2);
        context.refresh();
        CallCountingTransactionManager tm = new CallCountingTransactionManager();
        TransactionTemplate tt = new TransactionTemplate(tm);
        Set<DerivedTestBean> finallyDestroy = new HashSet<>();
        tt.execute(status -> {
            TestBean bean1 = context.getBean(TestBean.class);
            assertThat(context.getBean(TestBean.class)).isSameAs(bean1);
            DerivedTestBean bean2 = context.getBean(DerivedTestBean.class);
            assertThat(context.getBean(DerivedTestBean.class)).isSameAs(bean2);
            context.getBeanFactory().destroyScopedBean("txScopedObject2");
            assertThat(TransactionSynchronizationManager.hasResource("txScopedObject2")).isFalse();
            assertThat(bean2.wasDestroyed()).isTrue();
            DerivedTestBean bean2a = context.getBean(DerivedTestBean.class);
            assertThat(context.getBean(DerivedTestBean.class)).isSameAs(bean2a);
            assertThat(bean2a).isNotSameAs(bean2);
            context.getBeanFactory().getRegisteredScope("tx").remove("txScopedObject2");
            assertThat(TransactionSynchronizationManager.hasResource("txScopedObject2")).isFalse();
            assertThat(bean2a.wasDestroyed()).isFalse();
            DerivedTestBean bean2b = context.getBean(DerivedTestBean.class);
            finallyDestroy.add(bean2b);
            assertThat(context.getBean(DerivedTestBean.class)).isSameAs(bean2b);
            assertThat(bean2b).isNotSameAs(bean2);
            assertThat(bean2b).isNotSameAs(bean2a);
            Set<DerivedTestBean> immediatelyDestroy = new HashSet<>();
            TransactionTemplate tt2 = new TransactionTemplate(tm);
            tt2.setPropagationBehavior(TransactionTemplate.PROPAGATION_REQUIRED);
            tt2.execute(status2 -> {
                DerivedTestBean bean2c = context.getBean(DerivedTestBean.class);
                immediatelyDestroy.add(bean2c);
                assertThat(context.getBean(DerivedTestBean.class)).isSameAs(bean2c);
                assertThat(bean2c).isNotSameAs(bean2);
                assertThat(bean2c).isNotSameAs(bean2a);
                assertThat(bean2c).isNotSameAs(bean2b);
                return null;
            });
            assertThat(immediatelyDestroy.iterator().next().wasDestroyed()).isTrue();
            assertThat(bean2b.wasDestroyed()).isFalse();
            return null;
        });
        assertThat(finallyDestroy.iterator().next().wasDestroyed()).isTrue();
    }
}
Also used : GenericApplicationContext(cn.taketoday.context.support.GenericApplicationContext) TestBean(cn.taketoday.beans.testfixture.beans.TestBean) DerivedTestBean(cn.taketoday.beans.testfixture.beans.DerivedTestBean) CallCountingTransactionManager(cn.taketoday.transaction.CallCountingTransactionManager) RootBeanDefinition(cn.taketoday.beans.factory.support.RootBeanDefinition) DerivedTestBean(cn.taketoday.beans.testfixture.beans.DerivedTestBean) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Aggregations

DerivedTestBean (cn.taketoday.beans.testfixture.beans.DerivedTestBean)40 Test (org.junit.jupiter.api.Test)40 IndexedTestBean (cn.taketoday.beans.testfixture.beans.IndexedTestBean)16 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)14 StandardBeanFactory (cn.taketoday.beans.factory.support.StandardBeanFactory)12 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)12 PropertyEditorSupport (java.beans.PropertyEditorSupport)12 PropertyValues (cn.taketoday.beans.PropertyValues)6 Advised (cn.taketoday.aop.framework.Advised)4 ProxyFactory (cn.taketoday.aop.framework.ProxyFactory)4 RootBeanDefinition (cn.taketoday.beans.factory.support.RootBeanDefinition)4 GenericApplicationContext (cn.taketoday.context.support.GenericApplicationContext)4 ResourceTestBean (cn.taketoday.tests.sample.beans.ResourceTestBean)4 DataBinder (cn.taketoday.validation.DataBinder)4 Errors (cn.taketoday.validation.Errors)4 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4 ConcurrencyThrottleInterceptor (cn.taketoday.aop.interceptor.ConcurrencyThrottleInterceptor)2 DefaultSingletonBeanRegistry (cn.taketoday.beans.factory.support.DefaultSingletonBeanRegistry)2 CallCountingTransactionManager (cn.taketoday.transaction.CallCountingTransactionManager)2 InputStream (java.io.InputStream)2