BAEL-633 more descriptive code
This commit is contained in:
parent
895bcc1921
commit
31cf702160
@ -3,6 +3,6 @@ package com.baeldung.cglib.mixin;
|
|||||||
public class Class1 implements Interface1 {
|
public class Class1 implements Interface1 {
|
||||||
@Override
|
@Override
|
||||||
public String first() {
|
public String first() {
|
||||||
return "first";
|
return "first behaviour";
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -3,6 +3,6 @@ package com.baeldung.cglib.mixin;
|
|||||||
public class Class2 implements Interface2 {
|
public class Class2 implements Interface2 {
|
||||||
@Override
|
@Override
|
||||||
public String second() {
|
public String second() {
|
||||||
return "second";
|
return "second behaviour";
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -16,13 +16,13 @@ public class BeanGeneratorTest {
|
|||||||
BeanGenerator beanGenerator = new BeanGenerator();
|
BeanGenerator beanGenerator = new BeanGenerator();
|
||||||
|
|
||||||
//when
|
//when
|
||||||
beanGenerator.addProperty("value", String.class);
|
beanGenerator.addProperty("name", String.class);
|
||||||
Object myBean = beanGenerator.create();
|
Object myBean = beanGenerator.create();
|
||||||
Method setter = myBean.getClass().getMethod("setValue", String.class);
|
Method setter = myBean.getClass().getMethod("setName", String.class);
|
||||||
setter.invoke(myBean, "some string value set by a cglib");
|
setter.invoke(myBean, "some string value set by a cglib");
|
||||||
|
|
||||||
//then
|
//then
|
||||||
Method getter = myBean.getClass().getMethod("getValue");
|
Method getter = myBean.getClass().getMethod("getName");
|
||||||
assertEquals("some string value set by a cglib", getter.invoke(myBean));
|
assertEquals("some string value set by a cglib", getter.invoke(myBean));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -9,7 +9,7 @@ import static junit.framework.TestCase.assertEquals;
|
|||||||
public class MixinTest {
|
public class MixinTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMixin() throws Exception {
|
public void testMixinBehaviour() throws Exception {
|
||||||
//when
|
//when
|
||||||
Mixin mixin = Mixin.create(
|
Mixin mixin = Mixin.create(
|
||||||
new Class[]{Interface1.class, Interface2.class, MixinInterface.class},
|
new Class[]{Interface1.class, Interface2.class, MixinInterface.class},
|
||||||
@ -18,7 +18,7 @@ public class MixinTest {
|
|||||||
MixinInterface mixinDelegate = (MixinInterface) mixin;
|
MixinInterface mixinDelegate = (MixinInterface) mixin;
|
||||||
|
|
||||||
//then
|
//then
|
||||||
assertEquals("first", mixinDelegate.first());
|
assertEquals("first behaviour", mixinDelegate.first());
|
||||||
assertEquals("second", mixinDelegate.second());
|
assertEquals("second behaviour", mixinDelegate.second());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -25,14 +25,14 @@ public class PersonServiceProxyTest {
|
|||||||
//given
|
//given
|
||||||
Enhancer enhancer = new Enhancer();
|
Enhancer enhancer = new Enhancer();
|
||||||
enhancer.setSuperclass(PersonService.class);
|
enhancer.setSuperclass(PersonService.class);
|
||||||
enhancer.setCallback((FixedValue) () -> "Hello cglib!");
|
enhancer.setCallback((FixedValue) () -> "Hello Tom!");
|
||||||
PersonService proxy = (PersonService) enhancer.create();
|
PersonService proxy = (PersonService) enhancer.create();
|
||||||
|
|
||||||
//when
|
//when
|
||||||
String res = proxy.sayHello(null);
|
String res = proxy.sayHello(null);
|
||||||
|
|
||||||
//then
|
//then
|
||||||
assertEquals("Hello cglib!", res);
|
assertEquals("Hello Tom!", res);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@ -42,7 +42,7 @@ public class PersonServiceProxyTest {
|
|||||||
enhancer.setSuperclass(PersonService.class);
|
enhancer.setSuperclass(PersonService.class);
|
||||||
enhancer.setCallback((MethodInterceptor) (obj, method, args, proxy) -> {
|
enhancer.setCallback((MethodInterceptor) (obj, method, args, proxy) -> {
|
||||||
if (method.getDeclaringClass() != Object.class && method.getReturnType() == String.class) {
|
if (method.getDeclaringClass() != Object.class && method.getReturnType() == String.class) {
|
||||||
return "Hello cglib!";
|
return "Hello Tom!";
|
||||||
} else {
|
} else {
|
||||||
return proxy.invokeSuper(obj, args);
|
return proxy.invokeSuper(obj, args);
|
||||||
}
|
}
|
||||||
@ -52,8 +52,7 @@ public class PersonServiceProxyTest {
|
|||||||
PersonService proxy = (PersonService) enhancer.create();
|
PersonService proxy = (PersonService) enhancer.create();
|
||||||
|
|
||||||
//then
|
//then
|
||||||
assertEquals("Hello cglib!", proxy.sayHello(null));
|
assertEquals("Hello Tom!", proxy.sayHello(null));
|
||||||
|
|
||||||
int lengthOfName = proxy.lengthOfName("Mary");
|
int lengthOfName = proxy.lengthOfName("Mary");
|
||||||
assertEquals(4, lengthOfName);
|
assertEquals(4, lengthOfName);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user