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

The following examples show how to use org.springframework.tests.sample.beans.TestBean#getAge() . 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: CglibProxyTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testAddAdviceAtRuntime() {
	TestBean bean = new TestBean();
	CountingBeforeAdvice cba = new CountingBeforeAdvice();

	ProxyFactory pf = new ProxyFactory();
	pf.setTarget(bean);
	pf.setFrozen(false);
	pf.setOpaque(false);
	pf.setProxyTargetClass(true);

	TestBean proxy = (TestBean) pf.getProxy();
	assertTrue(AopUtils.isCglibProxy(proxy));

	proxy.getAge();
	assertEquals(0, cba.getCalls());

	((Advised) proxy).addAdvice(cba);
	proxy.getAge();
	assertEquals(1, cba.getCalls());
}
 
Example 2
Source File: AutoProxyCreatorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testAutoProxyCreatorWithFactoryBeanAndPrototype() {
	StaticApplicationContext sac = new StaticApplicationContext();
	sac.registerSingleton("testAutoProxyCreator", TestAutoProxyCreator.class);

	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("singleton", "false");
	sac.registerSingleton("prototypeFactoryToBeProxied", DummyFactory.class, pvs);

	sac.refresh();

	TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("testAutoProxyCreator");
	tapc.testInterceptor.nrOfInvocations = 0;

	FactoryBean<?> prototypeFactory = (FactoryBean<?>) sac.getBean("&prototypeFactoryToBeProxied");
	assertTrue(AopUtils.isCglibProxy(prototypeFactory));
	TestBean tb = (TestBean) sac.getBean("prototypeFactoryToBeProxied");
	assertTrue(AopUtils.isCglibProxy(tb));

	assertEquals(2, tapc.testInterceptor.nrOfInvocations);
	tb.getAge();
	assertEquals(3, tapc.testInterceptor.nrOfInvocations);
}
 
Example 3
Source File: AutoProxyCreatorTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testAutoProxyCreatorWithFactoryBean() {
	StaticApplicationContext sac = new StaticApplicationContext();
	sac.registerSingleton("testAutoProxyCreator", TestAutoProxyCreator.class);
	sac.registerSingleton("singletonFactoryToBeProxied", DummyFactory.class);
	sac.refresh();

	TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("testAutoProxyCreator");
	tapc.testInterceptor.nrOfInvocations = 0;

	FactoryBean<?> factory = (FactoryBean<?>) sac.getBean("&singletonFactoryToBeProxied");
	assertTrue(AopUtils.isCglibProxy(factory));

	TestBean tb = (TestBean) sac.getBean("singletonFactoryToBeProxied");
	assertTrue(AopUtils.isCglibProxy(tb));
	assertEquals(2, tapc.testInterceptor.nrOfInvocations);
	tb.getAge();
	assertEquals(3, tapc.testInterceptor.nrOfInvocations);
}
 
Example 4
Source File: CglibProxyTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testAddAdviceAtRuntime() {
	TestBean bean = new TestBean();
	CountingBeforeAdvice cba = new CountingBeforeAdvice();

	ProxyFactory pf = new ProxyFactory();
	pf.setTarget(bean);
	pf.setFrozen(false);
	pf.setOpaque(false);
	pf.setProxyTargetClass(true);

	TestBean proxy = (TestBean) pf.getProxy();
	assertTrue(AopUtils.isCglibProxy(proxy));

	proxy.getAge();
	assertEquals(0, cba.getCalls());

	((Advised) proxy).addAdvice(cba);
	proxy.getAge();
	assertEquals(1, cba.getCalls());
}
 
Example 5
Source File: DataBinderTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Override
public void validate(@Nullable Object obj, Errors errors) {
	TestBean tb = (TestBean) obj;
	if (tb.getAge() < 32) {
		errors.rejectValue("age", "TOO_YOUNG", "simply too young");
	}
	if (tb.getAge() % 2 == 0) {
		errors.rejectValue("age", "AGE_NOT_ODD", "your age isn't odd");
	}
	if (tb.getName() == null || !tb.getName().equals("Rod")) {
		errors.rejectValue("name", "NOT_ROD", "are you sure you're not Rod?");
	}
	if (tb.getTouchy() == null || !tb.getTouchy().equals(tb.getName())) {
		errors.reject("NAME_TOUCHY_MISMATCH", "name and touchy do not match");
	}
	if (tb.getAge() == 0) {
		errors.reject("GENERAL_ERROR", new String[] {"arg"}, "msg");
	}
}
 
