From 3cb429339b590a1a1447ba4e52a17fff0f409a72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=87=A1=E6=AF=85?= Date: Sat, 6 Feb 2021 03:43:40 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=8D=95=E5=85=83=E6=B5=8B?= =?UTF-8?q?=E8=AF=95.=20#175?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../extension/register/CglibProxyFactory.java | 34 ++++++++++++++++++ .../register/ExtensionRegisterTest.java | 36 +++++++++++++++++++ .../cola/extension/register/SomeExtPt.java | 8 +++++ .../extension/register/SomeExtensionA.java | 16 +++++++++ .../extension/register/SomeExtensionB.java | 16 +++++++++ 5 files changed, 110 insertions(+) create mode 100644 cola-components/cola-component-extension-starter/src/test/java/com/alibaba/cola/extension/register/CglibProxyFactory.java create mode 100644 cola-components/cola-component-extension-starter/src/test/java/com/alibaba/cola/extension/register/ExtensionRegisterTest.java create mode 100644 cola-components/cola-component-extension-starter/src/test/java/com/alibaba/cola/extension/register/SomeExtPt.java create mode 100644 cola-components/cola-component-extension-starter/src/test/java/com/alibaba/cola/extension/register/SomeExtensionA.java create mode 100644 cola-components/cola-component-extension-starter/src/test/java/com/alibaba/cola/extension/register/SomeExtensionB.java diff --git a/cola-components/cola-component-extension-starter/src/test/java/com/alibaba/cola/extension/register/CglibProxyFactory.java b/cola-components/cola-component-extension-starter/src/test/java/com/alibaba/cola/extension/register/CglibProxyFactory.java new file mode 100644 index 000000000..b360a5513 --- /dev/null +++ b/cola-components/cola-component-extension-starter/src/test/java/com/alibaba/cola/extension/register/CglibProxyFactory.java @@ -0,0 +1,34 @@ +package com.alibaba.cola.extension.register; + +import java.lang.reflect.Method; + +import org.springframework.cglib.proxy.Enhancer; +import org.springframework.cglib.proxy.MethodInterceptor; +import org.springframework.cglib.proxy.MethodProxy; + +public class CglibProxyFactory { + + public static T createProxy(T object) { + Enhancer enhancer = new Enhancer(); + enhancer.setSuperclass(object.getClass()); + enhancer.setCallback(new ProxyCallback(object)); + return (T) enhancer.create(); + } + + public static class ProxyCallback implements MethodInterceptor { + + private Object target; + + public ProxyCallback(Object target) { + this.target = target; + } + + @Override + public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { + System.out.println("ProxyObject::before"); + Object object = proxy.invoke(target, args); + System.out.println("ProxyObject::after"); + return object; + } + } +} diff --git a/cola-components/cola-component-extension-starter/src/test/java/com/alibaba/cola/extension/register/ExtensionRegisterTest.java b/cola-components/cola-component-extension-starter/src/test/java/com/alibaba/cola/extension/register/ExtensionRegisterTest.java new file mode 100644 index 000000000..f671160b6 --- /dev/null +++ b/cola-components/cola-component-extension-starter/src/test/java/com/alibaba/cola/extension/register/ExtensionRegisterTest.java @@ -0,0 +1,36 @@ +package com.alibaba.cola.extension.register; + +import javax.annotation.Resource; + +import com.alibaba.cola.extension.BizScenario; +import com.alibaba.cola.extension.ExtensionExecutor; +import com.alibaba.cola.extension.test.Application; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringBootTest(classes = Application.class) +public class ExtensionRegisterTest { + + @Resource + private ExtensionRegister register; + + @Resource + private ExtensionExecutor executor; + + @Test + public void test() { + SomeExtPt extA = new SomeExtensionA(); + register.doRegistration(extA); + + SomeExtPt extB = CglibProxyFactory.createProxy(new SomeExtensionB()); + register.doRegistration(extB); + + executor.executeVoid(SomeExtPt.class, BizScenario.valueOf("A"), SomeExtPt::doSomeThing); + executor.executeVoid(SomeExtPt.class, BizScenario.valueOf("B"), SomeExtPt::doSomeThing); + } + +} diff --git a/cola-components/cola-component-extension-starter/src/test/java/com/alibaba/cola/extension/register/SomeExtPt.java b/cola-components/cola-component-extension-starter/src/test/java/com/alibaba/cola/extension/register/SomeExtPt.java new file mode 100644 index 000000000..c6707ec60 --- /dev/null +++ b/cola-components/cola-component-extension-starter/src/test/java/com/alibaba/cola/extension/register/SomeExtPt.java @@ -0,0 +1,8 @@ +package com.alibaba.cola.extension.register; + +import com.alibaba.cola.extension.ExtensionPointI; + +public interface SomeExtPt extends ExtensionPointI { + + public void doSomeThing(); +} diff --git a/cola-components/cola-component-extension-starter/src/test/java/com/alibaba/cola/extension/register/SomeExtensionA.java b/cola-components/cola-component-extension-starter/src/test/java/com/alibaba/cola/extension/register/SomeExtensionA.java new file mode 100644 index 000000000..c3a5ed97a --- /dev/null +++ b/cola-components/cola-component-extension-starter/src/test/java/com/alibaba/cola/extension/register/SomeExtensionA.java @@ -0,0 +1,16 @@ +package com.alibaba.cola.extension.register; + +import com.alibaba.cola.extension.Extension; + +import org.springframework.stereotype.Component; + +@Extension(bizId = "A") +@Component +public class SomeExtensionA implements SomeExtPt { + + @Override + public void doSomeThing() { + System.out.println("SomeExtensionA::doSomething"); + } + +} \ No newline at end of file diff --git a/cola-components/cola-component-extension-starter/src/test/java/com/alibaba/cola/extension/register/SomeExtensionB.java b/cola-components/cola-component-extension-starter/src/test/java/com/alibaba/cola/extension/register/SomeExtensionB.java new file mode 100644 index 000000000..afee1d7e9 --- /dev/null +++ b/cola-components/cola-component-extension-starter/src/test/java/com/alibaba/cola/extension/register/SomeExtensionB.java @@ -0,0 +1,16 @@ +package com.alibaba.cola.extension.register; + +import com.alibaba.cola.extension.Extension; + +import org.springframework.stereotype.Component; + +@Extension(bizId = "B") +@Component +public class SomeExtensionB implements SomeExtPt { + + @Override + public void doSomeThing() { + System.out.println("SomeExtensionB::doSomething"); + } + +}