Java Code Examples for org.springframework.tests.sample.beans.TestBean

The following examples show how to use org.springframework.tests.sample.beans.TestBean. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: spring4-understanding   Source File: ProxyFactoryTests.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testProxyTargetClassWithConcreteClassAsTarget() {
	ProxyFactory pf = new ProxyFactory();
	pf.setTargetClass(TestBean.class);
	Object proxy = pf.getProxy();
	assertTrue("Proxy is a CGLIB proxy", AopUtils.isCglibProxy(proxy));
	assertTrue(proxy instanceof TestBean);
	assertEquals(TestBean.class, AopProxyUtils.ultimateTargetClass(proxy));

	ProxyFactory pf2 = new ProxyFactory(proxy);
	pf2.setProxyTargetClass(true);
	Object proxy2 = pf2.getProxy();
	assertTrue("Proxy is a CGLIB proxy", AopUtils.isCglibProxy(proxy2));
	assertTrue(proxy2 instanceof TestBean);
	assertEquals(TestBean.class, AopProxyUtils.ultimateTargetClass(proxy2));
}
 
Example 2
@Test
public void testExtendedResourceInjectionWithSkippedOverriddenMethods() {
	RootBeanDefinition annotatedBd = new RootBeanDefinition(OverriddenExtendedResourceInjectionBean.class);
	bf.registerBeanDefinition("annotatedBean", annotatedBd);
	TestBean tb = new TestBean();
	bf.registerSingleton("testBean", tb);
	NestedTestBean ntb = new NestedTestBean();
	bf.registerSingleton("nestedTestBean", ntb);

	OverriddenExtendedResourceInjectionBean bean = (OverriddenExtendedResourceInjectionBean) bf.getBean("annotatedBean");
	assertSame(tb, bean.getTestBean());
	assertNull(bean.getTestBean2());
	assertSame(tb, bean.getTestBean3());
	assertSame(tb, bean.getTestBean4());
	assertSame(ntb, bean.getNestedTestBean());
	assertNull(bean.getBeanFactory());
	assertTrue(bean.baseInjected);
	assertTrue(bean.subInjected);
}
 
Example 3
Source Project: spring-analysis-note   Source File: AbstractAopProxyTests.java    License: MIT License 6 votes vote down vote up
@Test
public void testStaticMethodPointcut() throws Throwable {
	TestBean tb = new TestBean();
	ProxyFactory pc = new ProxyFactory();
	pc.addInterface(ITestBean.class);
	NopInterceptor di = new NopInterceptor();
	TestStaticPointcutAdvice sp = new TestStaticPointcutAdvice(di, "getAge");
	pc.addAdvisor(sp);
	pc.setTarget(tb);
	ITestBean it = (ITestBean) createProxy(pc);
	assertEquals(di.getCount(), 0);
	it.getAge();
	assertEquals(di.getCount(), 1);
	it.setAge(11);
	assertEquals(it.getAge(), 11);
	assertEquals(di.getCount(), 2);
}
 
Example 4
Source Project: java-technology-stack   Source File: MBeanExporterTests.java    License: MIT License 6 votes vote down vote up
@Test
public void testOnlyBonaFideMBeanIsExportedWhenAutodetectIsMBeanOnly() throws Exception {
	BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(Person.class);
	DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
	factory.registerBeanDefinition(OBJECT_NAME, builder.getBeanDefinition());
	String exportedBeanName = "spring:type=TestBean";
	factory.registerSingleton(exportedBeanName, new TestBean());

	MBeanExporter exporter = new MBeanExporter();
	exporter.setServer(getServer());
	exporter.setAssembler(new NamedBeanAutodetectCapableMBeanInfoAssemblerStub(exportedBeanName));
	exporter.setBeanFactory(factory);
	exporter.setAutodetectMode(MBeanExporter.AUTODETECT_MBEAN);
	start(exporter);

	assertIsRegistered("Bona fide MBean not autodetected in AUTODETECT_MBEAN mode",
			ObjectNameManager.getInstance(OBJECT_NAME));
	assertIsNotRegistered("Bean autodetected and (only) AUTODETECT_MBEAN mode is on",
			ObjectNameManager.getInstance(exportedBeanName));
}
 
