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

The following examples show how to use org.springframework.tests.sample.beans.TestBean#getSpouse() . 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: XmlBeanFactoryTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testRefToSeparatePrototypeInstances() throws Exception {
	DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
	XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
	reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE);
	reader.loadBeanDefinitions(REFTYPES_CONTEXT);

	TestBean emma = (TestBean) xbf.getBean("emma");
	TestBean georgia = (TestBean) xbf.getBean("georgia");
	ITestBean emmasJenks = emma.getSpouse();
	ITestBean georgiasJenks = georgia.getSpouse();
	assertTrue("Emma and georgia think they have a different boyfriend", emmasJenks != georgiasJenks);
	assertTrue("Emmas jenks has right name", emmasJenks.getName().equals("Andrew"));
	assertTrue("Emmas doesn't equal new ref", emmasJenks != xbf.getBean("jenks"));
	assertTrue("Georgias jenks has right name", emmasJenks.getName().equals("Andrew"));
	assertTrue("They are object equal", emmasJenks.equals(georgiasJenks));
	assertTrue("They object equal direct ref", emmasJenks.equals(xbf.getBean("jenks")));
}
 
Example 2
Source File: AbstractAspectJAdvisorFactoryTests.java    From spring-analysis-note 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 3
Source File: XmlBeanFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testRefToSeparatePrototypeInstances() throws Exception {
	DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
	XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
	reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE);
	reader.loadBeanDefinitions(REFTYPES_CONTEXT);

	TestBean emma = (TestBean) xbf.getBean("emma");
	TestBean georgia = (TestBean) xbf.getBean("georgia");
	ITestBean emmasJenks = emma.getSpouse();
	ITestBean georgiasJenks = georgia.getSpouse();
	assertTrue("Emma and georgia think they have a different boyfriend", emmasJenks != georgiasJenks);
	assertTrue("Emmas jenks has right name", emmasJenks.getName().equals("Andrew"));
	assertTrue("Emmas doesn't equal new ref", emmasJenks != xbf.getBean("jenks"));
	assertTrue("Georgias jenks has right name", emmasJenks.getName().equals("Andrew"));
	assertTrue("They are object equal", emmasJenks.equals(georgiasJenks));
	assertTrue("They object equal direct ref", emmasJenks.equals(xbf.getBean("jenks")));
}
 
Example 4
Source File: XmlBeanFactoryTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testRefToSeparatePrototypeInstances() {
	DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
	XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
	reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE);
	reader.loadBeanDefinitions(REFTYPES_CONTEXT);

	TestBean emma = (TestBean) xbf.getBean("emma");
	TestBean georgia = (TestBean) xbf.getBean("georgia");
	ITestBean emmasJenks = emma.getSpouse();
	ITestBean georgiasJenks = georgia.getSpouse();
	assertTrue("Emma and georgia think they have a different boyfriend", emmasJenks != georgiasJenks);
	assertTrue("Emmas jenks has right name", emmasJenks.getName().equals("Andrew"));
	assertTrue("Emmas doesn't equal new ref", emmasJenks != xbf.getBean("jenks"));
	assertTrue("Georgias jenks has right name", emmasJenks.getName().equals("Andrew"));
	assertTrue("They are object equal", emmasJenks.equals(georgiasJenks));
	assertTrue("They object equal direct ref", emmasJenks.equals(xbf.getBean("jenks")));
}
 
