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

The following examples show how to use org.springframework.tests.sample.beans.ITestBean#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: BenchmarkTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
private long testRepeatedAroundAdviceInvocations(String file, int howmany, String technology) {
	ClassPathXmlApplicationContext bf = new ClassPathXmlApplicationContext(file, CLASS);

	StopWatch sw = new StopWatch();
	sw.start(howmany + " repeated around advice invocations with " + technology);
	ITestBean adrian = (ITestBean) bf.getBean("adrian");

	assertTrue(AopUtils.isAopProxy(adrian));
	assertEquals(68, adrian.getAge());

	for (int i = 0; i < howmany; i++) {
		adrian.getAge();
	}

	sw.stop();
	System.out.println(sw.prettyPrint());
	return sw.getLastTaskTimeMillis();
}
 
Example 2
Source File: AbstractAspectJAdvisorFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testAspectMethodThrowsExceptionIllegalOnSignature() {
	TestBean target = new TestBean();
	RemoteException expectedException = new RemoteException();
	List<Advisor> advisors = getFixture().getAdvisors(
			new SingletonMetadataAwareAspectInstanceFactory(new ExceptionAspect(expectedException), "someBean"));
	assertEquals("One advice method was found", 1, advisors.size());
	ITestBean itb = (ITestBean) createProxy(target, advisors, ITestBean.class);

	try {
		itb.getAge();
		fail();
	}
	catch (UndeclaredThrowableException ex) {
		assertSame(expectedException, ex.getCause());
	}
}
 
Example 3
Source File: AopNamespaceHandlerTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testAdviceInvokedCorrectly() throws Exception {
	CountingBeforeAdvice getAgeCounter = (CountingBeforeAdvice) this.context.getBean("getAgeCounter");
	CountingBeforeAdvice getNameCounter = (CountingBeforeAdvice) this.context.getBean("getNameCounter");

	ITestBean bean = getTestBean();

	assertEquals("Incorrect initial getAge count", 0, getAgeCounter.getCalls("getAge"));
	assertEquals("Incorrect initial getName count", 0, getNameCounter.getCalls("getName"));

	bean.getAge();

	assertEquals("Incorrect getAge count on getAge counter", 1, getAgeCounter.getCalls("getAge"));
	assertEquals("Incorrect getAge count on getName counter", 0, getNameCounter.getCalls("getAge"));

	bean.getName();

	assertEquals("Incorrect getName count on getName counter", 1, getNameCounter.getCalls("getName"));
	assertEquals("Incorrect getName count on getAge counter", 0, getAgeCounter.getCalls("getName"));
}
 
Example 4
Source File: AbstractAspectJAdvisorFactoryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testAspectMethodThrowsExceptionLegalOnSignature() {
	TestBean target = new TestBean();
	UnsupportedOperationException expectedException = new UnsupportedOperationException();
	List<Advisor> advisors = getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(new ExceptionAspect(expectedException),"someBean"));
	assertEquals("One advice method was found", 1, advisors.size());
	ITestBean itb = (ITestBean) createProxy(target,
			advisors,
			ITestBean.class);
	try {
		itb.getAge();
		fail();
	}
	catch (UnsupportedOperationException ex) {
		assertSame(expectedException, ex);
	}
}
 
Example 5
Source File: AbstractAspectJAdvisorFactoryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testAspectMethodThrowsExceptionIllegalOnSignature() {
	TestBean target = new TestBean();
	RemoteException expectedException = new RemoteException();
	List<Advisor> advisors = getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(new ExceptionAspect(expectedException),"someBean"));
	assertEquals("One advice method was found", 1, advisors.size());
	ITestBean itb = (ITestBean) createProxy(target,
			advisors,
			ITestBean.class);
	try {
		itb.getAge();
		fail();
	}
	catch (UndeclaredThrowableException ex) {
		assertSame(expectedException, ex.getCause());
	}
}
 