Example 5
@Test
public void testCustomEditor() {
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	lbf.addPropertyEditorRegistrar(new PropertyEditorRegistrar() {
		@Override
		public void registerCustomEditors(PropertyEditorRegistry registry) {
			NumberFormat nf = NumberFormat.getInstance(Locale.GERMAN);
			registry.registerCustomEditor(Float.class, new CustomNumberEditor(Float.class, nf, true));
		}
	});
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("myFloat", "1,1");
	RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
	bd.setPropertyValues(pvs);
	lbf.registerBeanDefinition("testBean", bd);
	TestBean testBean = (TestBean) lbf.getBean("testBean");
	assertTrue(testBean.getMyFloat().floatValue() == 1.1f);
}
 
Example 6
@Test
public void explicitPropertySources() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	bf.registerBeanDefinition("testBean",
			genericBeanDefinition(TestBean.class)
				.addPropertyValue("name", "${my.name}")
				.getBeanDefinition());

	MutablePropertySources propertySources = new MutablePropertySources();
	propertySources.addLast(new MockPropertySource().withProperty("my.name", "foo"));

	PropertySourcesPlaceholderConfigurer pc = new PropertySourcesPlaceholderConfigurer();
	pc.setPropertySources(propertySources);
	pc.postProcessBeanFactory(bf);
	assertThat(bf.getBean(TestBean.class).getName(), equalTo("foo"));
	assertEquals(pc.getAppliedPropertySources().iterator().next(), propertySources.iterator().next());
}
 
Example 7
Source Project: java-technology-stack   Source File: BeanUtilsTests.java    License: MIT License 6 votes vote down vote up
@Test
public void testCopyPropertiesWithIgnore() throws Exception {
	TestBean tb = new TestBean();
	assertTrue("Name empty", tb.getName() == null);
	tb.setAge(32);
	tb.setTouchy("bla");
	TestBean tb2 = new TestBean();
	tb2.setName("rod");
	assertTrue("Age empty", tb2.getAge() == 0);
	assertTrue("Touchy empty", tb2.getTouchy() == null);

	// "spouse", "touchy", "age" should not be copied
	BeanUtils.copyProperties(tb, tb2, "spouse", "touchy", "age");
	assertTrue("Name copied", tb2.getName() == null);
	assertTrue("Age still empty", tb2.getAge() == 0);
	assertTrue("Touchy still empty", tb2.getTouchy() == null);
}
 
Example 8
Source Project: java-technology-stack   Source File: MBeanExporterTests.java    License: MIT License 6 votes vote down vote up
@Test
public void testBonaFideMBeanIsNotExportedWithAutodetectAssembler() throws Exception {
	BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(Person.class);
	DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
	factory.registerBeanDefinition(OBJECT_NAME, builder.getBeanDefinition());
	String exportedBeanName = "spring:type=TestBean";
	factory.registerSingleton(exportedBeanName, new TestBean());

	MBeanExporter exporter = new MBeanExporter();
	exporter.setServer(getServer());
	exporter.setAssembler(new NamedBeanAutodetectCapableMBeanInfoAssemblerStub(exportedBeanName));
	exporter.setBeanFactory(factory);
	exporter.setAutodetectMode(MBeanExporter.AUTODETECT_ASSEMBLER);
	start(exporter);
	assertIsNotRegistered("Bona fide MBean was autodetected in AUTODETECT_ASSEMBLER mode - must not have been",
			ObjectNameManager.getInstance(OBJECT_NAME));
	assertIsRegistered("Bean not autodetected in AUTODETECT_ASSEMBLER mode",
			ObjectNameManager.getInstance(exportedBeanName));
}
 