Example 5
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 6
Source File: CustomEditorTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testComplexObjectWithOldValueAccess() {
	TestBean tb = new TestBean();
	String newName = "Rod";
	String tbString = "Kerry_34";

	BeanWrapper bw = new BeanWrapperImpl(tb);
	bw.setExtractOldValueForEditor(true);
	bw.registerCustomEditor(ITestBean.class, new OldValueAccessingTestBeanEditor());
	MutablePropertyValues pvs = new MutablePropertyValues();
	pvs.addPropertyValue(new PropertyValue("age", new Integer(55)));
	pvs.addPropertyValue(new PropertyValue("name", newName));
	pvs.addPropertyValue(new PropertyValue("touchy", "valid"));
	pvs.addPropertyValue(new PropertyValue("spouse", tbString));

	bw.setPropertyValues(pvs);
	assertTrue("spouse is non-null", tb.getSpouse() != null);
	assertTrue("spouse name is Kerry and age is 34",
			tb.getSpouse().getName().equals("Kerry") && tb.getSpouse().getAge() == 34);
	ITestBean spouse = tb.getSpouse();

	bw.setPropertyValues(pvs);
	assertSame("Should have remained same object", spouse, tb.getSpouse());
}
 
Example 7
Source File: RequestScopeTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void requestScopedInnerBeanDestroyedWhileContainedBySingleton() throws Exception {
	MockHttpServletRequest request = new MockHttpServletRequest();
	ServletRequestAttributes requestAttributes = new ServletRequestAttributes(request);
	RequestContextHolder.setRequestAttributes(requestAttributes);

	String outerBeanName = "singletonOuterBean";
	TestBean outer1 = (TestBean) this.beanFactory.getBean(outerBeanName);
	assertNull(request.getAttribute(outerBeanName));
	TestBean inner1 = (TestBean) outer1.getSpouse();
	TestBean outer2 = (TestBean) this.beanFactory.getBean(outerBeanName);
	assertSame(outer1, outer2);
	assertSame(inner1, outer2.getSpouse());
	requestAttributes.requestCompleted();
	assertTrue(inner1.wasDestroyed());
	assertFalse(outer1.wasDestroyed());
}
 
Example 8
Source File: DefaultListableBeanFactoryTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testSimpleReference() {
	String PREFIX = "beans.";
	DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
	Properties p = new Properties();

	p.setProperty(PREFIX + "rod.(class)", TestBean.class.getName());
	p.setProperty(PREFIX + "rod.name", "Rod");

	p.setProperty(PREFIX + "kerry.(class)", TestBean.class.getName());
	p.setProperty(PREFIX + "kerry.name", "Kerry");
	p.setProperty(PREFIX + "kerry.age", "35");
	p.setProperty(PREFIX + "kerry.spouse(ref)", "rod");

	int count = (new PropertiesBeanDefinitionReader(lbf)).registerBeanDefinitions(p, PREFIX);
	assertTrue("2 beans registered, not " + count, count == 2);

	TestBean kerry = lbf.getBean("kerry", TestBean.class);
	assertTrue("Kerry name is Kerry", "Kerry".equals(kerry.getName()));
	ITestBean spouse = kerry.getSpouse();
	assertTrue("Kerry spouse is non null", spouse != null);
	assertTrue("Kerry spouse name is Rod", "Rod".equals(spouse.getName()));
}
 
Example 9
Source File: XmlBeanFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testInnerBeansWithoutDestroy() {
	DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
	XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
	reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE);
	reader.loadBeanDefinitions(REFTYPES_CONTEXT);

	// Let's create the outer bean named "innerBean",
	// to check whether it doesn't create any conflicts
	// with the actual inner beans named "innerBean".
	xbf.getBean("innerBean");

	TestBean hasInnerBeans = (TestBean) xbf.getBean("hasInnerBeansWithoutDestroy");
	assertEquals(5, hasInnerBeans.getAge());
	TestBean inner1 = (TestBean) hasInnerBeans.getSpouse();
	assertNotNull(inner1);
	assertTrue(inner1.getBeanName().startsWith("innerBean"));
	assertEquals("inner1", inner1.getName());
	assertEquals(6, inner1.getAge());

	assertNotNull(hasInnerBeans.getFriends());
	Object[] friends = hasInnerBeans.getFriends().toArray();
	assertEquals(3, friends.length);
	DerivedTestBean inner2 = (DerivedTestBean) friends[0];
	assertEquals("inner2", inner2.getName());
	assertTrue(inner2.getBeanName().startsWith(DerivedTestBean.class.getName()));
	assertNotNull(inner2);
	assertEquals(7, inner2.getAge());
	TestBean innerFactory = (TestBean) friends[1];
	assertEquals(DummyFactory.SINGLETON_NAME, innerFactory.getName());
	TestBean inner5 = (TestBean) friends[2];
	assertTrue(inner5.getBeanName().startsWith("innerBean"));
}
 