Example 6
Source File: DataBinderTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Override
public void validate(Object obj, Errors errors) {
	TestBean tb = (TestBean) obj;
	if (tb.getAge() < 32) {
		errors.rejectValue("age", "TOO_YOUNG", "simply too young");
	}
	if (tb.getAge() % 2 == 0) {
		errors.rejectValue("age", "AGE_NOT_ODD", "your age isn't odd");
	}
	if (tb.getName() == null || !tb.getName().equals("Rod")) {
		errors.rejectValue("name", "NOT_ROD", "are you sure you're not Rod?");
	}
	if (tb.getTouchy() == null || !tb.getTouchy().equals(tb.getName())) {
		errors.reject("NAME_TOUCHY_MISMATCH", "name and touchy do not match");
	}
	if (tb.getAge() == 0) {
		errors.reject("GENERAL_ERROR", new String[] {"arg"}, "msg");
	}
}
 
Example 7
Source File: AutoProxyCreatorTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testAutoProxyCreatorWithFactoryBean() {
	StaticApplicationContext sac = new StaticApplicationContext();
	sac.registerSingleton("testAutoProxyCreator", TestAutoProxyCreator.class);
	sac.registerSingleton("singletonFactoryToBeProxied", DummyFactory.class);
	sac.refresh();

	TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("testAutoProxyCreator");
	tapc.testInterceptor.nrOfInvocations = 0;

	FactoryBean<?> factory = (FactoryBean<?>) sac.getBean("&singletonFactoryToBeProxied");
	assertTrue(AopUtils.isCglibProxy(factory));

	TestBean tb = (TestBean) sac.getBean("singletonFactoryToBeProxied");
	assertTrue(AopUtils.isCglibProxy(tb));
	assertEquals(2, tapc.testInterceptor.nrOfInvocations);
	tb.getAge();
	assertEquals(3, tapc.testInterceptor.nrOfInvocations);
}
 
Example 8
Source File: AutoProxyCreatorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testAutoProxyCreatorWithFactoryBeanAndPrototype() {
	StaticApplicationContext sac = new StaticApplicationContext();
	sac.registerSingleton("testAutoProxyCreator", TestAutoProxyCreator.class);

	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("singleton", "false");
	sac.registerSingleton("prototypeFactoryToBeProxied", DummyFactory.class, pvs);

	sac.refresh();

	TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("testAutoProxyCreator");
	tapc.testInterceptor.nrOfInvocations = 0;

	FactoryBean<?> prototypeFactory = (FactoryBean<?>) sac.getBean("&prototypeFactoryToBeProxied");
	assertTrue(AopUtils.isCglibProxy(prototypeFactory));
	TestBean tb = (TestBean) sac.getBean("prototypeFactoryToBeProxied");
	assertTrue(AopUtils.isCglibProxy(tb));

	assertEquals(2, tapc.testInterceptor.nrOfInvocations);
	tb.getAge();
	assertEquals(3, tapc.testInterceptor.nrOfInvocations);
}
 
Example 9
Source File: AutoProxyCreatorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testAutoProxyCreatorWithFactoryBean() {
	StaticApplicationContext sac = new StaticApplicationContext();
	sac.registerSingleton("testAutoProxyCreator", TestAutoProxyCreator.class);
	sac.registerSingleton("singletonFactoryToBeProxied", DummyFactory.class);
	sac.refresh();

	TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("testAutoProxyCreator");
	tapc.testInterceptor.nrOfInvocations = 0;

	FactoryBean<?> factory = (FactoryBean<?>) sac.getBean("&singletonFactoryToBeProxied");
	assertTrue(AopUtils.isCglibProxy(factory));

	TestBean tb = (TestBean) sac.getBean("singletonFactoryToBeProxied");
	assertTrue(AopUtils.isCglibProxy(tb));
	assertEquals(2, tapc.testInterceptor.nrOfInvocations);
	tb.getAge();
	assertEquals(3, tapc.testInterceptor.nrOfInvocations);
}
 
