Merge branch 'SonarCloud-Reports-Issue#1012'
# Conflicts: # pom.xml
This commit is contained in:
@@ -44,6 +44,12 @@
|
||||
<artifactId>junit-vintage-engine</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<version>5.0.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-core</artifactId>
|
||||
|
@@ -35,6 +35,7 @@ public class App {
|
||||
*
|
||||
* @param args no argument sent
|
||||
*/
|
||||
|
||||
public static void main(String[] args) {
|
||||
var ram = new Student(1, "Ram", "Street 9, Cupertino");
|
||||
var shyam = new Student(2, "Shyam", "Z bridge, Pune");
|
||||
|
@@ -29,9 +29,6 @@ package com.iluwatar.unitofwork;
|
||||
* @param <T> Any generic entity
|
||||
*/
|
||||
public interface IUnitOfWork<T> {
|
||||
String INSERT = "INSERT";
|
||||
String DELETE = "DELETE";
|
||||
String MODIFY = "MODIFY";
|
||||
|
||||
/**
|
||||
* Any register new operation occurring on UnitOfWork is only going to be performed on commit.
|
||||
|
@@ -52,20 +52,20 @@ public class StudentRepository implements IUnitOfWork<Student> {
|
||||
@Override
|
||||
public void registerNew(Student student) {
|
||||
LOGGER.info("Registering {} for insert in context.", student.getName());
|
||||
register(student, IUnitOfWork.INSERT);
|
||||
register(student, UnitActions.INSERT.getActionValue());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerModified(Student student) {
|
||||
LOGGER.info("Registering {} for modify in context.", student.getName());
|
||||
register(student, IUnitOfWork.MODIFY);
|
||||
register(student, UnitActions.MODIFY.getActionValue());
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerDeleted(Student student) {
|
||||
LOGGER.info("Registering {} for delete in context.", student.getName());
|
||||
register(student, IUnitOfWork.DELETE);
|
||||
register(student, UnitActions.DELETE.getActionValue());
|
||||
}
|
||||
|
||||
private void register(Student student, String operation) {
|
||||
@@ -86,21 +86,21 @@ public class StudentRepository implements IUnitOfWork<Student> {
|
||||
return;
|
||||
}
|
||||
LOGGER.info("Commit started");
|
||||
if (context.containsKey(IUnitOfWork.INSERT)) {
|
||||
if (context.containsKey(UnitActions.INSERT.getActionValue())) {
|
||||
commitInsert();
|
||||
}
|
||||
|
||||
if (context.containsKey(IUnitOfWork.MODIFY)) {
|
||||
if (context.containsKey(UnitActions.MODIFY.getActionValue())) {
|
||||
commitModify();
|
||||
}
|
||||
if (context.containsKey(IUnitOfWork.DELETE)) {
|
||||
if (context.containsKey(UnitActions.DELETE.getActionValue())) {
|
||||
commitDelete();
|
||||
}
|
||||
LOGGER.info("Commit finished.");
|
||||
}
|
||||
|
||||
private void commitInsert() {
|
||||
var studentsToBeInserted = context.get(IUnitOfWork.INSERT);
|
||||
var studentsToBeInserted = context.get(UnitActions.INSERT.getActionValue());
|
||||
for (var student : studentsToBeInserted) {
|
||||
LOGGER.info("Saving {} to database.", student.getName());
|
||||
studentDatabase.insert(student);
|
||||
@@ -108,7 +108,7 @@ public class StudentRepository implements IUnitOfWork<Student> {
|
||||
}
|
||||
|
||||
private void commitModify() {
|
||||
var modifiedStudents = context.get(IUnitOfWork.MODIFY);
|
||||
var modifiedStudents = context.get(UnitActions.MODIFY.getActionValue());
|
||||
for (var student : modifiedStudents) {
|
||||
LOGGER.info("Modifying {} to database.", student.getName());
|
||||
studentDatabase.modify(student);
|
||||
@@ -116,7 +116,7 @@ public class StudentRepository implements IUnitOfWork<Student> {
|
||||
}
|
||||
|
||||
private void commitDelete() {
|
||||
var deletedStudents = context.get(IUnitOfWork.DELETE);
|
||||
var deletedStudents = context.get(UnitActions.DELETE.getActionValue());
|
||||
for (var student : deletedStudents) {
|
||||
LOGGER.info("Deleting {} to database.", student.getName());
|
||||
studentDatabase.delete(student);
|
||||
|
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2019 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.
|
||||
*/
|
||||
|
||||
package com.iluwatar.unitofwork;
|
||||
|
||||
public enum UnitActions {
|
||||
INSERT("INSERT"),
|
||||
DELETE("DELETE"),
|
||||
MODIFY("MODIFY");
|
||||
|
||||
private final String actionValue;
|
||||
|
||||
UnitActions(String actionValue) {
|
||||
this.actionValue = actionValue;
|
||||
}
|
||||
|
||||
public String getActionValue() {
|
||||
return actionValue;
|
||||
}
|
||||
}
|
@@ -25,12 +25,15 @@ package com.iluwatar.unitofwork;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
|
||||
|
||||
/**
|
||||
* AppTest
|
||||
*/
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
App.main(new String[]{});
|
||||
public void shouldExecuteWithoutException() {
|
||||
assertDoesNotThrow(() -> App.main(new String[]{}));
|
||||
}
|
||||
}
|
||||
|
@@ -63,7 +63,7 @@ public class StudentRepositoryTest {
|
||||
studentRepository.registerNew(student1);
|
||||
studentRepository.registerNew(student2);
|
||||
|
||||
assertEquals(2, context.get(IUnitOfWork.INSERT).size());
|
||||
assertEquals(2, context.get(UnitActions.INSERT.getActionValue()).size());
|
||||
verifyNoMoreInteractions(studentDatabase);
|
||||
}
|
||||
|
||||
@@ -72,7 +72,7 @@ public class StudentRepositoryTest {
|
||||
studentRepository.registerDeleted(student1);
|
||||
studentRepository.registerDeleted(student2);
|
||||
|
||||
assertEquals(2, context.get(IUnitOfWork.DELETE).size());
|
||||
assertEquals(2, context.get(UnitActions.DELETE.getActionValue()).size());
|
||||
verifyNoMoreInteractions(studentDatabase);
|
||||
}
|
||||
|
||||
@@ -81,15 +81,15 @@ public class StudentRepositoryTest {
|
||||
studentRepository.registerModified(student1);
|
||||
studentRepository.registerModified(student2);
|
||||
|
||||
assertEquals(2, context.get(IUnitOfWork.MODIFY).size());
|
||||
assertEquals(2, context.get(UnitActions.MODIFY.getActionValue()).size());
|
||||
verifyNoMoreInteractions(studentDatabase);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldSaveAllLocalChangesToDb() {
|
||||
context.put(IUnitOfWork.INSERT, List.of(student1));
|
||||
context.put(IUnitOfWork.MODIFY, List.of(student1));
|
||||
context.put(IUnitOfWork.DELETE, List.of(student1));
|
||||
context.put(UnitActions.INSERT.getActionValue(), List.of(student1));
|
||||
context.put(UnitActions.MODIFY.getActionValue(), List.of(student1));
|
||||
context.put(UnitActions.DELETE.getActionValue(), List.of(student1));
|
||||
|
||||
studentRepository.commit();
|
||||
|
||||
@@ -118,8 +118,8 @@ public class StudentRepositoryTest {
|
||||
|
||||
@Test
|
||||
public void shouldNotInsertToDbIfNoRegisteredStudentsToBeCommitted() {
|
||||
context.put(IUnitOfWork.MODIFY, List.of(student1));
|
||||
context.put(IUnitOfWork.DELETE, List.of(student1));
|
||||
context.put(UnitActions.MODIFY.getActionValue(), List.of(student1));
|
||||
context.put(UnitActions.DELETE.getActionValue(), List.of(student1));
|
||||
|
||||
studentRepository.commit();
|
||||
|
||||
@@ -128,8 +128,8 @@ public class StudentRepositoryTest {
|
||||
|
||||
@Test
|
||||
public void shouldNotModifyToDbIfNotRegisteredStudentsToBeCommitted() {
|
||||
context.put(IUnitOfWork.INSERT, List.of(student1));
|
||||
context.put(IUnitOfWork.DELETE, List.of(student1));
|
||||
context.put(UnitActions.INSERT.getActionValue(), List.of(student1));
|
||||
context.put(UnitActions.DELETE.getActionValue(), List.of(student1));
|
||||
|
||||
studentRepository.commit();
|
||||
|
||||
@@ -138,8 +138,8 @@ public class StudentRepositoryTest {
|
||||
|
||||
@Test
|
||||
public void shouldNotDeleteFromDbIfNotRegisteredStudentsToBeCommitted() {
|
||||
context.put(IUnitOfWork.INSERT, List.of(student1));
|
||||
context.put(IUnitOfWork.MODIFY, List.of(student1));
|
||||
context.put(UnitActions.INSERT.getActionValue(), List.of(student1));
|
||||
context.put(UnitActions.MODIFY.getActionValue(), List.of(student1));
|
||||
|
||||
studentRepository.commit();
|
||||
|
||||
|
Reference in New Issue
Block a user