Example 6
Source File: AbstractAopProxyTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testDynamicMethodPointcutThatAppliesStaticallyOnlyToSetters() throws Throwable {
	TestBean tb = new TestBean();
	ProxyFactory pc = new ProxyFactory();
	pc.addInterface(ITestBean.class);
	// Could apply dynamically to getAge/setAge but not to getName
	TestDynamicPointcutForSettersOnly dp = new TestDynamicPointcutForSettersOnly(new NopInterceptor(), "Age");
	pc.addAdvisor(dp);
	this.mockTargetSource.setTarget(tb);
	pc.setTargetSource(mockTargetSource);
	ITestBean it = (ITestBean) createProxy(pc);
	assertEquals(0, dp.count);
	it.getAge();
	// Statically vetoed
	assertEquals(0, dp.count);
	it.setAge(11);
	assertEquals(11, it.getAge());
	assertEquals(1, dp.count);
	// Applies statically but not dynamically
	it.setName("joe");
	assertEquals(1, dp.count);
}
 
Example 7
Source File: AbstractAspectJAdvisorFactoryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testFailureWithoutExplicitDeclarePrecedence() {
	TestBean target = new TestBean();
	MetadataAwareAspectInstanceFactory aspectInstanceFactory = new SingletonMetadataAwareAspectInstanceFactory(
		new NoDeclarePrecedenceShouldFail(), "someBean");
	ITestBean itb = (ITestBean) createProxy(target,
		getFixture().getAdvisors(aspectInstanceFactory), ITestBean.class);
	itb.getAge();
}
 
Example 8
Source File: ProxyFactoryBeanTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Note that we can't add or remove interfaces without reconfiguring the
 * singleton.
 */
@Test
public void testCanAddAndRemoveAdvicesOnSingleton() {
	ITestBean it = (ITestBean) factory.getBean("test1");
	Advised pc = (Advised) it;
	it.getAge();
	NopInterceptor di = new NopInterceptor();
	pc.addAdvice(0, di);
	assertEquals(0, di.getCount());
	it.setAge(25);
	assertEquals(25, it.getAge());
	assertEquals(2, di.getCount());
}
 
Example 9
Source File: AutoProxyCreatorTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testCustomAutoProxyCreator() {
	StaticApplicationContext sac = new StaticApplicationContext();
	sac.registerSingleton("testAutoProxyCreator", TestAutoProxyCreator.class);
	sac.registerSingleton("noInterfaces", NoInterfaces.class);
	sac.registerSingleton("containerCallbackInterfacesOnly", ContainerCallbackInterfacesOnly.class);
	sac.registerSingleton("singletonNoInterceptor", TestBean.class);
	sac.registerSingleton("singletonToBeProxied", TestBean.class);
	sac.registerPrototype("prototypeToBeProxied", TestBean.class);
	sac.refresh();

	MessageSource messageSource = (MessageSource) sac.getBean("messageSource");
	NoInterfaces noInterfaces = (NoInterfaces) sac.getBean("noInterfaces");
	ContainerCallbackInterfacesOnly containerCallbackInterfacesOnly =
			(ContainerCallbackInterfacesOnly) sac.getBean("containerCallbackInterfacesOnly");
	ITestBean singletonNoInterceptor = (ITestBean) sac.getBean("singletonNoInterceptor");
	ITestBean singletonToBeProxied = (ITestBean) sac.getBean("singletonToBeProxied");
	ITestBean prototypeToBeProxied = (ITestBean) sac.getBean("prototypeToBeProxied");
	assertFalse(AopUtils.isCglibProxy(messageSource));
	assertTrue(AopUtils.isCglibProxy(noInterfaces));
	assertTrue(AopUtils.isCglibProxy(containerCallbackInterfacesOnly));
	assertTrue(AopUtils.isCglibProxy(singletonNoInterceptor));
	assertTrue(AopUtils.isCglibProxy(singletonToBeProxied));
	assertTrue(AopUtils.isCglibProxy(prototypeToBeProxied));

	TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("testAutoProxyCreator");
	assertEquals(0, tapc.testInterceptor.nrOfInvocations);
	singletonNoInterceptor.getName();
	assertEquals(0, tapc.testInterceptor.nrOfInvocations);
	singletonToBeProxied.getAge();
	assertEquals(1, tapc.testInterceptor.nrOfInvocations);
	prototypeToBeProxied.getSpouse();
	assertEquals(2, tapc.testInterceptor.nrOfInvocations);
}
 