Example 10
Source File: AbstractAspectJAdvisorFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testPerTypeWithinAspect() throws SecurityException, NoSuchMethodException {
	TestBean target = new TestBean();
	int realAge = 65;
	target.setAge(realAge);
	PerTypeWithinAspectInstanceFactory aif = new PerTypeWithinAspectInstanceFactory();
	TestBean itb = (TestBean) createProxy(target, getFixture().getAdvisors(aif), TestBean.class);
	assertEquals("No method calls", 0, aif.getInstantiationCount());
	assertEquals("Around advice must now apply", 0, itb.getAge());

	Advised advised = (Advised) itb;
	// Will be ExposeInvocationInterceptor, synthetic instantiation advisor, 2 method advisors
	assertEquals(4, advised.getAdvisors().length);
	ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor sia =
			(ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor) advised.getAdvisors()[1];
	assertTrue(sia.getPointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null));
	InstantiationModelAwarePointcutAdvisorImpl imapa = (InstantiationModelAwarePointcutAdvisorImpl) advised.getAdvisors()[2];
	LazySingletonAspectInstanceFactoryDecorator maaif =
			(LazySingletonAspectInstanceFactoryDecorator) imapa.getAspectInstanceFactory();
	assertTrue(maaif.isMaterialized());

	// Check that the perclause pointcut is valid
	assertTrue(maaif.getAspectMetadata().getPerClausePointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null));
	assertNotSame(imapa.getDeclaredPointcut(), imapa.getPointcut());

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

	assertTrue(maaif.isMaterialized());

	assertTrue(imapa.getDeclaredPointcut().getMethodMatcher().matches(TestBean.class.getMethod("getAge"), null));

	assertEquals("Around advice must still apply", 1, itb.getAge());
	assertEquals("Around advice must still apply", 2, itb.getAge());

	TestBean itb2 = (TestBean) createProxy(target, getFixture().getAdvisors(aif), TestBean.class);
	assertEquals(1, aif.getInstantiationCount());
	assertEquals("Around advice be independent for second instance", 0, itb2.getAge());
	assertEquals(2, aif.getInstantiationCount());
}
 
Example 11
Source File: AbstractAspectJAdvisorFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testPerThisAspect() throws SecurityException, NoSuchMethodException {
	TestBean target = new TestBean();
	int realAge = 65;
	target.setAge(realAge);
	TestBean itb = (TestBean) createProxy(target,
			getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(new PerThisAspect(), "someBean")),
			TestBean.class);
	assertEquals("Around advice must NOT apply", realAge, itb.getAge());

	Advised advised = (Advised) itb;
	// Will be ExposeInvocationInterceptor, synthetic instantiation advisor, 2 method advisors
	assertEquals(4, advised.getAdvisors().length);
	ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor sia =
			(ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor) advised.getAdvisors()[1];
	assertTrue(sia.getPointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null));
	InstantiationModelAwarePointcutAdvisorImpl imapa = (InstantiationModelAwarePointcutAdvisorImpl) advised.getAdvisors()[2];
	LazySingletonAspectInstanceFactoryDecorator maaif =
			(LazySingletonAspectInstanceFactoryDecorator) imapa.getAspectInstanceFactory();
	assertFalse(maaif.isMaterialized());

	// Check that the perclause pointcut is valid
	assertTrue(maaif.getAspectMetadata().getPerClausePointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null));
	assertNotSame(imapa.getDeclaredPointcut(), imapa.getPointcut());

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

	assertTrue(maaif.isMaterialized());

	assertTrue(imapa.getDeclaredPointcut().getMethodMatcher().matches(TestBean.class.getMethod("getAge"), null));

	assertEquals("Around advice must apply", 0, itb.getAge());
	assertEquals("Around advice must apply", 1, itb.getAge());
}
 
