Skip to content

Commit ed7c169

Browse files
aliraza995pivovarit
authored andcommitted
Merging BAEL-968: Apache Commons BeanUtils (eugenp#2149)
1 parent 526fd05 commit ed7c169

5 files changed

Lines changed: 127 additions & 1 deletion

File tree

‎libraries/pom.xml‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@
106106
<artifactId>cglib</artifactId>
107107
<version>${cglib.version}</version>
108108
</dependency>
109+
<dependency>
110+
<groupId>commons-beanutils</groupId>
111+
<artifactId>commons-beanutils</artifactId>
112+
<version>${commons-beanutils.version}</version>
113+
</dependency>
109114
<dependency>
110115
<groupId>org.apache.commons</groupId>
111116
<artifactId>commons-lang3</artifactId>
@@ -383,6 +388,7 @@
383388
<multiverse.version>0.7.0</multiverse.version>
384389
<cglib.version>3.2.4</cglib.version>
385390
<commons-lang.version>3.5</commons-lang.version>
391+
<commons-beanutils.version>1.9.3</commons-beanutils.version>
386392
<jasypt.version>1.9.2</jasypt.version>
387393
<javatuples.version>1.2</javatuples.version>
388394
<javaassist.version>3.21.0-GA</javaassist.version>
@@ -406,4 +412,4 @@
406412
<java-lsh.version>0.10</java-lsh.version>
407413
</properties>
408414

409-
</project>
415+
</project>
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 Course {
8+
private String name;
9+
private List<String> codes;
10+
private Map<String, Student> enrolledStudent = 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 setEnrolledStudent(String enrolledId, Student student) {
29+
enrolledStudent.put(enrolledId, student);
30+
}
31+
32+
public Student getEnrolledStudent(String enrolledId) {
33+
return enrolledStudent.get(enrolledId);
34+
}
35+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.baeldung.commons.beanutils;
2+
3+
import java.lang.reflect.InvocationTargetException;
4+
import java.util.List;
5+
6+
import org.apache.commons.beanutils.PropertyUtils;
7+
8+
public class CourseService {
9+
10+
public static void setValues(Course course, String name, List<String> codes)
11+
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
12+
// Setting the simple properties
13+
PropertyUtils.setSimpleProperty(course, "name", name);
14+
PropertyUtils.setSimpleProperty(course, "codes", codes);
15+
}
16+
17+
public static void setIndexedValue(Course course, int codeIndex, String code)
18+
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
19+
// Setting the indexed properties
20+
PropertyUtils.setIndexedProperty(course, "codes[" + codeIndex + "]", code);
21+
}
22+
23+
public static void setMappedValue(Course course, String enrollId, Student student)
24+
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
25+
// Setting the mapped properties
26+
PropertyUtils.setMappedProperty(course, "enrolledStudent(" + enrollId + ")", student);
27+
}
28+
29+
public static String getNestedValue(Course course, String enrollId, String nestedPropertyName)
30+
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
31+
return (String) PropertyUtils.getNestedProperty(
32+
course, "enrolledStudent(" + enrollId + ")." + nestedPropertyName);
33+
}
34+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung.commons.beanutils;
2+
3+
public class Student {
4+
private String name;
5+
6+
public String getName() {
7+
return name;
8+
}
9+
10+
public void setName(String name) {
11+
this.name = name;
12+
}
13+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.baeldung.commons.beanutils;
2+
3+
import java.lang.reflect.InvocationTargetException;
4+
import java.util.Arrays;
5+
import java.util.List;
6+
7+
import org.junit.Assert;
8+
import org.junit.Test;
9+
10+
public class CourseServiceTest {
11+
12+
@Test
13+
public void givenCourse_whenSetValuesUsingPropertyUtil_thenReturnSetValues()
14+
throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
15+
Course course = new Course();
16+
String name = "Computer Science";
17+
List<String> codes = Arrays.asList("CS", "CS01");
18+
CourseService.setValues(course, name, codes);
19+
20+
Assert.assertEquals(name, course.getName());
21+
Assert.assertEquals(2, course.getCodes().size());
22+
Assert.assertEquals("CS", course.getCodes().get(0));
23+
24+
CourseService.setIndexedValue(course, 1, "CS02");
25+
Assert.assertEquals("CS02", course.getCodes().get(1));
26+
27+
Student student = new Student();
28+
String studentName = "Joe";
29+
student.setName(studentName);
30+
31+
CourseService.setMappedValue(course, "ST-1", student);
32+
Assert.assertEquals(student, course.getEnrolledStudent("ST-1"));
33+
34+
String accessedStudentName = CourseService.getNestedValue(course, "ST-1", "name");
35+
Assert.assertEquals(studentName, accessedStudentName);
36+
}
37+
38+
}

0 commit comments

Comments
 (0)