org.springframework.aop.support.DefaultIntroductionAdvisor Java Examples

The following examples show how to use org.springframework.aop.support.DefaultIntroductionAdvisor. 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: AdvisedSupport.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Cannot add introductions this way unless the advice implements IntroductionInfo.
 */
@Override
public void addAdvice(int pos, Advice advice) throws AopConfigException {
	Assert.notNull(advice, "Advice must not be null");
	if (advice instanceof IntroductionInfo) {
		// We don't need an IntroductionAdvisor for this kind of introduction:
		// It's fully self-describing.
		addAdvisor(pos, new DefaultIntroductionAdvisor(advice, (IntroductionInfo) advice));
	}
	else if (advice instanceof DynamicIntroductionAdvice) {
		// We need an IntroductionAdvisor for this kind of introduction.
		throw new AopConfigException("DynamicIntroductionAdvice may only be added as part of IntroductionAdvisor");
	}
	else {
		addAdvisor(pos, new DefaultPointcutAdvisor(advice));
	}
}
 
Example #2
Source File: AdvisedSupport.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Cannot add introductions this way unless the advice implements IntroductionInfo.
 */
@Override
public void addAdvice(int pos, Advice advice) throws AopConfigException {
	Assert.notNull(advice, "Advice must not be null");
	if (advice instanceof IntroductionInfo) {
		// We don't need an IntroductionAdvisor for this kind of introduction:
		// It's fully self-describing.
		addAdvisor(pos, new DefaultIntroductionAdvisor(advice, (IntroductionInfo) advice));
	}
	else if (advice instanceof DynamicIntroductionAdvice) {
		// We need an IntroductionAdvisor for this kind of introduction.
		throw new AopConfigException("DynamicIntroductionAdvice may only be added as part of IntroductionAdvisor");
	}
	else {
		addAdvisor(pos, new DefaultPointcutAdvisor(advice));
	}
}
 
Example #3
Source File: PermissionCheckCollection.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Helper method to create a {@link PermissionCheckCollection} from an existing <code>Collection</code>
 * 
 * @param <TT>              the type of the <code>Collection</code>
 * @param collection        the <code>Collection</code> to proxy
 * @param targetResultCount the desired number of results or default to the collection size
 * @param cutOffAfterTimeMs the number of milliseconds to wait before cut-off or zero to use the system default
 *                          time-based cut-off.
 * @param cutOffAfterCount  the number of permission checks to process before cut-off or zero to use the system default
 *                          count-based cut-off.
 * @return                  a <code>Collection</code> of the same type but including the
 *                          {@link PermissionCheckCollection} interface
 */
@SuppressWarnings("unchecked")
public static final <TT> Collection<TT> create(
        Collection<TT> collection,
        int targetResultCount, long cutOffAfterTimeMs, int cutOffAfterCount)
{
    if (targetResultCount <= 0)
    {
        targetResultCount = collection.size();
    }
    // Create the mixin
    DelegatingIntroductionInterceptor mixin = new PermissionCheckCollectionMixin<Integer>(
            targetResultCount,
            cutOffAfterTimeMs,
            cutOffAfterCount);
    // Create the advisor
    IntroductionAdvisor advisor = new DefaultIntroductionAdvisor(mixin, PermissionCheckCollection.class);
    // Proxy
    ProxyFactory pf = new ProxyFactory(collection);
    pf.addAdvisor(advisor);
    Object proxiedObject = pf.getProxy();
    
    // Done
    return (Collection<TT>) proxiedObject;
}
 
Example #4
Source File: PermissionCheckedCollection.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Helper method to create a {@link PermissionCheckedCollection} from an existing <code>Collection</code>
 * 
 * @param <TT>              the type of the <code>Collection</code>
 * @param collection        the <code>Collection</code> to proxy
 * @param isCutOff          <tt>true</tt> if permission checking was cut off before completion
 * @param sizeUnchecked     number of entries from the original collection that were not checked
 * @param sizeOriginal      number of entries in the original, pre-checked collection
 * @return                  a <code>Collection</code> of the same type but including the
 *                          {@link PermissionCheckedCollection} interface
 */
