Search in sources :

Example 6 with DerivedTestBean

use of org.springframework.tests.sample.beans.DerivedTestBean in project spring-framework by spring-projects.

the class BeanUtilsTests method testCopyPropertiesWithDifferentTypes2.

@Test
public void testCopyPropertiesWithDifferentTypes2() throws Exception {
    TestBean tb = new TestBean();
    tb.setName("rod");
    tb.setAge(32);
    tb.setTouchy("touchy");
    DerivedTestBean tb2 = new DerivedTestBean();
    assertTrue("Name empty", tb2.getName() == null);
    assertTrue("Age empty", tb2.getAge() == 0);
    assertTrue("Touchy empty", tb2.getTouchy() == null);
    BeanUtils.copyProperties(tb, tb2);
    assertTrue("Name copied", tb2.getName().equals(tb.getName()));
    assertTrue("Age copied", tb2.getAge() == tb.getAge());
    assertTrue("Touchy copied", tb2.getTouchy().equals(tb.getTouchy()));
}
Also used : DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) Test(org.junit.Test)

Example 7 with DerivedTestBean

use of org.springframework.tests.sample.beans.DerivedTestBean in project spring-framework by spring-projects.

the class DataBinderTests method testDirectBindingToEmptyIndexedFieldWithRegisteredGenericEditor.

@Test
public void testDirectBindingToEmptyIndexedFieldWithRegisteredGenericEditor() {
    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");
    assertEquals(1, errors.getFieldErrorCount("map[key0]"));
    assertEquals("NOT_NULL", errors.getFieldError("map[key0]").getCode());
    assertEquals("NOT_NULL.tb.map[key0]", errors.getFieldError("map[key0]").getCodes()[0]);
    assertEquals("NOT_NULL.tb.map", errors.getFieldError("map[key0]").getCodes()[1]);
    assertEquals("NOT_NULL.map[key0]", errors.getFieldError("map[key0]").getCodes()[2]);
    assertEquals("NOT_NULL.map", errors.getFieldError("map[key0]").getCodes()[3]);
    // 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.
    assertEquals("NOT_NULL.org.springframework.tests.sample.beans.TestBean", errors.getFieldError("map[key0]").getCodes()[4]);
    assertEquals("NOT_NULL", errors.getFieldError("map[key0]").getCodes()[5]);
}
Also used : IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) PropertyEditorSupport(java.beans.PropertyEditorSupport) Test(org.junit.Test)

Example 8 with DerivedTestBean

use of org.springframework.tests.sample.beans.DerivedTestBean in project spring-framework by spring-projects.

the class DataBinderTests method testDirectBindingToEmptyIndexedFieldWithRegisteredSpecificEditor.

@Test
public void testDirectBindingToEmptyIndexedFieldWithRegisteredSpecificEditor() {
    IndexedTestBean tb = new IndexedTestBean();
    DataBinder binder = new DataBinder(tb, "tb");
    binder.registerCustomEditor(TestBean.class, "map[key0]", 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");
    assertEquals(1, errors.getFieldErrorCount("map[key0]"));
    assertEquals("NOT_NULL", errors.getFieldError("map[key0]").getCode());
    assertEquals("NOT_NULL.tb.map[key0]", errors.getFieldError("map[key0]").getCodes()[0]);
    assertEquals("NOT_NULL.tb.map", errors.getFieldError("map[key0]").getCodes()[1]);
    assertEquals("NOT_NULL.map[key0]", errors.getFieldError("map[key0]").getCodes()[2]);
    assertEquals("NOT_NULL.map", errors.getFieldError("map[key0]").getCodes()[3]);
    // 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.
    assertEquals("NOT_NULL.org.springframework.tests.sample.beans.TestBean", errors.getFieldError("map[key0]").getCodes()[4]);
    assertEquals("NOT_NULL", errors.getFieldError("map[key0]").getCodes()[5]);
}
Also used : IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) PropertyEditorSupport(java.beans.PropertyEditorSupport) Test(org.junit.Test)

