diff --git a/mylibrary/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/exception/BookAlreadyExistsException.java b/mylibrary/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/exception/BookAlreadyExistsException.java new file mode 100644 index 0000000..a21df9e --- /dev/null +++ b/mylibrary/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/exception/BookAlreadyExistsException.java @@ -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))); + } +} diff --git a/mylibrary/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/exception/BookNotFoundException.java b/mylibrary/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/exception/BookNotFoundException.java new file mode 100644 index 0000000..679f2aa --- /dev/null +++ b/mylibrary/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/exception/BookNotFoundException.java @@ -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))); + } +} diff --git a/mylibrary/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/exception/IllegalBookQuantityException.java b/mylibrary/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/exception/IllegalBookQuantityException.java new file mode 100644 index 0000000..9dd11ef --- /dev/null +++ b/mylibrary/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/exception/IllegalBookQuantityException.java @@ -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)); + } +} diff --git a/mylibrary/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/exception/NotValidBookException.java b/mylibrary/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/exception/NotValidBookException.java new file mode 100644 index 0000000..6eab151 --- /dev/null +++ b/mylibrary/src/main/java/fr/iut_fbleau/but3/dev62/mylibrary/book/exception/NotValidBookException.java @@ -0,0 +1,8 @@ +package fr.iut_fbleau.but3.dev62.mylibrary.book.exception; + +public class NotValidBookException extends Exception { + + public NotValidBookException(String message) { + super(message); + } +} diff --git a/mylibrary/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/book/exception/BookAlreadyExistsExceptionTest.java b/mylibrary/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/book/exception/BookAlreadyExistsExceptionTest.java new file mode 100644 index 0000000..f19a9fe --- /dev/null +++ b/mylibrary/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/book/exception/BookAlreadyExistsExceptionTest.java @@ -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); + } +} diff --git a/mylibrary/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/book/exception/BookNotFoundExceptionTest.java b/mylibrary/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/book/exception/BookNotFoundExceptionTest.java new file mode 100644 index 0000000..1a0cfe2 --- /dev/null +++ b/mylibrary/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/book/exception/BookNotFoundExceptionTest.java @@ -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")); + } + } +} diff --git a/mylibrary/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/book/exception/IllegalBookQuantityExceptionTest.java b/mylibrary/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/book/exception/IllegalBookQuantityExceptionTest.java new file mode 100644 index 0000000..1aa191f --- /dev/null +++ b/mylibrary/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/book/exception/IllegalBookQuantityExceptionTest.java @@ -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()); + } +} diff --git a/mylibrary/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/book/exception/NotValidBookExceptionTest.java b/mylibrary/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/book/exception/NotValidBookExceptionTest.java new file mode 100644 index 0000000..5f826b5 --- /dev/null +++ b/mylibrary/src/test/java/fr/iut_fbleau/but3/dev62/mylibrary/book/exception/NotValidBookExceptionTest.java @@ -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()); + } + } +}