diff --git a/src/tests/AddItemBookTest.java b/src/tests/AddItemBookTest.java index 2b559e2da17214d6966d262b79306e7faa0b8f18..9c1e0f02f480a7ebc4f47916472c1bc52a7a9d7e 100644 --- a/src/tests/AddItemBookTest.java +++ b/src/tests/AddItemBookTest.java @@ -24,43 +24,27 @@ public class AddItemBookTest { private static int addItemBookBadEntryTest(ISocialNetwork sn, String login, String pwd, String title, String kind, String author, int nbPages, String testId, String errorMessage) { - int nbBooks = sn.nbBooks(); // Number of books when starting to - // run this method + int nbBooks = sn.nbBooks(); // Number of books when starting to run this method try { sn.addItemBook(login, pwd, title, kind, author, nbPages); // Try to add this book - // Reaching this point means that no exception was thrown by - // addItemBook() - System.out.println("Err " + testId + " : " + errorMessage); // display - // the - // error - // message + System.out.println("Err " + testId + " : " + errorMessage); // display error return 1; // and return the "error" value - } catch (BadEntryException e) { // BadEntry exception was thrown by - // addItemBook() : this is a good start! - // Let's now check if 'sn' was not - // impacted - if (sn.nbBooks() != nbBooks) { // The number of members has - // changed : this is an error - // case. + } catch (BadEntryException e) { // BadEntry exception was thrown + // check if 'sn' was not impacted + if (sn.nbBooks() != nbBooks) { System.out .println("Err " + testId - + " : BadEntry was thrown but the number of books was changed"); // Display - // a - // specific - // error - // message + + " : BadEntry was thrown but the number of books was changed"); + // Display error for the number of books return 1; // return "error" value } else - // The number of books hasn't changed, which is considered a - // good indicator that 'sn' was not modified - return 0; // return success value : everything seems OK, nothing - // to display - } catch (Exception e) { // An exception was thrown by addItemBook(), but - // it was not the expected exception BadEntry + // The number of books hasn't changed,'sn' not modified + return 0; // everything seems OK + } catch (Exception e) { // An exception was thrown by addItemBook(), Unexpected exception System.out.println("Err " + testId + " : unexpected exception. " + e); // Display a specific error message - e.printStackTrace(); // Display contextual info about what happened + e.printStackTrace(); return 1; // return error value } } @@ -81,40 +65,25 @@ public class AddItemBookTest { private static int addItemBookAlreadyExistsTest(ISocialNetwork sn, String login, String pwd, String title, String kind, String author, int nbPages, String testId, String errorMessage) { - int nbBooks = sn.nbBooks(); // Number of members when starting to - // process this method + int nbBooks = sn.nbBooks(); // Number of members when starting to process this method try { - sn.addItemBook(login, pwd, title, kind, author, nbPages); // Try to add this book - // Reaching this point means that no exception was thrown by - // addItemBook() - System.out.println("Err " + testId + " : " + errorMessage); // display - // the - // error - // message + sn.addItemBook(login, pwd, title, kind, author, nbPages); // Try to add this book that exists + System.out.println("Err " + testId + " : " + errorMessage); // display error message return 1; // and return the "error" value - } catch ( ItemBookAlreadyExistsException e) {// AlreadyExists exception was - // thrown by addItemBook() : - // this is a good start! - // Let's now check if 'sn' - // was not impacted + } catch ( ItemBookAlreadyExistsException e) {// AlreadyExists exception was launched + //checking sn changes if (sn.nbBooks() != nbBooks) { System.out .println("Err " + testId + " : ItemBookAlreadyExists was thrown, but the number of books was changed"); // Display - // a - // specific - // error - // message - return 1;// and return the "error" value + // error message + return 1;// error value assigned } else - return 0; // return success value : everything is OK, nothing to + return 0; // OK value // display - } catch (Exception e) { // An exception was thrown by addMember(), but - // it was not the expected exception - // AlreadyExists - System.out.println("Err " + testId + " : unexpected exception. " - + e); // Display a specific error message + } catch (Exception e) { // An exception was thrown by addMember() + System.out.println("Err " + testId + " : unexpected exception. " + e); // Display unexpected exception e.printStackTrace(); // Display contextual info about what happened return 1; // return error value } @@ -135,27 +104,20 @@ public class AddItemBookTest { */ private static int addItemBookOKTest(ISocialNetwork sn, String login, String pwd, String title, String kind, String author, int nbPages, String testId) { - int nbBooks = sn.nbBooks(); // Number of books when starting to - // process this method + int nbBooks = sn.nbBooks(); // Number of books try { sn.addItemBook(login, pwd, title, kind, author, nbPages); // Try to add this book - // No exception was thrown. That's a good start ! - if (sn.nbBooks() != nbBooks + 1) { // But the number of books - // hasn't changed - // accordingly + if (sn.nbBooks() != nbBooks + 1) { // Number does not changed System.out.println("Err " + testId + " : the number of books (" + nbBooks + ") was not incremented"); // Error message displayed return 1; // return error code } else - return 0; // return success code : everything is OK, nothing to + return 0; // return OK code // display - } catch (Exception e) {// An exception was thrown by addMember() : this - // is an error case + } catch (Exception e) {// An exception was thrown by addMember() System.out - .println("Err " + testId + " : unexpected exception " + e); // Error - // message - // displayed + .println("Err " + testId + " : unexpected exception " + e); // Error message e.printStackTrace(); // Display contextual info about what happened return 1; // return error code } @@ -181,31 +143,20 @@ public class AddItemBookTest { // process this method try { sn.addItemBook(login, pwd, title, kind, author, nbPages); // Try to add this book - // Reaching this point means that no exception was thrown by - // addItemBook() - System.out.println("Err " + testId + " : " + errorMessage); // display - // the - // error - // message + // Reaching this point means that no exception was thrown by addItemBook() + System.out.println("Err " + testId + " : " + errorMessage); // display error message return 1; // and return the "error" value - } catch ( NotMemberException e) {// AlreadyExists exception was - // thrown by addItemBook() : - // this is a good start! - // Let's now check if 'sn' - // was not impacted + } catch ( NotMemberException e) {// AlreadyExists exception was thrown by addItemBook() : + //check if 'sn' was not impacted if (sn.nbBooks() != nbBooks) { System.out .println("Err " + testId + " : NotMember was thrown, but the number of books was changed"); // Display - // a - // specific - // error - // message + // a error message launched return 1;// and return the "error" value } else - return 0; // return success value : everything is OK, nothing to - // display + return 0; // return OK value } catch (Exception e) { // An exception was thrown by addMember(), but // it was not the expected exception // AlreadyExists @@ -241,14 +192,10 @@ public class AddItemBookTest { + ") was not incremented"); // Error message displayed return 1; // return error code } else - return 0; // return success code : everything is OK, nothing to - // display - } catch (Exception e) {// An exception was thrown by addMember() : this - // is an error case + return 0; // return success code + } catch (Exception e) {// An exception was thrown by addMember() : error case System.out - .println("Err " + testId + " : unexpected exception " + e); // Error - // message - // displayed + .println("Err " + testId + " : unexpected exception " + e); // Error message e.printStackTrace(); // Display contextual info about what happened return 1; // return error code } @@ -268,8 +215,7 @@ public class AddItemBookTest { System.out.println("Testing addItemBook()"); // <=> test n°1 - // check if incorrect parameters cause addItemBook() to throw BadEntry - // exception + // check if incorrect parameters cause addItemBook() to throw BadEntry exception //populate 'sn' with 3 members nbErrors += addMemberOKTest(sn,"Paul","paul","Student","1.0a"); @@ -357,7 +303,7 @@ public class AddItemBookTest { System.out.println("AddItemBookTest : " + tr); return tr; } - catch (NotTestReportException e){ //This shouldn't happen + catch (NotTestReportException e){ System.out.println("Unexpected error in AddMemberTest test code - Can't return valuable test results"); return null; }