Example 9
Source Project: spring-analysis-note   Source File: BindTagTests.java    License: MIT License 6 votes vote down vote up
@Test
public void bindTagWithMappedProperties() throws JspException {
	PageContext pc = createPageContext();
	IndexedTestBean tb = new IndexedTestBean();
	Errors errors = new ServletRequestDataBinder(tb, "tb").getBindingResult();
	errors.rejectValue("map[key1]", "code1", "message1");
	errors.rejectValue("map[key1]", "code2", "message2");
	pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors);

	BindTag tag = new BindTag();
	tag.setPageContext(pc);
	tag.setPath("tb.map[key1]");
	assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
	BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
	assertTrue("Has status variable", status != null);
	assertTrue("Correct expression", "map[key1]".equals(status.getExpression()));
	assertTrue("Value is TestBean", status.getValue() instanceof TestBean);
	assertTrue("Correct value", "name4".equals(((TestBean) status.getValue()).getName()));
	assertTrue("Correct isError", status.isError());
	assertTrue("Correct errorCodes", status.getErrorCodes().length == 2);
	assertTrue("Correct errorMessages", status.getErrorMessages().length == 2);
	assertTrue("Correct errorCode", "code1".equals(status.getErrorCodes()[0]));
	assertTrue("Correct errorCode", "code2".equals(status.getErrorCodes()[1]));
	assertTrue("Correct errorMessage", "message1".equals(status.getErrorMessages()[0]));
	assertTrue("Correct errorMessage", "message2".equals(status.getErrorMessages()[1]));
}
 
Example 10
Source Project: java-technology-stack   Source File: AutowireWithExclusionTests.java    License: MIT License 6 votes vote down vote up
@Test
public void byTypeAutowireWithPrimaryInParentAndChild() throws Exception {
	CountingFactory.reset();
	DefaultListableBeanFactory parent = getBeanFactory("autowire-with-exclusion.xml");
	parent.getBeanDefinition("props1").setPrimary(true);
	parent.preInstantiateSingletons();
	DefaultListableBeanFactory child = new DefaultListableBeanFactory(parent);
	RootBeanDefinition robDef = new RootBeanDefinition(TestBean.class);
	robDef.setAutowireMode(RootBeanDefinition.AUTOWIRE_BY_TYPE);
	robDef.getPropertyValues().add("spouse", new RuntimeBeanReference("sally"));
	child.registerBeanDefinition("rob2", robDef);
	RootBeanDefinition propsDef = new RootBeanDefinition(PropertiesFactoryBean.class);
	propsDef.getPropertyValues().add("properties", "name=props3");
	propsDef.setPrimary(true);
	child.registerBeanDefinition("props3", propsDef);
	TestBean rob = (TestBean) child.getBean("rob2");
	assertEquals("props3", rob.getSomeProperties().getProperty("name"));
	assertEquals(1, CountingFactory.getFactoryBeanInstanceCount());
}
 
Example 11
@Test
public void testCustomAnnotationOptionalMethodResourceInjection() {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
	bpp.setAutowiredAnnotationType(MyAutowired.class);
	bpp.setRequiredParameterName("optional");
	bpp.setRequiredParameterValue(false);
	bpp.setBeanFactory(bf);
	bf.addBeanPostProcessor(bpp);
	bf.registerBeanDefinition("customBean", new RootBeanDefinition(
			CustomAnnotationOptionalMethodResourceInjectionBean.class));
	TestBean tb = new TestBean();
	bf.registerSingleton("testBean", tb);

	CustomAnnotationOptionalMethodResourceInjectionBean bean = (CustomAnnotationOptionalMethodResourceInjectionBean) bf.getBean("customBean");
	assertSame(tb, bean.getTestBean3());
	assertNull(bean.getTestBean());
	assertNull(bean.getTestBean2());
	bf.destroySingletons();
}
 
