ajout des exceptions du module book et leurs tests
This commit is contained in:
+13
@@ -0,0 +1,13 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.book.exception;
|
||||||
|
|
||||||
|
import java.text.MessageFormat;
|
||||||
|
|
||||||
|
public class BookAlreadyExistsException extends Exception {
|
||||||
|
|
||||||
|
public static final String A_BOOK_WITH_ISBN_ALREADY_EXISTS_MESSAGE = "A book with isbn {0} already exists";
|
||||||
|
|
||||||
|
public BookAlreadyExistsException(long isbn) {
|
||||||
|
// ISBN passe en String pour ne pas que MessageFormat ajoute le separateur de milliers
|
||||||
|
super(MessageFormat.format(A_BOOK_WITH_ISBN_ALREADY_EXISTS_MESSAGE, String.valueOf(isbn)));
|
||||||
|
}
|
||||||
|
}
|
||||||
+13
@@ -0,0 +1,13 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.book.exception;
|
||||||
|
|
||||||
|
import java.text.MessageFormat;
|
||||||
|
|
||||||
|
public class BookNotFoundException extends Exception {
|
||||||
|
|
||||||
|
public static final String THE_BOOK_WITH_ISBN_DOES_NOT_EXIST_MESSAGE = "The book with isbn {0} does not exist";
|
||||||
|
|
||||||
|
public BookNotFoundException(long isbn) {
|
||||||
|
// ISBN passe en String pour ne pas que MessageFormat ajoute le separateur de milliers
|
||||||
|
super(MessageFormat.format(THE_BOOK_WITH_ISBN_DOES_NOT_EXIST_MESSAGE, String.valueOf(isbn)));
|
||||||
|
}
|
||||||
|
}
|
||||||
+12
@@ -0,0 +1,12 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.book.exception;
|
||||||
|
|
||||||
|
import java.text.MessageFormat;
|
||||||
|
|
||||||
|
public class IllegalBookQuantityException extends Exception {
|
||||||
|
|
||||||
|
public static final String CANNOT_REMOVE_STOCK = "Cannot remove {0} units from {1} units in stock";
|
||||||
|
|
||||||
|
public IllegalBookQuantityException(int needed, int actual) {
|
||||||
|
super(MessageFormat.format(CANNOT_REMOVE_STOCK, needed, actual));
|
||||||
|
}
|
||||||
|
}
|
||||||
+8
@@ -0,0 +1,8 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.book.exception;
|
||||||
|
|
||||||
|
public class NotValidBookException extends Exception {
|
||||||
|
|
||||||
|
public NotValidBookException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
||||||
+26
@@ -0,0 +1,26 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.book.exception;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class BookAlreadyExistsExceptionTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Exception message should contain the ISBN provided")
|
||||||
|
void testExceptionMessageContainsIsbn() {
|
||||||
|
long isbn = 9780321125217L;
|
||||||
|
|
||||||
|
BookAlreadyExistsException exception = new BookAlreadyExistsException(isbn);
|
||||||
|
|
||||||
|
assertTrue(exception.getMessage().contains(String.valueOf(isbn)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Exception should expose its message constant")
|
||||||
|
void testConstantMessage() {
|
||||||
|
assertEquals("A book with isbn {0} already exists",
|
||||||
|
BookAlreadyExistsException.A_BOOK_WITH_ISBN_ALREADY_EXISTS_MESSAGE);
|
||||||
|
}
|
||||||
|
}
|
||||||
+37
@@ -0,0 +1,37 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.book.exception;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class BookNotFoundExceptionTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Exception message should contain the ISBN provided")
|
||||||
|
void testExceptionMessageContainsIsbn() {
|
||||||
|
long isbn = 9780321125217L;
|
||||||
|
|
||||||
|
BookNotFoundException exception = new BookNotFoundException(isbn);
|
||||||
|
|
||||||
|
assertTrue(exception.getMessage().contains(String.valueOf(isbn)));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Exception should expose its message constant")
|
||||||
|
void testConstantMessage() {
|
||||||
|
assertEquals("The book with isbn {0} does not exist",
|
||||||
|
BookNotFoundException.THE_BOOK_WITH_ISBN_DOES_NOT_EXIST_MESSAGE);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Exception should be properly thrown and caught")
|
||||||
|
void testExceptionCanBeThrownAndCaught() {
|
||||||
|
long isbn = 1L;
|
||||||
|
try {
|
||||||
|
throw new BookNotFoundException(isbn);
|
||||||
|
} catch (BookNotFoundException e) {
|
||||||
|
assertTrue(e.getMessage().contains("1"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
+33
@@ -0,0 +1,33 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.book.exception;
|
||||||
|
|
||||||
|
import java.text.MessageFormat;
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.CsvSource;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class IllegalBookQuantityExceptionTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Exception message should contain the requested and actual quantities")
|
||||||
|
void testExceptionMessageContainsQuantities() {
|
||||||
|
IllegalBookQuantityException exception = new IllegalBookQuantityException(10, 3);
|
||||||
|
String expected = "Cannot remove 10 units from 3 units in stock";
|
||||||
|
assertEquals(expected, exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@CsvSource({
|
||||||
|
"10, 3",
|
||||||
|
"100, 0",
|
||||||
|
"5, 4"
|
||||||
|
})
|
||||||
|
@DisplayName("Message should be formatted using the constant template")
|
||||||
|
void testFormattedMessage(int needed, int actual) {
|
||||||
|
IllegalBookQuantityException exception = new IllegalBookQuantityException(needed, actual);
|
||||||
|
String expected = MessageFormat.format(IllegalBookQuantityException.CANNOT_REMOVE_STOCK, needed, actual);
|
||||||
|
assertEquals(expected, exception.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
+44
@@ -0,0 +1,44 @@
|
|||||||
|
package fr.iut_fbleau.but3.dev62.mylibrary.book.exception;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.DisplayName;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.ValueSource;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class NotValidBookExceptionTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Exception should be created with the provided message")
|
||||||
|
void testExceptionCreation() {
|
||||||
|
String msg = "title cannot be blank";
|
||||||
|
NotValidBookException exception = new NotValidBookException(msg);
|
||||||
|
assertEquals(msg, exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest
|
||||||
|
@ValueSource(strings = {
|
||||||
|
"Title cannot be blank",
|
||||||
|
"Author cannot be blank",
|
||||||
|
"Price must be strictly positive",
|
||||||
|
"At least one category is required"
|
||||||
|
})
|
||||||
|
@DisplayName("Exception should propagate any validation message")
|
||||||
|
void testExceptionWithDifferentMessages(String message) {
|
||||||
|
NotValidBookException exception = new NotValidBookException(message);
|
||||||
|
assertEquals(message, exception.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
@DisplayName("Exception should be catchable as a general Exception")
|
||||||
|
void testExceptionInheritance() {
|
||||||
|
String msg = "invalid book";
|
||||||
|
try {
|
||||||
|
throw new NotValidBookException(msg);
|
||||||
|
} catch (Exception e) {
|
||||||
|
assertEquals(NotValidBookException.class, e.getClass());
|
||||||
|
assertEquals(msg, e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user