Example 12
Source File: SimplePropertyNamespaceHandlerTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void innerBeanConfigured() throws Exception {
	DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
	new XmlBeanDefinitionReader(beanFactory).loadBeanDefinitions(
			new ClassPathResource("simplePropertyNamespaceHandlerTests.xml", getClass()));
	TestBean sally = (TestBean) beanFactory.getBean("sally2");
	ITestBean rob = sally.getSpouse();
	assertEquals("Rob Harrop", rob.getName());
	assertEquals(24, rob.getAge());
	assertEquals(rob.getSpouse(), sally);
}
 
Example 13
Source File: AbstractAspectJAdvisorFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testPerTargetAspect() throws SecurityException, NoSuchMethodException {
	TestBean target = new TestBean();
	int realAge = 65;
	target.setAge(realAge);
	TestBean itb = (TestBean) createProxy(target,
			getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(new PerTargetAspect(), "someBean")),
			TestBean.class);
	assertEquals("Around advice must NOT apply", realAge, itb.getAge());

	Advised advised = (Advised) itb;
	ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor sia =
			(ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor) advised.getAdvisors()[1];
	assertTrue(sia.getPointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null));
	InstantiationModelAwarePointcutAdvisorImpl imapa = (InstantiationModelAwarePointcutAdvisorImpl) advised.getAdvisors()[3];
	LazySingletonAspectInstanceFactoryDecorator maaif =
			(LazySingletonAspectInstanceFactoryDecorator) imapa.getAspectInstanceFactory();
	assertFalse(maaif.isMaterialized());

	// Check that the perclause pointcut is valid
	assertTrue(maaif.getAspectMetadata().getPerClausePointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null));
	assertNotSame(imapa.getDeclaredPointcut(), imapa.getPointcut());

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

	assertTrue(maaif.isMaterialized());

	assertEquals("Around advice must apply", 0, itb.getAge());
	assertEquals("Around advice must apply", 1, itb.getAge());
}
 
Example 14
Source File: SimplePropertyNamespaceHandlerTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void innerBeanConfigured() throws Exception {
	DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
	new XmlBeanDefinitionReader(beanFactory).loadBeanDefinitions(
			new ClassPathResource("simplePropertyNamespaceHandlerTests.xml", getClass()));
	TestBean sally = (TestBean) beanFactory.getBean("sally2");
	ITestBean rob = sally.getSpouse();
	assertEquals("Rob Harrop", rob.getName());
	assertEquals(24, rob.getAge());
	assertEquals(rob.getSpouse(), sally);
}
 
Example 15
Source File: XmlBeanFactoryTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testInnerBeansWithoutDestroy() {
	DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
	XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
	reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE);
	reader.loadBeanDefinitions(REFTYPES_CONTEXT);

	// Let's create the outer bean named "innerBean",
	// to check whether it doesn't create any conflicts
	// with the actual inner beans named "innerBean".
	xbf.getBean("innerBean");

	TestBean hasInnerBeans = (TestBean) xbf.getBean("hasInnerBeansWithoutDestroy");
	assertEquals(5, hasInnerBeans.getAge());
	TestBean inner1 = (TestBean) hasInnerBeans.getSpouse();
	assertNotNull(inner1);
	assertTrue(inner1.getBeanName().startsWith("innerBean"));
	assertEquals("inner1", inner1.getName());
	assertEquals(6, inner1.getAge());

	assertNotNull(hasInnerBeans.getFriends());
	Object[] friends = hasInnerBeans.getFriends().toArray();
	assertEquals(3, friends.length);
	DerivedTestBean inner2 = (DerivedTestBean) friends[0];
	assertEquals("inner2", inner2.getName());
	assertTrue(inner2.getBeanName().startsWith(DerivedTestBean.class.getName()));
	assertNotNull(inner2);
	assertEquals(7, inner2.getAge());
	TestBean innerFactory = (TestBean) friends[1];
	assertEquals(DummyFactory.SINGLETON_NAME, innerFactory.getName());
	TestBean inner5 = (TestBean) friends[2];
	assertTrue(inner5.getBeanName().startsWith("innerBean"));
}
 
