org.springframework.tests.sample.beans.ITestBean Java Examples

The following examples show how to use org.springframework.tests.sample.beans.ITestBean. 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 java-technology-stack with MIT License 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: ControlFlowPointcutTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testMatches() {
	TestBean target = new TestBean();
	target.setAge(27);
	NopInterceptor nop = new NopInterceptor();
	ControlFlowPointcut cflow = new ControlFlowPointcut(One.class, "getAge");
	ProxyFactory pf = new ProxyFactory(target);
	ITestBean proxied = (ITestBean) pf.getProxy();
	pf.addAdvisor(new DefaultPointcutAdvisor(cflow, nop));

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

	// Will be advised
	assertEquals(target.getAge(), new One().getAge(proxied));
	assertEquals(1, nop.getCount());

	// Won't be advised
	assertEquals(target.getAge(), new One().nomatch(proxied));
	assertEquals(1, nop.getCount());
	assertEquals(3, cflow.getEvaluations());
}
 
Example #3
Source File: AspectJAutoProxyCreatorTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
protected void doTestAspectsAndAdvisorAreApplied(ApplicationContext ac, ITestBean shouldBeWeaved) {
	TestBeanAdvisor tba = (TestBeanAdvisor) ac.getBean("advisor");

	MultiplyReturnValue mrv = (MultiplyReturnValue) ac.getBean("aspect");
	assertEquals(3, mrv.getMultiple());

	tba.count = 0;
	mrv.invocations = 0;

	assertTrue("Autoproxying must apply from @AspectJ aspect", AopUtils.isAopProxy(shouldBeWeaved));
	assertEquals("Adrian", shouldBeWeaved.getName());
	assertEquals(0, mrv.invocations);
	assertEquals(34 * mrv.getMultiple(), shouldBeWeaved.getAge());
	assertEquals("Spring advisor must be invoked", 2, tba.count);
	assertEquals("Must be able to hold state in aspect", 1, mrv.invocations);
}
 
Example #4
Source File: JndiObjectFactoryBeanTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testLookupWithProxyInterfaceAndLazyLookup() throws Exception {
	JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
	final TestBean tb = new TestBean();
	jof.setJndiTemplate(new JndiTemplate() {
		@Override
		public Object lookup(String name) {
			if ("foo".equals(name)) {
				tb.setName("tb");
				return tb;
			}
			return null;
		}
	});
	jof.setJndiName("foo");
	jof.setProxyInterface(ITestBean.class);
	jof.setLookupOnStartup(false);
	jof.afterPropertiesSet();
	assertTrue(jof.getObject() instanceof ITestBean);
	ITestBean proxy = (ITestBean) jof.getObject();
	assertNull(tb.getName());
	assertEquals(0, tb.getAge());
	proxy.setAge(99);
	assertEquals("tb", tb.getName());
	assertEquals(99, tb.getAge());
}
 
Example #5
Source File: ExposeBeanNameAdvisorsTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testWithIntroduction() {
	String beanName = "foo";
	TestBean target = new RequiresBeanNameBoundTestBean(beanName);
	ProxyFactory pf = new ProxyFactory(target);
	pf.addAdvisor(ExposeInvocationInterceptor.ADVISOR);
	pf.addAdvisor(ExposeBeanNameAdvisors.createAdvisorIntroducingNamedBean(beanName));
	ITestBean proxy = (ITestBean) pf.getProxy();

	assertTrue("Introduction was made", proxy instanceof NamedBean);
	// Requires binding
	proxy.getAge();

	NamedBean nb = (NamedBean) proxy;
	assertEquals("Name returned correctly", beanName, nb.getBeanName());
}
 
Example #6
Source File: ProxyFactoryBeanTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * The instances are equal, but do not have object identity.
 * Interceptors and interfaces and the target are the same.
 */
