Next page | Contents page |

Dependence on other objects

Consider class Customer and interface Order, given below. (For completeness we give the Person superclass of Customer too: for practice you may like to write a test class for Person that exercises every line of its code.)

Class Customer has a list of orders, as defined by interface Order. For unit testing we need to test Customer by itself, not including anything about Orders. In any case we have no constructor for Order objects. So how can we test Customer?

package net.grelf.course;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class Customer extends Person
{
	private List <Order> orders;
	public List <Order> getOrders () { return this.orders; }
	
	public Customer (String theName, Person.Sex theSex, Date theDob)
	{
		super (theName, theSex, theDob);
		orders = new ArrayList <Order> ();
	}

	/** @return whether successful */
	public boolean addOrder (Order order)
	{
		return this.orders.add (order);
	}
	
	/** @return whether successful */
	public boolean removeOrder (Order order)
	{
		return this.orders.remove (order);
	}
	
	public List <String> getOrderReferences ()
	{
		List <String> references = new ArrayList <String> ();
		
		for (Order order : this.orders)
		{
			references.add (order.getReference ());
		}
		
		return references;
	}
} // Customer

---------------------------------------------------------------------------

package net.grelf.course;

import java.util.List;

public interface Order
{
	/** @return order reference */
	String getReference ();
	
	/** @return products in the order */
	List <Product> getProducts ();
	
	/** @return whether successful. */
	boolean addProduct (Product p);

	/** @return whether successful. */
	boolean removeProduct (Product p);
	
	// The full interface may have several more methods of course.
} // Order

----------------------------------------------------------------------------

package net.grelf.course;

import java.util.Date;

public class Person implements Cloneable, java.io.Serializable
{
	public enum Sex { MALE, FEMALE };

	protected String name;
	public String getName () { return this.name; }
	
	protected Sex sex;
	public Sex getSex () { return this.sex; }
	
	protected Date dateOfBirth;
	public Date getDateOfBirth () { return this.dateOfBirth; }

	public Person (String theName, Sex theSex, Date theDob)
	{
		name = theName;
		sex = theSex;
		dateOfBirth = (Date) theDob.clone ();
		// NB: Why should we clone this but not the other two parameters?
	}

	@Override
	public String toString ()
	{
		return name + " (" + sex + ") born " + dateOfBirth.toString ();
	}

	@Override
	public boolean equals (Object other)
	{
		if (null == other) return false;

		if (!getClass ().equals (other.getClass ())) return false;

		Person x = (Person) other;

		if (!this.name.equals (x.name)) return false;
		if (this.sex != x.sex) return false;
		if (!this.dateOfBirth.equals (x.dateOfBirth)) return false;

		return true;
	}

	@Override
	public Person clone ()
	{
		try
		{
			Person copy = (Person) super.clone ();
			copy.name = name; // Immutable!
			copy.sex = sex;
			copy.dateOfBirth = (Date) dateOfBirth.clone ();
			return copy;
		}
		catch (CloneNotSupportedException ex)
		{
			return null;
		}
	}

	@Override
	public int hashCode ()
	{
		return 7 * name.hashCode () + 11 * sex.hashCode () + 13 * dateOfBirth.hashCode ();
	}

	/** Convenience method for serialisation
	  * @param filePath File name or full path ending with file name */
	public void save (String filePath) throws java.io.IOException, ClassNotFoundException
	{
		java.io.ObjectOutputStream sout = new java.io.ObjectOutputStream (new java.io.FileOutputStream (new java.io.File (filePath)));
		sout.writeObject (this);
		sout.close ();
	}

	/** Convenience method for deserialisation
	  * @param filePath File name or full path ending with file name */
	public static Person load (String filePath) throws java.io.IOException, ClassNotFoundException
	{
		java.io.ObjectInputStream sin = new java.io.ObjectInputStream (new java.io.FileInputStream (new java.io.File (filePath)));
		Object copy = sin.readObject ();
		sin.close ();

		if (copy instanceof Person)
		{
			return (Person) copy;
		}
		else
		{
			return null;
		}
	}

} // Person
Next page | Contents page |