@SuppressWarnings("unchecked")
public static final <TT> Collection<TT> create(
        Collection<TT> collection,
        boolean isCutOff, int sizeUnchecked, int sizeOriginal)
{
    // Create the mixin
    DelegatingIntroductionInterceptor mixin = new PermissionCheckedCollectionMixin<Integer>(
            isCutOff,
            sizeUnchecked,
            sizeOriginal
            );
    // Create the advisor
    IntroductionAdvisor advisor = new DefaultIntroductionAdvisor(mixin, PermissionCheckedCollection.class);
    // Proxy
    ProxyFactory pf = new ProxyFactory(collection);
    pf.addAdvisor(advisor);
    Object proxiedObject = pf.getProxy();
    
    // Done
    return (Collection<TT>) proxiedObject;
}
 
Example #5
Source File: PermissionCheckedValue.java    From alfresco-repository with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * Helper method to create a {@link PermissionCheckedValue} from an existing <code>Object</code>.
 * 
 * @param object        the <code>Object</code> to proxy
 * @return                  a <code>Object</code> of the same type but including the
 *                          {@link PermissionCheckedValue} interface
 */
@SuppressWarnings("unchecked")
public static final <T extends Object> T create(T object)
{
    // Create the mixin
    DelegatingIntroductionInterceptor mixin = new PermissionCheckedValueMixin();
    // Create the advisor
    IntroductionAdvisor advisor = new DefaultIntroductionAdvisor(mixin, PermissionCheckedValue.class);
    // Proxy
    ProxyFactory pf = new ProxyFactory(object);
    pf.addAdvisor(advisor);
    Object proxiedObject = pf.getProxy();
    
    // Done
    return (T) proxiedObject;
}
 
Example #6
Source File: AbstractAopProxyTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testUseAsHashKey() {
	TestBean target1 = new TestBean();
	ProxyFactory pf1 = new ProxyFactory(target1);
	pf1.addAdvice(new NopInterceptor());
	ITestBean proxy1 = (ITestBean) createProxy(pf1);

	TestBean target2 = new TestBean();
	ProxyFactory pf2 = new ProxyFactory(target2);
	pf2.addAdvisor(new DefaultIntroductionAdvisor(new TimestampIntroductionInterceptor()));
	ITestBean proxy2 = (ITestBean) createProxy(pf2);

	HashMap<ITestBean, Object> h = new HashMap<>();
	Object value1 = "foo";
	Object value2 = "bar";
	assertNull(h.get(proxy1));
	h.put(proxy1, value1);
	h.put(proxy2, value2);
	assertEquals(h.get(proxy1), value1);
	assertEquals(h.get(proxy2), value2);
}
 
Example #7
Source File: AbstractAopProxyTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * Should only be able to introduce interfaces, not classes.
 */
@Test
public void testCannotAddIntroductionAdviceToIntroduceClass() throws Throwable {
	TestBean target = new TestBean();
	target.setAge(21);
	ProxyFactory pc = new ProxyFactory(target);
	try {
		pc.addAdvisor(0, new DefaultIntroductionAdvisor(new TimestampIntroductionInterceptor(), TestBean.class));
		fail("Shouldn't be able to add introduction advice that introduces a class, rather than an interface");
	}
	catch (IllegalArgumentException ex) {
		assertTrue(ex.getMessage().contains("interface"));
	}
	// Check it still works: proxy factory state shouldn't have been corrupted
	ITestBean proxied = (ITestBean) createProxy(pc);
	assertEquals(target.getAge(), proxied.getAge());
}
 
Example #8
Source File: AdvisedSupport.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Cannot add introductions this way unless the advice implements IntroductionInfo.
 */
