forked from alibaba/COLA
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4506cfa
commit 3cb4293
Showing
5 changed files
with
110 additions
and
0 deletions.
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
...xtension-starter/src/test/java/com/alibaba/cola/extension/register/CglibProxyFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> 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; | ||
} | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
...sion-starter/src/test/java/com/alibaba/cola/extension/register/ExtensionRegisterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
...ponent-extension-starter/src/test/java/com/alibaba/cola/extension/register/SomeExtPt.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.alibaba.cola.extension.register; | ||
|
||
import com.alibaba.cola.extension.ExtensionPointI; | ||
|
||
public interface SomeExtPt extends ExtensionPointI { | ||
|
||
public void doSomeThing(); | ||
} |
16 changes: 16 additions & 0 deletions
16
...t-extension-starter/src/test/java/com/alibaba/cola/extension/register/SomeExtensionA.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
} | ||
|
||
} |
16 changes: 16 additions & 0 deletions
16
...t-extension-starter/src/test/java/com/alibaba/cola/extension/register/SomeExtensionB.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
} | ||
|
||
} |