Example 10
Source File: BenchmarkTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private long testMix(String file, int howmany, String technology) {
	ClassPathXmlApplicationContext bf = new ClassPathXmlApplicationContext(file, CLASS);

	StopWatch sw = new StopWatch();
	sw.start(howmany + " repeated mixed invocations with " + technology);
	ITestBean adrian = (ITestBean) bf.getBean("adrian");

	assertTrue(AopUtils.isAopProxy(adrian));
	Advised a = (Advised) adrian;
	assertTrue(a.getAdvisors().length >= 3);

	for (int i = 0; i < howmany; i++) {
		// Hit all 3 joinpoints
		adrian.getAge();
		adrian.getName();
		adrian.setAge(i);

		// Invoke three non-advised methods
		adrian.getDoctor();
		adrian.getLawyer();
		adrian.getSpouse();
	}

	sw.stop();
	System.out.println(sw.prettyPrint());
	return sw.getLastTaskTimeMillis();
}
 
Example 11
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());
}
 
Example 12
Source File: AutoProxyCreatorTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testCustomAutoProxyCreator() {
	StaticApplicationContext sac = new StaticApplicationContext();
	sac.registerSingleton("testAutoProxyCreator", TestAutoProxyCreator.class);
	sac.registerSingleton("noInterfaces", NoInterfaces.class);
	sac.registerSingleton("containerCallbackInterfacesOnly", ContainerCallbackInterfacesOnly.class);
	sac.registerSingleton("singletonNoInterceptor", TestBean.class);
	sac.registerSingleton("singletonToBeProxied", TestBean.class);
	sac.registerPrototype("prototypeToBeProxied", TestBean.class);
	sac.refresh();

	MessageSource messageSource = (MessageSource) sac.getBean("messageSource");
	NoInterfaces noInterfaces = (NoInterfaces) sac.getBean("noInterfaces");
	ContainerCallbackInterfacesOnly containerCallbackInterfacesOnly =
			(ContainerCallbackInterfacesOnly) sac.getBean("containerCallbackInterfacesOnly");
	ITestBean singletonNoInterceptor = (ITestBean) sac.getBean("singletonNoInterceptor");
	ITestBean singletonToBeProxied = (ITestBean) sac.getBean("singletonToBeProxied");
	ITestBean prototypeToBeProxied = (ITestBean) sac.getBean("prototypeToBeProxied");
	assertFalse(AopUtils.isCglibProxy(messageSource));
	assertTrue(AopUtils.isCglibProxy(noInterfaces));
	assertTrue(AopUtils.isCglibProxy(containerCallbackInterfacesOnly));
	assertTrue(AopUtils.isCglibProxy(singletonNoInterceptor));
	assertTrue(AopUtils.isCglibProxy(singletonToBeProxied));
	assertTrue(AopUtils.isCglibProxy(prototypeToBeProxied));

	TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("testAutoProxyCreator");
	assertEquals(0, tapc.testInterceptor.nrOfInvocations);
	singletonNoInterceptor.getName();
	assertEquals(0, tapc.testInterceptor.nrOfInvocations);
	singletonToBeProxied.getAge();
	assertEquals(1, tapc.testInterceptor.nrOfInvocations);
	prototypeToBeProxied.getSpouse();
	assertEquals(2, tapc.testInterceptor.nrOfInvocations);
}
 
