package cz.muni.fi.pb162.collections; import java.util.HashSet; import java.util.Set; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; import java.util.List; public class LibraryTest extends TestCase { public LibraryTest(String testName) { super(testName); } public static Test suite() { TestSuite suite = new TestSuite(LibraryTest.class); return suite; } protected void assertException(String when, Throwable t) { assertTrue("Vyjimka " + t.getClass().getName() + " vyvolana pri " + "pokusu o " + when + " neobsahuje podrobny popis!", t.getMessage() != null && t.getMessage().trim().length() > 0); } public void testRules() { BasicRulesTester.testMethodsAndAttributes(BookImpl.class); BasicRulesTester.testMethodsAndAttributes(ReaderImpl.class); BasicRulesTester.testMethodsAndAttributes(ReadersRegisterImpl.class); } public final void testNewReader() { Reader reader = new ReaderImpl("aaa", "55"); assertEquals("Nove vytvoreny ctenar ma jine jmeno, nez bylo zadano","aaa", reader.getName()); assertEquals("Nove vytvoreny ctenar ma jine ID, nez bylo zadano","55", reader.getId()); try { Reader r = new ReaderImpl(null, "55"); fail("Pri pokusu o vytvoreni ctenare s praznym jmenem se nevyhodila pozadovana vyjimka"); } catch (IllegalArgumentException ex) { assertException("vytvoreni ctenare s parametrem name nastavenym na null", ex); } catch (NullPointerException ex) { assertException("vytvoreni ctenare s parametrem name nastavenym na null", ex); } catch (Exception ex) { fail("Pri pokusu o vytvoreni ctenare s praznym jmenem se vyhodila spatna vyjimka"); } try { Reader r = new ReaderImpl("aaa", null); fail("Pri pokusu o vytvoreni ctenare s praznym ID se nevyhodila vyjimka"); } catch (IllegalArgumentException ex) { assertException("vytvoreni ctenare s parametrem id nastavenym na null", ex); } catch (NullPointerException ex) { assertException("vytvoreni ctenare s parametrem id nastavenym na null", ex); } catch (Exception ex) { fail("Pri pokusu o vytvoreni ctenare s praznym id se vyhodila spatna vyjimka"); } } public final void testReaderEquals() { Reader reader = new ReaderImpl("aaa", "55"); assertTrue("Metoda equals vraci false, i kdyz je jejim parametrem " + "instance ctenare se stejnym id (chyba v metode ReaderImpl.equals)", reader.equals(new ReaderImpl("bbb", "55"))); assertFalse("Metoda equals vraci true, i kdyz je jejim parametrem " + "instance ctenare s jinym id (chyba v metode ReaderImpl.equals)", reader.equals(new ReaderImpl("bbb", "66"))); try { assertFalse("Metoda equals vraci true, i kdyz je jejim parametrem " + "hodnota null (chyba v metode ReaderImpl.equals)", reader.equals(null)); } catch (Exception ex) { fail("Kdyz se zavola metoda ReaderImpl.equals() s parametrem null," + " metoda nevrati false, ale vyhodi vyjimku " + ex.getClass().getName()); } try { assertFalse("Metoda equals vraci true, i kdyz je jejim parametrem " + "objekt jineho typu (chyba v metode ReaderImpl.equals)", reader.equals(new Object())); } catch (Exception ex) { fail("Kdyz se zavola metoda ReaderImpl.equals() s parametrem," + " jineho typu, metoda nevrati false, ale vyhodi vyjimku " + ex.getClass().getName()); } } public final void testReaderHashCode() { Reader r1 = new ReaderImpl("aaa", "55"); Reader r2 = new ReaderImpl("bbb", "55"); Reader r3 = new ReaderImpl("aaa", "66"); assertEquals("Metoda ReaderImpl.hashCode() vraci pro dva stejne ctenare" + " ruznou hodnotu, takze porusuje obecny kontrakt metody " + "Object.hashCode() ", r1.hashCode(), r2.hashCode()); assertFalse("Metoda ReaderImpl.hashCode() vraci pro dva ruzne ctenare " + "stejny kod; to sice neporusuje obecny kontrakt metody " + "Object.hashCode(), ale v tomto pripade lze snadno vytvorit " + "temer bezkolizni implementaci teto metody", r1.hashCode() == r3.hashCode()); } public final void testNewBook() { Book book = new BookImpl("test author", "test title", 44, new ReadersRegisterImpl()); assertEquals("Nove vytvorena kniha ma jineho autora, nez bylo zadano","test author", book.getAuthor()); assertEquals("Nove vytvorena kniha ma jiny nazev, nez bylo zadano","test title", book.getTitle()); assertEquals("Nove vytvorena kniha ma jine ID, nez bylo zadano",44, book.getId()); try { Book r = new BookImpl(null, "test title", 44, new ReadersRegisterImpl()); fail("Pri pokusu o vytvoreni knihy s parametrem author nastavenym na null se nevyhodila vyjimka"); } catch (IllegalArgumentException ex) { assertException("vytvoreni knihy s parametrem author nastavenym na null", ex); } catch (NullPointerException ex) { assertException("vytvoreni knihy s parametrem author nastavenym na null", ex); } catch (Exception ex) { fail("Pri pokusu o vytvoreni knihy s praznym autorem se vyhodila spatna vyjimka"); } try { Book r = new BookImpl("aaa", null, 44, new ReadersRegisterImpl()); fail("Pri pokusu o vytvoreni knihy s parametrem title nastavenym na null se nevyhodila vyjimka"); } catch (IllegalArgumentException ex) { assertException("vytvoreni knihy s parametrem title nastavenym na null", ex); } catch (NullPointerException ex) { assertException("vytvoreni knihy s parametrem title nastavenym na null", ex); } catch (Exception ex) { fail("Pri pokusu o vytvoreni knihy s praznym nazvem se vyhodila spatna vyjimka"); } try { Book r = new BookImpl("aaa", "bb", 44, null); fail("Pri pokusu o vytvoreni knihy s parametrem reg nastavenym na null se nevyhodila vyjimka"); } catch (IllegalArgumentException ex) { assertException("vytvoreni knihy s parametrem reg nastavenym na null", ex); } catch (NullPointerException ex) { assertException("vytvoreni knihy s parametrem reg nastavenym na null", ex); } catch (Exception ex) { fail("Pri pokusu o vytvoreni knihy s praznym registrem se vyhodila spatna vyjimka"); } } public final void testBookEquals() { ReadersRegister reg = new ReadersRegisterImpl(); Book book = new BookImpl("aaa", "ttt", 55, reg); assertTrue("Metoda equals vraci false, i kdyz je jejim parametrem " + "instance knihy se stejnym id (chyba v metode BookImpl.equals)", book.equals(new BookImpl("bbb", "ccc", book.getId(), reg))); assertFalse("Metoda equals vraci true, i kdyz je jejim parametrem " + "instance knihy s jinym id (chyba v metode BookImpl.equals)", book.equals(new BookImpl("bbb", "ccc", book.getId()+1, reg))); try { assertFalse("Metoda equals vraci true, i kdyz je jejim parametrem " + "hodnota null (chyba v metode BookImpl.equals)", book.equals(null)); } catch (Exception ex) { fail("Kdyz se zavola metoda BookImpl.equals() s parametrem null," + " metoda nevrati false, ale vyhodi vyjimku " + ex.getClass().getName()); } try { assertFalse("Metoda equals vraci true, i kdyz je jejim parametrem " + "objekt jineho typu (chyba v metode BookImpl.equals)", book.equals(new Object())); } catch (Exception ex) { fail("Kdyz se zavola metoda BookImpl.equals() s parametrem," + " jineho typu, metoda nevrati false, ale vyhodi vyjimku " + ex.getClass().getName()); } } public final void testBookHashCode() { ReadersRegister reg = new ReadersRegisterImpl(); Book b1 = new BookImpl("test author", "test title", 44, reg); Book b2 = new BookImpl("test author2", "test title2", 44, reg); Book b3 = new BookImpl("test author", "test title", 55, reg); assertEquals("Metoda BookImpl.hashCode() vraci pro dve stejne knihy" + " ruznou hodnotu, takze porusuje obecny kontrakt metody " + "Object.hashCode() ", b1.hashCode(), b2.hashCode()); assertFalse("Metoda BookImpl.hashCode() vraci pro dve ruzne knihy " + "stejny kod; to sice neporusuje obecny kontrakt metody " + "Object.hashCode(), ale v tomto pripade lze snadno vytvorit " + "temer bezkolizni implementaci teto metody", b1.hashCode() == b3.hashCode()); } public final void testReadersRegister() { ReadersRegister reg = new ReadersRegisterImpl(); Reader r1 = new ReaderImpl("abc", "66"); Reader r2 = new ReaderImpl("cba", "55"); Reader r3 = new ReaderImpl("abc", "77"); Reader r4 = new ReaderImpl("aaa", "88"); assertTrue("ReadersRegister.registerReader() vraci false pri registraci " + "noveho ctenare", reg.registerReader(r1)); assertFalse("ReadersRegister.registerReader() vraci true pri opetovne " + "registraci ctenare", reg.registerReader(r1)); reg.registerReader(r2); assertTrue("ReadersRegister.unregisterReader() vraci false pri ruseni " + "registrovaneho ctenare", reg.unregisterReader(r1)); assertFalse("ReadersRegister.unregisterReader() vraci true pri ruseni " + "neregistrovaneho ctenare", reg.unregisterReader(r1)); assertTrue("ReadersRegister.readerExists() vraci false pro " + "registrovaneho ctenare", reg.readerExists(r2)); assertFalse("ReadersRegister.readerExists() vraci true pro " + "neregistrovaneho ctenare", reg.readerExists(r1)); reg.registerReader(r1); reg.registerReader(r3); reg.registerReader(r4); List col = reg.getSortedNames(); assertTrue("ReadersRegister.getSortedNames() nevraci setridenou kolekci jmen", col != null); try { assertEquals("Vracena kolekce jmen ma spatne usporadani","cba",col.get(3)); assertEquals("Vracena kolekce jmen ma spatne usporadani","abc",col.get(2)); assertEquals("Vracena kolekce jmen ma spatne usporadani","abc",col.get(1)); assertEquals("Vracena kolekce jmen ma spatne usporadani","aaa",col.get(0)); } catch(IndexOutOfBoundsException ex) { fail("Chybny pocet jmen vraceny metodou ReadersRegister.getSortedNames(); mozna byla ostranena duplicitni jmena"); } } public final void testBookBorrowing() { Reader r = new ReaderImpl("reader", "55"); Reader rr = new ReaderImpl("reader", "66"); ReadersRegister reg = new ReadersRegisterImpl(); reg.registerReader(r); Book book = new BookImpl("test author", "test title", 44, reg); assertFalse("Pri pokusu o pujceni knihy neregistrovanemu ctenari " + "vratila BookImpl.borrow() true", book.borrow(rr)); assertTrue("Pri pokusu o pujceni nove knihy registrovanemu ctenari " + "vratila BookImpl.borrow() false", book.borrow(r)); assertFalse("Pri pokusu o pujceni jiz pujcene knihy registrovanemu ctenari " + "vratila BookImpl.borrow() true", book.borrow(r)); assertEquals("BookImpl.countLoans() vraci chybny pocet vypujcek " + "u ctenare, ktery si pujcil jednou a jeste nevratil", 1, book.countLoans(r)); assertEquals("BookImpl.countLoans() vraci chybny pocet vypujcek " + "u ctenare, ktery si knihu nikdy nepujcil", 0, book.countLoans(rr)); try { assertEquals("BookImpl.countLoans() vraci chybny pocet vypujcek " + "u ctenare, ktery si knihu nikdy nepujcil", 0, book.countLoans(null)); } catch (Exception ex) { fail("BookImpl.countLoans() vyhodi vyjimku, pokud je parametr reader " + "nastaven na null; mela by vratit 0"); } assertTrue("BookImpl.giveBack() vraci false, prestoze je kniha pujcena", book.giveBack()); assertFalse("BookImpl.giveBack() vraci true, prestoze neni kniha pujcena", book.giveBack()); assertTrue("Pri pokusu o opetovne pujceni knihy registrovanemu ctenari " + "vratila BookImpl.borrow() false", book.borrow(r)); assertEquals("BookImpl.countLoans() vraci chybny pocet vypujcek " + "u ctenare, ktery si pujcil knihu podruhe", 2, book.countLoans(r)); } }