@Override
public void addAdvice(int pos, Advice advice) throws AopConfigException {
	Assert.notNull(advice, "Advice must not be null");
	if (advice instanceof IntroductionInfo) {
		// We don't need an IntroductionAdvisor for this kind of introduction:
		// It's fully self-describing.
		addAdvisor(pos, new DefaultIntroductionAdvisor(advice, (IntroductionInfo) advice));
	}
	else if (advice instanceof DynamicIntroductionAdvice) {
		// We need an IntroductionAdvisor for this kind of introduction.
		throw new AopConfigException("DynamicIntroductionAdvice may only be added as part of IntroductionAdvisor");
	}
	else {
		addAdvisor(pos, new DefaultPointcutAdvisor(advice));
	}
}
 
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: AbstractAopProxyTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
@Test
public void testRejectsBogusDynamicIntroductionAdviceWithNoAdapter() throws Throwable {
	TestBean target = new TestBean();
	target.setAge(21);
	ProxyFactory pc = new ProxyFactory(target);
	pc.addAdvisor(new DefaultIntroductionAdvisor(new DummyIntroductionAdviceImpl(), Comparable.class));
	try {
		// TODO May fail on either call: may want to tighten up definition
		ITestBean proxied = (ITestBean) createProxy(pc);
		proxied.getName();
		fail("Bogus introduction");
	}
	catch (Exception ex) {
		// TODO used to catch UnknownAdviceTypeException, but
		// with CGLIB some errors are in proxy creation and are wrapped
		// in aspect exception. Error message is still fine.
		//assertTrue(ex.getMessage().indexOf("ntroduction") > -1);
	}
}
 
Example #11
Source File: AsyncExecutionTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
public DynamicAsyncMethodsInterfaceBean() {
	ProxyFactory pf = new ProxyFactory(new HashMap<>());
	DefaultIntroductionAdvisor advisor = new DefaultIntroductionAdvisor(new MethodInterceptor() {
		@Override
		public Object invoke(MethodInvocation invocation) throws Throwable {
			assertTrue(!Thread.currentThread().getName().equals(originalThreadName));
			if (Future.class.equals(invocation.getMethod().getReturnType())) {
				return new AsyncResult<>(invocation.getArguments()[0].toString());
			}
			return null;
		}
	});
	advisor.addInterface(AsyncMethodsInterface.class);
	pf.addAdvisor(advisor);
	this.proxy = (AsyncMethodsInterface) pf.getProxy();
}
 
Example #12
Source File: AsyncExecutionTests.java    From java-technology-stack with MIT License 6 votes vote down vote up
public DynamicAsyncInterfaceBean() {
	ProxyFactory pf = new ProxyFactory(new HashMap<>());
	DefaultIntroductionAdvisor advisor = new DefaultIntroductionAdvisor(new MethodInterceptor() {
		@Override
		public Object invoke(MethodInvocation invocation) throws Throwable {
			assertTrue(!Thread.currentThread().getName().equals(originalThreadName));
			if (Future.class.equals(invocation.getMethod().getReturnType())) {
				return new AsyncResult<>(invocation.getArguments()[0].toString());
			}
			return null;
		}
	});
	advisor.addInterface(AsyncInterface.class);
	pf.addAdvisor(advisor);
	this.proxy = (AsyncInterface) pf.getProxy();
}
 
Example #13
Source File: AsyncExecutionTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
public DynamicAsyncInterfaceBean() {
	ProxyFactory pf = new ProxyFactory(new HashMap<>());
	DefaultIntroductionAdvisor advisor = new DefaultIntroductionAdvisor(new MethodInterceptor() {
		@Override
		public Object invoke(MethodInvocation invocation) throws Throwable {
			assertTrue(!Thread.currentThread().getName().equals(originalThreadName));
			if (Future.class.equals(invocation.getMethod().getReturnType())) {
				return new AsyncResult<String>(invocation.getArguments()[0].toString());
			}
			return null;
		}
	});
	advisor.addInterface(AsyncInterface.class);
	pf.addAdvisor(advisor);
	this.proxy = (AsyncInterface) pf.getProxy();
}
 