@Test
public void testSingletonInstancesAreEqual() {
	ITestBean test1 = (ITestBean) factory.getBean("test1");
	ITestBean test1_1 = (ITestBean) factory.getBean("test1");
	//assertTrue("Singleton instances ==", test1 == test1_1);
	assertEquals("Singleton instances ==", test1, test1_1);
	test1.setAge(25);
	assertEquals(test1.getAge(), test1_1.getAge());
	test1.setAge(250);
	assertEquals(test1.getAge(), test1_1.getAge());
	Advised pc1 = (Advised) test1;
	Advised pc2 = (Advised) test1_1;
	assertArrayEquals(pc1.getAdvisors(), pc2.getAdvisors());
	int oldLength = pc1.getAdvisors().length;
	NopInterceptor di = new NopInterceptor();
	pc1.addAdvice(1, di);
	assertArrayEquals(pc1.getAdvisors(), pc2.getAdvisors());
	assertEquals("Now have one more advisor", oldLength + 1, pc2.getAdvisors().length);
	assertEquals(di.getCount(), 0);
	test1.setAge(5);
	assertEquals(test1_1.getAge(), test1.getAge());
	assertEquals(di.getCount(), 3);
}
 
Example #7
Source File: JndiObjectFactoryBeanTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testLookupWithProxyInterfaceAndDefaultObject() throws Exception {
	JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
	TestBean tb = new TestBean();
	jof.setJndiTemplate(new ExpectedLookupTemplate("foo", tb));
	jof.setJndiName("myFoo");
	jof.setProxyInterface(ITestBean.class);
	jof.setDefaultObject(Boolean.TRUE);
	try {
		jof.afterPropertiesSet();
		fail("Should have thrown IllegalArgumentException");
	}
	catch (IllegalArgumentException ex) {
		// expected
	}
}
 
Example #8
Source File: CauchoRemotingTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void burlapProxyFactoryBeanWithCustomProxyFactory() throws Exception {
	TestBurlapProxyFactory proxyFactory = new TestBurlapProxyFactory();
	BurlapProxyFactoryBean factory = new BurlapProxyFactoryBean();
	factory.setServiceInterface(ITestBean.class);
	factory.setServiceUrl("http://localhosta/testbean");
	factory.setProxyFactory(proxyFactory);
	factory.setUsername("test");
	factory.setPassword("bean");
	factory.setOverloadEnabled(true);
	factory.afterPropertiesSet();

	assertTrue("Correct singleton value", factory.isSingleton());
	assertTrue(factory.getObject() instanceof ITestBean);
	ITestBean bean = (ITestBean) factory.getObject();

	assertEquals(proxyFactory.user, "test");
	assertEquals(proxyFactory.password, "bean");
	assertTrue(proxyFactory.overloadEnabled);

	exception.expect(RemoteAccessException.class);
	bean.setName("test");
}
 
Example #9
Source File: AbstractAopProxyTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Check that the introduction advice isn't allowed to introduce interfaces
 * that are unsupported by the IntroductionInterceptor.
 */
@Test
public void testCannotAddIntroductionAdviceWithUnimplementedInterface() throws Throwable {
	TestBean target = new TestBean();
	target.setAge(21);
	ProxyFactory pc = new ProxyFactory(target);
	try {
		pc.addAdvisor(0, new DefaultIntroductionAdvisor(new TimestampIntroductionInterceptor(), ITestBean.class));
		fail("Shouldn't be able to add introduction advice introducing an unimplemented interface");
	}
	catch (IllegalArgumentException ex) {
		//assertTrue(ex.getMessage().indexOf("ntroduction") > -1);
	}
	// Check it still works: proxy factory state shouldn't have been corrupted
	ITestBean proxied = (ITestBean) createProxy(pc);
	assertEquals(target.getAge(), proxied.getAge());
}
 
Example #10
Source File: MethodInvocationProceedingJoinPointTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testCanGetStaticPartFromJoinPoint() {
	final Object raw = new TestBean();
	ProxyFactory pf = new ProxyFactory(raw);
	pf.addAdvisor(ExposeInvocationInterceptor.ADVISOR);
	pf.addAdvice(new MethodBeforeAdvice() {
		@Override
		public void before(Method method, Object[] args, @Nullable Object target) throws Throwable {
			StaticPart staticPart = AbstractAspectJAdvice.currentJoinPoint().getStaticPart();
			assertEquals("Same static part must be returned on subsequent requests", staticPart, AbstractAspectJAdvice.currentJoinPoint().getStaticPart());
			assertEquals(ProceedingJoinPoint.METHOD_EXECUTION, staticPart.getKind());
			assertSame(AbstractAspectJAdvice.currentJoinPoint().getSignature(), staticPart.getSignature());
			assertEquals(AbstractAspectJAdvice.currentJoinPoint().getSourceLocation(), staticPart.getSourceLocation());
		}
	});
	ITestBean itb = (ITestBean) pf.getProxy();
	// Any call will do
	itb.getAge();
}
 
