Fixed most reported issues by SonarCloud.

This commit is contained in:
Toxic Dreamz
2020-08-15 21:47:39 +04:00
parent e7e3ace01f
commit 31471acb69
190 changed files with 1426 additions and 661 deletions

View File

@ -44,6 +44,11 @@
<artifactId>junit-vintage-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>

View File

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

View File

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

View File

@ -0,0 +1,18 @@
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;
}
}

View File

@ -25,12 +25,14 @@ 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[]{}));
}
}

View File

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