Merge branch 'BAEL-633' of https://github.com/tomekl007/tutorials into tomekl007-BAEL-633

This commit is contained in:
Pedja 2017-02-22 12:49:19 +01:00
commit 9d71f7295b
11 changed files with 205 additions and 0 deletions

47
cglib/pom.xml Normal file
View File

@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>parent-modules</artifactId>
<groupId>com.baeldung</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cglib</artifactId>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- https://mvnrepository.com/artifact/cglib/cglib -->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>${cglib.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<cglib.version>3.2.4</cglib.version>
<junit.version>4.12</junit.version>
</properties>
</project>

View File

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

View File

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

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

@ -0,0 +1,32 @@
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 givenBeanCreator_whenAddPropery_classShouldHaveFieldValue() throws Exception {
//given
BeanGenerator beanGenerator = new BeanGenerator();
//when
beanGenerator.addProperty("name", String.class);
Object myBean = beanGenerator.create();
Method setter = myBean
.getClass()
.getMethod("setName", String.class);
setter.invoke(myBean, "some string value set by a cglib");
//then
Method getter = myBean
.getClass()
.getMethod("getName");
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 givenTwoClasses_whenMixtThemIntoOne_mixinShouldHaveMethodsFromBothClasses() 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 behaviour", mixinDelegate.first());
assertEquals("second behaviour", mixinDelegate.second());
}
}

View File

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

View File

@ -28,6 +28,7 @@
<module>autovalue</module>
<module>cdi</module>
<module>cglib</module>
<!-- <module>core-java-9</module> -->
<module>core-java</module>
<module>couchbase-sdk</module>