Example #11
Source File: AbstractAopProxyTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testCanPreventCastToAdvisedUsingOpaque() {
	TestBean target = new TestBean();
	ProxyFactory pc = new ProxyFactory(target);
	pc.setInterfaces(new Class<?>[] {ITestBean.class});
	pc.addAdvice(new NopInterceptor());
	CountingBeforeAdvice mba = new CountingBeforeAdvice();
	Advisor advisor = new DefaultPointcutAdvisor(new NameMatchMethodPointcut().addMethodName("setAge"), mba);
	pc.addAdvisor(advisor);
	assertFalse("Opaque defaults to false", pc.isOpaque());
	pc.setOpaque(true);
	assertTrue("Opaque now true for this config", pc.isOpaque());
	ITestBean proxied = (ITestBean) createProxy(pc);
	proxied.setAge(10);
	assertEquals(10, proxied.getAge());
	assertEquals(1, mba.getCalls());

	assertFalse("Cannot be cast to Advised", proxied instanceof Advised);
}
 
Example #12
Source File: BeforeAdviceBindingTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Before
public void setup() throws Exception {
	ClassPathXmlApplicationContext ctx =
			new ClassPathXmlApplicationContext(getClass().getSimpleName() + ".xml", getClass());

	testBeanProxy = (ITestBean) ctx.getBean("testBean");
	assertTrue(AopUtils.isAopProxy(testBeanProxy));

	// we need the real target too, not just the proxy...
	testBeanTarget = (TestBean) ((Advised) testBeanProxy).getTargetSource().getTarget();

	AdviceBindingTestAspect beforeAdviceAspect = (AdviceBindingTestAspect) ctx.getBean("testAspect");

	mockCollaborator = mock(AdviceBindingCollaborator.class);
	beforeAdviceAspect.setCollaborator(mockCollaborator);
}
 
Example #13
Source File: AbstractAopProxyTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testReentrance() {
	int age1 = 33;

	TestBean target1 = new TestBean();
	ProxyFactory pf1 = new ProxyFactory(target1);
	NopInterceptor di1 = new NopInterceptor();
	pf1.addAdvice(0, di1);
	ITestBean advised1 = (ITestBean) createProxy(pf1);
	advised1.setAge(age1); // = 1 invocation
	advised1.setSpouse(advised1); // = 2 invocations

	assertEquals("one was invoked correct number of times", 2, di1.getCount());

	assertEquals("Advised one has correct age", age1, advised1.getAge()); // = 3 invocations
	assertEquals("one was invoked correct number of times", 3, di1.getCount());

	// = 5 invocations, as reentrant call to spouse is advised also
	assertEquals("Advised spouse has correct age", age1, advised1.getSpouse().getAge());

	assertEquals("one was invoked correct number of times", 5, di1.getCount());
}
 
Example #14
Source File: AbstractPropertyAccessorTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void setPropertyValuesIgnoresInvalidNestedOnRequest() {
	ITestBean target = new TestBean();
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.addPropertyValue(new PropertyValue("name", "rod"));
	pvs.addPropertyValue(new PropertyValue("graceful.rubbish", "tony"));
	pvs.addPropertyValue(new PropertyValue("more.garbage", new Object()));
	AbstractPropertyAccessor accessor = createAccessor(target);
	accessor.setPropertyValues(pvs, true);
	assertTrue("Set valid and ignored invalid", target.getName().equals("rod"));
	try {
		// Don't ignore: should fail
		accessor.setPropertyValues(pvs, false);
		fail("Shouldn't have ignored invalid updates");
	}
	catch (NotWritablePropertyException ex) {
		// OK: but which exception??
	}
}
 