Example 12
Source Project: spring4-understanding   Source File: CustomEditorTests.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testComplexObject() {
	TestBean tb = new TestBean();
	String newName = "Rod";
	String tbString = "Kerry_34";

	BeanWrapper bw = new BeanWrapperImpl(tb);
	bw.registerCustomEditor(ITestBean.class, new TestBeanEditor());
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.addPropertyValue(new PropertyValue("age", new Integer(55)));
	pvs.addPropertyValue(new PropertyValue("name", newName));
	pvs.addPropertyValue(new PropertyValue("touchy", "valid"));
	pvs.addPropertyValue(new PropertyValue("spouse", tbString));
	bw.setPropertyValues(pvs);
	assertTrue("spouse is non-null", tb.getSpouse() != null);
	assertTrue("spouse name is Kerry and age is 34",
			tb.getSpouse().getName().equals("Kerry") && tb.getSpouse().getAge() == 34);
}
 
Example 13
Source Project: spring4-understanding   Source File: OptionsTagTests.java    License: Apache License 2.0 6 votes vote down vote up
@Override
protected void extendRequest(MockHttpServletRequest request) {
	TestBean bean = new TestBean();
	bean.setName("foo");
	bean.setCountry("UK");
	bean.setMyFloat(new Float("12.34"));
	request.setAttribute(COMMAND_NAME, bean);

	List floats = new ArrayList();
	floats.add(new Float("12.30"));
	floats.add(new Float("12.31"));
	floats.add(new Float("12.32"));
	floats.add(new Float("12.33"));
	floats.add(new Float("12.34"));
	floats.add(new Float("12.35"));

	request.setAttribute("floats", floats);
}
 
Example 14
Source Project: spring-analysis-note   Source File: ErrorsTagTests.java    License: MIT License 6 votes vote down vote up
@Test
public void withExplicitEmptyWhitespaceBodyContent() throws Exception {
	this.tag.setBodyContent(new MockBodyContent("", getWriter()));

	// construct an errors instance of the tag
	TestBean target = new TestBean();
	target.setName("Rob Harrop");
	Errors errors = new BeanPropertyBindingResult(target, COMMAND_NAME);
	errors.rejectValue("name", "some.code", "Default Message");

	exposeBindingResult(errors);

	int result = this.tag.doStartTag();
	assertEquals(BodyTag.EVAL_BODY_BUFFERED, result);

	result = this.tag.doEndTag();
	assertEquals(Tag.EVAL_PAGE, result);

	String output = getOutput();
	assertElementTagOpened(output);
	assertElementTagClosed(output);

	assertContainsAttribute(output, "id", "name.errors");
	assertBlockTagContains(output, "Default Message");
}
 
Example 15
@Test
public void testGetFromScopeThroughDynamicProxy() throws Exception {
	String name = "requestScopedProxy";
	ITestBean bean = (ITestBean) this.beanFactory.getBean(name);
	assertTrue(AopUtils.isJdkDynamicProxy(bean));

	MockHttpServletRequest request = new MockHttpServletRequest();
	RequestAttributes requestAttributes = new ServletRequestAttributes(request);
	RequestContextHolder.setRequestAttributes(requestAttributes);

	try {
		assertNull(request.getAttribute("scopedTarget." + name));
		assertEquals("scoped", bean.getName());
		assertNotNull(request.getAttribute("scopedTarget." + name));
		TestBean target = (TestBean) request.getAttribute("scopedTarget." + name);
		assertEquals(TestBean.class, target.getClass());
		assertEquals("scoped", target.getName());
		assertSame(bean, this.beanFactory.getBean(name));
		assertEquals(bean.toString(), target.toString());
	}
	finally {
		RequestContextHolder.setRequestAttributes(null);
	}
}
 
