All pastes #968905 Raw Edit

Untitled

public text v1 · immutable
·published 2008-04-03 11:36 UTC
rendered paste body
/* Copyright (c) 2008 Sebastian Nowicki
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 *
 * Except as contained in this notice, the name(s) of the above copyright holders
 * shall not be used in advertising or otherwise to promote the sale, use or other
 * dealings in this Software without prior written authorization.
 */

import org.junit.*;
import static org.junit.Assert.*;

public class TestCustomer
{
    private Customer mCustomer;
    private Video mVideo;
    private VideoCopy mVideoCopy;

    @Before public void setUp()
    {
        mCustomer = new Customer("John Doe", 10);
        mVideo = new Video("I Am Legend", 20, 7);
        mVideoCopy = new VideoCopy(42, mVideo);
    }

    @After public void tearDown()
    {
        mCustomer.ReturnVideo(mVideoCopy);
    }

    @Test(expected = IllegalArgumentException.class)
    public void SetFullNameWithPostfixedFirst()
    {
        mCustomer.SetFullName("John ");
    }

    @Test(expected = IllegalArgumentException.class)
    public void SetFullNameWithPrefixedFirst()
    {
        mCustomer.SetFullName(" John");
    }

    @Test(expected = IllegalArgumentException.class)
    public void SetFullNameWithThreeNames()
    {
        mCustomer.SetFullName("John H. Doe");
    }

    @Test(expected = IllegalArgumentException.class)
    public void SetFullNameWithOnlyFirst()
    {
        mCustomer.SetFullName("John");
    }

    @Test(expected = IllegalArgumentException.class)
    public void SetFullNameWithDoubleSpaces()
    {
        mCustomer.SetFullName("John  Doe");
    }
    
    @Test public void SetFullName()
    {
        mCustomer.SetFullName("Chris Alec");
        assertEquals("First name does not match", "Chris", mCustomer.GetFirstName());
        assertEquals("Last name does not match", "Alec", mCustomer.GetLastName());
    }

    @Test public void GetFullName()
    {
        mCustomer.SetFirstName("Jamie");
        mCustomer.SetLastName("Richardson");
        assertEquals("Jamie Richardson", mCustomer.GetFullName());
    }

    @Test public void RentVideo()
    {
        int size = mCustomer.GetRentedVideos().size();
        assertTrue(mCustomer.RentVideo(mVideoCopy));
        assertTrue(mVideoCopy.IsCheckedOut());
        assertEquals(size+1, mCustomer.GetRentedVideos().size());
    }

    @Test public void RentCheckedOutVideo()
    {
        int size = mCustomer.GetRentedVideos().size();
        mVideoCopy.Checkout(); // Make sure it's checked out
        assertFalse(mCustomer.RentVideo(mVideoCopy));
        assertTrue(mVideoCopy.IsCheckedOut());
        assertEquals(size, mCustomer.GetRentedVideos().size());
    }

    @Test public void RentVideoTwice()
    {
        int size = mCustomer.GetRentedVideos().size();
        mCustomer.RentVideo(mVideoCopy);
        assertFalse(mCustomer.RentVideo(mVideoCopy));
        assertTrue(mVideoCopy.IsCheckedOut());
        assertEquals(size+1, mCustomer.GetRentedVideos().size());
    }

    @Test public void ReturnVideo()
    {
        mCustomer.RentVideo(mVideoCopy);
        int size = mCustomer.GetRentedVideos().size();
        assertTrue(mCustomer.ReturnVideo(mVideoCopy));
        assertFalse(mVideoCopy.IsCheckedOut());
        assertEquals(size-1, mCustomer.GetRentedVideos().size());
    }

    @Test public void ReturnReturnedVideo()
    {
        int size = mCustomer.GetRentedVideos().size();
        assertFalse("Somehow succeeded at returning a video which wasn't rented", mCustomer.ReturnVideo(mVideoCopy));
        assertFalse("Video wasn't supposed to be checked out", mVideoCopy.IsCheckedOut());
        assertEquals("RentedVideos list was altered", size, mCustomer.GetRentedVideos().size());
    }

    @Test public void ValidateName()
    {
        assertTrue(Customer.ValidateName("John Smith"));
    }

    @Test public void ValidateNameWithDoubleSpaces()
    {
        assertFalse("Multiple spaces are not caught", Customer.ValidateName("John  Smith"));
    }
}