BAEL-633 more descriptive code

This commit is contained in:
Tomasz Lelek 2017-02-17 15:57:35 +01:00
parent 895bcc1921
commit 31cf702160
5 changed files with 12 additions and 13 deletions

View File

@ -3,6 +3,6 @@ package com.baeldung.cglib.mixin;
public class Class1 implements Interface1 {
@Override
public String first() {
return "first";
return "first behaviour";
}
}

View File

@ -3,6 +3,6 @@ package com.baeldung.cglib.mixin;
public class Class2 implements Interface2 {
@Override
public String second() {
return "second";
return "second behaviour";
}
}

View File

@ -16,13 +16,13 @@ public class BeanGeneratorTest {
BeanGenerator beanGenerator = new BeanGenerator();
//when
beanGenerator.addProperty("value", String.class);
beanGenerator.addProperty("name", String.class);
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");
//then
Method getter = myBean.getClass().getMethod("getValue");
Method getter = myBean.getClass().getMethod("getName");
assertEquals("some string value set by a cglib", getter.invoke(myBean));
}
}

View File

@ -9,7 +9,7 @@ import static junit.framework.TestCase.assertEquals;
public class MixinTest {
@Test
public void testMixin() throws Exception {
public void testMixinBehaviour() throws Exception {
//when
Mixin mixin = Mixin.create(
new Class[]{Interface1.class, Interface2.class, MixinInterface.class},
@ -18,7 +18,7 @@ public class MixinTest {
MixinInterface mixinDelegate = (MixinInterface) mixin;
//then
assertEquals("first", mixinDelegate.first());
assertEquals("second", mixinDelegate.second());
assertEquals("first behaviour", mixinDelegate.first());
assertEquals("second behaviour", mixinDelegate.second());
}
}

View File

@ -25,14 +25,14 @@ public class PersonServiceProxyTest {
//given
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(PersonService.class);
enhancer.setCallback((FixedValue) () -> "Hello cglib!");
enhancer.setCallback((FixedValue) () -> "Hello Tom!");
PersonService proxy = (PersonService) enhancer.create();
//when
String res = proxy.sayHello(null);
//then
assertEquals("Hello cglib!", res);
assertEquals("Hello Tom!", res);
}
@Test
@ -42,7 +42,7 @@ public class PersonServiceProxyTest {
enhancer.setSuperclass(PersonService.class);
enhancer.setCallback((MethodInterceptor) (obj, method, args, proxy) -> {
if (method.getDeclaringClass() != Object.class && method.getReturnType() == String.class) {
return "Hello cglib!";
return "Hello Tom!";
} else {
return proxy.invokeSuper(obj, args);
}
@ -52,8 +52,7 @@ public class PersonServiceProxyTest {
PersonService proxy = (PersonService) enhancer.create();
//then
assertEquals("Hello cglib!", proxy.sayHello(null));
assertEquals("Hello Tom!", proxy.sayHello(null));
int lengthOfName = proxy.lengthOfName("Mary");
assertEquals(4, lengthOfName);
}