First review changes++
First review changes++
This commit is contained in:
parent
06e0a15400
commit
8529d6e34b
Binary file not shown.
Before Width: | Height: | Size: 53 KiB After Width: | Height: | Size: 39 KiB |
@ -23,71 +23,61 @@
|
||||
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.11.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>data-mapper</artifactId>
|
||||
<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.11.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>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>log4j</groupId>
|
||||
<artifactId>log4j</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
|
||||
<build>
|
||||
<!-- log4j.xml file will be copied both in ${project.build.outputDirectory} and ${project.build.directory}. Thanks to Sean Patrick Floyd (http://stackoverflow.com/questions/5637532/maven-how-to-place-resource-file-together-with-jar) -->
|
||||
<resources>
|
||||
<resource> <!-- regular processing for every resource file -->
|
||||
<directory>src/main/resources</directory>
|
||||
</resource>
|
||||
<resource> <!-- processing with a different output directory for log4j.xml -->
|
||||
<directory>src/main/resources</directory>
|
||||
<includes>
|
||||
<include>log4j.xml</include>
|
||||
</includes>
|
||||
<targetPath>..</targetPath> <!-- relative to target/classes i.e. ${project.build.directory} -->
|
||||
</resource>
|
||||
</resources>
|
||||
|
||||
<!--
|
||||
log4j.xml file will be copied both in ${project.build.outputDirectory}
|
||||
and ${project.build.directory}. Thanks to Sean Patrick Floyd
|
||||
(http://stackoverflow.com/questions/5637532/maven-how-to-place-resource-file-together-with-jar)
|
||||
-->
|
||||
|
||||
<resources>
|
||||
<resource> <!-- regular processing for every resource file -->
|
||||
<directory>src/main/resources</directory>
|
||||
</resource>
|
||||
<resource> <!-- processing with a different output directory for log4j.xml -->
|
||||
<directory>src/main/resources</directory>
|
||||
<includes>
|
||||
<include>log4j.xml</include>
|
||||
</includes>
|
||||
<targetPath>..</targetPath> <!-- relative to target/classes i.e. ${project.build.directory} -->
|
||||
</resource>
|
||||
</resources>
|
||||
<plugins>
|
||||
|
||||
<plugins>
|
||||
<!-- This will exclude log4j.xml file from generated JAR -->
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>2.6</version>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>log4j.xml</exclude>
|
||||
</excludes>
|
||||
<archive>
|
||||
<manifest>
|
||||
<addClasspath>true</addClasspath>
|
||||
</manifest>
|
||||
</archive>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<!--
|
||||
This will exclude log4j.xml file from generated JAR
|
||||
-->
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>2.6</version>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>log4j.xml</exclude>
|
||||
</excludes>
|
||||
<archive>
|
||||
<manifest>
|
||||
<addClasspath>true</addClasspath>
|
||||
</manifest>
|
||||
</archive>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
</build>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
|
@ -1,5 +1,5 @@
|
||||
/**
|
||||
* 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,
|
||||
@ -37,9 +37,6 @@ public final class App {
|
||||
|
||||
private static Logger log = Logger.getLogger(App.class);
|
||||
|
||||
private static final String DB_TYPE_FIRST = "first";
|
||||
private static final String DB_TYPE_SECOND = "second";
|
||||
|
||||
/**
|
||||
* Program entry point.
|
||||
*
|
||||
@ -47,49 +44,21 @@ public final class App {
|
||||
*/
|
||||
public static void main(final String... args) {
|
||||
|
||||
if (log.isInfoEnabled() & args.length > 0) {
|
||||
log.debug("App.main(), 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 StudentFirstDataMapper();
|
||||
|
||||
} else if (args.length > 0 && DB_TYPE_FIRST.equalsIgnoreCase(args[0])) {
|
||||
|
||||
/* Create new data mapper for type 'first' */
|
||||
mapper = new StudentFirstDataMapper();
|
||||
|
||||
} else if (args.length > 0 && DB_TYPE_SECOND.equalsIgnoreCase(args[0])) {
|
||||
|
||||
/* Create new data mapper for type 'second' */
|
||||
mapper = new StudentSecondDataMapper();
|
||||
} else {
|
||||
|
||||
/* Don't couple any Data Mapper to java.sql.SQLException */
|
||||
throw new DataMapperException("Following data mapping type(" + args[0] + ") is not supported");
|
||||
}
|
||||
/* 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 db */
|
||||
/* Add student in respectibe store */
|
||||
mapper.insert(student);
|
||||
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("App.main(), student : " + student + ", is inserted");
|
||||
}
|
||||
log.debug("App.main(), student : " + student + ", is inserted");
|
||||
|
||||
/* Find this student */
|
||||
final Optional<Student> studentToBeFound = mapper.find(student.getStudentId());
|
||||
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("App.main(), student : " + studentToBeFound + ", is searched");
|
||||
}
|
||||
log.debug("App.main(), student : " + studentToBeFound + ", is searched");
|
||||
|
||||
/* Update existing student object */
|
||||
student = new Student(student.getStudentId(), "AdamUpdated", 'A');
|
||||
@ -97,15 +66,10 @@ public final class App {
|
||||
/* Update student in respectibe db */
|
||||
mapper.update(student);
|
||||
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("App.main(), student : " + student + ", is updated");
|
||||
}
|
||||
log.debug("App.main(), student : " + student + ", is updated");
|
||||
log.debug("App.main(), student : " + student + ", is going to be deleted");
|
||||
|
||||
/* Delete student in db */
|
||||
|
||||
if (log.isDebugEnabled()) {
|
||||
log.debug("App.main(), student : " + student + ", is deleted");
|
||||
}
|
||||
mapper.delete(student);
|
||||
}
|
||||
|
||||
|
@ -19,6 +19,8 @@
|
||||
package com.iluwatar.datamapper;
|
||||
|
||||
/**
|
||||
* Using Runtime Exception for avoiding dependancy on implementation exceptions. This helps in
|
||||
* decoupling.
|
||||
*
|
||||
* @author amit.dixit
|
||||
*
|
||||
|
@ -22,7 +22,7 @@ import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public final class StudentFirstDataMapper implements StudentDataMapper {
|
||||
public final class StudentDataMapperImpl implements StudentDataMapper {
|
||||
|
||||
/* Note: Normally this would be in the form of an actual database */
|
||||
private List<Student> students = new ArrayList<>();
|
||||
@ -59,7 +59,7 @@ public final class StudentFirstDataMapper implements StudentDataMapper {
|
||||
|
||||
} else {
|
||||
|
||||
/* Throw user error */
|
||||
/* Throw user error after wrapping in a runtime exception */
|
||||
throw new DataMapperException("Student [" + studentToBeUpdated.getName() + "] is not found");
|
||||
}
|
||||
}
|
||||
@ -75,7 +75,7 @@ public final class StudentFirstDataMapper implements StudentDataMapper {
|
||||
|
||||
} else {
|
||||
|
||||
/* Throw user error */
|
||||
/* Throw user error after wrapping in a runtime exception */
|
||||
throw new DataMapperException("Student already [" + studentToBeInserted.getName() + "] exists");
|
||||
}
|
||||
}
|
||||
@ -91,7 +91,7 @@ public final class StudentFirstDataMapper implements StudentDataMapper {
|
||||
|
||||
} else {
|
||||
|
||||
/* Throw user error */
|
||||
/* Throw user error after wrapping in a runtime exception */
|
||||
throw new DataMapperException("Student [" + studentToBeDeleted.getName() + "] is not found");
|
||||
}
|
||||
}
|
@ -1,101 +0,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.
|
||||
*/
|
||||
package com.iluwatar.datamapper;
|
||||
|
||||
import java.util.Vector;
|
||||
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 Vector<>();
|
||||
|
||||
@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;
|
||||
}
|
||||
}
|
@ -1,6 +1,5 @@
|
||||
/**
|
||||
* 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,
|
||||
|
@ -24,8 +24,7 @@ import org.junit.Test;
|
||||
|
||||
import com.iluwatar.datamapper.Student;
|
||||
import com.iluwatar.datamapper.StudentDataMapper;
|
||||
import com.iluwatar.datamapper.StudentFirstDataMapper;
|
||||
import com.iluwatar.datamapper.StudentSecondDataMapper;
|
||||
import com.iluwatar.datamapper.StudentDataMapperImpl;
|
||||
|
||||
/**
|
||||
* The Data Mapper (DM) is a layer of software that separates the in-memory objects from the
|
||||
@ -45,41 +44,7 @@ public class DataMapperTest {
|
||||
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();
|
||||
final StudentDataMapper mapper = new StudentDataMapperImpl();
|
||||
|
||||
/* Create new student */
|
||||
Student student = new Student(1, "Adam", 'A');
|
||||
|
@ -0,0 +1,50 @@
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
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));
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user