committed by
GitHub
parent
db5dc24fdb
commit
86029a3487
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.cglib.proxy;
|
||||
|
||||
|
||||
import net.sf.cglib.beans.BeanGenerator;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import static junit.framework.TestCase.assertEquals;
|
||||
|
||||
public class BeanGeneratorTest {
|
||||
|
||||
@Test
|
||||
public void givenBeanCreator_whenAddProperty_thenClassShouldHaveFieldValue() throws Exception {
|
||||
//given
|
||||
BeanGenerator beanGenerator = new BeanGenerator();
|
||||
|
||||
//when
|
||||
beanGenerator.addProperty("name", String.class);
|
||||
Object myBean = beanGenerator.create();
|
||||
Method setter = myBean
|
||||
.getClass()
|
||||
.getMethod("setName", String.class);
|
||||
setter.invoke(myBean, "some string value set by a cglib");
|
||||
|
||||
//then
|
||||
Method getter = myBean
|
||||
.getClass()
|
||||
.getMethod("getName");
|
||||
assertEquals("some string value set by a cglib", getter.invoke(myBean));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.cglib.proxy;
|
||||
|
||||
import com.baeldung.cglib.mixin.*;
|
||||
import net.sf.cglib.proxy.Mixin;
|
||||
import org.junit.Test;
|
||||
|
||||
import static junit.framework.TestCase.assertEquals;
|
||||
|
||||
public class MixinTest {
|
||||
|
||||
@Test
|
||||
public void givenTwoClasses_whenMixedIntoOne_thenMixinShouldHaveMethodsFromBothClasses() throws Exception {
|
||||
//when
|
||||
Mixin mixin = Mixin.create(
|
||||
new Class[]{Interface1.class, Interface2.class, MixinInterface.class},
|
||||
new Object[]{new Class1(), new Class2()}
|
||||
);
|
||||
MixinInterface mixinDelegate = (MixinInterface) mixin;
|
||||
|
||||
//then
|
||||
assertEquals("first behaviour", mixinDelegate.first());
|
||||
assertEquals("second behaviour", mixinDelegate.second());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package com.baeldung.cglib.proxy;
|
||||
|
||||
import net.sf.cglib.proxy.Enhancer;
|
||||
import net.sf.cglib.proxy.FixedValue;
|
||||
import net.sf.cglib.proxy.MethodInterceptor;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
public class ProxyTest {
|
||||
@Test
|
||||
public void givenPersonService_whenSayHello_thenReturnResult() {
|
||||
//given
|
||||
PersonService personService = new PersonService();
|
||||
|
||||
//when
|
||||
String res = personService.sayHello("Tom");
|
||||
|
||||
//then
|
||||
assertEquals(res, "Hello Tom");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEnhancerProxy_whenExtendPersonService_thenInterceptMethod() throws Exception {
|
||||
//given
|
||||
Enhancer enhancer = new Enhancer();
|
||||
enhancer.setSuperclass(PersonService.class);
|
||||
enhancer.setCallback((FixedValue) () -> "Hello Tom!");
|
||||
PersonService proxy = (PersonService) enhancer.create();
|
||||
|
||||
//when
|
||||
String res = proxy.sayHello(null);
|
||||
|
||||
//then
|
||||
assertEquals("Hello Tom!", res);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEnhancer_whenExecuteMethodOnProxy_thenInterceptOnlyStringReturnTypeMethod() throws Exception {
|
||||
//given
|
||||
Enhancer enhancer = new Enhancer();
|
||||
enhancer.setSuperclass(PersonService.class);
|
||||
enhancer.setCallback((MethodInterceptor) (obj, method, args, proxy) -> {
|
||||
if (method.getDeclaringClass() != Object.class && method.getReturnType() == String.class) {
|
||||
return "Hello Tom!";
|
||||
} else {
|
||||
return proxy.invokeSuper(obj, args);
|
||||
}
|
||||
});
|
||||
|
||||
//when
|
||||
PersonService proxy = (PersonService) enhancer.create();
|
||||
|
||||
//then
|
||||
assertEquals("Hello Tom!", proxy.sayHello(null));
|
||||
int lengthOfName = proxy.lengthOfName("Mary");
|
||||
assertEquals(4, lengthOfName);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user