Example #15
Source File: AbstractTransactionAspectTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Check that a transaction is created and committed.
 */
@Test
public void transactionShouldSucceedWithNotNew() throws Exception {
	TransactionAttribute txatt = new DefaultTransactionAttribute();

	MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
	tas.register(getNameMethod, txatt);

	TransactionStatus status = mock(TransactionStatus.class);
	PlatformTransactionManager ptm = mock(PlatformTransactionManager.class);
	// expect a transaction
	given(ptm.getTransaction(txatt)).willReturn(status);

	TestBean tb = new TestBean();
	ITestBean itb = (ITestBean) advised(tb, ptm, tas);

	checkTransactionStatus(false);
	// verification!?
	itb.getName();
	checkTransactionStatus(false);

	verify(ptm).commit(status);
}
 
Example #16
Source File: ProxyFactoryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testInterceptorInclusionMethods() {
	class MyInterceptor implements MethodInterceptor {
		@Override
		public Object invoke(MethodInvocation invocation) throws Throwable {
			throw new UnsupportedOperationException();
		}
	}

	NopInterceptor di = new NopInterceptor();
	NopInterceptor diUnused = new NopInterceptor();
	ProxyFactory factory = new ProxyFactory(new TestBean());
	factory.addAdvice(0, di);
	assertThat(factory.getProxy(), instanceOf(ITestBean.class));
	assertTrue(factory.adviceIncluded(di));
	assertTrue(!factory.adviceIncluded(diUnused));
	assertTrue(factory.countAdvicesOfType(NopInterceptor.class) == 1);
	assertTrue(factory.countAdvicesOfType(MyInterceptor.class) == 0);

	factory.addAdvice(0, diUnused);
	assertTrue(factory.adviceIncluded(diUnused));
	assertTrue(factory.countAdvicesOfType(NopInterceptor.class) == 2);
}
 
Example #17
Source File: CauchoRemotingTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void burlapProxyFactoryBeanWithAuthenticationAndAccessError() throws Exception {
	BurlapProxyFactoryBean factory = new BurlapProxyFactoryBean();
	factory.setServiceInterface(ITestBean.class);
	factory.setServiceUrl("http://localhosta/testbean");
	factory.setUsername("test");
	factory.setPassword("bean");
	factory.setOverloadEnabled(true);
	factory.afterPropertiesSet();

	assertTrue("Correct singleton value", factory.isSingleton());
	assertTrue(factory.getObject() instanceof ITestBean);
	ITestBean bean = (ITestBean) factory.getObject();

	exception.expect(RemoteAccessException.class);
	bean.setName("test");
}
 
Example #18
Source File: JndiObjectFactoryBeanTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testLookupWithProxyInterfaceAndDefaultObject() throws Exception {
	JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
	TestBean tb = new TestBean();
	jof.setJndiTemplate(new ExpectedLookupTemplate("foo", tb));
	jof.setJndiName("myFoo");
	jof.setProxyInterface(ITestBean.class);
	jof.setDefaultObject(Boolean.TRUE);
	try {
		jof.afterPropertiesSet();
		fail("Should have thrown IllegalArgumentException");
	}
	catch (IllegalArgumentException ex) {
		// expected
	}
}
 
Example #19
Source File: CglibProxyTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testProxyAProxyWithAdditionalInterface() {
	ITestBean target = new TestBean();
	mockTargetSource.setTarget(target);

	AdvisedSupport as = new AdvisedSupport();
	as.setTargetSource(mockTargetSource);
	as.addAdvice(new NopInterceptor());
	as.addInterface(Serializable.class);
	CglibAopProxy cglib = new CglibAopProxy(as);

	ITestBean proxy1 = (ITestBean) cglib.getProxy();

	mockTargetSource.setTarget(proxy1);
	as = new AdvisedSupport(new Class<?>[]{});
	as.setTargetSource(mockTargetSource);
	as.addAdvice(new NopInterceptor());
	cglib = new CglibAopProxy(as);

	ITestBean proxy2 = (ITestBean) cglib.getProxy();
	assertTrue(proxy2 instanceof Serializable);
}
 
