Merging BAEL-968: Apache Commons BeanUtils (#2149)
This commit is contained in:
parent
526fd057ce
commit
ed7c1694a7
|
@ -106,6 +106,11 @@
|
|||
<artifactId>cglib</artifactId>
|
||||
<version>${cglib.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-beanutils</groupId>
|
||||
<artifactId>commons-beanutils</artifactId>
|
||||
<version>${commons-beanutils.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
|
@ -383,6 +388,7 @@
|
|||
<multiverse.version>0.7.0</multiverse.version>
|
||||
<cglib.version>3.2.4</cglib.version>
|
||||
<commons-lang.version>3.5</commons-lang.version>
|
||||
<commons-beanutils.version>1.9.3</commons-beanutils.version>
|
||||
<jasypt.version>1.9.2</jasypt.version>
|
||||
<javatuples.version>1.2</javatuples.version>
|
||||
<javaassist.version>3.21.0-GA</javaassist.version>
|
||||
|
@ -406,4 +412,4 @@
|
|||
<java-lsh.version>0.10</java-lsh.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
</project>
|
||||
|
|
|
@ -0,0 +1,35 @@
|
|||
package com.baeldung.commons.beanutils;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class Course {
|
||||
private String name;
|
||||
private List<String> codes;
|
||||
private Map<String, Student> enrolledStudent = new HashMap<String, Student>();
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public List<String> getCodes() {
|
||||
return codes;
|
||||
}
|
||||
|
||||
public void setCodes(List<String> codes) {
|
||||
this.codes = codes;
|
||||
}
|
||||
|
||||
public void setEnrolledStudent(String enrolledId, Student student) {
|
||||
enrolledStudent.put(enrolledId, student);
|
||||
}
|
||||
|
||||
public Student getEnrolledStudent(String enrolledId) {
|
||||
return enrolledStudent.get(enrolledId);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
package com.baeldung.commons.beanutils;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.List;
|
||||
|
||||
import org.apache.commons.beanutils.PropertyUtils;
|
||||
|
||||
public class CourseService {
|
||||
|
||||
public static void setValues(Course course, String name, List<String> codes)
|
||||
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
|
||||
// Setting the simple properties
|
||||
PropertyUtils.setSimpleProperty(course, "name", name);
|
||||
PropertyUtils.setSimpleProperty(course, "codes", codes);
|
||||
}
|
||||
|
||||
public static void setIndexedValue(Course course, int codeIndex, String code)
|
||||
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
|
||||
// Setting the indexed properties
|
||||
PropertyUtils.setIndexedProperty(course, "codes[" + codeIndex + "]", code);
|
||||
}
|
||||
|
||||
public static void setMappedValue(Course course, String enrollId, Student student)
|
||||
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
|
||||
// Setting the mapped properties
|
||||
PropertyUtils.setMappedProperty(course, "enrolledStudent(" + enrollId + ")", student);
|
||||
}
|
||||
|
||||
public static String getNestedValue(Course course, String enrollId, String nestedPropertyName)
|
||||
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
|
||||
return (String) PropertyUtils.getNestedProperty(
|
||||
course, "enrolledStudent(" + enrollId + ")." + nestedPropertyName);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
package com.baeldung.commons.beanutils;
|
||||
|
||||
public class Student {
|
||||
private String name;
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
package com.baeldung.commons.beanutils;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class CourseServiceTest {
|
||||
|
||||
@Test
|
||||
public void givenCourse_whenSetValuesUsingPropertyUtil_thenReturnSetValues()
|
||||
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
|
||||
Course course = new Course();
|
||||
String name = "Computer Science";
|
||||
List<String> codes = Arrays.asList("CS", "CS01");
|
||||
CourseService.setValues(course, name, codes);
|
||||
|
||||
Assert.assertEquals(name, course.getName());
|
||||
Assert.assertEquals(2, course.getCodes().size());
|
||||
Assert.assertEquals("CS", course.getCodes().get(0));
|
||||
|
||||
CourseService.setIndexedValue(course, 1, "CS02");
|
||||
Assert.assertEquals("CS02", course.getCodes().get(1));
|
||||
|
||||
Student student = new Student();
|
||||
String studentName = "Joe";
|
||||
student.setName(studentName);
|
||||
|
||||
CourseService.setMappedValue(course, "ST-1", student);
|
||||
Assert.assertEquals(student, course.getEnrolledStudent("ST-1"));
|
||||
|
||||
String accessedStudentName = CourseService.getNestedValue(course, "ST-1", "name");
|
||||
Assert.assertEquals(studentName, accessedStudentName);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue