Feature/bael 968 (#2154)
* Merging BAEL-968: Apache Commons BeanUtils * BAEL-968: Updated code for copyProperties example * BAEL-968: Updated code based on reviewer comments * BAEL-968: Fixed merge issue
This commit is contained in:
parent
c499ecee27
commit
fd790bef2f
@ -0,0 +1,35 @@
|
|||||||
|
package com.baeldung.commons.beanutils;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class CourseEntity {
|
||||||
|
private String name;
|
||||||
|
private List<String> codes;
|
||||||
|
private Map<String, Student> students = 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 setStudent(String id, Student student) {
|
||||||
|
students.put(id, student);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Student getStudent(String enrolledId) {
|
||||||
|
return students.get(enrolledId);
|
||||||
|
}
|
||||||
|
}
|
@ -3,6 +3,7 @@ package com.baeldung.commons.beanutils;
|
|||||||
import java.lang.reflect.InvocationTargetException;
|
import java.lang.reflect.InvocationTargetException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.apache.commons.beanutils.BeanUtils;
|
||||||
import org.apache.commons.beanutils.PropertyUtils;
|
import org.apache.commons.beanutils.PropertyUtils;
|
||||||
|
|
||||||
public class CourseService {
|
public class CourseService {
|
||||||
@ -31,4 +32,9 @@ public class CourseService {
|
|||||||
return (String) PropertyUtils.getNestedProperty(
|
return (String) PropertyUtils.getNestedProperty(
|
||||||
course, "enrolledStudent(" + enrollId + ")." + nestedPropertyName);
|
course, "enrolledStudent(" + enrollId + ")." + nestedPropertyName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void copyProperties(Course course, CourseEntity courseEntity)
|
||||||
|
throws IllegalAccessException, InvocationTargetException {
|
||||||
|
BeanUtils.copyProperties(course, courseEntity);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -35,4 +35,19 @@ public class CourseServiceTest {
|
|||||||
Assert.assertEquals(studentName, accessedStudentName);
|
Assert.assertEquals(studentName, accessedStudentName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenCopyProperties_whenCopyCourseToCourseEntity_thenCopyPropertyWithSameName()
|
||||||
|
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
|
||||||
|
Course course = new Course();
|
||||||
|
course.setName("Computer Science");
|
||||||
|
course.setCodes(Arrays.asList("CS"));
|
||||||
|
course.setEnrolledStudent("ST-1", new Student());
|
||||||
|
|
||||||
|
CourseEntity courseEntity = new CourseEntity();
|
||||||
|
|
||||||
|
CourseService.copyProperties(course, courseEntity);
|
||||||
|
Assert.assertEquals(course.getName(), courseEntity.getName());
|
||||||
|
Assert.assertEquals(course.getCodes(), courseEntity.getCodes());
|
||||||
|
Assert.assertNull(courseEntity.getStudent("ST-1"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user