Example #20
Source File: JdkDynamicProxyTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testProxyIsJustInterface() {
	TestBean raw = new TestBean();
	raw.setAge(32);
	AdvisedSupport pc = new AdvisedSupport(ITestBean.class);
	pc.setTarget(raw);
	JdkDynamicAopProxy aop = new JdkDynamicAopProxy(pc);

	Object proxy = aop.getProxy();
	assertTrue(proxy instanceof ITestBean);
	assertFalse(proxy instanceof TestBean);
}
 
Example #21
Source File: AbstractAopProxyTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultiAdvice() throws Throwable {
	CountingMultiAdvice cca = new CountingMultiAdvice();
	@SuppressWarnings("serial")
	Advisor matchesNoArgs = new StaticMethodMatcherPointcutAdvisor(cca) {
		@Override
		public boolean matches(Method m, Class<?> targetClass) {
			return m.getParameterTypes().length == 0 || "exceptional".equals(m.getName());
		}
	};
	TestBean target = new TestBean();
	target.setAge(80);
	ProxyFactory pf = new ProxyFactory(target);
	pf.addAdvice(new NopInterceptor());
	pf.addAdvisor(matchesNoArgs);
	assertEquals("Advisor was added", matchesNoArgs, pf.getAdvisors()[1]);
	ITestBean proxied = (ITestBean) createProxy(pf);

	assertEquals(0, cca.getCalls());
	assertEquals(0, cca.getCalls("getAge"));
	assertEquals(target.getAge(), proxied.getAge());
	assertEquals(2, cca.getCalls());
	assertEquals(2, cca.getCalls("getAge"));
	assertEquals(0, cca.getCalls("setAge"));
	// Won't be advised
	proxied.setAge(26);
	assertEquals(2, cca.getCalls());
	assertEquals(26, proxied.getAge());
	assertEquals(4, cca.getCalls());
	try {
		proxied.exceptional(new SpecializedUncheckedException("foo", (SQLException)null));
		fail("Should have thrown CannotGetJdbcConnectionException");
	}
	catch (SpecializedUncheckedException ex) {
		// expected
	}
	assertEquals(6, cca.getCalls());
}
 
Example #22
Source File: JndiObjectFactoryBeanTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testLookupWithProxyInterface() throws Exception {
	JndiObjectFactoryBean jof = new JndiObjectFactoryBean();
	TestBean tb = new TestBean();
	jof.setJndiTemplate(new ExpectedLookupTemplate("foo", tb));
	jof.setJndiName("foo");
	jof.setProxyInterface(ITestBean.class);
	jof.afterPropertiesSet();
	assertTrue(jof.getObject() instanceof ITestBean);
	ITestBean proxy = (ITestBean) jof.getObject();
	assertEquals(0, tb.getAge());
	proxy.setAge(99);
	assertEquals(99, tb.getAge());
}
 
Example #23
Source File: AbstractAspectJAdvisorFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
private void testNamedPointcuts(Object aspectInstance) {
	TestBean target = new TestBean();
	int realAge = 65;
	target.setAge(realAge);
	ITestBean itb = (ITestBean) createProxy(target,
			getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(aspectInstance, "someBean")),
			ITestBean.class);
	assertEquals("Around advice must apply", -1, itb.getAge());
	assertEquals(realAge, target.getAge());
}
 
Example #24
Source File: AbstractAopProxyTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testMultiAdvice() throws Throwable {
	CountingMultiAdvice cca = new CountingMultiAdvice();
	@SuppressWarnings("serial")
	Advisor matchesNoArgs = new StaticMethodMatcherPointcutAdvisor(cca) {
		@Override
		public boolean matches(Method m, @Nullable Class<?> targetClass) {
			return m.getParameterCount() == 0 || "exceptional".equals(m.getName());
		}
	};
	TestBean target = new TestBean();
	target.setAge(80);
	ProxyFactory pf = new ProxyFactory(target);
	pf.addAdvice(new NopInterceptor());
	pf.addAdvisor(matchesNoArgs);
	assertEquals("Advisor was added", matchesNoArgs, pf.getAdvisors()[1]);
	ITestBean proxied = (ITestBean) createProxy(pf);

	assertEquals(0, cca.getCalls());
	assertEquals(0, cca.getCalls("getAge"));
	assertEquals(target.getAge(), proxied.getAge());
	assertEquals(2, cca.getCalls());
	assertEquals(2, cca.getCalls("getAge"));
	assertEquals(0, cca.getCalls("setAge"));
	// Won't be advised
	proxied.setAge(26);
	assertEquals(2, cca.getCalls());
	assertEquals(26, proxied.getAge());
	assertEquals(4, cca.getCalls());
	try {
		proxied.exceptional(new SpecializedUncheckedException("foo", (SQLException)null));
		fail("Should have thrown CannotGetJdbcConnectionException");
	}
	catch (SpecializedUncheckedException ex) {
		// expected
	}
	assertEquals(6, cca.getCalls());
}
 