Example 16
@Test
public void testFieldInjectionWithMap() {
	RootBeanDefinition bd = new RootBeanDefinition(MapFieldInjectionBean.class);
	bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	bf.registerBeanDefinition("annotatedBean", bd);
	TestBean tb1 = new TestBean("tb1");
	TestBean tb2 = new TestBean("tb2");
	bf.registerSingleton("testBean1", tb1);
	bf.registerSingleton("testBean2", tb2);

	MapFieldInjectionBean bean = (MapFieldInjectionBean) bf.getBean("annotatedBean");
	assertEquals(2, bean.getTestBeanMap().size());
	assertTrue(bean.getTestBeanMap().keySet().contains("testBean1"));
	assertTrue(bean.getTestBeanMap().keySet().contains("testBean2"));
	assertTrue(bean.getTestBeanMap().values().contains(tb1));
	assertTrue(bean.getTestBeanMap().values().contains(tb2));

	bean = (MapFieldInjectionBean) bf.getBean("annotatedBean");
	assertEquals(2, bean.getTestBeanMap().size());
	assertTrue(bean.getTestBeanMap().keySet().contains("testBean1"));
	assertTrue(bean.getTestBeanMap().keySet().contains("testBean2"));
	assertTrue(bean.getTestBeanMap().values().contains(tb1));
	assertTrue(bean.getTestBeanMap().values().contains(tb2));
}
 
Example 17
@Test
public void testRefSubelementsBuildCollection() throws Exception {
	TestBean jen = (TestBean) this.beanFactory.getBean("jenny");
	TestBean dave = (TestBean) this.beanFactory.getBean("david");
	TestBean rod = (TestBean) this.beanFactory.getBean("rod");

	// Must be a list to support ordering
	// Our bean doesn't modify the collection:
	// of course it could be a different copy in a real object.
	Object[] friends = rod.getFriends().toArray();
	assertTrue(friends.length == 2);

	assertTrue("First friend must be jen, not " + friends[0], friends[0] == jen);
	assertTrue(friends[1] == dave);
	// Should be ordered
}
 
Example 18
Source Project: spring4-understanding   Source File: MBeanExporterTests.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testIgnoreBeanName() throws MalformedObjectNameException {
	DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
	String firstBeanName = "spring:type=TestBean";
	factory.registerSingleton(firstBeanName, new TestBean("test"));
	String secondBeanName = "spring:type=TestBean2";
	factory.registerSingleton(secondBeanName, new TestBean("test2"));

	MBeanExporter exporter = new MBeanExporter();
	exporter.setServer(getServer());
	exporter.setAssembler(new NamedBeanAutodetectCapableMBeanInfoAssemblerStub(firstBeanName, secondBeanName));
	exporter.setBeanFactory(factory);
	exporter.setAutodetectMode(MBeanExporter.AUTODETECT_ALL);
	exporter.addExcludedBean(secondBeanName);

	start(exporter);
	assertIsRegistered("Bean not autodetected in (AUTODETECT_ALL) mode",
			ObjectNameManager.getInstance(firstBeanName));
	assertIsNotRegistered("Bean should have been excluded",
			ObjectNameManager.getInstance(secondBeanName));
}
 
Example 19
@Test
public void testConstructorResourceInjectionWithNullFromFactoryBean() {
	RootBeanDefinition bd = new RootBeanDefinition(ConstructorResourceInjectionBean.class);
	bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	bf.registerBeanDefinition("annotatedBean", bd);
	TestBean tb = new TestBean();
	bf.registerSingleton("testBean", tb);
	bf.registerBeanDefinition("nestedTestBean", new RootBeanDefinition(NullNestedTestBeanFactoryBean.class));
	bf.registerSingleton("nestedTestBean2", new NestedTestBean());

	ConstructorResourceInjectionBean bean = (ConstructorResourceInjectionBean) bf.getBean("annotatedBean");
	assertSame(tb, bean.getTestBean());
	assertSame(tb, bean.getTestBean2());
	assertSame(tb, bean.getTestBean3());
	assertSame(tb, bean.getTestBean4());
	assertNull(bean.getNestedTestBean());
	assertSame(bf, bean.getBeanFactory());

	bean = (ConstructorResourceInjectionBean) bf.getBean("annotatedBean");
	assertSame(tb, bean.getTestBean());
	assertSame(tb, bean.getTestBean2());
	assertSame(tb, bean.getTestBean3());
	assertSame(tb, bean.getTestBean4());
	assertNull(bean.getNestedTestBean());
	assertSame(bf, bean.getBeanFactory());
}
 