Example #14
Source File: AsyncExecutionTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
public DynamicAsyncMethodsInterfaceBean() {
	ProxyFactory pf = new ProxyFactory(new HashMap<>());
	DefaultIntroductionAdvisor advisor = new DefaultIntroductionAdvisor(new MethodInterceptor() {
		@Override
		public Object invoke(MethodInvocation invocation) throws Throwable {
			assertTrue(!Thread.currentThread().getName().equals(originalThreadName));
			if (Future.class.equals(invocation.getMethod().getReturnType())) {
				return new AsyncResult<String>(invocation.getArguments()[0].toString());
			}
			return null;
		}
	});
	advisor.addInterface(AsyncMethodsInterface.class);
	pf.addAdvisor(advisor);
	this.proxy = (AsyncMethodsInterface) pf.getProxy();
}
 
Example #15
Source File: AbstractAopProxyTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testUseAsHashKey() {
	TestBean target1 = new TestBean();
	ProxyFactory pf1 = new ProxyFactory(target1);
	pf1.addAdvice(new NopInterceptor());
	ITestBean proxy1 = (ITestBean) createProxy(pf1);

	TestBean target2 = new TestBean();
	ProxyFactory pf2 = new ProxyFactory(target2);
	pf2.addAdvisor(new DefaultIntroductionAdvisor(new TimestampIntroductionInterceptor()));
	ITestBean proxy2 = (ITestBean) createProxy(pf2);

	HashMap<ITestBean, Object> h = new HashMap<>();
	Object value1 = "foo";
	Object value2 = "bar";
	assertNull(h.get(proxy1));
	h.put(proxy1, value1);
	h.put(proxy2, value2);
	assertEquals(h.get(proxy1), value1);
	assertEquals(h.get(proxy2), value2);
}
 
Example #16
Source File: AbstractAopProxyTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Should only be able to introduce interfaces, not classes.
 */
@Test
public void testCannotAddIntroductionAdviceToIntroduceClass() throws Throwable {
	TestBean target = new TestBean();
	target.setAge(21);
	ProxyFactory pc = new ProxyFactory(target);
	try {
		pc.addAdvisor(0, new DefaultIntroductionAdvisor(new TimestampIntroductionInterceptor(), TestBean.class));
		fail("Shouldn't be able to add introduction advice that introduces a class, rather than an interface");
	}
	catch (IllegalArgumentException ex) {
		assertTrue(ex.getMessage().contains("interface"));
	}
	// Check it still works: proxy factory state shouldn't have been corrupted
	ITestBean proxied = (ITestBean) createProxy(pc);
	assertEquals(target.getAge(), proxied.getAge());
}
 
Example #17
Source File: AbstractAopProxyTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testRejectsBogusDynamicIntroductionAdviceWithNoAdapter() throws Throwable {
	TestBean target = new TestBean();
	target.setAge(21);
	ProxyFactory pc = new ProxyFactory(target);
	pc.addAdvisor(new DefaultIntroductionAdvisor(new DummyIntroductionAdviceImpl(), Comparable.class));
	try {
		// TODO May fail on either call: may want to tighten up definition
		ITestBean proxied = (ITestBean) createProxy(pc);
		proxied.getName();
		fail("Bogus introduction");
	}
	catch (Exception ex) {
		// TODO used to catch UnknownAdviceTypeException, but
		// with CGLIB some errors are in proxy creation and are wrapped
		// in aspect exception. Error message is still fine.
		//assertTrue(ex.getMessage().indexOf("ntroduction") > -1);
	}
}
 
