FileDocCategorySizeDatePackage
CheckinCheckoutTests.javaAPI DocExample9828Thu Oct 13 12:07:18 BST 2005com.samscdrental.tests

CheckinCheckoutTests

public class CheckinCheckoutTests extends TestCase

Title: Sams CD Rental Store

Description:

Copyright: Copyright (c) 2004

Company:

author
Ken Pugh
version
1.0

Fields Summary
private RentalOperations
theRentalOperations
private MaintenanceOperations
theMaintenanceOperations
private TestOnlyOperations
theTestOnlyOperations
private static String
CDRELEASE_FILENAME
private static String
CDDISC_FILENAME
private static String
CUSTOMER_FILENAME
Constructors Summary
public CheckinCheckoutTests()


	
public CheckinCheckoutTests(String name)

		super( name );
	
Methods Summary
public static voidmain(java.lang.String[] args)

		setupFilenames( args );
		StoreDataAccess.makeNew();
		// If errors occur, then let them occur here
		new CheckinCheckoutTests();
		junit.textui.TestRunner.run( suite() );
	
protected voidsetUp()
Sets up the fixture, for example, open a network connection.

throws
Exception
todo
Implement this junit.framework.TestCase method

		theRentalOperations = RentalOperations.getInstance();
		theMaintenanceOperations =
			MaintenanceOperations.getInstance();
		theTestOnlyOperations = TestOnlyOperations.getInstance();

		theTestOnlyOperations.collectionsClear();
		theMaintenanceOperations.collectionsInitialize(
			CUSTOMER_FILENAME,
			CDDISC_FILENAME,
			CDRELEASE_FILENAME );

	
private static voidsetupFilenames(java.lang.String[] args)

		if ( args.length == 0 )
		{
			return;
		}
		if ( args.length < 3 )
		{
			System.out.println( "You must specify 3 filenames" +
								"- CDRelease, CDDisc, Customer" );
			return;
		}
		CDRELEASE_FILENAME = args[0];
		CDDISC_FILENAME = args[1];
		CUSTOMER_FILENAME = args[2];

	
public static junit.framework.Testsuite()

		TestSuite suite = new TestSuite();
		suite.addTest( new CheckinCheckoutTests( "testNormalOperation" ) );
		suite.addTest( new CheckinCheckoutTests( "testLateReturn" ) );
		suite.addTest( new CheckinCheckoutTests( "testBadPhysicalID" ) );
		suite.addTest( new CheckinCheckoutTests( "testBadCustomerID" ) );
		suite.addTest( new CheckinCheckoutTests( "testAlreadyRented" ) );
		suite.addTest( new CheckinCheckoutTests( "testNonExistentPhysicalID" ) );
		suite.addTest( new CheckinCheckoutTests( "testReturnNotRented" ) );

		TestResult testResult = new TestResult();
		suite.run( testResult );
		return suite;
	
protected voidtearDown()
Tears down the fixture, for example, close a network connection.

throws
Exception
todo
Implement this junit.framework.TestCase method


		theRentalOperations.dispose();

	
public voidtestAlreadyRented()

		try
		{
			PhysicalID aPhysicalID = new PhysicalID( "1234567890" );
			CustomerID aCustomerID = new CustomerID( "ABCDEFGHIJ" );
			try
			{
				theRentalOperations.checkoutCDDisc( aPhysicalID, aCustomerID );
			}
			catch ( CheckOutDeviation e )
			{
				fail( "Checkout failed " + e.getMessage() );

			}

			boolean flag = theRentalOperations.isCDDiscRented( aPhysicalID );
			assertTrue( "CDDisc not rented", flag );

			CustomerID anotherCustomerID = new CustomerID( "DEFGHIJKLM" );
			theRentalOperations.checkoutCDDisc( aPhysicalID, anotherCustomerID );
			fail( "Checkout was supposed to fail" );
		}
		catch ( PhysicalIDFormatDeviation e )
		{
			fail(
				"Physical id failed " + e.getMessage() );
		}
		catch ( CustomerIDFormatDeviation e )
		{
			fail( "Customer id failed " + e.getMessage() );
		}
		catch ( CheckOutDeviation e )
		{
			;
		}

	
public voidtestBadCustomerID()

		try
		{
			new CustomerID( "ABCDEFG" );
			fail( "Customer id supposed to fail" );
		}
		catch ( CustomerIDFormatDeviation e )
		{
			;
		}

	
public voidtestBadPhysicalID()

		try
		{
			new PhysicalID( "123456" );
			fail( "Physical id supposed to fail" );
		}
		catch ( PhysicalIDFormatDeviation e )
		{
			;
		}
	
