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

The following examples show how to use org.springframework.tests.sample.beans.TestBean#setAge() . 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: AbstractPropertyAccessorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void setEmptyPropertyValues() {
	TestBean target = new TestBean();
	int age = 50;
	String name = "Tony";
	target.setAge(age);
	target.setName(name);
	try {
		AbstractPropertyAccessor accessor = createAccessor(target);
		assertTrue("age is OK", target.getAge() == age);
		assertTrue("name is OK", name.equals(target.getName()));
		accessor.setPropertyValues(new MutablePropertyValues());
		// Check its unchanged
		assertTrue("age is OK", target.getAge() == age);
		assertTrue("name is OK", name.equals(target.getName()));
	}
	catch (BeansException ex) {
		fail("Shouldn't throw exception when everything is valid");
	}
}
 
Example 2
Source File: XmlBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testPrototypeInheritanceFromParentFactorySingleton() throws Exception {
	DefaultListableBeanFactory parent = new DefaultListableBeanFactory();
	new XmlBeanDefinitionReader(parent).loadBeanDefinitions(PARENT_CONTEXT);
	DefaultListableBeanFactory child = new DefaultListableBeanFactory(parent);
	new XmlBeanDefinitionReader(child).loadBeanDefinitions(CHILD_CONTEXT);
	TestBean inherits = (TestBean) child.getBean("protoypeInheritsFromParentFactorySingleton");
	// Name property value is overridden
	assertTrue(inherits.getName().equals("prototypeOverridesInheritedSingleton"));
	// Age property is inherited from bean in parent factory
	assertTrue(inherits.getAge() == 1);
	TestBean inherits2 = (TestBean) child.getBean("protoypeInheritsFromParentFactorySingleton");
	assertFalse(inherits2 == inherits);
	inherits2.setAge(13);
	assertTrue(inherits2.getAge() == 13);
	// Shouldn't have changed first instance
	assertTrue(inherits.getAge() == 1);
}
 
Example 3
Source File: XmlBeanFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testPrototypeInheritanceFromParentFactoryPrototype() throws Exception {
	DefaultListableBeanFactory parent = new DefaultListableBeanFactory();
	new XmlBeanDefinitionReader(parent).loadBeanDefinitions(PARENT_CONTEXT);
	DefaultListableBeanFactory child = new DefaultListableBeanFactory(parent);
	new XmlBeanDefinitionReader(child).loadBeanDefinitions(CHILD_CONTEXT);
	assertEquals(TestBean.class, child.getType("prototypeInheritsFromParentFactoryPrototype"));
	TestBean inherits = (TestBean) child.getBean("prototypeInheritsFromParentFactoryPrototype");
	// Name property value is overridden
	assertTrue(inherits.getName().equals("prototype-override"));
	// Age property is inherited from bean in parent factory
	assertTrue(inherits.getAge() == 2);
	TestBean inherits2 = (TestBean) child.getBean("prototypeInheritsFromParentFactoryPrototype");
	assertFalse(inherits2 == inherits);
	inherits2.setAge(13);
	assertTrue(inherits2.getAge() == 13);
	// Shouldn't have changed first instance
	assertTrue(inherits.getAge() == 2);
}
 