Example #18
Source File: AbstractAopProxyTests.java    From spring-analysis-note 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 #19
Source File: AbstractAopProxyTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
@Test
public void testRejectsBogusDynamicIntroductionAdviceWithNoAdapter() throws Throwable {
	TestBean target = new TestBean();
	target.setAge(21);
	ProxyFactory pc = new ProxyFactory(target);
	pc.addAdvisor(new DefaultIntroductionAdvisor(new DummyIntroductionAdviceImpl(), Comparable.class));
	try {
		// TODO May fail on either call: may want to tighten up definition
		ITestBean proxied = (ITestBean) createProxy(pc);
		proxied.getName();
		fail("Bogus introduction");
	}
	catch (Exception ex) {
		// TODO used to catch UnknownAdviceTypeException, but
		// with CGLIB some errors are in proxy creation and are wrapped
		// in aspect exception. Error message is still fine.
		//assertTrue(ex.getMessage().indexOf("ntroduction") > -1);
	}
}
 
Example #20
Source File: AsyncExecutionTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
public DynamicAsyncMethodsInterfaceBean() {
	ProxyFactory pf = new ProxyFactory(new HashMap<>());
	DefaultIntroductionAdvisor advisor = new DefaultIntroductionAdvisor(new MethodInterceptor() {
		@Override
		public Object invoke(MethodInvocation invocation) throws Throwable {
			assertTrue(!Thread.currentThread().getName().equals(originalThreadName));
			if (Future.class.equals(invocation.getMethod().getReturnType())) {
				return new AsyncResult<>(invocation.getArguments()[0].toString());
			}
			return null;
		}
	});
	advisor.addInterface(AsyncMethodsInterface.class);
	pf.addAdvisor(advisor);
	this.proxy = (AsyncMethodsInterface) pf.getProxy();
}
 
Example #21
Source File: AsyncExecutionTests.java    From spring-analysis-note with MIT License 6 votes vote down vote up
public DynamicAsyncInterfaceBean() {
	ProxyFactory pf = new ProxyFactory(new HashMap<>());
	DefaultIntroductionAdvisor advisor = new DefaultIntroductionAdvisor(new MethodInterceptor() {
		@Override
		public Object invoke(MethodInvocation invocation) throws Throwable {
			assertTrue(!Thread.currentThread().getName().equals(originalThreadName));
			if (Future.class.equals(invocation.getMethod().getReturnType())) {
				return new AsyncResult<>(invocation.getArguments()[0].toString());
			}
			return null;
		}
	});
	advisor.addInterface(AsyncInterface.class);
	pf.addAdvisor(advisor);
	this.proxy = (AsyncInterface) pf.getProxy();
}
 
Example #22
Source File: AbstractAopProxyTests.java    From spring4-understanding with Apache License 2.0 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 #23
Source File: AdvisedSupport.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * Cannot add introductions this way unless the advice implements IntroductionInfo.
 */
@Override
public void addAdvice(int pos, Advice advice) throws AopConfigException {
	Assert.notNull(advice, "Advice must not be null");
	if (advice instanceof IntroductionInfo) {
		// We don't need an IntroductionAdvisor for this kind of introduction:
		// It's fully self-describing.
		addAdvisor(pos, new DefaultIntroductionAdvisor(advice, (IntroductionInfo) advice));
	}
	else if (advice instanceof DynamicIntroductionAdvice) {
		// We need an IntroductionAdvisor for this kind of introduction.
		throw new AopConfigException("DynamicIntroductionAdvice may only be added as part of IntroductionAdvisor");
	}
	else {
		addAdvisor(pos, new DefaultPointcutAdvisor(advice));
	}
}
 
Example #24
Source File: AbstractAopProxyTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * Should only be able to introduce interfaces, not classes.
 */