public voidtestLateReturn()

		PhysicalID aPhysicalID = null;
		CustomerID aCustomerID = null;
		try
		{
			aPhysicalID = new PhysicalID( "1234567890" );
			aCustomerID = new CustomerID( "ABCDEFGHIJ" );
			theRentalOperations.checkoutCDDisc( aPhysicalID, aCustomerID );
			boolean flag = theRentalOperations.isCDDiscRented( aPhysicalID );
			assertTrue( "CDDisc not rented", flag );
			// What do I do - wait for two days and then test?
			// I really want to perform this test at the command level
			// Because Sam wants a dialog box put up.
			theTestOnlyOperations.setStartTimeForRentalBackSomeDays(
				aPhysicalID, 3 );
			theRentalOperations.checkinCDDisc(
				aPhysicalID );
			fail( "Rental supposed to be overdue" );
		}
		catch ( PhysicalIDFormatDeviation e )
		{
			fail(
				"Physical id failed " + e.getMessage() );
		}
		catch ( CustomerIDFormatDeviation e )
		{
			fail( "Customer id failed " + e.getMessage() );
		}
		catch ( CheckOutDeviation e )
		{
			fail( "Checkout failed " + e.getMessage() );

		}
		catch ( CheckInDeviation e )
		{
			fail( "Checkin failed " + e.getMessage() );

		}
		catch ( LateReturnDeviation e )
		{
			// Check the contents of the DTO
			OverdueRentalDTO ordto = e.getOverdueRentalDTO();
			Timestamp today = new Timestamp();
			Timestamp start = today.addDays( -3 );
			assertEquals( "Start date incorrect", start,
						  ordto.theRentalStartTime );
			Timestamp due_date = start.addDays( 2 );
			assertEquals( "Due date incorrect", due_date,
						  ordto.theRentalDueTime );
			Timestamp end_date = today;
			assertEquals( "End date incoorect", end_date,
						  ordto.theRentalEndTime );
			assertEquals( " Customer name incorrect", new Name( "Sammy" ),
						  ordto.theCustomerName );
			assertEquals( "Title incorrect", new Name( "Really Good Title" ),
						  ordto.theCDReleaseTitle );
			assertEquals( "Customer id incorrect", aCustomerID,
						  ordto.theCustomerID );
			assertEquals( "Physical id incorrect", aPhysicalID,
						  ordto.theCDDiscPhysicalID );
			;
		}

	
public voidtestNonExistentCustomerID()

		try
		{
			PhysicalID aPhysicalID = new PhysicalID( "1234567890" );
			CustomerID aCustomerID = new CustomerID( "ABCDEQQQQQ" );
			theRentalOperations.checkoutCDDisc( aPhysicalID,
												aCustomerID );
			fail( "Checkout should fail" );
		}
		catch ( PhysicalIDFormatDeviation e )
		{
			fail(
				"Physical id failed " + e.getMessage() );
		}
		catch ( CustomerIDFormatDeviation e )
		{
			fail( "Customer id failed " + e.getMessage() );
		}
		catch ( CheckOutDeviation e )
		{
			;
		}
	
public voidtestNonExistentPhysicalID()

		try
		{
			PhysicalID aPhysicalID = new PhysicalID( "1234567000" );
			CustomerID aCustomerID = new CustomerID( "ABCDEFGHIJ" );
			theRentalOperations.checkoutCDDisc( aPhysicalID,
												aCustomerID );
			fail( "Checkout should fail" );
		}
		catch ( PhysicalIDFormatDeviation e )
		{
			fail(
				"Physical id failed " + e.getMessage() );
		}
		catch ( CustomerIDFormatDeviation e )
		{
			fail( "Customer id failed " + e.getMessage() );
		}
		catch ( CheckOutDeviation e )
		{
			;
		}

	
public voidtestNormalOperation()
runNormalOperationTest

		try
		{
			PhysicalID aPhysicalID = new PhysicalID( "1234567890" );
			CustomerID aCustomerID = new CustomerID( "ABCDEFGHIJ" );
			RentalContractDTO rcdto = theRentalOperations.
				checkoutCDDisc( aPhysicalID, aCustomerID );
			// Check DTO values
			assertEquals( "Rental fee incorrect", new Dollar( 3.0 ),
						  rcdto.theRentalFee );
			assertEquals( "Physical ID DTO incorrect", aPhysicalID,
						  rcdto.theCDDiscPhysicalID );
			Timestamp today = new Timestamp();
			assertEquals( " Start date incorrect", today,
						  rcdto.theRentalStartTime );
			Timestamp due_date = today.addDays( 2 );
			assertEquals( " End date incorrect ", due_date,
						  rcdto.theRentalDueTime );
			assertEquals( " Customer name incorrect", new Name( "Sammy" ),
						  rcdto.theCustomerName );
			assertEquals( "Title incorrect", new Name( "Really Good Title" ),
						  rcdto.theCDReleaseTitle );
			boolean flag = theRentalOperations.isCDDiscRented( aPhysicalID );
			assertTrue( "CDDisc not rented", flag );
			theRentalOperations.checkinCDDisc( aPhysicalID );
		}
		catch ( PhysicalIDFormatDeviation e )
		{
			fail(
				"Physical id failed " + e.getMessage() );
		}
		catch ( CustomerIDFormatDeviation e )
		{
			fail( "Customer id failed " + e.getMessage() );
		}
		catch ( CheckOutDeviation e )
		{
			fail( "Checkout failed " + e.getMessage() );

		}
		catch ( CheckInDeviation e )
		{
			fail( "Checkin failed " + e.getMessage() );

		}
		catch ( LateReturnDeviation e )
		{
			fail( "Should not be late return " );
		}
	
public voidtestReturnNotRented()

		try
		{
			PhysicalID aPhysicalID = new PhysicalID( "1234567890" );
			theRentalOperations.checkinCDDisc(
				aPhysicalID );
			fail( "Checkin suppose to fail" );
		}
		catch ( PhysicalIDFormatDeviation e )
		{
			fail(
				"Physical id failed " + e.getMessage() );
		}
		catch ( CheckInDeviation e )
		{
			;
		}