Skip to content

Commit 895bcc1

Browse files
author
Tomasz Lelek
committed
BAEL-633 add mixing and bean creator test
1 parent b3303dc commit 895bcc1

11 files changed

Lines changed: 154 additions & 27 deletions

File tree

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.cglib.mixin;
2+
3+
public class Class1 implements Interface1 {
4+
@Override
5+
public String first() {
6+
return "first";
7+
}
8+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.baeldung.cglib.mixin;
2+
3+
public class Class2 implements Interface2 {
4+
@Override
5+
public String second() {
6+
return "second";
7+
}
8+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.cglib.mixin;
2+
3+
public interface Interface1 {
4+
String first();
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.baeldung.cglib.mixin;
2+
3+
public interface Interface2 {
4+
String second();
5+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.baeldung.cglib.mixin;
2+
3+
public interface MixinInterface extends Interface1, Interface2 {
4+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.baeldung.cglib.proxy;
2+
3+
public class PersonService {
4+
public String sayHello(String name) {
5+
return "Hello " + name;
6+
}
7+
8+
public Integer lengthOfName(String name) {
9+
return name.length();
10+
}
11+
}

‎cglib/src/main/java/com/baeldung/cglib/proxy/SampleClass.java‎

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.baeldung.cglib.proxy;
2+
3+
4+
import net.sf.cglib.beans.BeanGenerator;
5+
import org.junit.Test;
6+
7+
import java.lang.reflect.Method;
8+
9+
import static junit.framework.TestCase.assertEquals;
10+
11+
public class BeanGeneratorTest {
12+
13+
@Test
14+
public void testBeanGenerator() throws Exception {
15+
//given
16+
BeanGenerator beanGenerator = new BeanGenerator();
17+
18+
//when
19+
beanGenerator.addProperty("value", String.class);
20+
Object myBean = beanGenerator.create();
21+
Method setter = myBean.getClass().getMethod("setValue", String.class);
22+
setter.invoke(myBean, "some string value set by a cglib");
23+
24+
//then
25+
Method getter = myBean.getClass().getMethod("getValue");
26+
assertEquals("some string value set by a cglib", getter.invoke(myBean));
27+
}
28+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.cglib.proxy;
2+
3+
import com.baeldung.cglib.mixin.*;
4+
import net.sf.cglib.proxy.Mixin;
5+
import org.junit.Test;
6+
7+
import static junit.framework.TestCase.assertEquals;
8+
9+
public class MixinTest {
10+
11+
@Test
12+
public void testMixin() throws Exception {
13+
//when
14+
Mixin mixin = Mixin.create(
15+
new Class[]{Interface1.class, Interface2.class, MixinInterface.class},
16+
new Object[]{new Class1(), new Class2()}
17+
);
18+
MixinInterface mixinDelegate = (MixinInterface) mixin;
19+
20+
//then
21+
assertEquals("first", mixinDelegate.first());
22+
assertEquals("second", mixinDelegate.second());
23+
}
24+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.baeldung.cglib.proxy;
2+
3+
import net.sf.cglib.proxy.Enhancer;
4+
import net.sf.cglib.proxy.FixedValue;
5+
import net.sf.cglib.proxy.MethodInterceptor;
6+
import org.junit.Test;
7+
8+
import static org.junit.Assert.assertEquals;
9+
10+
public class PersonServiceProxyTest {
11+
@Test
12+
public void testService() {
13+
//given
14+
PersonService personService = new PersonService();
15+
16+
//when
17+
String res = personService.sayHello("Tom");
18+
19+
//then
20+
assertEquals(res, "Hello Tom");
21+
}
22+
23+
@Test
24+
public void testFixedValue() throws Exception {
25+
//given
26+
Enhancer enhancer = new Enhancer();
27+
enhancer.setSuperclass(PersonService.class);
28+
enhancer.setCallback((FixedValue) () -> "Hello cglib!");
29+
PersonService proxy = (PersonService) enhancer.create();
30+
31+
//when
32+
String res = proxy.sayHello(null);
33+
34+
//then
35+
assertEquals("Hello cglib!", res);
36+
}
37+
38+
@Test
39+
public void testMethodInterceptor() throws Exception {
40+
//given
41+
Enhancer enhancer = new Enhancer();
42+
enhancer.setSuperclass(PersonService.class);
43+
enhancer.setCallback((MethodInterceptor) (obj, method, args, proxy) -> {
44+
if (method.getDeclaringClass() != Object.class && method.getReturnType() == String.class) {
45+
return "Hello cglib!";
46+
} else {
47+
return proxy.invokeSuper(obj, args);
48+
}
49+
});
50+
51+
//when
52+
PersonService proxy = (PersonService) enhancer.create();
53+
54+
//then
55+
assertEquals("Hello cglib!", proxy.sayHello(null));
56+
57+
int lengthOfName = proxy.lengthOfName("Mary");
58+
assertEquals(4, lengthOfName);
59+
}
60+
61+
}

0 commit comments

Comments
 (0)