@Test
public void testCannotAddIntroductionAdviceToIntroduceClass() throws Throwable {
	TestBean target = new TestBean();
	target.setAge(21);
	ProxyFactory pc = new ProxyFactory(target);
	try {
		pc.addAdvisor(0, new DefaultIntroductionAdvisor(new TimestampIntroductionInterceptor(), TestBean.class));
		fail("Shouldn't be able to add introduction advice that introduces a class, rather than an interface");
	}
	catch (IllegalArgumentException ex) {
		assertTrue(ex.getMessage().indexOf("interface") > -1);
	}
	// Check it still works: proxy factory state shouldn't have been corrupted
	ITestBean proxied = (ITestBean) createProxy(pc);
	assertEquals(target.getAge(), proxied.getAge());
}
 
Example #25
Source File: AbstractAopProxyTests.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
@Test
public void testUseAsHashKey() {
	TestBean target1 = new TestBean();
	ProxyFactory pf1 = new ProxyFactory(target1);
	pf1.addAdvice(new NopInterceptor());
	ITestBean proxy1 = (ITestBean) createProxy(pf1);

	TestBean target2 = new TestBean();
	ProxyFactory pf2 = new ProxyFactory(target2);
	pf2.addAdvisor(new DefaultIntroductionAdvisor(new TimestampIntroductionInterceptor()));
	ITestBean proxy2 = (ITestBean) createProxy(pf2);

	HashMap<ITestBean, Object> h = new HashMap<ITestBean, Object>();
	Object value1 = "foo";
	Object value2 = "bar";
	assertNull(h.get(proxy1));
	h.put(proxy1, value1);
	h.put(proxy2, value2);
	assertEquals(h.get(proxy1), value1);
	assertEquals(h.get(proxy2), value2);
}
 
Example #26
Source File: AbstractAopProxyTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
/**
 * Note that an introduction can't throw an unexpected checked exception,
 * as it's constrained by the interface.
 */
@Test
public void testIntroductionThrowsUncheckedException() throws Throwable {
	TestBean target = new TestBean();
	target.setAge(21);
	ProxyFactory pc = new ProxyFactory(target);

	@SuppressWarnings("serial")
	class MyDi extends DelegatingIntroductionInterceptor implements TimeStamped {
		/**
		 * @see test.util.TimeStamped#getTimeStamp()
		 */
		@Override
		public long getTimeStamp() {
			throw new UnsupportedOperationException();
		}
	}
	pc.addAdvisor(new DefaultIntroductionAdvisor(new MyDi()));

	TimeStamped ts = (TimeStamped) createProxy(pc);
	try {
		ts.getTimeStamp();
		fail("Should throw UnsupportedOperationException");
	}
	catch (UnsupportedOperationException ex) {
	}
}
 
Example #27
Source File: ProxyFactoryTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetsAllInterfaces() throws Exception {
	// Extend to get new interface
	class TestBeanSubclass extends TestBean implements Comparable<Object> {
		@Override
		public int compareTo(Object arg0) {
			throw new UnsupportedOperationException("compareTo");
		}
	}
	TestBeanSubclass raw = new TestBeanSubclass();
	ProxyFactory factory = new ProxyFactory(raw);
	//System.out.println("Proxied interfaces are " + StringUtils.arrayToDelimitedString(factory.getProxiedInterfaces(), ","));
	assertEquals("Found correct number of interfaces", 5, factory.getProxiedInterfaces().length);
	ITestBean tb = (ITestBean) factory.getProxy();
	assertThat("Picked up secondary interface", tb, instanceOf(IOther.class));

	raw.setAge(25);
	assertTrue(tb.getAge() == raw.getAge());

	long t = 555555L;
	TimestampIntroductionInterceptor ti = new TimestampIntroductionInterceptor(t);

	Class<?>[] oldProxiedInterfaces = factory.getProxiedInterfaces();

	factory.addAdvisor(0, new DefaultIntroductionAdvisor(ti, TimeStamped.class));

	Class<?>[] newProxiedInterfaces = factory.getProxiedInterfaces();
	assertEquals("Advisor proxies one more interface after introduction", oldProxiedInterfaces.length + 1, newProxiedInterfaces.length);

	TimeStamped ts = (TimeStamped) factory.getProxy();
	assertTrue(ts.getTimeStamp() == t);
	// Shouldn't fail;
	 ((IOther) ts).absquatulate();
}
 