Example 20
Source Project: spring-analysis-note   Source File: AbstractAopProxyTests.java    License: MIT License 6 votes vote down vote up
/**
 * Check that casting to Advised can't get around advice freeze.
 */
@Test
public void testCannotAddAdvisorWhenFrozenUsingCast() throws Throwable {
	TestBean target = new TestBean();
	target.setAge(21);
	ProxyFactory pc = new ProxyFactory(target);
	assertFalse(pc.isFrozen());
	pc.addAdvice(new NopInterceptor());
	ITestBean proxied = (ITestBean) createProxy(pc);
	pc.setFrozen(true);
	Advised advised = (Advised) proxied;

	assertTrue(pc.isFrozen());
	try {
		advised.addAdvisor(new DefaultPointcutAdvisor(new NopInterceptor()));
		fail("Shouldn't be able to add Advisor when frozen");
	}
	catch (AopConfigException ex) {
		assertTrue(ex.getMessage().contains("frozen"));
	}
	// Check it still works: proxy factory state shouldn't have been corrupted
	assertEquals(target.getAge(), proxied.getAge());
	assertEquals(1, advised.getAdvisors().length);
}
 
Example 21
Source Project: spring-analysis-note   Source File: RequestScopeTests.java    License: MIT License 6 votes vote down vote up
@Test
public void requestScopedInnerBeanDestroyedWhileContainedBySingleton() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest();
	ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request);
	RequestContextHolder.setRequestAttributes(requestAttributes);

	String outerBeanName = "singletonOuterBean";
	TestBean outer1 = (TestBean) this.beanFactory.getBean(outerBeanName);
	assertNull(request.getAttribute(outerBeanName));
	TestBean inner1 = (TestBean) outer1.getSpouse();
	TestBean outer2 = (TestBean) this.beanFactory.getBean(outerBeanName);
	assertSame(outer1, outer2);
	assertSame(inner1, outer2.getSpouse());
	requestAttributes.requestCompleted();
	assertTrue(inner1.wasDestroyed());
	assertFalse(outer1.wasDestroyed());
}
 
Example 22
@Test(expected = IllegalStateException.class)
public void testScopingBeanToUnregisteredScopeResultsInAnException() throws Exception {
	BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(TestBean.class);
	AbstractBeanDefinition beanDefinition = builder.getBeanDefinition();
	beanDefinition.setScope("he put himself so low could hardly look me in the face");

	DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
	factory.registerBeanDefinition("testBean", beanDefinition);
	factory.getBean("testBean");
}
 
Example 23
@Test
public void testListHasDefaultType() throws Exception {
	TestBean bean = (TestBean) this.beanFactory.getBean("testBean");
	for (Object o : bean.getSomeList()) {
		assertEquals("Value type is incorrect", Integer.class, o.getClass());
	}
}
 
Example 24
Source Project: spring-analysis-note   Source File: WebRequestDataBinderTests.java    License: MIT License 5 votes vote down vote up
@Test
public void testMultipartFileAsString() {
	TestBean target = new TestBean();
	WebRequestDataBinder binder = new WebRequestDataBinder(target);
	binder.registerCustomEditor(String.class, new StringMultipartFileEditor());

	MockMultipartHttpServletRequest request = new MockMultipartHttpServletRequest();
	request.addFile(new MockMultipartFile("name", "Juergen".getBytes()));
	binder.bind(new ServletWebRequest(request));
	assertEquals("Juergen", target.getName());
}
 
