From 895bcc1921eb716d6f7bd08bbef8c1e124753445 Mon Sep 17 00:00:00 2001 From: Tomasz Lelek Date: Fri, 17 Feb 2017 12:19:57 +0100 Subject: [PATCH] BAEL-633 add mixing and bean creator test --- .../java/com/baeldung/cglib/mixin/Class1.java | 8 +++ .../java/com/baeldung/cglib/mixin/Class2.java | 8 +++ .../com/baeldung/cglib/mixin/Interface1.java | 5 ++ .../com/baeldung/cglib/mixin/Interface2.java | 5 ++ .../baeldung/cglib/mixin/MixinInterface.java | 4 ++ .../baeldung/cglib/proxy/PersonService.java | 11 ++++ .../com/baeldung/cglib/proxy/SampleClass.java | 7 --- .../cglib/proxy/BeanGeneratorTest.java | 28 +++++++++ .../com/baeldung/cglib/proxy/MixinTest.java | 24 ++++++++ .../cglib/proxy/PersonServiceProxyTest.java | 61 +++++++++++++++++++ .../baeldung/cglib/proxy/SampleClassTest.java | 20 ------ 11 files changed, 154 insertions(+), 27 deletions(-) create mode 100644 cglib/src/main/java/com/baeldung/cglib/mixin/Class1.java create mode 100644 cglib/src/main/java/com/baeldung/cglib/mixin/Class2.java create mode 100644 cglib/src/main/java/com/baeldung/cglib/mixin/Interface1.java create mode 100644 cglib/src/main/java/com/baeldung/cglib/mixin/Interface2.java create mode 100644 cglib/src/main/java/com/baeldung/cglib/mixin/MixinInterface.java create mode 100644 cglib/src/main/java/com/baeldung/cglib/proxy/PersonService.java delete mode 100644 cglib/src/main/java/com/baeldung/cglib/proxy/SampleClass.java create mode 100644 cglib/src/test/java/com/baeldung/cglib/proxy/BeanGeneratorTest.java create mode 100644 cglib/src/test/java/com/baeldung/cglib/proxy/MixinTest.java create mode 100644 cglib/src/test/java/com/baeldung/cglib/proxy/PersonServiceProxyTest.java delete mode 100644 cglib/src/test/java/com/baeldung/cglib/proxy/SampleClassTest.java diff --git a/cglib/src/main/java/com/baeldung/cglib/mixin/Class1.java b/cglib/src/main/java/com/baeldung/cglib/mixin/Class1.java new file mode 100644 index 0000000000..2921cda8a6 --- /dev/null +++ b/cglib/src/main/java/com/baeldung/cglib/mixin/Class1.java @@ -0,0 +1,8 @@ +package com.baeldung.cglib.mixin; + +public class Class1 implements Interface1 { + @Override + public String first() { + return "first"; + } +} \ No newline at end of file diff --git a/cglib/src/main/java/com/baeldung/cglib/mixin/Class2.java b/cglib/src/main/java/com/baeldung/cglib/mixin/Class2.java new file mode 100644 index 0000000000..0098c9debd --- /dev/null +++ b/cglib/src/main/java/com/baeldung/cglib/mixin/Class2.java @@ -0,0 +1,8 @@ +package com.baeldung.cglib.mixin; + +public class Class2 implements Interface2 { + @Override + public String second() { + return "second"; + } +} \ No newline at end of file diff --git a/cglib/src/main/java/com/baeldung/cglib/mixin/Interface1.java b/cglib/src/main/java/com/baeldung/cglib/mixin/Interface1.java new file mode 100644 index 0000000000..5aab155c1d --- /dev/null +++ b/cglib/src/main/java/com/baeldung/cglib/mixin/Interface1.java @@ -0,0 +1,5 @@ +package com.baeldung.cglib.mixin; + +public interface Interface1 { + String first(); +} \ No newline at end of file diff --git a/cglib/src/main/java/com/baeldung/cglib/mixin/Interface2.java b/cglib/src/main/java/com/baeldung/cglib/mixin/Interface2.java new file mode 100644 index 0000000000..7fd2e78608 --- /dev/null +++ b/cglib/src/main/java/com/baeldung/cglib/mixin/Interface2.java @@ -0,0 +1,5 @@ +package com.baeldung.cglib.mixin; + +public interface Interface2 { + String second(); +} \ No newline at end of file diff --git a/cglib/src/main/java/com/baeldung/cglib/mixin/MixinInterface.java b/cglib/src/main/java/com/baeldung/cglib/mixin/MixinInterface.java new file mode 100644 index 0000000000..e7ed362a25 --- /dev/null +++ b/cglib/src/main/java/com/baeldung/cglib/mixin/MixinInterface.java @@ -0,0 +1,4 @@ +package com.baeldung.cglib.mixin; + +public interface MixinInterface extends Interface1, Interface2 { +} \ No newline at end of file diff --git a/cglib/src/main/java/com/baeldung/cglib/proxy/PersonService.java b/cglib/src/main/java/com/baeldung/cglib/proxy/PersonService.java new file mode 100644 index 0000000000..9085e6c49a --- /dev/null +++ b/cglib/src/main/java/com/baeldung/cglib/proxy/PersonService.java @@ -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(); + } +} \ No newline at end of file diff --git a/cglib/src/main/java/com/baeldung/cglib/proxy/SampleClass.java b/cglib/src/main/java/com/baeldung/cglib/proxy/SampleClass.java deleted file mode 100644 index 3e0d098dd1..0000000000 --- a/cglib/src/main/java/com/baeldung/cglib/proxy/SampleClass.java +++ /dev/null @@ -1,7 +0,0 @@ -package com.baeldung.cglib.proxy; - -public class SampleClass { - public String test(String input) { - return "Hello world!"; - } -} \ No newline at end of file diff --git a/cglib/src/test/java/com/baeldung/cglib/proxy/BeanGeneratorTest.java b/cglib/src/test/java/com/baeldung/cglib/proxy/BeanGeneratorTest.java new file mode 100644 index 0000000000..4b401ae690 --- /dev/null +++ b/cglib/src/test/java/com/baeldung/cglib/proxy/BeanGeneratorTest.java @@ -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)); + } +} diff --git a/cglib/src/test/java/com/baeldung/cglib/proxy/MixinTest.java b/cglib/src/test/java/com/baeldung/cglib/proxy/MixinTest.java new file mode 100644 index 0000000000..31e7b2204f --- /dev/null +++ b/cglib/src/test/java/com/baeldung/cglib/proxy/MixinTest.java @@ -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()); + } +} diff --git a/cglib/src/test/java/com/baeldung/cglib/proxy/PersonServiceProxyTest.java b/cglib/src/test/java/com/baeldung/cglib/proxy/PersonServiceProxyTest.java new file mode 100644 index 0000000000..2ae6392a05 --- /dev/null +++ b/cglib/src/test/java/com/baeldung/cglib/proxy/PersonServiceProxyTest.java @@ -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); + } + +} \ No newline at end of file diff --git a/cglib/src/test/java/com/baeldung/cglib/proxy/SampleClassTest.java b/cglib/src/test/java/com/baeldung/cglib/proxy/SampleClassTest.java deleted file mode 100644 index 15037b3f1c..0000000000 --- a/cglib/src/test/java/com/baeldung/cglib/proxy/SampleClassTest.java +++ /dev/null @@ -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)); - - } - -} \ No newline at end of file