Merge pull request #417 from inbravo/master

Data Mapper #34
This commit is contained in:
Ilkka Seppälä 2016-04-24 00:02:48 +03:00
commit 5b72510c1c
15 changed files with 740 additions and 1 deletions

7
.gitignore vendored
View File

@ -11,4 +11,9 @@ target
.idea
*.iml
*.swp
datanucleus.log
datanucleus.log
/bin/
/bin/
/bin/
data-mapper/src/main/resources/log4j.xml

Binary file not shown.

After

Width:  |  Height:  |  Size: 39 KiB

View File

@ -0,0 +1,72 @@
<?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="359" width="148" x="868" y="175"/>
<display autosize="false" 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="733" y="241"/>
<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.StudentDataMapperImpl" project="java-design-patterns"
file="/java-design-patterns/java/com/iluwatar/datamapper/StudentDataMapperImpl.java" binary="false"
corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="734" y="447"/>
<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.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="501" y="341"/>
<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>
<dependency id="5">
<end type="SOURCE" refId="4"/>
<end type="TARGET" refId="3"/>
</dependency>
<dependency id="6">
<end type="SOURCE" refId="4"/>
<end type="TARGET" refId="1"/>
</dependency>
<association id="7">
<end type="SOURCE" refId="3" navigable="false">
<attribute id="8" name="students"/>
<multiplicity id="9" minimum="0" maximum="2147483647"/>
</end>
<end type="TARGET" refId="1" navigable="true"/>
<display labels="true" multiplicity="true"/>
</association>
<dependency id="10">
<end type="SOURCE" refId="4"/>
<end type="TARGET" refId="2"/>
</dependency>
<realization id="11">
<end type="SOURCE" refId="3"/>
<end type="TARGET" refId="2"/>
</realization>
<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>

25
data-mapper/index.md Normal file
View File

@ -0,0 +1,25 @@
---
layout: pattern
title: Data Mapper
folder: data-mapper
permalink: /patterns/dm/
categories: Persistence Tier
tags:
- Java
- Difficulty-Beginner
---
## Intent
A layer of mappers that moves data between objects and a database while keeping them independent of each other and the mapper itself
![alt text](./etc/data-mapper.png "Data Mapper")
## Applicability
Use the Data Mapper in any of the following situations
* when you want to decouple data objects from DB access layer
* when you want to write multiple data retrieval/persistence implementations
## Credits
* [Data Mapper](http://richard.jp.leguen.ca/tutoring/soen343-f2010/tutorials/implementing-data-mapper/)

45
data-mapper/pom.xml Normal file
View File

@ -0,0 +1,45 @@
<?xml version="1.0"?>
<!--
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.
-->
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.iluwatar</groupId>
<artifactId>java-design-patterns</artifactId>
<version>1.12.0-SNAPSHOT</version>
</parent>
<artifactId>data-mapper</artifactId>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,77 @@
/**
* 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.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
* 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: Create, Read, Update, and Delete.
*
*/
public final class App {
private static Logger log = Logger.getLogger(App.class);
/**
* Program entry point.
*
* @param args command line args.
*/
public static void main(final String... args) {
/* Create new data mapper for type 'first' */
final StudentDataMapper mapper = new StudentDataMapperImpl();
/* Create new student */
Student student = new Student(1, "Adam", 'A');
/* Add student in respectibe store */
mapper.insert(student);
log.debug("App.main(), student : " + student + ", is inserted");
/* Find this student */
final Optional<Student> studentToBeFound = mapper.find(student.getStudentId());
log.debug("App.main(), student : " + studentToBeFound + ", is searched");
/* Update existing student object */
student = new Student(student.getStudentId(), "AdamUpdated", 'A');
/* Update student in respectibe db */
mapper.update(student);
log.debug("App.main(), student : " + student + ", is updated");
log.debug("App.main(), student : " + student + ", is going to be deleted");
/* Delete student in db */
mapper.delete(student);
}
private App() {}
}

View File

@ -0,0 +1,42 @@
/**
* 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;
/**
* Using Runtime Exception for avoiding dependancy on implementation exceptions. This helps in
* decoupling.
*
* @author amit.dixit
*
*/
public final class DataMapperException extends RuntimeException {
private static final long serialVersionUID = 1L;
/**
* Constructs a new runtime exception with the specified detail message. The cause is not
* initialized, and may subsequently be initialized by a call to {@link #initCause}.
*
* @param message the detail message. The detail message is saved for later retrieval by the
* {@link #getMessage()} method.
*/
public DataMapperException(final String message) {
super(message);
}
}

View File

@ -0,0 +1,139 @@
/**
* 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.io.Serializable;
public final class Student implements Serializable {
private static final long serialVersionUID = 1L;
private int studentId;
private String name;
private char grade;
/**
* 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();
this.studentId = studentId;
this.name = name;
this.grade = grade;
}
/**
*
* @return the student id
*/
public int getStudentId() {
return studentId;
}
/**
*
* @param studentId as unique student id
*/
public void setStudentId(final int studentId) {
this.studentId = studentId;
}
/**
*
* @return name of student
*/
public String getName() {
return name;
}
/**
*
* @param name as 'name' of student
*/
public void setName(final String name) {
this.name = name;
}
/**
*
* @return grade of student
*/
public char getGrade() {
return grade;
}
/**
*
* @param grade as 'grade of student'
*/
public void setGrade(final char grade) {
this.grade = grade;
}
/**
*
*/
@Override
public boolean equals(final Object inputObject) {
boolean isEqual = false;
/* Check if both objects are same */
if (this == inputObject) {
isEqual = true;
} else if (inputObject != null && getClass() == inputObject.getClass()) {
final Student inputStudent = (Student) inputObject;
/* If student id matched */
if (this.getStudentId() == inputStudent.getStudentId()) {
isEqual = true;
}
}
return isEqual;
}
/**
*
*/
@Override
public int hashCode() {
/* Student id is assumed to be unique */
return this.getStudentId();
}
/**
*
*/
@Override
public String toString() {
return "Student [studentId=" + studentId + ", name=" + name + ", grade=" + grade + "]";
}
}

