Skip to content

Commit fd790be

Browse files
aliraza995pivovarit
authored andcommitted
Feature/bael 968 (eugenp#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
1 parent c499ece commit fd790be

3 files changed

Lines changed: 67 additions & 11 deletions

File tree

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.baeldung.commons.beanutils;
2+
3+
import java.util.HashMap;
4+
import java.util.List;
5+
import java.util.Map;
6+
7+
public class CourseEntity {
8+
private String name;
9+
private List<String> codes;
10+
private Map<String, Student> students = new HashMap<String, Student>();
11+
12+
public String getName() {
13+
return name;
14+
}
15+
16+
public void setName(String name) {
17+
this.name = name;
18+
}
19+
20+
public List<String> getCodes() {
21+
return codes;
22+
}
23+
24+
public void setCodes(List<String> codes) {
25+
this.codes = codes;
26+
}
27+
28+
public void setStudent(String id, Student student) {
29+
students.put(id, student);
30+
}
31+
32+
public Student getStudent(String enrolledId) {
33+
return students.get(enrolledId);
34+
}
35+
}

‎libraries/src/main/java/com/baeldung/commons/beanutils/CourseService.java‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,38 @@
33
import java.lang.reflect.InvocationTargetException;
44
import java.util.List;
55

6+
import org.apache.commons.beanutils.BeanUtils;
67
import org.apache.commons.beanutils.PropertyUtils;
78

89
public class CourseService {
910

1011
public static void setValues(Course course, String name, List<String> codes)
11-
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
12+
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
1213
// Setting the simple properties
1314
PropertyUtils.setSimpleProperty(course, "name", name);
1415
PropertyUtils.setSimpleProperty(course, "codes", codes);
1516
}
1617

1718
public static void setIndexedValue(Course course, int codeIndex, String code)
18-
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
19+
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
1920
// Setting the indexed properties
2021
PropertyUtils.setIndexedProperty(course, "codes[" + codeIndex + "]", code);
2122
}
2223

2324
public static void setMappedValue(Course course, String enrollId, Student student)
24-
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
25+
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
2526
// Setting the mapped properties
2627
PropertyUtils.setMappedProperty(course, "enrolledStudent(" + enrollId + ")", student);
2728
}
2829

2930
public static String getNestedValue(Course course, String enrollId, String nestedPropertyName)
30-
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
31+
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
3132
return (String) PropertyUtils.getNestedProperty(
3233
course, "enrolledStudent(" + enrollId + ")." + nestedPropertyName);
3334
}
35+
36+
public static void copyProperties(Course course, CourseEntity courseEntity)
37+
throws IllegalAccessException, InvocationTargetException {
38+
BeanUtils.copyProperties(course, courseEntity);
39+
}
3440
}

‎libraries/src/test/java/com/baeldung/commons/beanutils/CourseServiceTest.java‎

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,31 +8,46 @@
88
import org.junit.Test;
99

1010
public class CourseServiceTest {
11-
11+
1212
@Test
1313
public void givenCourse_whenSetValuesUsingPropertyUtil_thenReturnSetValues()
14-
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
14+
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
1515
Course course = new Course();
1616
String name = "Computer Science";
1717
List<String> codes = Arrays.asList("CS", "CS01");
1818
CourseService.setValues(course, name, codes);
19-
19+
2020
Assert.assertEquals(name, course.getName());
2121
Assert.assertEquals(2, course.getCodes().size());
2222
Assert.assertEquals("CS", course.getCodes().get(0));
23-
23+
2424
CourseService.setIndexedValue(course, 1, "CS02");
2525
Assert.assertEquals("CS02", course.getCodes().get(1));
26-
26+
2727
Student student = new Student();
2828
String studentName = "Joe";
2929
student.setName(studentName);
30-
30+
3131
CourseService.setMappedValue(course, "ST-1", student);
3232
Assert.assertEquals(student, course.getEnrolledStudent("ST-1"));
33-
33+
3434
String accessedStudentName = CourseService.getNestedValue(course, "ST-1", "name");
3535
Assert.assertEquals(studentName, accessedStudentName);
3636
}
3737

38+
@Test
39+
public void givenCopyProperties_whenCopyCourseToCourseEntity_thenCopyPropertyWithSameName()
40+
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
41+
Course course = new Course();
42+
course.setName("Computer Science");
43+
course.setCodes(Arrays.asList("CS"));
44+
course.setEnrolledStudent("ST-1", new Student());
45+
46+
CourseEntity courseEntity = new CourseEntity();
47+
48+
CourseService.copyProperties(course, courseEntity);
49+
Assert.assertEquals(course.getName(), courseEntity.getName());
50+
Assert.assertEquals(course.getCodes(), courseEntity.getCodes());
51+
Assert.assertNull(courseEntity.getStudent("ST-1"));
52+
}
3853
}

0 commit comments

Comments
 (0)