Java Code Examples for org.springframework.tests.sample.beans.TestBean#setName()

The following examples show how to use org.springframework.tests.sample.beans.TestBean#setName() . 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 File: ErrorsTagTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void withExplicitWhitespaceBodyContent() throws Exception {
	this.tag.setBodyContent(new MockBodyContent("\t\n   ", 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 2
Source File: LazyAutowiredAnnotationBeanPostProcessorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testLazyResourceInjectionWithField() {
	doTestLazyResourceInjection(FieldResourceInjectionBean.class);

	AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext();
	RootBeanDefinition abd = new RootBeanDefinition(FieldResourceInjectionBean.class);
	abd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
	ac.registerBeanDefinition("annotatedBean", abd);
	RootBeanDefinition tbd = new RootBeanDefinition(TestBean.class);
	tbd.setLazyInit(true);
	ac.registerBeanDefinition("testBean", tbd);
	ac.refresh();

	FieldResourceInjectionBean bean = ac.getBean("annotatedBean", FieldResourceInjectionBean.class);
	assertFalse(ac.getBeanFactory().containsSingleton("testBean"));
	assertFalse(bean.getTestBeans().isEmpty());
	assertNull(bean.getTestBeans().get(0).getName());
	assertTrue(ac.getBeanFactory().containsSingleton("testBean"));
	TestBean tb = (TestBean) ac.getBean("testBean");
	tb.setName("tb");
	assertSame("tb", bean.getTestBean().getName());
}
 
Example 3
Source File: ErrorsTagTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void withErrorsAndDynamicAttributes() throws Exception {
	String dynamicAttribute1 = "attr1";
	String dynamicAttribute2 = "attr2";

	this.tag.setDynamicAttribute(null, dynamicAttribute1, dynamicAttribute1);
	this.tag.setDynamicAttribute(null, dynamicAttribute2, dynamicAttribute2);

	// 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");
	errors.rejectValue("name", "too.short", "Too Short");

	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");
	assertContainsAttribute(output, dynamicAttribute1, dynamicAttribute1);
	assertContainsAttribute(output, dynamicAttribute2, dynamicAttribute2);
	assertBlockTagContains(output, "<br/>");
	assertBlockTagContains(output, "Default Message");
	assertBlockTagContains(output, "Too Short");
}
 
Example 4
Source File: ValidationUtilsTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testValidationUtilsEmptyOrWhitespace() throws Exception {
	TestBean tb = new TestBean();
	Validator testValidator = new EmptyOrWhitespaceValidator();

	// Test null
	Errors errors = new BeanPropertyBindingResult(tb, "tb");
	testValidator.validate(tb, errors);
	assertTrue(errors.hasFieldErrors("name"));
	assertEquals("EMPTY_OR_WHITESPACE", errors.getFieldError("name").getCode());

	// Test empty String
	tb.setName("");
	errors = new BeanPropertyBindingResult(tb, "tb");
	testValidator.validate(tb, errors);
	assertTrue(errors.hasFieldErrors("name"));
	assertEquals("EMPTY_OR_WHITESPACE", errors.getFieldError("name").getCode());

	// Test whitespace String
	tb.setName(" ");
	errors = new BeanPropertyBindingResult(tb, "tb");
	testValidator.validate(tb, errors);
	assertTrue(errors.hasFieldErrors("name"));
	assertEquals("EMPTY_OR_WHITESPACE", errors.getFieldError("name").getCode());

	// Test OK
	tb.setName("Roddy");
	errors = new BeanPropertyBindingResult(tb, "tb");
	testValidator.validate(tb, errors);
	assertFalse(errors.hasFieldErrors("name"));
}
 
Example 5
Source File: AbstractBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Override
public void setAsText(String text) {
	TestBean tb = new TestBean();
	StringTokenizer st = new StringTokenizer(text, "_");
	tb.setName(st.nextToken());
	tb.setAge(Integer.parseInt(st.nextToken()));
	setValue(tb);
}
 
Example 6
Source File: ErrorsTagTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void withErrorsAndDynamicAttributes() throws Exception {
	String dynamicAttribute1 = "attr1";
	String dynamicAttribute2 = "attr2";

	this.tag.setDynamicAttribute(null, dynamicAttribute1, dynamicAttribute1);
	this.tag.setDynamicAttribute(null, dynamicAttribute2, dynamicAttribute2);

	// 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");
	errors.rejectValue("name", "too.short", "Too Short");

	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");
	assertContainsAttribute(output, dynamicAttribute1, dynamicAttribute1);
	assertContainsAttribute(output, dynamicAttribute2, dynamicAttribute2);
	assertBlockTagContains(output, "<br/>");
	assertBlockTagContains(output, "Default Message");
	assertBlockTagContains(output, "Too Short");
}
 
Example 7
Source File: TextareaTagTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
protected TestBean createTestBean() {
	// set up test data
	this.rob = new TestBean();
	rob.setName("Rob");
	rob.setMyFloat(new Float(12.34));

	TestBean sally = new TestBean();
	sally.setName("Sally");
	rob.setSpouse(sally);

	return rob;
}
 
Example 8
Source File: DataBinderTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testNestedValidatorWithoutNestedPath() {
	TestBean tb = new TestBean();
	tb.setName("XXX");
	Errors errors = new BeanPropertyBindingResult(tb, "tb");
	Validator spouseValidator = new SpouseValidator();
	spouseValidator.validate(tb, errors);

	assertTrue(errors.hasGlobalErrors());
	assertEquals(1, errors.getGlobalErrorCount());
	assertEquals("SPOUSE_NOT_AVAILABLE", errors.getGlobalError().getCode());
	assertEquals("tb", (errors.getGlobalErrors().get(0)).getObjectName());
}
 
Example 9
Source File: InputTagTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
protected TestBean createTestBean() {
	// set up test data
	this.rob = new TestBean();
	this.rob.setName("Rob");
	this.rob.setMyFloat(new Float(12.34));

	TestBean sally = new TestBean();
	sally.setName("Sally");
	this.rob.setSpouse(sally);

	return this.rob;
}
 
Example 10
Source File: Portlet20AnnotationControllerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@RequestMapping("EDIT")
@RenderMapping
public String myOtherHandle(TB tb, BindingResult errors, ExtendedModelMap model, MySpecialArg arg) {
	TestBean tbReal = (TestBean) tb;
	tbReal.setName("myName");
	assertTrue(model.get("ITestBean") instanceof DerivedTestBean);
	assertNotNull(arg);
	return super.myHandle(tbReal, errors, model);
}
 
Example 11
Source File: CustomEditorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public void setAsText(String text) {
	TestBean tb = new TestBean();
	StringTokenizer st = new StringTokenizer(text, "_");
	tb.setName(st.nextToken());
	tb.setAge(Integer.parseInt(st.nextToken()));
	setValue(tb);
}
 
Example 12
Source File: CustomEditorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void setAsText(String text) {
	TestBean tb = new TestBean();
	StringTokenizer st = new StringTokenizer(text, "_");
	tb.setName(st.nextToken());
	tb.setAge(Integer.parseInt(st.nextToken()));
	setValue(tb);
}
 
Example 13
Source File: ConfigurationClassAspectIntegrationTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Before("execution(* org.springframework.tests.sample.beans.TestBean.absquatulate(..)) && target(testBean)")
public void touchBean(TestBean testBean) {
	testBean.setName("advisedName");
}
 
Example 14
Source File: AbstractAopProxyTests.java    From spring-analysis-note with MIT License 4 votes vote down vote up
@Test
public void testExistingProxyChangesTarget() throws Throwable {
	TestBean tb1 = new TestBean();
	tb1.setAge(33);

	TestBean tb2 = new TestBean();
	tb2.setAge(26);
	tb2.setName("Juergen");
	TestBean tb3 = new TestBean();
	tb3.setAge(37);
	ProxyFactory pc = new ProxyFactory(tb1);
	NopInterceptor nop = new NopInterceptor();
	pc.addAdvice(nop);
	ITestBean proxy = (ITestBean) createProxy(pc);
	assertEquals(nop.getCount(), 0);
	assertEquals(tb1.getAge(), proxy.getAge());
	assertEquals(nop.getCount(), 1);
	// Change to a new static target
	pc.setTarget(tb2);
	assertEquals(tb2.getAge(), proxy.getAge());
	assertEquals(nop.getCount(), 2);

	// Change to a new dynamic target
	HotSwappableTargetSource hts = new HotSwappableTargetSource(tb3);
	pc.setTargetSource(hts);
	assertEquals(tb3.getAge(), proxy.getAge());
	assertEquals(nop.getCount(), 3);
	hts.swap(tb1);
	assertEquals(tb1.getAge(), proxy.getAge());
	tb1.setName("Colin");
	assertEquals(tb1.getName(), proxy.getName());
	assertEquals(nop.getCount(), 5);

	// Change back, relying on casting to Advised
	Advised advised = (Advised) proxy;
	assertSame(hts, advised.getTargetSource());
	SingletonTargetSource sts = new SingletonTargetSource(tb2);
	advised.setTargetSource(sts);
	assertEquals(tb2.getName(), proxy.getName());
	assertSame(sts, advised.getTargetSource());
	assertEquals(tb2.getAge(), proxy.getAge());
}
 
Example 15
Source File: TestBeanCreator.java    From spring-analysis-note with MIT License 4 votes vote down vote up
public static TestBean createTestBean() {
	TestBean tb = new TestBean();
	tb.setName("Tristan");
	tb.setAge(2);
	return tb;
}
 
Example 16
Source File: DefaultListableBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
public TestBean create() {
	TestBean tb = new TestBean();
	tb.setName(this.name);
	return tb;
}
 
Example 17
Source File: ConfigurationClassAspectIntegrationTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Before("execution(* org.springframework.tests.sample.beans.TestBean.absquatulate(..)) && target(testBean)")
public void touchBean(TestBean testBean) {
	testBean.setName("advisedName");
}
 
Example 18
Source File: DefaultListableBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
public TestBean createWithArgs(String arg) {
	TestBean tb = new TestBean();
	tb.setName(arg);
	return tb;
}
 
Example 19
Source File: FactoryMethods.java    From spring-analysis-note with MIT License 4 votes vote down vote up
public static FactoryMethods defaultInstance() {
	TestBean tb = new TestBean();
	tb.setName("defaultInstance");
	return new FactoryMethods(tb, "default", 0);
}
 
Example 20
Source File: AbstractAopProxyTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
@Test
public void testExistingProxyChangesTarget() throws Throwable {
	TestBean tb1 = new TestBean();
	tb1.setAge(33);

	TestBean tb2 = new TestBean();
	tb2.setAge(26);
	tb2.setName("Juergen");
	TestBean tb3 = new TestBean();
	tb3.setAge(37);
	ProxyFactory pc = new ProxyFactory(tb1);
	NopInterceptor nop = new NopInterceptor();
	pc.addAdvice(nop);
	ITestBean proxy = (ITestBean) createProxy(pc);
	assertEquals(nop.getCount(), 0);
	assertEquals(tb1.getAge(), proxy.getAge());
	assertEquals(nop.getCount(), 1);
	// Change to a new static target
	pc.setTarget(tb2);
	assertEquals(tb2.getAge(), proxy.getAge());
	assertEquals(nop.getCount(), 2);

	// Change to a new dynamic target
	HotSwappableTargetSource hts = new HotSwappableTargetSource(tb3);
	pc.setTargetSource(hts);
	assertEquals(tb3.getAge(), proxy.getAge());
	assertEquals(nop.getCount(), 3);
	hts.swap(tb1);
	assertEquals(tb1.getAge(), proxy.getAge());
	tb1.setName("Colin");
	assertEquals(tb1.getName(), proxy.getName());
	assertEquals(nop.getCount(), 5);

	// Change back, relying on casting to Advised
	Advised advised = (Advised) proxy;
	assertSame(hts, advised.getTargetSource());
	SingletonTargetSource sts = new SingletonTargetSource(tb2);
	advised.setTargetSource(sts);
	assertEquals(tb2.getName(), proxy.getName());
	assertSame(sts, advised.getTargetSource());
	assertEquals(tb2.getAge(), proxy.getAge());
}