Example 13
Source File: AbstractAopProxyTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testCanCastProxyToProxyConfig() throws Throwable {
	TestBean tb = new TestBean();
	ProxyFactory pc = new ProxyFactory(tb);
	NopInterceptor di = new NopInterceptor();
	pc.addAdvice(0, di);

	ITestBean t = (ITestBean) createProxy(pc);
	assertEquals(0, di.getCount());
	t.setAge(23);
	assertEquals(23, t.getAge());
	assertEquals(2, di.getCount());

	Advised advised = (Advised) t;
	assertEquals("Have 1 advisor", 1, advised.getAdvisors().length);
	assertEquals(di, advised.getAdvisors()[0].getAdvice());
	NopInterceptor di2 = new NopInterceptor();
	advised.addAdvice(1, di2);
	t.getName();
	assertEquals(3, di.getCount());
	assertEquals(1, di2.getCount());
	// will remove di
	advised.removeAdvisor(0);
	t.getAge();
	// Unchanged
	assertEquals(3, di.getCount());
	assertEquals(2, di2.getCount());

	CountingBeforeAdvice cba = new CountingBeforeAdvice();
	assertEquals(0, cba.getCalls());
	advised.addAdvice(cba);
	t.setAge(16);
	assertEquals(16, t.getAge());
	assertEquals(2, cba.getCalls());
}
 
Example 14
Source File: AbstractAopProxyTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testUndeclaredUncheckedException() throws Throwable {
	final RuntimeException unexpectedException = new RuntimeException();
	// Test return value
	MethodInterceptor mi = new MethodInterceptor() {
		@Override
		public Object invoke(MethodInvocation invocation) throws Throwable {
			throw unexpectedException;
		}
	};
	AdvisedSupport pc = new AdvisedSupport(ITestBean.class);
	pc.addAdvice(ExposeInvocationInterceptor.INSTANCE);
	pc.addAdvice(mi);

	// We don't care about the object
	pc.setTarget(new TestBean());
	AopProxy aop = createAopProxy(pc);
	ITestBean tb = (ITestBean) aop.getProxy();

	try {
		// Note: exception param below isn't used
		tb.getAge();
		fail("Should have wrapped exception raised by interceptor");
	}
	catch (RuntimeException thrown) {
		assertEquals("exception matches", unexpectedException, thrown);
	}
}
 
Example 15
Source File: AutoProxyCreatorTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testAutoProxyCreatorWithFallbackToTargetClass() {
	StaticApplicationContext sac = new StaticApplicationContext();
	sac.registerSingleton("testAutoProxyCreator", FallbackTestAutoProxyCreator.class);
	sac.registerSingleton("noInterfaces", NoInterfaces.class);
	sac.registerSingleton("containerCallbackInterfacesOnly", ContainerCallbackInterfacesOnly.class);
	sac.registerSingleton("singletonNoInterceptor", TestBean.class);
	sac.registerSingleton("singletonToBeProxied", TestBean.class);
	sac.registerPrototype("prototypeToBeProxied", TestBean.class);
	sac.refresh();

	MessageSource messageSource = (MessageSource) sac.getBean("messageSource");
	NoInterfaces noInterfaces = (NoInterfaces) sac.getBean("noInterfaces");
	ContainerCallbackInterfacesOnly containerCallbackInterfacesOnly =
			(ContainerCallbackInterfacesOnly) sac.getBean("containerCallbackInterfacesOnly");
	ITestBean singletonNoInterceptor = (ITestBean) sac.getBean("singletonNoInterceptor");
	ITestBean singletonToBeProxied = (ITestBean) sac.getBean("singletonToBeProxied");
	ITestBean prototypeToBeProxied = (ITestBean) sac.getBean("prototypeToBeProxied");
	assertFalse(AopUtils.isCglibProxy(messageSource));
	assertTrue(AopUtils.isCglibProxy(noInterfaces));
	assertTrue(AopUtils.isCglibProxy(containerCallbackInterfacesOnly));
	assertFalse(AopUtils.isCglibProxy(singletonNoInterceptor));
	assertFalse(AopUtils.isCglibProxy(singletonToBeProxied));
	assertFalse(AopUtils.isCglibProxy(prototypeToBeProxied));

	TestAutoProxyCreator tapc = (TestAutoProxyCreator) sac.getBean("testAutoProxyCreator");
	assertEquals(0, tapc.testInterceptor.nrOfInvocations);
	singletonNoInterceptor.getName();
	assertEquals(0, tapc.testInterceptor.nrOfInvocations);
	singletonToBeProxied.getAge();
	assertEquals(1, tapc.testInterceptor.nrOfInvocations);
	prototypeToBeProxied.getSpouse();
	assertEquals(2, tapc.testInterceptor.nrOfInvocations);
}
 