Example #28
Source File: AbstractAopProxyTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Note that an introduction can't throw an unexpected checked exception,
 * as it's constained by the interface.
 */
@Test
public void testIntroductionThrowsUncheckedException() throws Throwable {
	TestBean target = new TestBean();
	target.setAge(21);
	ProxyFactory pc = new ProxyFactory(target);

	@SuppressWarnings("serial")
	class MyDi extends DelegatingIntroductionInterceptor implements TimeStamped {
		/**
		 * @see test.util.TimeStamped#getTimeStamp()
		 */
		@Override
		public long getTimeStamp() {
			throw new UnsupportedOperationException();
		}
	}
	pc.addAdvisor(new DefaultIntroductionAdvisor(new MyDi()));

	TimeStamped ts = (TimeStamped) createProxy(pc);
	try {
		ts.getTimeStamp();
		fail("Should throw UnsupportedOperationException");
	}
	catch (UnsupportedOperationException ex) {
	}
}
 
Example #29
Source File: ProxyFactoryTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void testGetsAllInterfaces() throws Exception {
	// Extend to get new interface
	class TestBeanSubclass extends TestBean implements Comparable<Object> {
		@Override
		public int compareTo(Object arg0) {
			throw new UnsupportedOperationException("compareTo");
		}
	}
	TestBeanSubclass raw = new TestBeanSubclass();
	ProxyFactory factory = new ProxyFactory(raw);
	//System.out.println("Proxied interfaces are " + StringUtils.arrayToDelimitedString(factory.getProxiedInterfaces(), ","));
	assertEquals("Found correct number of interfaces", 5, factory.getProxiedInterfaces().length);
	ITestBean tb = (ITestBean) factory.getProxy();
	assertThat("Picked up secondary interface", tb, instanceOf(IOther.class));

	raw.setAge(25);
	assertTrue(tb.getAge() == raw.getAge());

	long t = 555555L;
	TimestampIntroductionInterceptor ti = new TimestampIntroductionInterceptor(t);

	Class<?>[] oldProxiedInterfaces = factory.getProxiedInterfaces();

	factory.addAdvisor(0, new DefaultIntroductionAdvisor(ti, TimeStamped.class));

	Class<?>[] newProxiedInterfaces = factory.getProxiedInterfaces();
	assertEquals("Advisor proxies one more interface after introduction", oldProxiedInterfaces.length + 1, newProxiedInterfaces.length);

	TimeStamped ts = (TimeStamped) factory.getProxy();
	assertTrue(ts.getTimeStamp() == t);
	// Shouldn't fail;
	((IOther) ts).absquatulate();
}
 
Example #30
Source File: AbstractAopProxyTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
/**
 * Note that an introduction can't throw an unexpected checked exception,
 * as it's constrained by the interface.
 */
@Test
public void testIntroductionThrowsUncheckedException() throws Throwable {
	TestBean target = new TestBean();
	target.setAge(21);
	ProxyFactory pc = new ProxyFactory(target);

	@SuppressWarnings("serial")
	class MyDi extends DelegatingIntroductionInterceptor implements TimeStamped {
		/**
		 * @see test.util.TimeStamped#getTimeStamp()
		 */
		@Override
		public long getTimeStamp() {
			throw new UnsupportedOperationException();
		}
	}
	pc.addAdvisor(new DefaultIntroductionAdvisor(new MyDi()));

	TimeStamped ts = (TimeStamped) createProxy(pc);
	try {
		ts.getTimeStamp();
		fail("Should throw UnsupportedOperationException");
	}
	catch (UnsupportedOperationException ex) {
	}
}