Example 16
Source File: AbstractAspectJAdvisorFactoryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testPerThisAspect() throws SecurityException, NoSuchMethodException {
	TestBean target = new TestBean();
	int realAge = 65;
	target.setAge(realAge);
	TestBean itb = (TestBean) createProxy(target,
			getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(new PerThisAspect(), "someBean")),
			TestBean.class);
	assertEquals("Around advice must NOT apply", realAge, itb.getAge());

	Advised advised = (Advised) itb;
	// Will be ExposeInvocationInterceptor, synthetic instantiation advisor, 2 method advisors
	assertEquals(4, advised.getAdvisors().length);
	SyntheticInstantiationAdvisor sia = (SyntheticInstantiationAdvisor) advised.getAdvisors()[1];
	assertTrue(sia.getPointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null));
	InstantiationModelAwarePointcutAdvisorImpl imapa = (InstantiationModelAwarePointcutAdvisorImpl) advised.getAdvisors()[2];
	LazySingletonAspectInstanceFactoryDecorator maaif =
			(LazySingletonAspectInstanceFactoryDecorator) imapa.getAspectInstanceFactory();
	assertFalse(maaif.isMaterialized());

	// Check that the perclause pointcut is valid
	assertTrue(maaif.getAspectMetadata().getPerClausePointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null));
	assertNotSame(imapa.getDeclaredPointcut(), imapa.getPointcut());

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

	assertTrue(maaif.isMaterialized());

	assertTrue(imapa.getDeclaredPointcut().getMethodMatcher().matches(TestBean.class.getMethod("getAge"), null));

	assertEquals("Around advice must apply", 0, itb.getAge());
	assertEquals("Around advice must apply", 1, itb.getAge());
}
 
Example 17
Source File: AbstractAspectJAdvisorFactoryTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testPerTypeWithinAspect() throws SecurityException, NoSuchMethodException {
	TestBean target = new TestBean();
	int realAge = 65;
	target.setAge(realAge);
	PerTypeWithinAspectInstanceFactory aif = new PerTypeWithinAspectInstanceFactory();
	TestBean itb = (TestBean) createProxy(target, getFixture().getAdvisors(aif), TestBean.class);
	assertEquals("No method calls", 0, aif.getInstantiationCount());
	assertEquals("Around advice must now apply", 0, itb.getAge());

	Advised advised = (Advised) itb;
	// Will be ExposeInvocationInterceptor, synthetic instantiation advisor, 2 method advisors
	assertEquals(4, advised.getAdvisors().length);
	ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor sia =
			(ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor) advised.getAdvisors()[1];
	assertTrue(sia.getPointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null));
	InstantiationModelAwarePointcutAdvisorImpl imapa = (InstantiationModelAwarePointcutAdvisorImpl) advised.getAdvisors()[2];
	LazySingletonAspectInstanceFactoryDecorator maaif =
			(LazySingletonAspectInstanceFactoryDecorator) imapa.getAspectInstanceFactory();
	assertTrue(maaif.isMaterialized());

	// Check that the perclause pointcut is valid
	assertTrue(maaif.getAspectMetadata().getPerClausePointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null));
	assertNotSame(imapa.getDeclaredPointcut(), imapa.getPointcut());

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

	assertTrue(maaif.isMaterialized());

	assertTrue(imapa.getDeclaredPointcut().getMethodMatcher().matches(TestBean.class.getMethod("getAge"), null));

	assertEquals("Around advice must still apply", 1, itb.getAge());
	assertEquals("Around advice must still apply", 2, itb.getAge());

	TestBean itb2 = (TestBean) createProxy(target, getFixture().getAdvisors(aif), TestBean.class);
	assertEquals(1, aif.getInstantiationCount());
	assertEquals("Around advice be independent for second instance", 0, itb2.getAge());
	assertEquals(2, aif.getInstantiationCount());
}
 