Example #25
Source File: AdvisorAutoProxyCreatorTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testWithOptimizedProxy() throws Exception {
	BeanFactory beanFactory = new ClassPathXmlApplicationContext(OPTIMIZED_CONTEXT, CLASS);

	ITestBean testBean = (ITestBean) beanFactory.getBean("optimizedTestBean");
	assertTrue(AopUtils.isAopProxy(testBean));

	CountingBeforeAdvice beforeAdvice = (CountingBeforeAdvice) beanFactory.getBean("countingAdvice");

	testBean.setAge(23);
	testBean.getAge();

	assertEquals("Incorrect number of calls to proxy", 2, beforeAdvice.getCalls());
}
 
Example #26
Source File: ScopedProxyTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testScopedOverride() throws Exception {
	GenericApplicationContext ctx = new GenericApplicationContext();
	new XmlBeanDefinitionReader(ctx).loadBeanDefinitions(OVERRIDE_CONTEXT);
	SimpleMapScope scope = new SimpleMapScope();
	ctx.getBeanFactory().registerScope("request", scope);
	ctx.refresh();

	ITestBean bean = (ITestBean) ctx.getBean("testBean");
	assertEquals("male", bean.getName());
	assertEquals(99, bean.getAge());

	assertTrue(scope.getMap().containsKey("scopedTarget.testBean"));
	assertEquals(TestBean.class, scope.getMap().get("scopedTarget.testBean").getClass());
}
 
Example #27
Source File: AfterThrowingAdviceBindingTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Before
public void setup() {
	ClassPathXmlApplicationContext ctx =
			new ClassPathXmlApplicationContext(getClass().getSimpleName() + ".xml", getClass());

	testBean = (ITestBean) ctx.getBean("testBean");
	afterThrowingAdviceAspect = (AfterThrowingAdviceBindingTestAspect) ctx.getBean("testAspect");

	mockCollaborator = mock(AfterThrowingAdviceBindingCollaborator.class);
	afterThrowingAdviceAspect.setCollaborator(mockCollaborator);
}
 
Example #28
Source File: PropertyPathFactoryBeanTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testPropertyPathFactoryBeanWithSingletonResult() {
	DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
	new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONTEXT);
	assertEquals(new Integer(12), xbf.getBean("propertyPath1"));
	assertEquals(new Integer(11), xbf.getBean("propertyPath2"));
	assertEquals(new Integer(10), xbf.getBean("tb.age"));
	assertEquals(ITestBean.class, xbf.getType("otb.spouse"));
	Object result1 = xbf.getBean("otb.spouse");
	Object result2 = xbf.getBean("otb.spouse");
	assertTrue(result1 instanceof TestBean);
	assertTrue(result1 == result2);
	assertEquals(99, ((TestBean) result1).getAge());
}
 
Example #29
Source File: ProxyFactoryBeanTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Test invoker is automatically added to manipulate target.
 */
@Test
public void testAutoInvoker() {
	String name = "Hieronymous";
	TestBean target = (TestBean) factory.getBean("test");
	target.setName(name);
	ITestBean autoInvoker = (ITestBean) factory.getBean("autoInvoker");
	assertTrue(autoInvoker.getName().equals(name));
}
 
Example #30
Source File: ProxyFactoryBeanTests.java    From java-technology-stack with MIT License 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());
}