Example 9 with DerivedTestBean

use of org.springframework.tests.sample.beans.DerivedTestBean in project spring-framework by spring-projects.

the class SimpleTransactionScopeTests method getWithTransactionManager.

@Test
public void getWithTransactionManager() throws Exception {
    try (GenericApplicationContext context = new GenericApplicationContext()) {
        context.getBeanFactory().registerScope("tx", new SimpleTransactionScope());
        GenericBeanDefinition bd1 = new GenericBeanDefinition();
        bd1.setBeanClass(TestBean.class);
        bd1.setScope("tx");
        bd1.setPrimary(true);
        context.registerBeanDefinition("txScopedObject1", bd1);
        GenericBeanDefinition bd2 = new GenericBeanDefinition();
        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);
            assertSame(bean1, context.getBean(TestBean.class));
            DerivedTestBean bean2 = context.getBean(DerivedTestBean.class);
            assertSame(bean2, context.getBean(DerivedTestBean.class));
            context.getBeanFactory().destroyScopedBean("txScopedObject2");
            assertFalse(TransactionSynchronizationManager.hasResource("txScopedObject2"));
            assertTrue(bean2.wasDestroyed());
            DerivedTestBean bean2a = context.getBean(DerivedTestBean.class);
            assertSame(bean2a, context.getBean(DerivedTestBean.class));
            assertNotSame(bean2, bean2a);
            context.getBeanFactory().getRegisteredScope("tx").remove("txScopedObject2");
            assertFalse(TransactionSynchronizationManager.hasResource("txScopedObject2"));
            assertFalse(bean2a.wasDestroyed());
            DerivedTestBean bean2b = context.getBean(DerivedTestBean.class);
            finallyDestroy.add(bean2b);
            assertSame(bean2b, context.getBean(DerivedTestBean.class));
            assertNotSame(bean2, bean2b);
            assertNotSame(bean2a, bean2b);
            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);
                assertSame(bean2c, context.getBean(DerivedTestBean.class));
                assertNotSame(bean2, bean2c);
                assertNotSame(bean2a, bean2c);
                assertNotSame(bean2b, bean2c);
                return null;
            });
            assertTrue(immediatelyDestroy.iterator().next().wasDestroyed());
            assertFalse(bean2b.wasDestroyed());
            return null;
        });
        assertTrue(finallyDestroy.iterator().next().wasDestroyed());
    }
}
Also used : GenericBeanDefinition(org.springframework.beans.factory.support.GenericBeanDefinition) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) CallCountingTransactionManager(org.springframework.tests.transaction.CallCountingTransactionManager) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 10 with DerivedTestBean

use of org.springframework.tests.sample.beans.DerivedTestBean in project spring-framework by spring-projects.

the class SimpleTransactionScopeTests method getFromScope.