View File

@ -0,0 +1,32 @@
/**
* 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.Optional;
public interface StudentDataMapper {
Optional<Student> find(int studentId);
void insert(Student student) throws DataMapperException;
void update(Student student) throws DataMapperException;
void delete(Student student) throws DataMapperException;
}

View File

@ -0,0 +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 StudentDataMapperImpl 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 after wrapping in a runtime exception */
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 after wrapping in a runtime exception */
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 after wrapping in a runtime exception */
throw new DataMapperException("Student [" + studentToBeDeleted.getName() + "] is not found");
}
}
public List<Student> getStudents() {
return this.students;
}
}

View File

@ -0,0 +1,41 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!--
The MIT License
Copyright (c) 2014 Ilkka Seppälä
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.
-->
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true"
xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
</layout>
</appender>
<root>
<level value="INFO" />
<appender-ref ref="console" />
</root>
</log4j:configuration>

View File

@ -0,0 +1,34 @@
/**
* 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 com.iluwatar.datamapper.App;
import org.junit.Test;
/**
* Tests that Data-Mapper example runs without errors.
*/
public final class AppTest {
@Test
public void test() {
final String[] args = {};
App.main(args);
}
}

View File

@ -0,0 +1,73 @@
/**
* 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.StudentDataMapperImpl;
/**
* 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 StudentDataMapperImpl();
/* 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());
}
}

View File

@ -0,0 +1,51 @@
/**
* 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 org.junit.Test;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
public final class StudentTest {
@Test
/**
* This API tests the equality behaviour of Student object
* Object Equality should work as per logic defined in equals method
*
* @throws Exception if any execution error during test
*/
public void testEquality() throws Exception {
/* Create some students */
final Student firstStudent = new Student(1, "Adam", 'A');
final Student secondStudent = new Student(2, "Donald", 'B');
final Student secondSameStudent = new Student(2, "Donald", 'B');
final Student firstSameStudent = firstStudent;
/* Check equals functionality: should return 'true' */
assertTrue(firstStudent.equals(firstSameStudent));
/* Check equals functionality: should return 'false' */
assertFalse(firstStudent.equals(secondStudent));
/* Check equals functionality: should return 'true' */
assertTrue(secondStudent.equals(secondSameStudent));
}
}

View File

@ -57,6 +57,7 @@
<module>bridge</module>
<module>composite</module>
<module>dao</module>
<module>data-mapper</module>
<module>decorator</module>
<module>facade</module>
<module>flyweight</module>