Example 18
Source File: AbstractAspectJAdvisorFactoryTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testPerThisAspect() throws SecurityException, NoSuchMethodException {
	TestBean target = new TestBean();
	int realAge = 65;
	target.setAge(realAge);
	TestBean itb = (TestBean) createProxy(target,
			getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(new PerThisAspect(), "someBean")),
			TestBean.class);
	assertEquals("Around advice must NOT apply", realAge, itb.getAge());

	Advised advised = (Advised) itb;
	// Will be ExposeInvocationInterceptor, synthetic instantiation advisor, 2 method advisors
	assertEquals(4, advised.getAdvisors().length);
	ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor sia =
			(ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor) advised.getAdvisors()[1];
	assertTrue(sia.getPointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null));
	InstantiationModelAwarePointcutAdvisorImpl imapa = (InstantiationModelAwarePointcutAdvisorImpl) advised.getAdvisors()[2];
	LazySingletonAspectInstanceFactoryDecorator maaif =
			(LazySingletonAspectInstanceFactoryDecorator) imapa.getAspectInstanceFactory();
	assertFalse(maaif.isMaterialized());

	// Check that the perclause pointcut is valid
	assertTrue(maaif.getAspectMetadata().getPerClausePointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null));
	assertNotSame(imapa.getDeclaredPointcut(), imapa.getPointcut());

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

	assertTrue(maaif.isMaterialized());

	assertTrue(imapa.getDeclaredPointcut().getMethodMatcher().matches(TestBean.class.getMethod("getAge"), null));

	assertEquals("Around advice must apply", 0, itb.getAge());
	assertEquals("Around advice must apply", 1, itb.getAge());
}
 
Example 19
Source File: AbstractAspectJAdvisorFactoryTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void testPerTargetAspect() throws SecurityException, NoSuchMethodException {
	TestBean target = new TestBean();
	int realAge = 65;
	target.setAge(realAge);
	TestBean itb = (TestBean) createProxy(target,
			getFixture().getAdvisors(new SingletonMetadataAwareAspectInstanceFactory(new PerTargetAspect(), "someBean")),
			TestBean.class);
	assertEquals("Around advice must NOT apply", realAge, itb.getAge());

	Advised advised = (Advised) itb;
	ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor sia =
			(ReflectiveAspectJAdvisorFactory.SyntheticInstantiationAdvisor) advised.getAdvisors()[1];
	assertTrue(sia.getPointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null));
	InstantiationModelAwarePointcutAdvisorImpl imapa = (InstantiationModelAwarePointcutAdvisorImpl) advised.getAdvisors()[3];
	LazySingletonAspectInstanceFactoryDecorator maaif =
			(LazySingletonAspectInstanceFactoryDecorator) imapa.getAspectInstanceFactory();
	assertFalse(maaif.isMaterialized());

	// Check that the perclause pointcut is valid
	assertTrue(maaif.getAspectMetadata().getPerClausePointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null));
	assertNotSame(imapa.getDeclaredPointcut(), imapa.getPointcut());

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

	assertTrue(maaif.isMaterialized());

	assertEquals("Around advice must apply", 0, itb.getAge());
	assertEquals("Around advice must apply", 1, itb.getAge());
}
 
Example 20
Source File: CountingFactory.java    From spring-analysis-note with MIT License 4 votes vote down vote up
public void setTestBean(TestBean tb) {
	if (tb.getSpouse() == null) {
		throw new IllegalStateException("TestBean needs to have spouse");
	}
}