BAEL-633 add mixing and bean creator test

This commit is contained in:
Tomasz Lelek 2017-02-17 12:19:57 +01:00
parent b3303dc0cf
commit 895bcc1921
11 changed files with 154 additions and 27 deletions

View File

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

View File

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

View File

@ -0,0 +1,5 @@
package com.baeldung.cglib.mixin;
public interface Interface1 {
String first();
}

View File

@ -0,0 +1,5 @@
package com.baeldung.cglib.mixin;
public interface Interface2 {
String second();
}

View File

@ -0,0 +1,4 @@
package com.baeldung.cglib.mixin;
public interface MixinInterface extends Interface1, Interface2 {
}

View File

@ -0,0 +1,11 @@
package com.baeldung.cglib.proxy;
public class PersonService {
public String sayHello(String name) {
return "Hello " + name;
}
public Integer lengthOfName(String name) {
return name.length();
}
}

View File

@ -1,7 +0,0 @@
package com.baeldung.cglib.proxy;
public class SampleClass {
public String test(String input) {
return "Hello world!";
}
}

View File

@ -0,0 +1,28 @@
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 testBeanGenerator() throws Exception {
//given
BeanGenerator beanGenerator = new BeanGenerator();
//when
beanGenerator.addProperty("value", String.class);
Object myBean = beanGenerator.create();
Method setter = myBean.getClass().getMethod("setValue", String.class);
setter.invoke(myBean, "some string value set by a cglib");
//then
Method getter = myBean.getClass().getMethod("getValue");
assertEquals("some string value set by a cglib", getter.invoke(myBean));
}
}

View File

@ -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 testMixin() 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", mixinDelegate.first());
assertEquals("second", mixinDelegate.second());
}
}

View File

@ -0,0 +1,61 @@
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 PersonServiceProxyTest {
@Test
public void testService() {
//given
PersonService personService = new PersonService();
//when
String res = personService.sayHello("Tom");
//then
assertEquals(res, "Hello Tom");
}
@Test
public void testFixedValue() throws Exception {
//given
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(PersonService.class);
enhancer.setCallback((FixedValue) () -> "Hello cglib!");
PersonService proxy = (PersonService) enhancer.create();
//when
String res = proxy.sayHello(null);
//then
assertEquals("Hello cglib!", res);
}
@Test
public void testMethodInterceptor() 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 cglib!";
} else {
return proxy.invokeSuper(obj, args);
}
});
//when
PersonService proxy = (PersonService) enhancer.create();
//then
assertEquals("Hello cglib!", proxy.sayHello(null));
int lengthOfName = proxy.lengthOfName("Mary");
assertEquals(4, lengthOfName);
}
}

View File

@ -1,20 +0,0 @@
package com.baeldung.cglib.proxy;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.FixedValue;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class SampleClassTest {
@Test
public void testFixedValue() throws Exception {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(SampleClass.class);
enhancer.setCallback((FixedValue) () -> "Hello cglib!");
SampleClass proxy = (SampleClass) enhancer.create();
assertEquals("Hello cglib!", proxy.test(null));
}
}