Example 4
Source File: AbstractAopProxyTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testCannotAddInterceptorWhenFrozen() 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);
	try {
		pc.addAdvice(0, new NopInterceptor());
		fail("Shouldn't be able to add interceptor 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) proxied).getAdvisors().length);
}
 
Example 5
Source File: XmlBeanFactoryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testPrototypeInheritanceFromParentFactorySingleton() {
	DefaultListableBeanFactory parent = new DefaultListableBeanFactory();
	new XmlBeanDefinitionReader(parent).loadBeanDefinitions(PARENT_CONTEXT);
	DefaultListableBeanFactory child = new DefaultListableBeanFactory(parent);
	new XmlBeanDefinitionReader(child).loadBeanDefinitions(CHILD_CONTEXT);
	TestBean inherits = (TestBean) child.getBean("protoypeInheritsFromParentFactorySingleton");
	// Name property value is overridden
	assertTrue(inherits.getName().equals("prototypeOverridesInheritedSingleton"));
	// Age property is inherited from bean in parent factory
	assertTrue(inherits.getAge() == 1);
	TestBean inherits2 = (TestBean) child.getBean("protoypeInheritsFromParentFactorySingleton");
	assertFalse(inherits2 == inherits);
	inherits2.setAge(13);
	assertTrue(inherits2.getAge() == 13);
	// Shouldn't have changed first instance
	assertTrue(inherits.getAge() == 1);
}
 
Example 6
Source File: RegexpMethodPointcutAdvisorIntegrationTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testMultiplePatterns() throws Throwable {
	DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
	new XmlBeanDefinitionReader(bf).loadBeanDefinitions(CONTEXT);
	// This is a CGLIB proxy, so we can proxy it to the target class
	TestBean advised = (TestBean) bf.getBean("settersAndAbsquatulateAdvised");
	// Interceptor behind regexp advisor
	NopInterceptor nop = (NopInterceptor) bf.getBean("nopInterceptor");
	assertEquals(0, nop.getCount());

	int newAge = 12;
	// Not advised
	advised.exceptional(null);
	assertEquals(0, nop.getCount());

	// This is proxied
	advised.absquatulate();
	assertEquals(1, nop.getCount());
	advised.setAge(newAge);
	assertEquals(newAge, advised.getAge());
	// Only setter fired
	assertEquals(2, nop.getCount());
}
 
Example 7
Source File: AbstractAopProxyTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testRejectsBogusDynamicIntroductionAdviceWithNoAdapter() throws Throwable {
	TestBean target = new TestBean();
	target.setAge(21);
	ProxyFactory pc = new ProxyFactory(target);
	pc.addAdvisor(new DefaultIntroductionAdvisor(new DummyIntroductionAdviceImpl(), Comparable.class));
	try {
		// TODO May fail on either call: may want to tighten up definition
		ITestBean proxied = (ITestBean) createProxy(pc);
		proxied.getName();
		fail("Bogus introduction");
	}
	catch (Exception ex) {
		// TODO used to catch UnknownAdviceTypeException, but
		// with CGLIB some errors are in proxy creation and are wrapped
		// in aspect exception. Error message is still fine.
		//assertTrue(ex.getMessage().indexOf("ntroduction") > -1);
	}
}
 
Example 8
Source File: AbstractAspectJAdvisorFactoryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testMultiplePerTargetAspectsWithOrderAnnotation() throws SecurityException, NoSuchMethodException {
	TestBean target = new TestBean();
	int realAge = 65;
	target.setAge(realAge);

	List<Advisor> advisors = new LinkedList<Advisor>();
	PerTargetAspectWithOrderAnnotation10 aspect1 = new PerTargetAspectWithOrderAnnotation10();
	aspect1.count = 100;
	advisors.addAll(
			getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(aspect1, "someBean1")));
	PerTargetAspectWithOrderAnnotation5 aspect2 = new PerTargetAspectWithOrderAnnotation5();
	advisors.addAll(
			getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(aspect2, "someBean2")));
	Collections.sort(advisors, new OrderComparator());

	TestBean itb = (TestBean) createProxy(target, advisors, TestBean.class);
	assertEquals("Around advice must NOT apply", realAge, itb.getAge());

	// Hit the method in the per clause to instantiate the aspect
	itb.getSpouse();

	assertEquals("Around advice must apply", 0, itb.getAge());
	assertEquals("Around advice must apply", 1, itb.getAge());
}
 
Example 9
Source File: AbstractAspectJAdvisorFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testMultiplePerTargetAspectsWithOrderAnnotation() throws SecurityException, NoSuchMethodException {
	TestBean target = new TestBean();
	int realAge = 65;
	target.setAge(realAge);

	List<Advisor> advisors = new LinkedList<>();
	PerTargetAspectWithOrderAnnotation10 aspect1 = new PerTargetAspectWithOrderAnnotation10();
	aspect1.count = 100;
	advisors.addAll(
			getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(aspect1, "someBean1")));
	PerTargetAspectWithOrderAnnotation5 aspect2 = new PerTargetAspectWithOrderAnnotation5();
	advisors.addAll(
			getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(aspect2, "someBean2")));
	Collections.sort(advisors, new OrderComparator());

	TestBean itb = (TestBean) createProxy(target, advisors, TestBean.class);
	assertEquals("Around advice must NOT apply", realAge, itb.getAge());

	// Hit the method in the per clause to instantiate the aspect
	itb.getSpouse();

	assertEquals("Around advice must apply", 0, itb.getAge());
	assertEquals("Around advice must apply", 1, itb.getAge());
}
 
Example 10
Source File: AbstractAopProxyTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testCannotRemoveAdvisorWhenFrozen() 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.removeAdvisor(0);
		fail("Shouldn't be able to remove Advisor when frozen");
	}
	catch (AopConfigException ex) {
		assertTrue(ex.getMessage().contains("frozen"));
	}
	// Didn't get removed
	assertEquals(1, advised.getAdvisors().length);
	pc.setFrozen(false);
	// Can now remove it
	advised.removeAdvisor(0);
	// Check it still works: proxy factory state shouldn't have been corrupted
	assertEquals(target.getAge(), proxied.getAge());
	assertEquals(0, advised.getAdvisors().length);
}
 
Example 11
Source File: AbstractPropertyAccessorTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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 12
Source File: AbstractBeanFactoryTests.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: ConfigurationClassPostConstructAndAutowiringTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Bean
public TestBean beanMethod() {
	beanMethodCallCount++;
	TestBean testBean = new TestBean();
	testBean.setAge(1);
	return testBean;
}
 
