From ed7c1694a7354e678db77740afc936f3e502c449 Mon Sep 17 00:00:00 2001 From: Syed Ali Raza Date: Sun, 25 Jun 2017 19:12:51 +0500 Subject: [PATCH] Merging BAEL-968: Apache Commons BeanUtils (#2149) --- libraries/pom.xml | 8 +++- .../baeldung/commons/beanutils/Course.java | 35 +++++++++++++++++ .../commons/beanutils/CourseService.java | 34 +++++++++++++++++ .../baeldung/commons/beanutils/Student.java | 13 +++++++ .../commons/beanutils/CourseServiceTest.java | 38 +++++++++++++++++++ 5 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 libraries/src/main/java/com/baeldung/commons/beanutils/Course.java create mode 100644 libraries/src/main/java/com/baeldung/commons/beanutils/CourseService.java create mode 100644 libraries/src/main/java/com/baeldung/commons/beanutils/Student.java create mode 100644 libraries/src/test/java/com/baeldung/commons/beanutils/CourseServiceTest.java diff --git a/libraries/pom.xml b/libraries/pom.xml index 8285323f07..b7a33abbfe 100644 --- a/libraries/pom.xml +++ b/libraries/pom.xml @@ -106,6 +106,11 @@ cglib ${cglib.version} + + commons-beanutils + commons-beanutils + ${commons-beanutils.version} + org.apache.commons commons-lang3 @@ -383,6 +388,7 @@ 0.7.0 3.2.4 3.5 + 1.9.3 1.9.2 1.2 3.21.0-GA @@ -406,4 +412,4 @@ 0.10 - \ No newline at end of file + diff --git a/libraries/src/main/java/com/baeldung/commons/beanutils/Course.java b/libraries/src/main/java/com/baeldung/commons/beanutils/Course.java new file mode 100644 index 0000000000..03251586fd --- /dev/null +++ b/libraries/src/main/java/com/baeldung/commons/beanutils/Course.java @@ -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 codes; + private Map enrolledStudent = new HashMap(); + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public List getCodes() { + return codes; + } + + public void setCodes(List codes) { + this.codes = codes; + } + + public void setEnrolledStudent(String enrolledId, Student student) { + enrolledStudent.put(enrolledId, student); + } + + public Student getEnrolledStudent(String enrolledId) { + return enrolledStudent.get(enrolledId); + } +} diff --git a/libraries/src/main/java/com/baeldung/commons/beanutils/CourseService.java b/libraries/src/main/java/com/baeldung/commons/beanutils/CourseService.java new file mode 100644 index 0000000000..f519365dab --- /dev/null +++ b/libraries/src/main/java/com/baeldung/commons/beanutils/CourseService.java @@ -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 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); + } +} diff --git a/libraries/src/main/java/com/baeldung/commons/beanutils/Student.java b/libraries/src/main/java/com/baeldung/commons/beanutils/Student.java new file mode 100644 index 0000000000..3790bf6f9e --- /dev/null +++ b/libraries/src/main/java/com/baeldung/commons/beanutils/Student.java @@ -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; + } +} diff --git a/libraries/src/test/java/com/baeldung/commons/beanutils/CourseServiceTest.java b/libraries/src/test/java/com/baeldung/commons/beanutils/CourseServiceTest.java new file mode 100644 index 0000000000..d2ae6d7d55 --- /dev/null +++ b/libraries/src/test/java/com/baeldung/commons/beanutils/CourseServiceTest.java @@ -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 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); + } + +}