Java Code Examples for org.springframework.tests.sample.beans.ITestBean#getSpouse()
The following examples show how to use
org.springframework.tests.sample.beans.ITestBean#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: AspectJAutoProxyCreatorTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void testPerTargetAspect() throws SecurityException, NoSuchMethodException { ClassPathXmlApplicationContext bf = newContext("pertarget.xml"); ITestBean adrian1 = (ITestBean) bf.getBean("adrian"); assertTrue(AopUtils.isAopProxy(adrian1)); // Does not trigger advice or count int explicitlySetAge = 25; adrian1.setAge(explicitlySetAge); assertEquals("Setter does not initiate advice", explicitlySetAge, adrian1.getAge()); // Fire aspect AspectMetadata am = new AspectMetadata(PerTargetAspect.class, "someBean"); assertTrue(am.getPerClausePointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null)); adrian1.getSpouse(); assertEquals("Advice has now been instantiated", 0, adrian1.getAge()); adrian1.setAge(11); assertEquals("Any int setter increments", 2, adrian1.getAge()); adrian1.setName("Adrian"); //assertEquals("Any other setter does not increment", 2, adrian1.getAge()); ITestBean adrian2 = (ITestBean) bf.getBean("adrian"); assertNotSame(adrian1, adrian2); assertTrue(AopUtils.isAopProxy(adrian1)); assertEquals(34, adrian2.getAge()); adrian2.getSpouse(); assertEquals("Aspect now fired", 0, adrian2.getAge()); assertEquals(1, adrian2.getAge()); assertEquals(2, adrian2.getAge()); assertEquals(3, adrian1.getAge()); }
Example 2
Source File: AutoProxyCreatorTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@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 3
Source File: AutoProxyCreatorTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@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 4
Source File: AspectJAutoProxyCreatorTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void testPerTargetAspect() throws SecurityException, NoSuchMethodException { ClassPathXmlApplicationContext bf = newContext("pertarget.xml"); ITestBean adrian1 = (ITestBean) bf.getBean("adrian"); assertTrue(AopUtils.isAopProxy(adrian1)); // Does not trigger advice or count int explicitlySetAge = 25; adrian1.setAge(explicitlySetAge); assertEquals("Setter does not initiate advice", explicitlySetAge, adrian1.getAge()); // Fire aspect AspectMetadata am = new AspectMetadata(PerTargetAspect.class, "someBean"); assertTrue(am.getPerClausePointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null)); adrian1.getSpouse(); assertEquals("Advice has now been instantiated", 0, adrian1.getAge()); adrian1.setAge(11); assertEquals("Any int setter increments", 2, adrian1.getAge()); adrian1.setName("Adrian"); //assertEquals("Any other setter does not increment", 2, adrian1.getAge()); ITestBean adrian2 = (ITestBean) bf.getBean("adrian"); assertNotSame(adrian1, adrian2); assertTrue(AopUtils.isAopProxy(adrian1)); assertEquals(34, adrian2.getAge()); adrian2.getSpouse(); assertEquals("Aspect now fired", 0, adrian2.getAge()); assertEquals(1, adrian2.getAge()); assertEquals(2, adrian2.getAge()); assertEquals(3, adrian1.getAge()); }
Example 5
Source File: BenchmarkTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
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 6
Source File: MethodInvocationProceedingJoinPointTests.java From spring4-understanding with Apache License 2.0 | 5 votes |
@Test public void toShortAndLongStringFormedCorrectly() throws Exception { 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, Object target) throws Throwable { // makeEncSJP, although meant for computing the enclosing join point, // it serves our purpose here JoinPoint.StaticPart aspectJVersionJp = Factory.makeEncSJP(method); JoinPoint jp = AbstractAspectJAdvice.currentJoinPoint(); assertEquals(aspectJVersionJp.getSignature().toLongString(), jp.getSignature().toLongString()); assertEquals(aspectJVersionJp.getSignature().toShortString(), jp.getSignature().toShortString()); assertEquals(aspectJVersionJp.getSignature().toString(), jp.getSignature().toString()); assertEquals(aspectJVersionJp.toLongString(), jp.toLongString()); assertEquals(aspectJVersionJp.toShortString(), jp.toShortString()); assertEquals(aspectJVersionJp.toString(), jp.toString()); } }); ITestBean itb = (ITestBean) pf.getProxy(); itb.getAge(); itb.setName("foo"); itb.getDoctor(); itb.getStringArray(); itb.getSpouse(); itb.setSpouse(new TestBean()); try { itb.unreliableFileOperation(); } catch (IOException ex) { // we don't realy care... } }
Example 7
Source File: AutoProxyCreatorTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void testAutoProxyCreatorWithFallbackToDynamicProxy() { StaticApplicationContext sac = new StaticApplicationContext(); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("proxyFactoryBean", "false"); sac.registerSingleton("testAutoProxyCreator", TestAutoProxyCreator.class, pvs); sac.registerSingleton("noInterfaces", NoInterfaces.class); sac.registerSingleton("containerCallbackInterfacesOnly", ContainerCallbackInterfacesOnly.class); sac.registerSingleton("singletonNoInterceptor", CustomProxyFactoryBean.class); sac.registerSingleton("singletonToBeProxied", CustomProxyFactoryBean.class); sac.registerPrototype("prototypeToBeProxied", SpringProxyFactoryBean.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 8
Source File: AutoProxyCreatorTests.java From java-technology-stack with MIT License | 5 votes |
@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 9
Source File: AutoProxyCreatorTests.java From java-technology-stack with MIT License | 5 votes |
@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: MethodInvocationProceedingJoinPointTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void toShortAndLongStringFormedCorrectly() throws Exception { 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 { // makeEncSJP, although meant for computing the enclosing join point, // it serves our purpose here JoinPoint.StaticPart aspectJVersionJp = Factory.makeEncSJP(method); JoinPoint jp = AbstractAspectJAdvice.currentJoinPoint(); assertEquals(aspectJVersionJp.getSignature().toLongString(), jp.getSignature().toLongString()); assertEquals(aspectJVersionJp.getSignature().toShortString(), jp.getSignature().toShortString()); assertEquals(aspectJVersionJp.getSignature().toString(), jp.getSignature().toString()); assertEquals(aspectJVersionJp.toLongString(), jp.toLongString()); assertEquals(aspectJVersionJp.toShortString(), jp.toShortString()); assertEquals(aspectJVersionJp.toString(), jp.toString()); } }); ITestBean itb = (ITestBean) pf.getProxy(); itb.getAge(); itb.setName("foo"); itb.getDoctor(); itb.getStringArray(); itb.getSpouse(); itb.setSpouse(new TestBean()); try { itb.unreliableFileOperation(); } catch (IOException ex) { // we don't really care... } }
Example 11
Source File: BenchmarkTests.java From java-technology-stack with MIT License | 5 votes |
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 12
Source File: MethodInvocationProceedingJoinPointTests.java From java-technology-stack with MIT License | 5 votes |
@Test public void toShortAndLongStringFormedCorrectly() throws Exception { 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 { // makeEncSJP, although meant for computing the enclosing join point, // it serves our purpose here JoinPoint.StaticPart aspectJVersionJp = Factory.makeEncSJP(method); JoinPoint jp = AbstractAspectJAdvice.currentJoinPoint(); assertEquals(aspectJVersionJp.getSignature().toLongString(), jp.getSignature().toLongString()); assertEquals(aspectJVersionJp.getSignature().toShortString(), jp.getSignature().toShortString()); assertEquals(aspectJVersionJp.getSignature().toString(), jp.getSignature().toString()); assertEquals(aspectJVersionJp.toLongString(), jp.toLongString()); assertEquals(aspectJVersionJp.toShortString(), jp.toShortString()); assertEquals(aspectJVersionJp.toString(), jp.toString()); } }); ITestBean itb = (ITestBean) pf.getProxy(); itb.getAge(); itb.setName("foo"); itb.getDoctor(); itb.getStringArray(); itb.getSpouse(); itb.setSpouse(new TestBean()); try { itb.unreliableFileOperation(); } catch (IOException ex) { // we don't really care... } }
Example 13
Source File: AutoProxyCreatorTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void testAutoProxyCreatorWithFallbackToDynamicProxy() { StaticApplicationContext sac = new StaticApplicationContext(); MutablePropertyValues pvs = new MutablePropertyValues(); pvs.add("proxyFactoryBean", "false"); sac.registerSingleton("testAutoProxyCreator", TestAutoProxyCreator.class, pvs); sac.registerSingleton("noInterfaces", NoInterfaces.class); sac.registerSingleton("containerCallbackInterfacesOnly", ContainerCallbackInterfacesOnly.class); sac.registerSingleton("singletonNoInterceptor", CustomProxyFactoryBean.class); sac.registerSingleton("singletonToBeProxied", CustomProxyFactoryBean.class); sac.registerPrototype("prototypeToBeProxied", SpringProxyFactoryBean.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 14
Source File: AutoProxyCreatorTests.java From spring-analysis-note with MIT License | 5 votes |
@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 15
Source File: AutoProxyCreatorTests.java From spring-analysis-note with MIT License | 5 votes |
@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 16
Source File: AspectJAutoProxyCreatorTests.java From spring-analysis-note with MIT License | 5 votes |
@Test public void testPerTargetAspect() throws SecurityException, NoSuchMethodException { ClassPathXmlApplicationContext bf = newContext("pertarget.xml"); ITestBean adrian1 = (ITestBean) bf.getBean("adrian"); assertTrue(AopUtils.isAopProxy(adrian1)); // Does not trigger advice or count int explicitlySetAge = 25; adrian1.setAge(explicitlySetAge); assertEquals("Setter does not initiate advice", explicitlySetAge, adrian1.getAge()); // Fire aspect AspectMetadata am = new AspectMetadata(PerTargetAspect.class, "someBean"); assertTrue(am.getPerClausePointcut().getMethodMatcher().matches(TestBean.class.getMethod("getSpouse"), null)); adrian1.getSpouse(); assertEquals("Advice has now been instantiated", 0, adrian1.getAge()); adrian1.setAge(11); assertEquals("Any int setter increments", 2, adrian1.getAge()); adrian1.setName("Adrian"); //assertEquals("Any other setter does not increment", 2, adrian1.getAge()); ITestBean adrian2 = (ITestBean) bf.getBean("adrian"); assertNotSame(adrian1, adrian2); assertTrue(AopUtils.isAopProxy(adrian1)); assertEquals(34, adrian2.getAge()); adrian2.getSpouse(); assertEquals("Aspect now fired", 0, adrian2.getAge()); assertEquals(1, adrian2.getAge()); assertEquals(2, adrian2.getAge()); assertEquals(3, adrian1.getAge()); }
Example 17
Source File: BenchmarkTests.java From spring-analysis-note with MIT License | 5 votes |
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: ProxyFactoryTests.java From java-technology-stack with MIT License | 4 votes |
/** * Should see effect immediately on behavior. */ @Test public void testCanAddAndRemoveAspectInterfacesOnSingleton() { ProxyFactory config = new ProxyFactory(new TestBean()); assertFalse("Shouldn't implement TimeStamped before manipulation", config.getProxy() instanceof TimeStamped); long time = 666L; TimestampIntroductionInterceptor ti = new TimestampIntroductionInterceptor(); ti.setTime(time); // Add to front of interceptor chain int oldCount = config.getAdvisors().length; config.addAdvisor(0, new DefaultIntroductionAdvisor(ti, TimeStamped.class)); assertTrue(config.getAdvisors().length == oldCount + 1); TimeStamped ts = (TimeStamped) config.getProxy(); assertTrue(ts.getTimeStamp() == time); // Can remove config.removeAdvice(ti); assertTrue(config.getAdvisors().length == oldCount); try { // Existing reference will fail ts.getTimeStamp(); fail("Existing object won't implement this interface any more"); } catch (RuntimeException ex) { } assertFalse("Should no longer implement TimeStamped", config.getProxy() instanceof TimeStamped); // Now check non-effect of removing interceptor that isn't there config.removeAdvice(new DebugInterceptor()); assertTrue(config.getAdvisors().length == oldCount); ITestBean it = (ITestBean) ts; DebugInterceptor debugInterceptor = new DebugInterceptor(); config.addAdvice(0, debugInterceptor); it.getSpouse(); assertEquals(1, debugInterceptor.getCount()); config.removeAdvice(debugInterceptor); it.getSpouse(); // not invoked again assertTrue(debugInterceptor.getCount() == 1); }
Example 19
Source File: ProxyFactoryTests.java From spring4-understanding with Apache License 2.0 | 4 votes |
/** * Should see effect immediately on behavior. */ @Test public void testCanAddAndRemoveAspectInterfacesOnSingleton() { ProxyFactory config = new ProxyFactory(new TestBean()); assertFalse("Shouldn't implement TimeStamped before manipulation", config.getProxy() instanceof TimeStamped); long time = 666L; TimestampIntroductionInterceptor ti = new TimestampIntroductionInterceptor(); ti.setTime(time); // Add to front of interceptor chain int oldCount = config.getAdvisors().length; config.addAdvisor(0, new DefaultIntroductionAdvisor(ti, TimeStamped.class)); assertTrue(config.getAdvisors().length == oldCount + 1); TimeStamped ts = (TimeStamped) config.getProxy(); assertTrue(ts.getTimeStamp() == time); // Can remove config.removeAdvice(ti); assertTrue(config.getAdvisors().length == oldCount); try { // Existing reference will fail ts.getTimeStamp(); fail("Existing object won't implement this interface any more"); } catch (RuntimeException ex) { } assertFalse("Should no longer implement TimeStamped", config.getProxy() instanceof TimeStamped); // Now check non-effect of removing interceptor that isn't there config.removeAdvice(new DebugInterceptor()); assertTrue(config.getAdvisors().length == oldCount); ITestBean it = (ITestBean) ts; DebugInterceptor debugInterceptor = new DebugInterceptor(); config.addAdvice(0, debugInterceptor); it.getSpouse(); assertEquals(1, debugInterceptor.getCount()); config.removeAdvice(debugInterceptor); it.getSpouse(); // not invoked again assertTrue(debugInterceptor.getCount() == 1); }
Example 20
Source File: ProxyFactoryTests.java From spring-analysis-note with MIT License | 4 votes |
/** * Should see effect immediately on behavior. */ @Test public void testCanAddAndRemoveAspectInterfacesOnSingleton() { ProxyFactory config = new ProxyFactory(new TestBean()); assertFalse("Shouldn't implement TimeStamped before manipulation", config.getProxy() instanceof TimeStamped); long time = 666L; TimestampIntroductionInterceptor ti = new TimestampIntroductionInterceptor(); ti.setTime(time); // Add to front of interceptor chain int oldCount = config.getAdvisors().length; config.addAdvisor(0, new DefaultIntroductionAdvisor(ti, TimeStamped.class)); assertTrue(config.getAdvisors().length == oldCount + 1); TimeStamped ts = (TimeStamped) config.getProxy(); assertTrue(ts.getTimeStamp() == time); // Can remove config.removeAdvice(ti); assertTrue(config.getAdvisors().length == oldCount); try { // Existing reference will fail ts.getTimeStamp(); fail("Existing object won't implement this interface any more"); } catch (RuntimeException ex) { } assertFalse("Should no longer implement TimeStamped", config.getProxy() instanceof TimeStamped); // Now check non-effect of removing interceptor that isn't there config.removeAdvice(new DebugInterceptor()); assertTrue(config.getAdvisors().length == oldCount); ITestBean it = (ITestBean) ts; DebugInterceptor debugInterceptor = new DebugInterceptor(); config.addAdvice(0, debugInterceptor); it.getSpouse(); assertEquals(1, debugInterceptor.getCount()); config.removeAdvice(debugInterceptor); it.getSpouse(); // not invoked again assertTrue(debugInterceptor.getCount() == 1); }