Skip to content

Commit

Permalink
添加单元测试. alibaba#175
Browse files Browse the repository at this point in the history
  • Loading branch information
fanyi-zhao committed Feb 5, 2021
1 parent 4506cfa commit 3cb4293
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 0 deletions.
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;
}
}
}
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);
}

}
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();
}
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");
}

}
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");
}

}

0 comments on commit 3cb4293

Please sign in to comment.