This is part of JavaUnitTestChallengeSolved. -- DonWells
First thing, let�s create a thread that does 5 puts in a row. That is all it will do for us.
import java.lang.*; public class PutThread extends Thread { public BoundedBuffer buffer; PutThread(BoundedBuffer aBuffer){ buffer = aBuffer;} public void run (){ put("a"); put("b"); put("c"); put("d"); put("e");} public void put(String aString){ try { buffer.put(aString);} catch (InterruptedException exception) {};} }Doing 5 puts will guarantee we overflow the buffer once. Now let's create a test which uses this thread to fill the buffer. Once the buffer is full, we will need to do one take so that the thread will finish all 5 puts.
public class PutThreadTest extends Test { public BoundedBuffer buffer; public PutThread putThread; public void setUp(){ buffer = new BoundedBuffer(); putThread = new PutThread(buffer);} public void runTest (){ putThread.start(); sleepHalfSecond(); should(buffer.buffer[0] == "a","First element should be a"); should(buffer.buffer[1] == "b","Second element should be b"); should(buffer.buffer[2] == "c","Third element should be c"); should(buffer.buffer[3] == "d","Fourth element should be d"); should(buffer.occupied == 4,"Buffer should be full now"); oneTake(); waitForPutToFinish(); should(buffer.buffer[0] == "e","First element should now be e");} public void sleepHalfSecond(){ try { Thread.sleep(500);} catch (InterruptedException exception) {};} public void waitForPutToFinish(){ try { putThread.join(1000);} catch (InterruptedException exception) {};} public void oneTake(){ try { buffer.take();} catch (InterruptedException exception){};} }This test runs fine now.