001package edu.pdx.cs.joy.di;
002
003import jakarta.xml.bind.JAXBException;
004import org.junit.jupiter.api.AfterEach;
005import org.junit.jupiter.api.BeforeEach;
006import org.junit.jupiter.api.Test;
007
008import java.io.File;
009import java.io.IOException;
010
011import static org.junit.jupiter.api.Assertions.assertEquals;
012
013/**
014 * Tests the {@link BookDatabase} cass
015 */
016public class BookDatabaseTest
017{
018    private static int nextInt = 0;
019
020    /**
021     * Put temp files in the current working directory
022     */
023    private final File directory = new File( System.getProperty( "user.dir" ) );
024
025    private String fileName;
026
027
028    @BeforeEach
029    public void setUp() {
030
031      this.fileName = generateFileName();
032    }
033
034    @AfterEach
035    public void tearDown() {
036      File dataFile = new File(directory, fileName);
037      dataFile.delete();
038    }
039
040    /**
041     * Tests that a book can be added to a book database and is persisted
042     */
043    @Test
044    public void testAddBook() throws JAXBException, IOException
045    {
046        BookDatabase db = new BookDatabase( this.directory, fileName );
047        Book book = new Book( "TestTitle", "TestAuthor", 1.23 );
048        db.add( book );
049        assertEquals( 1, db.getCopies( book ) );
050
051        db = new BookDatabase( this.directory, fileName );
052        assertEquals( 1, db.getCopies( book ) );
053    }
054
055    /**
056     * Tests that a multiple copies of the same book can be added to a book database and is persisted
057     */
058    @Test
059    public void testAddSameBook() throws JAXBException, IOException
060    {
061        BookDatabase db = new BookDatabase( this.directory, fileName );
062        Book book = new Book( "TestTitle", "TestAuthor", 1.23 );
063        db.add( book );
064        db.add( book );
065        db.add( book );
066        assertEquals( 3, db.getCopies( book ) );
067
068        db = new BookDatabase( this.directory, fileName );
069        assertEquals( 3, db.getCopies( book ) );
070    }
071
072    /**
073     * Tests that a multiple copies of multiple books can be added to a book database and is persisted
074     */
075    @Test
076    public void testAddMultipleBooks() throws JAXBException, IOException
077    {
078        BookDatabase db = new BookDatabase( this.directory, fileName );
079        Book book1 = new Book( "TestTitle1", "TestAuthor1", 1.23 );
080        db.add( book1, book1, book1 );
081        assertEquals( 3, db.getCopies( book1 ) );
082
083        Book book2 = new Book( "TestTitle2", "TestAuthor2", 1.23 );
084        db.add( book2, book2 );
085        assertEquals( 2, db.getCopies( book2 ) );
086
087        db = new BookDatabase( this.directory, fileName );
088        assertEquals( 3, db.getCopies( book1 ) );
089        assertEquals( 2, db.getCopies( book2 ) );
090    }
091
092    private String generateFileName()
093    {
094        return this.getClass().getName() + "_" + (nextInt++) + "_" + System.currentTimeMillis();
095    }
096}