Next page | Contents page |

Exercise 34 - Order

Write a simple implementation of our Order interface. Make the constructor of your new class take an OrderingSystem as a parameter in order to get a different order reference for each order.

A stub is provided (below) as an example of an ordering system: look at its code and observe that it is a singleton so that the method getUnusedOrderReference() avoids being static. A test class for the stub is also provided. Both the stub and its test would belong in the test/src directory - neither would be intended to become live code.

Fully unit test your booking class (100% coverage), mocking out all the other objects on which it depends.

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

package net.grelf.course;

public abstract class OrderingSystem
{
	public abstract String getUnusedOrderReference ();
} // OrderingSystem

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

package net.grelf.course;

import java.util.HashMap;
import java.util.Map;

/** A proxy for the ordering system. Written as a singleton so that methods such as
 * getUnusedOrderReference () can be non-static and therefore be mocked. */
public final class OrderingSystemStub extends OrderingSystem
{
	private static final OrderingSystemStub INSTANCE = new OrderingSystemStub ();

	private OrderingSystemStub () { /* Not to be available to anyone else */ }

	public static OrderingSystemStub getInstance ()
	{
		return INSTANCE;
	}

	/** Not suitable for production code! */
	@Override
	public String getUnusedOrderReference ()
	{
		String ref;
		final int MAX_COUNT = 100;
		int count = 0;

		do
		{
			ref = "";
			count++;

			for (int i = 0; i < 6; i++)
			{
				ref += (char) (26 * Math.random () + charBase);
			}
		}
		while (null != usedReferences.get (ref) && count < MAX_COUNT);

		usedReferences.put (ref, ref);
		return ref;
	} // getUnusedOrderReference

	private static final int charBase = (int) 'A';
	private static Map <String, String> usedReferences = new HashMap <String, String> ();
} // OrderingSystemStub

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

package net.grelf.course;

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

public class OrderingSystemStubTest
{
	@Test
	public void testGetInstance ()
	{
		assertNotNull (OrderingSystemStub.getInstance ());
	}

	/**
	 * Test method for {@link net.grelf.course.OrderingSystemStub#getUnusedOrderReference()}.
	 */
	@Test
	public void testGetUnusedOrderReference ()
	{
		final int N_REFS = 3;
		String [] refs = new String [N_REFS];
		
		for (int refNo = 0; refNo < N_REFS; refNo++)
		{
			refs [refNo] = OrderingSystemStub.getInstance ().getUnusedOrderReference ();
			assertEquals ("Unused order ref should have length 6", 6, refs [refNo].length ());
			
			for (int i = 0; i < 6; i++)
			{
				assertTrue ("Unused order ref should only be upper case letters", Character.isUpperCase (refs [refNo].charAt (i)));
			}
			
			if (refNo > 0)
			{
				assertFalse ("Successive unused order refs should differ", refs [refNo].equals (refs [refNo - 1]));
			}
		}
	}

} // OrderingSystemStubTest

------------------------------------------------------------
Next page | Contents page |