@Test
@SuppressWarnings("resource")
public void getFromScope() throws Exception {
    GenericApplicationContext context = new GenericApplicationContext();
    context.getBeanFactory().registerScope("tx", new SimpleTransactionScope());
    GenericBeanDefinition bd1 = new GenericBeanDefinition();
    bd1.setBeanClass(TestBean.class);
    bd1.setScope("tx");
    bd1.setPrimary(true);
    context.registerBeanDefinition("txScopedObject1", bd1);
    GenericBeanDefinition bd2 = new GenericBeanDefinition();
    bd2.setBeanClass(DerivedTestBean.class);
    bd2.setScope("tx");
    context.registerBeanDefinition("txScopedObject2", bd2);
    context.refresh();
    try {
        context.getBean(TestBean.class);
        fail("Should have thrown BeanCreationException");
    } catch (BeanCreationException ex) {
        // expected - no synchronization active
        assertTrue(ex.getCause() instanceof IllegalStateException);
    }
    try {
        context.getBean(DerivedTestBean.class);
        fail("Should have thrown BeanCreationException");
    } catch (BeanCreationException ex) {
        // expected - no synchronization active
        assertTrue(ex.getCause() instanceof IllegalStateException);
    }
    TestBean bean1 = null;
    DerivedTestBean bean2 = null;
    DerivedTestBean bean2a = null;
    DerivedTestBean bean2b = null;
    TransactionSynchronizationManager.initSynchronization();
    try {
        bean1 = context.getBean(TestBean.class);
        assertSame(bean1, context.getBean(TestBean.class));
        bean2 = context.getBean(DerivedTestBean.class);
        assertSame(bean2, context.getBean(DerivedTestBean.class));
        context.getBeanFactory().destroyScopedBean("txScopedObject2");
        assertFalse(TransactionSynchronizationManager.hasResource("txScopedObject2"));
        assertTrue(bean2.wasDestroyed());
        bean2a = context.getBean(DerivedTestBean.class);
        assertSame(bean2a, context.getBean(DerivedTestBean.class));
        assertNotSame(bean2, bean2a);
        context.getBeanFactory().getRegisteredScope("tx").remove("txScopedObject2");
        assertFalse(TransactionSynchronizationManager.hasResource("txScopedObject2"));
        assertFalse(bean2a.wasDestroyed());
        bean2b = context.getBean(DerivedTestBean.class);
        assertSame(bean2b, context.getBean(DerivedTestBean.class));
        assertNotSame(bean2, bean2b);
        assertNotSame(bean2a, bean2b);
    } finally {
        TransactionSynchronizationUtils.triggerAfterCompletion(TransactionSynchronization.STATUS_COMMITTED);
        TransactionSynchronizationManager.clearSynchronization();
    }
    assertFalse(bean2a.wasDestroyed());
    assertTrue(bean2b.wasDestroyed());
    assertTrue(TransactionSynchronizationManager.getResourceMap().isEmpty());
    try {
        context.getBean(TestBean.class);
        fail("Should have thrown IllegalStateException");
    } catch (BeanCreationException ex) {
        // expected - no synchronization active
        assertTrue(ex.getCause() instanceof IllegalStateException);
    }
    try {
        context.getBean(DerivedTestBean.class);
        fail("Should have thrown IllegalStateException");
    } catch (BeanCreationException ex) {
        // expected - no synchronization active
        assertTrue(ex.getCause() instanceof IllegalStateException);
    }
}
Also used : GenericBeanDefinition(org.springframework.beans.factory.support.GenericBeanDefinition) BeanCreationException(org.springframework.beans.factory.BeanCreationException) GenericApplicationContext(org.springframework.context.support.GenericApplicationContext) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) Test(org.junit.Test)

Aggregations

DerivedTestBean (org.springframework.tests.sample.beans.DerivedTestBean)27 Test (org.junit.Test)26 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)9 MockHttpServletRequest (org.springframework.mock.web.test.MockHttpServletRequest)6 ITestBean (org.springframework.tests.sample.beans.ITestBean)6 IndexedTestBean (org.springframework.tests.sample.beans.IndexedTestBean)6 TestBean (org.springframework.tests.sample.beans.TestBean)6 PropertyEditorSupport (java.beans.PropertyEditorSupport)4 WebApplicationContext (org.springframework.web.context.WebApplicationContext)3 GenericWebApplicationContext (org.springframework.web.context.support.GenericWebApplicationContext)3 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)2 GenericBeanDefinition (org.springframework.beans.factory.support.GenericBeanDefinition)2 ManagedList (org.springframework.beans.factory.support.ManagedList)2 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)2 GenericApplicationContext (org.springframework.context.support.GenericApplicationContext)2 MockHttpSession (org.springframework.mock.web.test.MockHttpSession)2 ResourceTestBean (org.springframework.tests.sample.beans.ResourceTestBean)2 InputStream (java.io.InputStream)1 Serializable (java.io.Serializable)1 HashSet (java.util.HashSet)1