Test/Doc added

Test/Doc added
This commit is contained in:
Amit Dixit 2016-04-04 15:31:43 +05:30
parent eb72493f13
commit 59b6b817f4
10 changed files with 475 additions and 328 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 53 KiB

View File

@ -0,0 +1,94 @@
<?xml version="1.0" encoding="UTF-8"?>
<class-diagram version="1.1.9" icons="true" always-add-relationships="false" generalizations="true" realizations="true"
associations="true" dependencies="false" nesting-relationships="true" router="FAN">
<class id="1" language="java" name="com.iluwatar.datamapper.Student" project="java-design-patterns"
file="/java-design-patterns/java/com/iluwatar/datamapper/Student.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="966" y="283"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<interface id="2" language="java" name="com.iluwatar.datamapper.StudentDataMapper" project="java-design-patterns"
file="/java-design-patterns/java/com/iluwatar/datamapper/StudentDataMapper.java" binary="false"
corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="749" y="278"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</interface>
<class id="3" language="java" name="com.iluwatar.datamapper.StudentFirstDataMapper" project="java-design-patterns"
file="/java-design-patterns/java/com/iluwatar/datamapper/StudentFirstDataMapper.java" binary="false"
corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="525" y="81"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<class id="4" language="java" name="com.iluwatar.datamapper.StudentSecondDataMapper" project="java-design-patterns"
file="/java-design-patterns/java/com/iluwatar/datamapper/StudentSecondDataMapper.java" binary="false"
corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="537" y="500"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<class id="5" language="java" name="com.iluwatar.datamapper.App" project="java-design-patterns"
file="/java-design-patterns/java/com/iluwatar/datamapper/App.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="485" y="285"/>
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</display>
</class>
<association id="6">
<end type="SOURCE" refId="4" navigable="false">
<attribute id="7" name="students"/>
<multiplicity id="8" minimum="0" maximum="2147483647"/>
</end>
<end type="TARGET" refId="1" navigable="true"/>
<display labels="true" multiplicity="true"/>
</association>
<association id="9">
<end type="SOURCE" refId="3" navigable="false">
<attribute id="10" name="students"/>
<multiplicity id="11" minimum="0" maximum="2147483647"/>
</end>
<end type="TARGET" refId="1" navigable="true"/>
<display labels="true" multiplicity="true"/>
</association>
<dependency id="12">
<end type="SOURCE" refId="5"/>
<end type="TARGET" refId="1"/>
</dependency>
<realization id="13">
<end type="SOURCE" refId="3"/>
<end type="TARGET" refId="2"/>
</realization>
<realization id="14">
<end type="SOURCE" refId="4"/>
<end type="TARGET" refId="2"/>
</realization>
<dependency id="15">
<end type="SOURCE" refId="5"/>
<end type="TARGET" refId="4"/>
</dependency>
<dependency id="16">
<end type="SOURCE" refId="5"/>
<end type="TARGET" refId="2"/>
</dependency>
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
sort-features="false" accessors="true" visibility="true">
<attributes public="true" package="true" protected="true" private="true" static="true"/>
<operations public="true" package="true" protected="true" private="true" static="true"/>
</classifier-display>
<association-display labels="true" multiplicity="true"/>
</class-diagram>

View File

@ -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

View File

@ -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');

View File

@ -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 + "]";
}
}

View File

@ -22,11 +22,11 @@ import java.util.Optional;
public interface StudentDataMapper {
public Optional<Student> find(final int studentId);
Optional<Student> 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;
}

View File

@ -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<Student> students = new ArrayList<>();
@Override
public final Optional<Student> 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<Student> 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<Student> students = new ArrayList<>();
@Override
public Optional<Student> 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<Student> getStudents() {
return this.students;
}
}

View File

@ -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<Student> students = new ArrayList<>();
@Override
public final Optional<Student> 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<Student> 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<Student> students = new ArrayList<>();
@Override
public Optional<Student> 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<Student> getStudents() {
return this.students;
}
}

View File

@ -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.
* <p>
* 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<Student> 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() {}
}

View File

@ -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.
* <p>
*/
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());
}
}