Example 16
Source File: ProxyFactoryBeanTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testMethodPointcuts() {
	ITestBean tb = (ITestBean) factory.getBean("pointcuts");
	PointcutForVoid.reset();
	assertTrue("No methods intercepted", PointcutForVoid.methodNames.isEmpty());
	tb.getAge();
	assertTrue("Not void: shouldn't have intercepted", PointcutForVoid.methodNames.isEmpty());
	tb.setAge(1);
	tb.getAge();
	tb.setName("Tristan");
	tb.toString();
	assertEquals("Recorded wrong number of invocations", 2, PointcutForVoid.methodNames.size());
	assertTrue(PointcutForVoid.methodNames.get(0).equals("setAge"));
	assertTrue(PointcutForVoid.methodNames.get(1).equals("setName"));
}
 
Example 17
Source File: BenchmarkTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
private long testMix(String file, int howmany, String technology) {
	ClassPathXmlApplicationContext bf = new ClassPathXmlApplicationContext(file, CLASS);

	StopWatch sw = new StopWatch();
	sw.start(howmany + " repeated mixed invocations with " + technology);
	ITestBean adrian = (ITestBean) bf.getBean("adrian");

	assertTrue(AopUtils.isAopProxy(adrian));
	Advised a = (Advised) adrian;
	assertTrue(a.getAdvisors().length >= 3);

	for (int i = 0; i < howmany; i++) {
		// Hit all 3 joinpoints
		adrian.getAge();
		adrian.getName();
		adrian.setAge(i);

		// Invoke three non-advised methods
		adrian.getDoctor();
		adrian.getLawyer();
		adrian.getSpouse();
	}

	sw.stop();
	System.out.println(sw.prettyPrint());
	return sw.getLastTaskTimeMillis();
}
 
Example 18
Source File: AbstractAopProxyTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * An interceptor throws a checked exception not on the method signature.
 * For efficiency, we don't bother unifying java.lang.reflect and
 * org.springframework.cglib UndeclaredThrowableException
 */
@Test
public void testUndeclaredCheckedException() throws Throwable {
	final Exception unexpectedException = new Exception();
	// Test return value
	MethodInterceptor mi = new MethodInterceptor() {
		@Override
		public Object invoke(MethodInvocation invocation) throws Throwable {
			throw unexpectedException;
		}
	};
	AdvisedSupport pc = new AdvisedSupport(new Class<?>[] {ITestBean.class});
	pc.addAdvice(ExposeInvocationInterceptor.INSTANCE);
	pc.addAdvice(mi);

	// We don't care about the object
	pc.setTarget(new TestBean());
	AopProxy aop = createAopProxy(pc);
	ITestBean tb = (ITestBean) aop.getProxy();

	try {
		// Note: exception param below isn't used
		tb.getAge();
		fail("Should have wrapped exception raised by interceptor");
	}
	catch (UndeclaredThrowableException thrown) {
		assertEquals("exception matches", unexpectedException, thrown.getUndeclaredThrowable());
	}
	catch (Exception ex) {
		ex.printStackTrace();
		fail("Didn't expect exception: " + ex);
	}
}
 
Example 19
Source File: ControlFlowPointcutTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
int nomatch(ITestBean proxied) {
	return proxied.getAge();
}
 
Example 20
Source File: ControlFlowPointcutTests.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
int getAge(ITestBean proxied) {
	return proxied.getAge();
}