Example 10
Source File: CglibProxyTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testAddAdviceAtRuntime() {
	TestBean bean = new TestBean();
	CountingBeforeAdvice cba = new CountingBeforeAdvice();

	ProxyFactory pf = new ProxyFactory();
	pf.setTarget(bean);
	pf.setFrozen(false);
	pf.setOpaque(false);
	pf.setProxyTargetClass(true);

	TestBean proxy = (TestBean) pf.getProxy();
	assertTrue(AopUtils.isCglibProxy(proxy));

	proxy.getAge();
	assertEquals(0, cba.getCalls());

	((Advised) proxy).addAdvice(cba);
	proxy.getAge();
	assertEquals(1, cba.getCalls());
}
 
Example 11
Source File: DataBinderTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Override
public void validate(@Nullable Object obj, Errors errors) {
	TestBean tb = (TestBean) obj;
	if (tb.getAge() < 32) {
		errors.rejectValue("age", "TOO_YOUNG", "simply too young");
	}
	if (tb.getAge() % 2 == 0) {
		errors.rejectValue("age", "AGE_NOT_ODD", "your age isn't odd");
	}
	if (tb.getName() == null || !tb.getName().equals("Rod")) {
		errors.rejectValue("name", "NOT_ROD", "are you sure you're not Rod?");
	}
	if (tb.getTouchy() == null || !tb.getTouchy().equals(tb.getName())) {
		errors.reject("NAME_TOUCHY_MISMATCH", "name and touchy do not match");
	}
	if (tb.getAge() == 0) {
		errors.reject("GENERAL_ERROR", new String[] {"arg"}, "msg");
	}
}
 
Example 12
Source File: AspectJExpressionPointcutTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testSimpleAdvice() {
	String expression = "execution(int org.springframework.tests.sample.beans.TestBean.getAge())";
	CallCountingInterceptor interceptor = new CallCountingInterceptor();
	TestBean testBean = getAdvisedProxy(expression, interceptor);

	assertEquals("Calls should be 0", 0, interceptor.getCount());
	testBean.getAge();
	assertEquals("Calls should be 1", 1, interceptor.getCount());
	testBean.setAge(90);
	assertEquals("Calls should still be 1", 1, interceptor.getCount());
}
 
Example 13
Source File: AutoProxyCreatorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testBeanNameAutoProxyCreatorWithFactoryBeanProxy() {
	StaticApplicationContext sac = new StaticApplicationContext();
	sac.registerSingleton("testInterceptor", TestInterceptor.class);

	RootBeanDefinition proxyCreator = new RootBeanDefinition(BeanNameAutoProxyCreator.class);
	proxyCreator.getPropertyValues().add("interceptorNames", "testInterceptor");
	proxyCreator.getPropertyValues().add("beanNames", "singletonToBeProxied,&singletonFactoryToBeProxied");
	sac.getDefaultListableBeanFactory().registerBeanDefinition("beanNameAutoProxyCreator", proxyCreator);

	RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
	sac.getDefaultListableBeanFactory().registerBeanDefinition("singletonToBeProxied", bd);

	sac.registerSingleton("singletonFactoryToBeProxied", DummyFactory.class);

	sac.refresh();

	ITestBean singletonToBeProxied = (ITestBean) sac.getBean("singletonToBeProxied");
	assertTrue(Proxy.isProxyClass(singletonToBeProxied.getClass()));

	TestInterceptor ti = (TestInterceptor) sac.getBean("testInterceptor");
	int initialNr = ti.nrOfInvocations;
	singletonToBeProxied.getName();
	assertEquals(initialNr + 1, ti.nrOfInvocations);

	FactoryBean<?> factory = (FactoryBean<?>) sac.getBean("&singletonFactoryToBeProxied");
	assertTrue(Proxy.isProxyClass(factory.getClass()));
	TestBean tb = (TestBean) sac.getBean("singletonFactoryToBeProxied");
	assertFalse(AopUtils.isAopProxy(tb));
	assertEquals(initialNr + 3, ti.nrOfInvocations);
	tb.getAge();
	assertEquals(initialNr + 3, ti.nrOfInvocations);
}
 
