public void testDefaultInterceptorSunnyDay() throws Exception { ((Advised) service).addAdvice(interceptor); service.service(); assertEquals(3, target.count); }
public void testCallbackWithThrowable() throws Exception { ((Advised) service).addAdvice(interceptor); try { service.error(); fail("Expected Error"); } catch (Error e) { assertEquals("Duh", e.getMessage().substring(0, 3)); } }
public void testCallbackWithException() throws Exception { ((Advised) service).addAdvice(interceptor); try { service.exception(); fail("Expected RuntimeException"); } catch (RuntimeException e) { assertEquals("Duh", e.getMessage().substring(0, 3)); } }
public void testSetTemplate() throws Exception { final List<Object> calls = new ArrayList<>(); interceptor.setRepeatOperations(new RepeatOperations() { @Override public RepeatStatus iterate(RepeatCallback callback) { try { Object result = callback.doInIteration(null); calls.add(result); } catch (Exception e) { throw new RepeatException("Encountered exception in repeat.", e); } return RepeatStatus.CONTINUABLE; } }); ((Advised) service).addAdvice(interceptor); service.service(); assertEquals(1, calls.size()); }
public void testCompleteOnFirstInvocation() throws Exception { ((Advised) service).addAdvice(interceptor); target.setMaxService(0); service.service(); assertEquals(1, target.count); }
public void testCallbackNotExecuted() throws Exception { final List<Object> calls = new ArrayList<>(); interceptor.setRepeatOperations(new RepeatOperations() { @Override public RepeatStatus iterate(RepeatCallback callback) { calls.add(null); return RepeatStatus.FINISHED; } }); ((Advised) service).addAdvice(interceptor); try { service.service(); fail("Expected IllegalStateException"); } catch (IllegalStateException e) { String message = e.getMessage(); assertTrue("Wrong exception message: "+message, message.toLowerCase().contains("no result available")); } assertEquals(1, calls.size()); }
public void testInterceptorChainWithRetry() throws Exception { ((Advised) service).addAdvice(interceptor); final List<Object> list = new ArrayList<>(); ((Advised) service).addAdvice(new MethodInterceptor() { @Override public Object invoke(MethodInvocation invocation) throws Throwable { list.add("chain"); return invocation.proceed(); } }); RepeatTemplate template = new RepeatTemplate(); template.setCompletionPolicy(new SimpleCompletionPolicy(2)); interceptor.setRepeatOperations(template); service.service(); assertEquals(2, target.count); assertEquals(2, list.size()); }
public void testCallbackWithBooleanReturningFalseFirstTime() throws Exception { target.setComplete(true); ((Advised) service).addAdvice(interceptor); // Complete without repeat when boolean return value is false assertFalse(service.isContinuable()); assertEquals(1, target.count); }
/** * 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()); }
public void testVoidServiceSunnyDay() throws Exception { ((Advised) service).addAdvice(interceptor); RepeatTemplate template = new RepeatTemplate(); // N.B. the default completion policy results in an infinite loop, so we // need to set the chunk size. template.setCompletionPolicy(new SimpleCompletionPolicy(2)); interceptor.setRepeatOperations(template); service.alternate(); assertEquals(2, target.count); }
public void testCallbackWithBoolean() throws Exception { RepeatTemplate template = new RepeatTemplate(); // N.B. the default completion policy results in an infinite loop, so we // need to set the chunk size. template.setCompletionPolicy(new SimpleCompletionPolicy(2)); interceptor.setRepeatOperations(template); ((Advised) service).addAdvice(interceptor); assertTrue(service.isContinuable()); assertEquals(2, target.count); }
@Test public void testAddAdviceAtRuntime() { TestBean bean = new TestBean(); CountingBeforeAdvice cba = new CountingBeforeAdvice(); ProxyFactory pf = new ProxyFactory(); pf.setTarget(bean); pf.setFrozen(false); pf.setOpaque(false); pf.setProxyTargetClass(true); TestBean proxy = (TestBean) pf.getProxy(); assertTrue(AopUtils.isCglibProxy(proxy)); proxy.getAge(); assertEquals(0, cba.getCalls()); ((Advised) proxy).addAdvice(cba); proxy.getAge(); assertEquals(1, cba.getCalls()); }
@Test public void testIndexOfMethods() { TestBean target = new TestBean(); ProxyFactory pf = new ProxyFactory(target); NopInterceptor nop = new NopInterceptor(); Advisor advisor = new DefaultPointcutAdvisor(new CountingBeforeAdvice()); Advised advised = (Advised) pf.getProxy(); // Can use advised and ProxyFactory interchangeably advised.addAdvice(nop); pf.addAdvisor(advisor); assertEquals(-1, pf.indexOf(new NopInterceptor())); assertEquals(0, pf.indexOf(nop)); assertEquals(1, pf.indexOf(advisor)); assertEquals(-1, advised.indexOf(new DefaultPointcutAdvisor(null))); }
@Test public void testSerializableSingletonProxy() throws Exception { DefaultListableBeanFactory bf = new DefaultListableBeanFactory(); new XmlBeanDefinitionReader(bf).loadBeanDefinitions(new ClassPathResource(SERIALIZATION_CONTEXT, CLASS)); Person p = (Person) bf.getBean("serializableSingleton"); assertSame("Should be a Singleton", p, bf.getBean("serializableSingleton")); Person p2 = (Person) SerializationTestUtils.serializeAndDeserialize(p); assertEquals(p, p2); assertNotSame(p, p2); assertEquals("serializableSingleton", p2.getName()); // Add unserializable advice Advice nop = new NopInterceptor(); ((Advised) p).addAdvice(nop); // Check it still works assertEquals(p2.getName(), p2.getName()); assertFalse("Not serializable because an interceptor isn't serializable", SerializationTestUtils.isSerializable(p)); // Remove offending interceptor... assertTrue(((Advised) p).removeAdvice(nop)); assertTrue("Serializable again because offending interceptor was removed", SerializationTestUtils.isSerializable(p)); }
/** * @param cls * @param advice */ public void addAdvice(Class cls, Advice advice) { Advised advisedService = (Advised) services.get(cls); if (advisedService.indexOf(advice) < 0) { advisedService.addAdvice(advice); } addedAdvice.computeIfAbsent(cls, k -> new HashSet<>()); getAddedAdvice(cls).add(advice); }
/** * 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); }
@Override public void setBeanFactory(BeanFactory beanFactory) { super.setBeanFactory(beanFactory); Object proxy = getObject(); if (proxy instanceof Advised) { Advised advised = (Advised) proxy; advised.addAdvice(0, this); } }
p2.getAge(); assertEquals(0, ni.getCount()); a2.addAdvice(ni); p2.getAge(); assertEquals(1, ni.getCount());
/** * Moves advisors and advice added by ServiceContext from the source service to the target one. * * @param source the existing service * @param target the new service */ private void moveAddedAOP(Advised source, Advised target) { Class serviceClass = source.getClass(); Set<Advisor> existingAdvisors = getAddedAdvisors(serviceClass); for (Advisor advisor : existingAdvisors) { target.addAdvisor(advisor); source.removeAdvisor(advisor); } Set<Advice> existingAdvice = getAddedAdvice(serviceClass); for (Advice advice : existingAdvice) { target.addAdvice(advice); source.removeAdvice(advice); } }