diff --git a/data-mapper/etc/data-mapper.png b/data-mapper/etc/data-mapper.png new file mode 100644 index 000000000..8fc90f591 Binary files /dev/null and b/data-mapper/etc/data-mapper.png differ diff --git a/data-mapper/etc/data-mapper.ucls b/data-mapper/etc/data-mapper.ucls new file mode 100644 index 000000000..ff10fc32f --- /dev/null +++ b/data-mapper/etc/data-mapper.ucls @@ -0,0 +1,94 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/data-mapper/index.md b/data-mapper/index.md index 4e640d5d6..684595c53 100644 --- a/data-mapper/index.md +++ b/data-mapper/index.md @@ -13,7 +13,7 @@ tags: Object provides an abstract interface to some type of database or other persistence mechanism. -![alt text](./etc/dm.png "Data Mapper") +![alt text](./etc/data-mapper.png "Data Mapper") ## Applicability Use the Data Mapper in any of the following situations diff --git a/data-mapper/src/main/java/com/iluwatar/datamapper/App.java b/data-mapper/src/main/java/com/iluwatar/datamapper/App.java index d8a968a17..63e3af41d 100644 --- a/data-mapper/src/main/java/com/iluwatar/datamapper/App.java +++ b/data-mapper/src/main/java/com/iluwatar/datamapper/App.java @@ -23,7 +23,6 @@ import java.util.Optional; import org.apache.log4j.Logger; /** - * * The Data Mapper (DM) is a layer of software that separates the in-memory objects from the * database. Its responsibility is to transfer data between the two and also to isolate them from * each other. With Data Mapper the in-memory objects needn't know even that there's a database @@ -39,43 +38,18 @@ public final class App { private static Logger log = Logger.getLogger(App.class); - private static final String DB_TYPE_ORACLE = "Oracle"; - private static final String DB_TYPE_MYSQL = "MySQL"; - /** * Program entry point. * * @param args command line args. */ - public static final void main(final String... args) { + public static void main(final String... args) { - if (log.isInfoEnabled() & args.length > 0) { - log.debug("App.main(), db type: " + args[0]); - } - StudentDataMapper mapper = null; - - /* Check the desired db type from runtime arguments */ - if (args.length == 0) { - - /* Create default data mapper for mysql */ - mapper = new StudentMySQLDataMapper(); - - } else if (args.length > 0 && DB_TYPE_ORACLE.equalsIgnoreCase(args[0])) { - - /* Create new data mapper for mysql */ - mapper = new StudentMySQLDataMapper(); - - } else if (args.length > 0 && DB_TYPE_MYSQL.equalsIgnoreCase(args[0])) { - - /* Create new data mapper for oracle */ - mapper = new StudentMySQLDataMapper(); - } else { - - /* Don't couple any Data Mapper to java.sql.SQLException */ - throw new DataMapperException("Following data mapping type(" + args[0] + ") is not supported"); - } + /* Create any type of mapper at implementation which is desired */ + /* final StudentDataMapper mapper = new StudentFirstDataMapper(); */ + final StudentDataMapper mapper = new StudentSecondDataMapper(); /* Create new student */ Student student = new Student(1, "Adam", 'A'); diff --git a/data-mapper/src/main/java/com/iluwatar/datamapper/Student.java b/data-mapper/src/main/java/com/iluwatar/datamapper/Student.java index 2f0c6d0a6..0164533c8 100644 --- a/data-mapper/src/main/java/com/iluwatar/datamapper/Student.java +++ b/data-mapper/src/main/java/com/iluwatar/datamapper/Student.java @@ -29,10 +29,14 @@ public final class Student implements Serializable { private String name; private char grade; - public Student() { - - } + /** + * Use this constructor to create a Student with all details + * + * @param studentId as unique student id + * @param name as student name + * @param grade as respective grade of student + */ public Student(final int studentId, final String name, final char grade) { super(); @@ -41,30 +45,57 @@ public final class Student implements Serializable { this.grade = grade; } - public final int getStudentId() { + /** + * + * @return the student id + */ + public int getStudentId() { return studentId; } - public final void setStudentId(final int studentId) { + /** + * + * @param studentId as unique student id + */ + public void setStudentId(final int studentId) { this.studentId = studentId; } - public final String getName() { + /** + * + * @return name of student + */ + public String getName() { return name; } - public final void setName(final String name) { + /** + * + * @param name as 'name' of student + */ + public void setName(final String name) { this.name = name; } - public final char getGrade() { + /** + * + * @return grade of student + */ + public char getGrade() { return grade; } - public final void setGrade(final char grade) { + /** + * + * @param grade as 'grade of student' + */ + public void setGrade(final char grade) { this.grade = grade; } + /** + * + */ @Override public boolean equals(final Object inputObject) { @@ -74,21 +105,23 @@ public final class Student implements Serializable { if (this == inputObject) { isEqual = true; - } - /* Check if objects belong to same class */ - else if (inputObject != null && getClass() == inputObject.getClass()) { + } else if (inputObject != null && getClass() == inputObject.getClass()) { - final Student student = (Student) inputObject; + final Student inputStudent = (Student) inputObject; /* If student id matched */ - if (this.getStudentId() == student.getStudentId()) { + if (this.getStudentId() == inputStudent.getStudentId()) { isEqual = true; } } + return isEqual; } + /** + * + */ @Override public int hashCode() { @@ -96,8 +129,11 @@ public final class Student implements Serializable { return this.getStudentId(); } + /** + * + */ @Override - public final String toString() { + public String toString() { return "Student [studentId=" + studentId + ", name=" + name + ", grade=" + grade + "]"; } } diff --git a/data-mapper/src/main/java/com/iluwatar/datamapper/StudentDataMapper.java b/data-mapper/src/main/java/com/iluwatar/datamapper/StudentDataMapper.java index 1fa33e067..40f0c5c72 100644 --- a/data-mapper/src/main/java/com/iluwatar/datamapper/StudentDataMapper.java +++ b/data-mapper/src/main/java/com/iluwatar/datamapper/StudentDataMapper.java @@ -22,11 +22,11 @@ import java.util.Optional; public interface StudentDataMapper { - public Optional find(final int studentId); + Optional find(int studentId); - public void insert(final Student student) throws DataMapperException; + void insert(Student student) throws DataMapperException; - public void update(final Student student) throws DataMapperException; + void update(Student student) throws DataMapperException; - public void delete(final Student student) throws DataMapperException; + void delete(Student student) throws DataMapperException; } diff --git a/data-mapper/src/main/java/com/iluwatar/datamapper/StudentMySQLDataMapper.java b/data-mapper/src/main/java/com/iluwatar/datamapper/StudentFirstDataMapper.java similarity index 85% rename from data-mapper/src/main/java/com/iluwatar/datamapper/StudentMySQLDataMapper.java rename to data-mapper/src/main/java/com/iluwatar/datamapper/StudentFirstDataMapper.java index ec7507eed..97f6d395d 100644 --- a/data-mapper/src/main/java/com/iluwatar/datamapper/StudentMySQLDataMapper.java +++ b/data-mapper/src/main/java/com/iluwatar/datamapper/StudentFirstDataMapper.java @@ -1,102 +1,102 @@ -/** - * The MIT License Copyright (c) 2016 Amit Dixit - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and - * associated documentation files (the "Software"), to deal in the Software without restriction, - * including without limitation the rights to use, copy, modify, merge, publish, distribute, - * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all copies or - * substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT - * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, - * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ -package com.iluwatar.datamapper; - -import java.util.ArrayList; -import java.util.List; -import java.util.Optional; - -public final class StudentMySQLDataMapper implements StudentDataMapper { - - /* Note: Normally this would be in the form of an actual database */ - private List students = new ArrayList<>(); - - @Override - public final Optional find(final int studentId) { - - /* Compare with existing students */ - for (final Student student : this.getStudents()) { - - /* Check if student is found */ - if (student.getStudentId() == studentId) { - - return Optional.of(student); - } - } - - /* Return empty value */ - return Optional.empty(); - } - - @Override - public final void update(final Student studentToBeUpdated) throws DataMapperException { - - - /* Check with existing students */ - if (this.getStudents().contains(studentToBeUpdated)) { - - /* Get the index of student in list */ - final int index = this.getStudents().indexOf(studentToBeUpdated); - - /* Update the student in list */ - this.getStudents().set(index, studentToBeUpdated); - - } else { - - /* Throw user error */ - throw new DataMapperException("Student [" + studentToBeUpdated.getName() + "] is not found"); - } - } - - @Override - public final void insert(final Student studentToBeInserted) throws DataMapperException { - - /* Check with existing students */ - if (!this.getStudents().contains(studentToBeInserted)) { - - /* Add student in list */ - this.getStudents().add(studentToBeInserted); - - } else { - - /* Throw user error */ - throw new DataMapperException("Student already [" + studentToBeInserted.getName() + "] exists"); - } - } - - @Override - public final void delete(final Student studentToBeDeleted) throws DataMapperException { - - /* Check with existing students */ - if (this.getStudents().contains(studentToBeDeleted)) { - - /* Delete the student from list */ - this.getStudents().remove(studentToBeDeleted); - - } else { - - /* Throw user error */ - throw new DataMapperException("Student [" + studentToBeDeleted.getName() + "] is not found"); - } - } - - public List getStudents() { - return this.students; - } -} +/** + * The MIT License Copyright (c) 2016 Amit Dixit + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and + * associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT + * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.iluwatar.datamapper; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +public final class StudentFirstDataMapper implements StudentDataMapper { + + /* Note: Normally this would be in the form of an actual database */ + private List students = new ArrayList<>(); + + @Override + public Optional find(int studentId) { + + /* Compare with existing students */ + for (final Student student : this.getStudents()) { + + /* Check if student is found */ + if (student.getStudentId() == studentId) { + + return Optional.of(student); + } + } + + /* Return empty value */ + return Optional.empty(); + } + + @Override + public void update(Student studentToBeUpdated) throws DataMapperException { + + + /* Check with existing students */ + if (this.getStudents().contains(studentToBeUpdated)) { + + /* Get the index of student in list */ + final int index = this.getStudents().indexOf(studentToBeUpdated); + + /* Update the student in list */ + this.getStudents().set(index, studentToBeUpdated); + + } else { + + /* Throw user error */ + throw new DataMapperException("Student [" + studentToBeUpdated.getName() + "] is not found"); + } + } + + @Override + public void insert(Student studentToBeInserted) throws DataMapperException { + + /* Check with existing students */ + if (!this.getStudents().contains(studentToBeInserted)) { + + /* Add student in list */ + this.getStudents().add(studentToBeInserted); + + } else { + + /* Throw user error */ + throw new DataMapperException("Student already [" + studentToBeInserted.getName() + "] exists"); + } + } + + @Override + public void delete(Student studentToBeDeleted) throws DataMapperException { + + /* Check with existing students */ + if (this.getStudents().contains(studentToBeDeleted)) { + + /* Delete the student from list */ + this.getStudents().remove(studentToBeDeleted); + + } else { + + /* Throw user error */ + throw new DataMapperException("Student [" + studentToBeDeleted.getName() + "] is not found"); + } + } + + public List getStudents() { + return this.students; + } +} diff --git a/data-mapper/src/main/java/com/iluwatar/datamapper/StudentOracleDataMapper.java b/data-mapper/src/main/java/com/iluwatar/datamapper/StudentSecondDataMapper.java similarity index 85% rename from data-mapper/src/main/java/com/iluwatar/datamapper/StudentOracleDataMapper.java rename to data-mapper/src/main/java/com/iluwatar/datamapper/StudentSecondDataMapper.java index ca66f11c9..62d39f90d 100644 --- a/data-mapper/src/main/java/com/iluwatar/datamapper/StudentOracleDataMapper.java +++ b/data-mapper/src/main/java/com/iluwatar/datamapper/StudentSecondDataMapper.java @@ -1,102 +1,101 @@ -/** - * The MIT License Copyright (c) 2016 Amit Dixit - * - * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and - * associated documentation files (the "Software"), to deal in the Software without restriction, - * including without limitation the rights to use, copy, modify, merge, publish, distribute, - * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all copies or - * substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT - * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND - * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, - * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. - */ -package com.iluwatar.datamapper; - -import java.util.ArrayList; -import java.util.List; -import java.util.Optional; - -public final class StudentOracleDataMapper implements StudentDataMapper { - - /* Note: Normally this would be in the form of an actual database */ - private List students = new ArrayList<>(); - - @Override - public final Optional find(final int studentId) { - - /* Compare with existing students */ - for (final Student student : this.getStudents()) { - - /* Check if student is found */ - if (student.getStudentId() == studentId) { - - return Optional.of(student); - } - } - - /* Return empty value */ - return Optional.empty(); - } - - @Override - public final void update(final Student studentToBeUpdated) throws DataMapperException { - - - /* Check with existing students */ - if (this.getStudents().contains(studentToBeUpdated)) { - - /* Get the index of student in list */ - final int index = this.getStudents().indexOf(studentToBeUpdated); - - /* Update the student in list */ - this.getStudents().set(index, studentToBeUpdated); - - } else { - - /* Throw user error */ - throw new DataMapperException("Student [" + studentToBeUpdated.getName() + "] is not found"); - } - } - - @Override - public final void insert(final Student studentToBeInserted) throws DataMapperException { - - /* Check with existing students */ - if (!this.getStudents().contains(studentToBeInserted)) { - - /* Add student in list */ - this.getStudents().add(studentToBeInserted); - - } else { - - /* Throw user error */ - throw new DataMapperException("Student already [" + studentToBeInserted.getName() + "] exists"); - } - } - - @Override - public final void delete(final Student studentToBeDeleted) throws DataMapperException { - - /* Check with existing students */ - if (this.getStudents().contains(studentToBeDeleted)) { - - /* Delete the student from list */ - this.getStudents().remove(studentToBeDeleted); - - } else { - - /* Throw user error */ - throw new DataMapperException("Student [" + studentToBeDeleted.getName() + "] is not found"); - } - } - - public List getStudents() { - return this.students; - } -} +/** + * The MIT License Copyright (c) 2016 Amit Dixit + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and + * associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT + * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.iluwatar.datamapper; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +public final class StudentSecondDataMapper implements StudentDataMapper { + + /* Note: Normally this would be in the form of an actual database */ + private List students = new ArrayList<>(); + + @Override + public Optional find(int studentId) { + + /* Compare with existing students */ + for (final Student student : this.getStudents()) { + + /* Check if student is found */ + if (student.getStudentId() == studentId) { + + return Optional.of(student); + } + } + + /* Return empty value */ + return Optional.empty(); + } + + @Override + public void update(Student studentToBeUpdated) throws DataMapperException { + + /* Check with existing students */ + if (this.getStudents().contains(studentToBeUpdated)) { + + /* Get the index of student in list */ + final int index = this.getStudents().indexOf(studentToBeUpdated); + + /* Update the student in list */ + this.getStudents().set(index, studentToBeUpdated); + + } else { + + /* Throw user error */ + throw new DataMapperException("Student [" + studentToBeUpdated.getName() + "] is not found"); + } + } + + @Override + public void insert(Student studentToBeInserted) throws DataMapperException { + + /* Check with existing students */ + if (!this.getStudents().contains(studentToBeInserted)) { + + /* Add student in list */ + this.getStudents().add(studentToBeInserted); + + } else { + + /* Throw user error */ + throw new DataMapperException("Student already [" + studentToBeInserted.getName() + "] exists"); + } + } + + @Override + public void delete(Student studentToBeDeleted) throws DataMapperException { + + /* Check with existing students */ + if (this.getStudents().contains(studentToBeDeleted)) { + + /* Delete the student from list */ + this.getStudents().remove(studentToBeDeleted); + + } else { + + /* Throw user error */ + throw new DataMapperException("Student [" + studentToBeDeleted.getName() + "] is not found"); + } + } + + public List getStudents() { + return this.students; + } +} diff --git a/data-mapper/src/test/java/com/iluwatar/datamapper/AppTest.java b/data-mapper/src/test/java/com/iluwatar/datamapper/AppTest.java index 6e415bccb..d3ad2dcb3 100644 --- a/data-mapper/src/test/java/com/iluwatar/datamapper/AppTest.java +++ b/data-mapper/src/test/java/com/iluwatar/datamapper/AppTest.java @@ -1,5 +1,6 @@ /** - * The MIT License Copyright (c) 2014 Ilkka Seppälä + * The MIT License Copyright (c) 2016 Amit Dixit + * * * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and * associated documentation files (the "Software"), to deal in the Software without restriction, @@ -18,82 +19,17 @@ */ package com.iluwatar.datamapper; -import java.util.Optional; -import java.util.UUID; - -import org.apache.log4j.Logger; +import com.iluwatar.datamapper.App; +import org.junit.Test; /** - * The Data Mapper (DM) is a layer of software that separates the in-memory objects from the - * database. Its responsibility is to transfer data between the two and also to isolate them from - * each other. With Data Mapper the in-memory objects needn't know even that there's a database - * present; they need no SQL interface code, and certainly no knowledge of the database schema. (The - * database schema is always ignorant of the objects that use it.) Since it's a form of Mapper , - * Data Mapper itself is even unknown to the domain layer. - *