Example 25
@Test
public void setPropertyIsReflectedImmediately() {
	TestBean target = new TestBean();
	int newAge = 33;
	try {
		AbstractPropertyAccessor accessor = createAccessor(target);
		target.setAge(newAge);
		Object bwAge = accessor.getPropertyValue("age");
		assertTrue("Age is an integer", bwAge instanceof Integer);
		assertTrue("Bean wrapper must pick up changes", (int) bwAge == newAge);
	}
	catch (Exception ex) {
		fail("Shouldn't throw exception when everything is valid");
	}
}
 
Example 26
Source Project: spring4-understanding   Source File: DataBinderTests.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testAutoGrowWithinCustomLimit() throws Exception {
	TestBean testBean = new TestBean();
	DataBinder binder = new DataBinder(testBean, "testBean");
	binder.setAutoGrowCollectionLimit(10);

	MutablePropertyValues mpvs = new MutablePropertyValues();
	mpvs.add("friends[4]", "");
	binder.bind(mpvs);

	assertEquals(5, testBean.getFriends().size());
}
 
Example 27
@Override
@RequestMapping(method = RequestMethod.GET)
public String get(Model model) {
	model.addAttribute("object1", new TestBean());
	model.addAttribute("object2", new TestBean());
	return "page1";
}
 
Example 28
@Test
public void testSingletonScoping() throws Exception {
	ITestBean scoped = (ITestBean) this.context.getBean("singletonScoped");
	assertTrue("Should be AOP proxy", AopUtils.isAopProxy(scoped));
	assertTrue("Should be target class proxy", scoped instanceof TestBean);
	String rob = "Rob Harrop";
	String bram = "Bram Smeets";
	assertEquals(rob, scoped.getName());
	scoped.setName(bram);
	assertEquals(bram, scoped.getName());
	ITestBean deserialized = (ITestBean) SerializationTestUtils.serializeAndDeserialize(scoped);
	assertEquals(bram, deserialized.getName());
}
 
Example 29
@Test
public void withResolvablePlaceholderAndFactoryBean() {
	AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
	ctx.register(ConfigWithResolvablePlaceholderAndFactoryBean.class);
	System.setProperty("path.to.properties", "org/springframework/context/annotation");
	ctx.refresh();
	assertThat(ctx.getBean(TestBean.class).getName(), equalTo("p1TestBean"));
	System.clearProperty("path.to.properties");
}
 
Example 30
Source Project: spring4-understanding   Source File: DataBinderTests.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testBindingNoErrors() throws Exception {
	TestBean rod = new TestBean();
	DataBinder binder = new DataBinder(rod, "person");
	assertTrue(binder.isIgnoreUnknownFields());
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("name", "Rod");
	pvs.add("age", "032");
	pvs.add("nonExisting", "someValue");

	binder.bind(pvs);
	binder.close();

	assertTrue("changed name correctly", rod.getName().equals("Rod"));
	assertTrue("changed age correctly", rod.getAge() == 32);

	Map<?, ?> map = binder.getBindingResult().getModel();
	assertTrue("There is one element in map", map.size() == 2);
	TestBean tb = (TestBean) map.get("person");
	assertTrue("Same object", tb.equals(rod));

	BindingResult other = new BeanPropertyBindingResult(rod, "person");
	assertEquals(other, binder.getBindingResult());
	assertEquals(binder.getBindingResult(), other);
	BindException ex = new BindException(other);
	assertEquals(ex, other);
	assertEquals(other, ex);
	assertEquals(ex, binder.getBindingResult());
	assertEquals(binder.getBindingResult(), ex);

	other.reject("xxx");
	assertTrue(!other.equals(binder.getBindingResult()));
}