Example 14
Source File: DataBinderTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Override
public void validate(@Nullable Object obj, Errors errors) {
	TestBean tb = (TestBean) obj;
	if (tb == null || "XXX".equals(tb.getName())) {
		errors.rejectValue("", "SPOUSE_NOT_AVAILABLE");
		return;
	}
	if (tb.getAge() < 32) {
		errors.rejectValue("age", "TOO_YOUNG", "simply too young");
	}
}
 
Example 15
Source File: DataBinderTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Override
public void validate(@Nullable Object obj, Errors errors) {
	TestBean tb = (TestBean) obj;
	if (tb == null || "XXX".equals(tb.getName())) {
		errors.rejectValue("", "SPOUSE_NOT_AVAILABLE");
		return;
	}
	if (tb.getAge() < 32) {
		errors.rejectValue("age", "TOO_YOUNG", "simply too young");
	}
}
 
Example 16
Source File: AutoProxyCreatorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testBeanNameAutoProxyCreatorWithFactoryBeanProxy() {
	StaticApplicationContext sac = new StaticApplicationContext();
	sac.registerSingleton("testInterceptor", TestInterceptor.class);

	RootBeanDefinition proxyCreator = new RootBeanDefinition(BeanNameAutoProxyCreator.class);
	proxyCreator.getPropertyValues().add("interceptorNames", "testInterceptor");
	proxyCreator.getPropertyValues().add("beanNames", "singletonToBeProxied,&singletonFactoryToBeProxied");
	sac.getDefaultListableBeanFactory().registerBeanDefinition("beanNameAutoProxyCreator", proxyCreator);

	RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
	sac.getDefaultListableBeanFactory().registerBeanDefinition("singletonToBeProxied", bd);

	sac.registerSingleton("singletonFactoryToBeProxied", DummyFactory.class);

	sac.refresh();

	ITestBean singletonToBeProxied = (ITestBean) sac.getBean("singletonToBeProxied");
	assertTrue(Proxy.isProxyClass(singletonToBeProxied.getClass()));

	TestInterceptor ti = (TestInterceptor) sac.getBean("testInterceptor");
	int initialNr = ti.nrOfInvocations;
	singletonToBeProxied.getName();
	assertEquals(initialNr + 1, ti.nrOfInvocations);

	FactoryBean<?> factory = (FactoryBean<?>) sac.getBean("&singletonFactoryToBeProxied");
	assertTrue(Proxy.isProxyClass(factory.getClass()));
	TestBean tb = (TestBean) sac.getBean("singletonFactoryToBeProxied");
	assertFalse(AopUtils.isAopProxy(tb));
	assertEquals(initialNr + 3, ti.nrOfInvocations);
	tb.getAge();
	assertEquals(initialNr + 3, ti.nrOfInvocations);
}
 
Example 17
Source File: AspectJExpressionPointcutTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testSimpleAdvice() {
	String expression = "execution(int org.springframework.tests.sample.beans.TestBean.getAge())";
	CallCountingInterceptor interceptor = new CallCountingInterceptor();
	TestBean testBean = getAdvisedProxy(expression, interceptor);

	assertEquals("Calls should be 0", 0, interceptor.getCount());
	testBean.getAge();
	assertEquals("Calls should be 1", 1, interceptor.getCount());
	testBean.setAge(90);
	assertEquals("Calls should still be 1", 1, interceptor.getCount());
}
 