Example 14
Source File: AbstractAopProxyTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private void testManyProxies(int howMany) {
	int age1 = 33;
	TestBean target1 = new TestBean();
	target1.setAge(age1);
	ProxyFactory pf1 = new ProxyFactory(target1);
	pf1.addAdvice(new NopInterceptor());
	pf1.addAdvice(new NopInterceptor());
	ITestBean[] proxies = new ITestBean[howMany];
	for (int i = 0; i < howMany; i++) {
		proxies[i] = (ITestBean) createAopProxy(pf1).getProxy();
		assertEquals(age1, proxies[i].getAge());
	}
}
 
Example 15
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 16
Source File: AbstractAopProxyTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testAdviceSupportListeners() throws Throwable {
	TestBean target = new TestBean();
	target.setAge(21);

	ProxyFactory pc = new ProxyFactory(target);
	CountingAdvisorListener l = new CountingAdvisorListener(pc);
	pc.addListener(l);
	RefreshCountingAdvisorChainFactory acf = new RefreshCountingAdvisorChainFactory();
	// Should be automatically added as a listener
	pc.addListener(acf);
	assertFalse(pc.isActive());
	assertEquals(0, l.activates);
	assertEquals(0, acf.refreshes);
	ITestBean proxied = (ITestBean) createProxy(pc);
	assertEquals(1, acf.refreshes);
	assertEquals(1, l.activates);
	assertTrue(pc.isActive());
	assertEquals(target.getAge(), proxied.getAge());
	assertEquals(0, l.adviceChanges);
	NopInterceptor di = new NopInterceptor();
	pc.addAdvice(0, di);
	assertEquals(1, l.adviceChanges);
	assertEquals(2, acf.refreshes);
	assertEquals(target.getAge(), proxied.getAge());
	pc.removeAdvice(di);
	assertEquals(2, l.adviceChanges);
	assertEquals(3, acf.refreshes);
	assertEquals(target.getAge(), proxied.getAge());
	pc.getProxy();
	assertEquals(1, l.activates);

	pc.removeListener(l);
	assertEquals(2, l.adviceChanges);
	pc.addAdvisor(new DefaultPointcutAdvisor(new NopInterceptor()));
	// No longer counting
	assertEquals(2, l.adviceChanges);
}
 
Example 17
Source File: AbstractAopProxyTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Note that an introduction can't throw an unexpected checked exception,
 * as it's constrained by the interface.
 */
@Test
public void testIntroductionThrowsUncheckedException() throws Throwable {
	TestBean target = new TestBean();
	target.setAge(21);
	ProxyFactory pc = new ProxyFactory(target);

	@SuppressWarnings("serial")
	class MyDi extends DelegatingIntroductionInterceptor implements TimeStamped {
		/**
		 * @see test.util.TimeStamped#getTimeStamp()
		 */
		@Override
		public long getTimeStamp() {
			throw new UnsupportedOperationException();
		}
	}
	pc.addAdvisor(new DefaultIntroductionAdvisor(new MyDi()));

	TimeStamped ts = (TimeStamped) createProxy(pc);
	try {
		ts.getTimeStamp();
		fail("Should throw UnsupportedOperationException");
	}
	catch (UnsupportedOperationException ex) {
	}
}
 
Example 18
Source File: ControlFlowPointcutTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Check that we can use a cflow pointcut only in conjunction with
 * a static pointcut: e.g. all setter methods that are invoked under
 * a particular class. This greatly reduces the number of calls
 * to the cflow pointcut, meaning that it's not so prohibitively
 * expensive.
 */
@Test
public void testSelectiveApplication() {
	TestBean target = new TestBean();
	target.setAge(27);
	NopInterceptor nop = new NopInterceptor();
	ControlFlowPointcut cflow = new ControlFlowPointcut(One.class);
	Pointcut settersUnderOne = Pointcuts.intersection(Pointcuts.SETTERS, cflow);
	ProxyFactory pf = new ProxyFactory(target);
	ITestBean proxied = (ITestBean) pf.getProxy();
	pf.addAdvisor(new DefaultPointcutAdvisor(settersUnderOne, nop));

	// Not advised, not under One
	target.setAge(16);
	assertEquals(0, nop.getCount());

	// Not advised; under One but not a setter
	assertEquals(16, new One().getAge(proxied));
	assertEquals(0, nop.getCount());

	// Won't be advised
	new One().set(proxied);
	assertEquals(1, nop.getCount());

	// We saved most evaluations
	assertEquals(1, cflow.getEvaluations());
}
 
Example 19
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 20
Source File: TestBeanCreator.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
public static TestBean createTestBean() {
	TestBean tb = new TestBean();
	tb.setName("Tristan");
	tb.setAge(2);
	return tb;
}