- * The below example demonstrates basic CRUD operations: select, add, update, and delete. - * + * Tests that Data-Mapper example runs without errors. */ -public final class App { +public final class AppTest { - private static Logger log = Logger.getLogger(App.class); - - - private static final String DB_TYPE_ORACLE = "Oracle"; - private static final String DB_TYPE_MYSQL = "MySQL"; - - - /** - * Program entry point. - * - * @param args command line args. - */ - public static final void main(final String... args) { - - if (log.isInfoEnabled() & args.length > 0) { - log.debug("App.main(), db type: " + args[0]); - } - - StudentDataMapper mapper = null; - - /* Check the desired db type from runtime arguments */ - if (DB_TYPE_ORACLE.equalsIgnoreCase(args[0])) { - - /* Create new data mapper for mysql */ - mapper = new StudentMySQLDataMapper(); - - } else if (DB_TYPE_MYSQL.equalsIgnoreCase(args[0])) { - - /* Create new data mapper for oracle */ - mapper = new StudentMySQLDataMapper(); - } else { - - /* Don't couple any Data Mapper to java.sql.SQLException */ - throw new DataMapperException("Following data source(" + args[0] + ") is not supported"); - } - - /* Create new student */ - Student student = new Student(UUID.randomUUID(), 1, "Adam", 'A'); - - /* Add student in respectibe db */ - mapper.insert(student); - - /* Find this student */ - final Optional studentToBeFound = mapper.find(student.getGuId()); - - if (log.isDebugEnabled()) { - log.debug("App.main(), db find returned : " + studentToBeFound); - } - - /* Update existing student object */ - student = new Student(student.getGuId(), 1, "AdamUpdated", 'A'); - - /* Update student in respectibe db */ - mapper.update(student); - - /* Delete student in db */ - mapper.delete(student); + @Test + public void test() { + final String[] args = {}; + App.main(args); } - - private App() {} } diff --git a/data-mapper/src/test/java/com/iluwatar/datamapper/DataMapperTest.java b/data-mapper/src/test/java/com/iluwatar/datamapper/DataMapperTest.java new file mode 100644 index 000000000..f77097df8 --- /dev/null +++ b/data-mapper/src/test/java/com/iluwatar/datamapper/DataMapperTest.java @@ -0,0 +1,108 @@ +/** + * The MIT License Copyright (c) 2016 Amit Dixit + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and + * associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT + * NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ +package com.iluwatar.datamapper; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; + +import com.iluwatar.datamapper.Student; +import com.iluwatar.datamapper.StudentDataMapper; +import com.iluwatar.datamapper.StudentFirstDataMapper; +import com.iluwatar.datamapper.StudentSecondDataMapper; + +/** + * The Data Mapper (DM) is a layer of software that separates the in-memory objects from the + * database. Its responsibility is to transfer data between the two and also to isolate them from + * each other. With Data Mapper the in-memory objects needn't know even that there's a database + * present; they need no SQL interface code, and certainly no knowledge of the database schema. (The + * database schema is always ignorant of the objects that use it.) Since it's a form of Mapper , + * Data Mapper itself is even unknown to the domain layer. + *

+ */ +public class DataMapperTest { + + /** + * This test verify that first data mapper is able to perform all CRUD operations on Student + */ + @Test + public void testFirstDataMapper() { + + /* Create new data mapper of first type */ + final StudentDataMapper mapper = new StudentFirstDataMapper(); + + /* Create new student */ + Student student = new Student(1, "Adam", 'A'); + + /* Add student in respectibe db */ + mapper.insert(student); + + /* Check if student is added in db */ + assertEquals(student.getStudentId(), mapper.find(student.getStudentId()).get().getStudentId()); + + /* Update existing student object */ + student = new Student(student.getStudentId(), "AdamUpdated", 'A'); + + /* Update student in respectibe db */ + mapper.update(student); + + /* Check if student is updated in db */ + assertEquals(mapper.find(student.getStudentId()).get().getName(), "AdamUpdated"); + + /* Delete student in db */ + mapper.delete(student); + + /* Result should be false */ + assertEquals(false, mapper.find(student.getStudentId()).isPresent()); + } + + /** + * This test verify that second data mapper is able to perform all CRUD operations on Student + */ + @Test + public void testSecondDataMapper() { + + /* Create new data mapper of second type */ + final StudentDataMapper mapper = new StudentSecondDataMapper(); + + /* Create new student */ + Student student = new Student(1, "Adam", 'A'); + + /* Add student in respectibe db */ + mapper.insert(student); + + /* Check if student is added in db */ + assertEquals(student.getStudentId(), mapper.find(student.getStudentId()).get().getStudentId()); + + /* Update existing student object */ + student = new Student(student.getStudentId(), "AdamUpdated", 'A'); + + /* Update student in respectibe db */ + mapper.update(student); + + /* Check if student is updated in db */ + assertEquals(mapper.find(student.getStudentId()).get().getName(), "AdamUpdated"); + + /* Delete student in db */ + mapper.delete(student); + + /* Result should be false */ + assertEquals(false, mapper.find(student.getStudentId()).isPresent()); + } +}