Example 18
Source File: AutoProxyCreatorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testAutoProxyCreatorWithFactoryBeanAndProxyObjectOnly() {
	StaticApplicationContext sac = new StaticApplicationContext();

	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.add("proxyFactoryBean", "false");
	sac.registerSingleton("testAutoProxyCreator", TestAutoProxyCreator.class, pvs);

	sac.registerSingleton("singletonFactoryToBeProxied", DummyFactory.class);

	sac.refresh();

	TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("testAutoProxyCreator");
	tapc.testInterceptor.nrOfInvocations = 0;

	FactoryBean<?> factory = (FactoryBean<?>) sac.getBean("&singletonFactoryToBeProxied");
	assertFalse(AopUtils.isAopProxy(factory));

	TestBean tb = (TestBean) sac.getBean("singletonFactoryToBeProxied");
	assertTrue(AopUtils.isCglibProxy(tb));
	assertEquals(0, tapc.testInterceptor.nrOfInvocations);
	tb.getAge();
	assertEquals(1, tapc.testInterceptor.nrOfInvocations);

	TestBean tb2 = (TestBean) sac.getBean("singletonFactoryToBeProxied");
	assertSame(tb, tb2);
	assertEquals(1, tapc.testInterceptor.nrOfInvocations);
	tb2.getAge();
	assertEquals(2, tapc.testInterceptor.nrOfInvocations);
}
 
Example 19
Source File: AutoProxyCreatorTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testBeanNameAutoProxyCreatorWithFactoryBeanProxy() {
	StaticApplicationContext sac = new StaticApplicationContext();
	sac.registerSingleton("testInterceptor", TestInterceptor.class);

	RootBeanDefinition proxyCreator = new RootBeanDefinition(BeanNameAutoProxyCreator.class);
	proxyCreator.getPropertyValues().add("interceptorNames", "testInterceptor");
	proxyCreator.getPropertyValues().add("beanNames", "singletonToBeProxied,&singletonFactoryToBeProxied");
	sac.getDefaultListableBeanFactory().registerBeanDefinition("beanNameAutoProxyCreator", proxyCreator);

	RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
	sac.getDefaultListableBeanFactory().registerBeanDefinition("singletonToBeProxied", bd);

	sac.registerSingleton("singletonFactoryToBeProxied", DummyFactory.class);

	sac.refresh();

	ITestBean singletonToBeProxied = (ITestBean) sac.getBean("singletonToBeProxied");
	assertTrue(Proxy.isProxyClass(singletonToBeProxied.getClass()));

	TestInterceptor ti = (TestInterceptor) sac.getBean("testInterceptor");
	int initialNr = ti.nrOfInvocations;
	singletonToBeProxied.getName();
	assertEquals(initialNr + 1, ti.nrOfInvocations);

	FactoryBean<?> factory = (FactoryBean<?>) sac.getBean("&singletonFactoryToBeProxied");
	assertTrue(Proxy.isProxyClass(factory.getClass()));
	TestBean tb = (TestBean) sac.getBean("singletonFactoryToBeProxied");
	assertFalse(AopUtils.isAopProxy(tb));
	assertEquals(initialNr + 3, ti.nrOfInvocations);
	tb.getAge();
	assertEquals(initialNr + 3, ti.nrOfInvocations);
}
 
Example 20
Source File: IntroductionBenchmarkTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void timeManyInvocations() {
	StopWatch sw = new StopWatch();

	TestBean target = new TestBean();
	ProxyFactory pf = new ProxyFactory(target);
	pf.setProxyTargetClass(false);
	pf.addAdvice(new SimpleCounterIntroduction());
	ITestBean proxy = (ITestBean) pf.getProxy();

	Counter counter = (Counter) proxy;

	sw.start(INVOCATIONS + " invocations on proxy, not hitting introduction");
	for (int i = 0; i < INVOCATIONS; i++) {
		proxy.getAge();
	}
	sw.stop();

	sw.start(INVOCATIONS + " invocations on proxy, hitting introduction");
	for (int i = 0; i < INVOCATIONS; i++) {
		counter.getCount();
	}
	sw.stop();

	sw.start(INVOCATIONS + " invocations on target");
	for (int i = 0; i < INVOCATIONS; i++) {
		target.getAge();
	}
	sw.stop();

	System.out.println(sw.prettyPrint());
}