Skip to content

Commit 7a3e3e7

Browse files
authored
Merge pull request #18750 from dvohra09/master
BAEL-9416 Get Property Value by Property Name in Java
2 parents f2261a0 + 7e8fe9e commit 7a3e3e7

File tree

1 file changed

+51
-3
lines changed

1 file changed

+51
-3
lines changed

libraries-apache-commons/src/test/java/com/baeldung/commons/beanutils/CourseServiceUnitTest.java

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,64 @@
22

33
import static org.assertj.core.api.Assertions.assertThat;
44
import static org.junit.jupiter.api.Assertions.assertEquals;
5-
5+
import static org.junit.jupiter.api.Assertions.assertNotNull;
6+
import static org.junit.jupiter.api.Assertions.assertNull;
7+
import org.junit.jupiter.api.BeforeEach;
8+
import org.apache.commons.beanutils.PropertyUtils;
69
import java.lang.reflect.InvocationTargetException;
710
import java.util.Arrays;
811
import java.util.List;
9-
12+
1013
import org.junit.Assert;
1114
import org.junit.Test;
1215

1316
public class CourseServiceUnitTest {
1417

18+
private static final String STUDENT_ID = "01";
19+
20+
@Test
21+
public void givenCourse_whenGettingSimplePropertyValueUsingPropertyUtil_thenValueReturned() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
22+
Course course = new Course();
23+
String name = "Computer Science";
24+
List<String> codes = Arrays.asList("CS101","CS102");
25+
CourseService.setValues(course, name, codes);
26+
27+
// Use getSimpleProperty to retrieve the 'name' property from the course bean
28+
String courseName = (String) PropertyUtils.getSimpleProperty(course, "name");
29+
30+
assertEquals("Computer Science", courseName);
31+
}
32+
33+
@Test
34+
public void givenCourse_whenGettingIndexedPropertyValueUsingPropertyUtil_thenValueReturned() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
35+
Course course = new Course();
36+
String name = "Computer Science";
37+
List<String> codes = Arrays.asList("CS101","CS102");
38+
CourseService.setValues(course, name, codes);
39+
// Use getIndexedProperty to retrieve the element at index 1 from the 'codes' list
40+
String secondCode = (String) PropertyUtils.getIndexedProperty(course, "codes[1]");
41+
42+
assertEquals("CS102", secondCode);
43+
}
44+
45+
@Test
46+
public void givenCourse_whenGettingMappedPropertyValueUsingPropertyUtil_thenValueReturned() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
47+
Course course = new Course();
48+
String name = "Computer Science";
49+
List<String> codes = Arrays.asList("CS101","CS102");
50+
CourseService.setValues(course, name, codes);
51+
52+
// 1. Create and set a Student
53+
Student student = new Student();
54+
student.setName("John Doe");
55+
CourseService.setMappedValue(course, STUDENT_ID, student);
56+
// Use getMappedProperty to retrieve the value associated with the key '01'
57+
// from the 'enrolledStudent' map
58+
Student enrolledStudent = (Student) PropertyUtils.getMappedProperty(course, "enrolledStudent(" + STUDENT_ID + ")");
59+
60+
assertEquals("John Doe", enrolledStudent.getName());
61+
}
62+
1563
@Test
1664
public void givenCourse_whenSetValuesUsingPropertyUtil_thenReturnSetValues() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
1765
Course course = new Course();
@@ -48,7 +96,7 @@ public void givenCopyProperties_whenCopyCourseToCourseEntity_thenCopyPropertyWit
4896

4997
CourseService.copyProperties(course, courseEntity);
5098
Assert.assertNotNull(course.getName());
51-
Assert.assertNotNull(courseEntity.getName());
99+
Assert.assertNotNull(courseEntity.getName());
52100
Assert.assertEquals(course.getName(), courseEntity.getName());
53101
Assert.assertEquals(course.getCodes(), courseEntity.getCodes());
54102
Assert.assertNull(courseEntity.getStudent("ST-1"));

0 commit comments

Comments
 (0)