<?xml version="1.0" encoding="ISO-8859-1"?>
<document>
<head>
    <name>Using Multi-Threaded Tests</name>
    <doc-version>$Date: 2003/10/03 14:26:45 $</doc-version>
    <author>Matt Albrecht</author>
</head>
<body>
<!--
The code in this document were generated with the jEdit "Code2HTML" plugin.
-->
<P>
The GroboUtils class <tt>MultiThreadedTestRunner</tt> was based on the
article "JUnit Best Practices" by Andy Schneider
(andrew.schneider@javaworld.com), published online at
<a href="http://www.javaworld.com/javaworld/jw-12-2000/jw-1221-junit_p.html">
http://www.javaworld.com/javaworld/jw-12-2000/jw-1221-junit_p.html</a>.
Since GroboUtils first started using that implementation, many changes have
occured in the code to make a more robust and stable testing environment.
Due to these changes, the use of the class will be fully described in this
document.
</P>

<P>
Let's start by testing a sample application:
<!--
import java.io.*;
public class WriterCache
{
    public static interface IWriterFactory
    {
        public Writer createWriter( String location )
            throws IOException;
    }
    
    private String openLocation;
    private Writer w;
    private IWriterFactory factory;
    private volatile int openCount = 0;
    
    public WriterCache( IWriterFactory wf )
    {
        if (wf == null)
        {
            throw new IllegalArgumentException( "factory cannot be null" );
        }
        this.factory = wf;
    }
    
    public void writeToFile( String location, String text )
        throws IOException
    {
        if (location == null) return;
        if (!location.equals( this.openLocation ))
        {
            if (this.w != null)
            {
                this.openCount = this.openCount - 1;
                this.w.close();
            }
            ++this.openCount;
            this.w = this.factory.createWriter( location );
        }
        this.w.write( text );
    }
    
    public int getNumberWritersOpen()
    {
        return this.openCount;
    }
}
-->
<PRE CLASS="JavaCode"><SPAN CLASS="gutter">   1:</SPAN><SPAN CLASS="syntax7">import</SPAN><SPAN CLASS="syntax125"> </SPAN>java.io.*;
<SPAN CLASS="gutter">   2:</SPAN><SPAN CLASS="syntax6">public</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax8">class</SPAN><SPAN CLASS="syntax125"> </SPAN>WriterCache
<SPAN CLASS="gutter">   3:</SPAN><SPAN CLASS="syntax11">{</SPAN>
<SPAN CLASS="gutter">   4:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax6">public</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax6">static</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax8">interface</SPAN><SPAN CLASS="syntax125"> </SPAN>IWriterFactory
<SPAN CLASS="gutterH">   5:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax11">{</SPAN>
<SPAN CLASS="gutter">   6:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax6">public</SPAN><SPAN CLASS="syntax125"> </SPAN>Writer<SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax9">createWriter</SPAN>(<SPAN CLASS="syntax125"> </SPAN>String<SPAN CLASS="syntax125"> </SPAN>location<SPAN CLASS="syntax125"> </SPAN>)
<SPAN CLASS="gutter">   7:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax6">throws</SPAN><SPAN CLASS="syntax125"> </SPAN>IOException;
<SPAN CLASS="gutter">   8:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax11">}</SPAN>
<SPAN CLASS="gutter">   9:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN>
<SPAN CLASS="gutterH">  10:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax6">private</SPAN><SPAN CLASS="syntax125"> </SPAN>String<SPAN CLASS="syntax125"> </SPAN>openLocation;
<SPAN CLASS="gutter">  11:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax6">private</SPAN><SPAN CLASS="syntax125"> </SPAN>Writer<SPAN CLASS="syntax125"> </SPAN>w;
<SPAN CLASS="gutter">  12:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax6">private</SPAN><SPAN CLASS="syntax125"> </SPAN>IWriterFactory<SPAN CLASS="syntax125"> </SPAN>factory;
<SPAN CLASS="gutter">  13:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax6">private volatile</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax8">int</SPAN><SPAN CLASS="syntax125"> </SPAN>openCount<SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax11">=</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax12">0</SPAN>;
<SPAN CLASS="gutter">  14:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN>
<SPAN CLASS="gutterH">  15:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax6">public</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax9">WriterCache</SPAN>(<SPAN CLASS="syntax125"> </SPAN>IWriterFactory<SPAN CLASS="syntax125"> </SPAN>wf<SPAN CLASS="syntax125"> </SPAN>)
<SPAN CLASS="gutter">  16:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax11">{</SPAN>
<SPAN CLASS="gutter">  17:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax6">if</SPAN><SPAN CLASS="syntax125"> </SPAN>(wf<SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax11">=</SPAN><SPAN CLASS="syntax11">=</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax4">null</SPAN>)
<SPAN CLASS="gutter">  18:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax11">{</SPAN>
<SPAN CLASS="gutter">  19:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax6">throw</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax6">new</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax9">IllegalArgumentException</SPAN>(<SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax3">&quot;</SPAN><SPAN CLASS="syntax3">factory</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax3">cannot</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax3">be</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax3">null</SPAN><SPAN CLASS="syntax3">&quot;</SPAN><SPAN CLASS="syntax125"> </SPAN>);
<SPAN CLASS="gutterH">  20:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax11">}</SPAN>
<SPAN CLASS="gutter">  21:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax4">this</SPAN>.factory<SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax11">=</SPAN><SPAN CLASS="syntax125"> </SPAN>wf;
<SPAN CLASS="gutter">  22:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax11">}</SPAN>
<SPAN CLASS="gutter">  23:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN>
<SPAN CLASS="gutter">  24:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax6">public</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax8">void</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax9">writeToFile</SPAN>(<SPAN CLASS="syntax125"> </SPAN>String<SPAN CLASS="syntax125"> </SPAN>location,<SPAN CLASS="syntax125"> </SPAN>String<SPAN CLASS="syntax125"> </SPAN>text<SPAN CLASS="syntax125"> </SPAN>)
<SPAN CLASS="gutterH">  25:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax6">throws</SPAN><SPAN CLASS="syntax125"> </SPAN>IOException
<SPAN CLASS="gutter">  26:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax11">{</SPAN>
<SPAN CLASS="gutter">  27:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax6">if</SPAN><SPAN CLASS="syntax125"> </SPAN>(location<SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax11">=</SPAN><SPAN CLASS="syntax11">=</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax4">null</SPAN>)<SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax6">return</SPAN>;
<SPAN CLASS="gutter">  28:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax6">if</SPAN><SPAN CLASS="syntax125"> </SPAN>(<SPAN CLASS="syntax11">!</SPAN>location.<SPAN CLASS="syntax9">equals</SPAN>(<SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax4">this</SPAN>.openLocation<SPAN CLASS="syntax125"> </SPAN>))
<SPAN CLASS="gutter">  29:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax11">{</SPAN>
<SPAN CLASS="gutterH">  30:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax6">if</SPAN><SPAN CLASS="syntax125"> </SPAN>(<SPAN CLASS="syntax4">this</SPAN>.w<SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax11">!</SPAN><SPAN CLASS="syntax11">=</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax4">null</SPAN>)
<SPAN CLASS="gutter">  31:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax11">{</SPAN>
<SPAN CLASS="gutter">  32:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax11">-</SPAN><SPAN CLASS="syntax11">-</SPAN><SPAN CLASS="syntax4">this</SPAN>.openCount;
<SPAN CLASS="gutter">  33:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax4">this</SPAN>.w.<SPAN CLASS="syntax9">close</SPAN>();
<SPAN CLASS="gutter">  34:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax11">}</SPAN>
<SPAN CLASS="gutterH">  35:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax11">+</SPAN><SPAN CLASS="syntax11">+</SPAN><SPAN CLASS="syntax4">this</SPAN>.openCount;
<SPAN CLASS="gutter">  36:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax4">this</SPAN>.w<SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax11">=</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax4">this</SPAN>.factory.<SPAN CLASS="syntax9">createWriter</SPAN>(<SPAN CLASS="syntax125"> </SPAN>location<SPAN CLASS="syntax125"> </SPAN>);
<SPAN CLASS="gutter">  37:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax11">}</SPAN>
<SPAN CLASS="gutter">  38:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax4">this</SPAN>.w.<SPAN CLASS="syntax9">write</SPAN>(<SPAN CLASS="syntax125"> </SPAN>text<SPAN CLASS="syntax125"> </SPAN>);
<SPAN CLASS="gutter">  39:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax11">}</SPAN>
<SPAN CLASS="gutterH">  40:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN>
<SPAN CLASS="gutter">  41:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax6">public</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax8">int</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax9">getNumberWritersOpen</SPAN>()
<SPAN CLASS="gutter">  42:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax11">{</SPAN>
<SPAN CLASS="gutter">  43:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax6">return</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax4">this</SPAN>.openCount;
<SPAN CLASS="gutter">  44:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax11">}</SPAN>
<SPAN CLASS="gutterH">  45:</SPAN><SPAN CLASS="syntax11">}</SPAN>
</PRE>
Obviously, this class isn't very thread safe - <tt>w</tt> could easily be
closed in one thread when a thread execution switch causes another thread
to run its write statement on the closed writer, or even writing to a
different writer than what was created (this is data corruption, which is
worse than a raised exception).
</P>
<P>
<I>Note that this class is designed for testing: we can easily create streams in
our tests and note what gets written and to which stream.  This follows a
pattern of designing for mock objects.</I>
</P>

<section>Beginning the Unit Test Class</section>
<P>
The test class starts off with the standard naming conventions and constructor,
and all necessary imports:
<!--
import net.sourceforge.groboutils.junit.v1.MultiThreadedTestRunner;
import net.sourceforge.groboutils.junit.v1.TestRunnable;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import java.io.*;
import java.util.*;

public class WriterCacheUTest extends TestCase
{
    public WriterCacheUTest( String name )
    {
        super( name );
    }
-->
<PRE CLASS="JavaCode"><SPAN CLASS="gutter"> 1:</SPAN><SPAN CLASS="syntax7">import</SPAN><SPAN CLASS="syntax125"> </SPAN>net.sourceforge.groboutils.junit.v1.MultiThreadedTestRunner;
<SPAN CLASS="gutter"> 2:</SPAN><SPAN CLASS="syntax7">import</SPAN><SPAN CLASS="syntax125"> </SPAN>net.sourceforge.groboutils.junit.v1.TestRunnable;
<SPAN CLASS="gutter"> 3:</SPAN><SPAN CLASS="syntax7">import</SPAN><SPAN CLASS="syntax125"> </SPAN>junit.framework.TestCase;
<SPAN CLASS="gutter"> 4:</SPAN><SPAN CLASS="syntax7">import</SPAN><SPAN CLASS="syntax125"> </SPAN>junit.framework.TestSuite;
<SPAN CLASS="gutterH"> 5:</SPAN><SPAN CLASS="syntax7">import</SPAN><SPAN CLASS="syntax125"> </SPAN>java.io.*;
<SPAN CLASS="gutter"> 6:</SPAN><SPAN CLASS="syntax7">import</SPAN><SPAN CLASS="syntax125"> </SPAN>java.util.*;
<SPAN CLASS="gutter"> 7:</SPAN>
<SPAN CLASS="gutter"> 8:</SPAN><SPAN CLASS="syntax6">public</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax8">class</SPAN><SPAN CLASS="syntax125"> </SPAN>WriterCacheUTest<SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax6">extends</SPAN><SPAN CLASS="syntax125"> </SPAN>TestCase
<SPAN CLASS="gutter"> 9:</SPAN><SPAN CLASS="syntax11">{</SPAN>
<SPAN CLASS="gutterH">10:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax6">public</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax9">WriterCacheUTest</SPAN>(<SPAN CLASS="syntax125"> </SPAN>String<SPAN CLASS="syntax125"> </SPAN>name<SPAN CLASS="syntax125"> </SPAN>)
<SPAN CLASS="gutter">11:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax11">{</SPAN>
<SPAN CLASS="gutter">12:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax4">super</SPAN>(<SPAN CLASS="syntax125"> </SPAN>name<SPAN CLASS="syntax125"> </SPAN>);
<SPAN CLASS="gutter">13:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax11">}</SPAN>
</PRE>
Next, we need a way to archive the <tt>WriterCache</tt> created streams in our
own <tt>IWriterFactory</tt>:
<!--
    public static class MyWriterFactory implements WriterCache.IWriterFactory
    {
        Hashtable nameToStream = new Hashtable();
        public Writer createWriter( String location )
            throws IOException
        {
            StringWriter sw = (StringWriter)nameToStream.get( location );
            if (sw == null)
            {
                sw = new StringWriter();
                nameToStream.put( location, sw );
            }
            return sw;
        }
    }
-->
<PRE CLASS="JavaCode"><SPAN CLASS="gutterH">15:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax6">public</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax6">static</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax8">class</SPAN><SPAN CLASS="syntax125"> </SPAN>MyWriterFactory<SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax6">implements</SPAN><SPAN CLASS="syntax125"> </SPAN>WriterCache.IWriterFactory
<SPAN CLASS="gutter">16:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax11">{</SPAN>
<SPAN CLASS="gutter">17:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN>Hashtable<SPAN CLASS="syntax125"> </SPAN>nameToStream<SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax11">=</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax6">new</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax9">Hashtable</SPAN>();
<SPAN CLASS="gutter">18:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax6">public</SPAN><SPAN CLASS="syntax125"> </SPAN>Writer<SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax9">createWriter</SPAN>(<SPAN CLASS="syntax125"> </SPAN>String<SPAN CLASS="syntax125"> </SPAN>location<SPAN CLASS="syntax125"> </SPAN>)
<SPAN CLASS="gutter">19:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax6">throws</SPAN><SPAN CLASS="syntax125"> </SPAN>IOException
<SPAN CLASS="gutterH">20:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax11">{</SPAN>
<SPAN CLASS="gutter">21:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN>StringWriter<SPAN CLASS="syntax125"> </SPAN>sw<SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax11">=</SPAN><SPAN CLASS="syntax125"> </SPAN>(StringWriter)nameToStream.<SPAN CLASS="syntax9">get</SPAN>(<SPAN CLASS="syntax125"> </SPAN>location<SPAN CLASS="syntax125"> </SPAN>);
<SPAN CLASS="gutter">22:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax6">if</SPAN><SPAN CLASS="syntax125"> </SPAN>(sw<SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax11">=</SPAN><SPAN CLASS="syntax11">=</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax4">null</SPAN>)
<SPAN CLASS="gutter">23:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax11">{</SPAN>
<SPAN CLASS="gutter">24:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN>sw<SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax11">=</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax6">new</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax9">StringWriter</SPAN>();
<SPAN CLASS="gutterH">25:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN>nameToStream.<SPAN CLASS="syntax9">put</SPAN>(<SPAN CLASS="syntax125"> </SPAN>location,<SPAN CLASS="syntax125"> </SPAN>sw<SPAN CLASS="syntax125"> </SPAN>);
<SPAN CLASS="gutter">26:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax11">}</SPAN>
<SPAN CLASS="gutter">27:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax6">return</SPAN><SPAN CLASS="syntax125"> </SPAN>sw;
<SPAN CLASS="gutter">28:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax11">}</SPAN>
<SPAN CLASS="gutter">29:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax11">}</SPAN>
<SPAN CLASS="gutterH">30:</SPAN><SPAN CLASS="syntax11">}</SPAN>
</PRE>
It uses <tt>StringWriter</tt> instances so that the tests can examine what
what written to which stream.
</P>
<P>
Ignoring the standard non-threaded tests for such a class (for brevity's sake),
we move onto the threaded tests.  But before we can write the tests, we need to
devise the different tests that we can perform.
    <OL>
        <LI>
        One thread can write to one location with a known text pattern, while
        another writes to a different location with a different text pattern.
        The test would check to make sure that each location contains only
        the expected text pattern, and exactly the number of text patterns
        actually written. Let's call this test "TextPattern"
        </LI>
        <LI>
        Use lots of writing threads (say 10), but this time we check for the
        number of opened writers to ensure it never becomes negative, and
        generate some kind of warning if it is not 0 or 1.  Let's call this
        test "OpenWriterCount".
        </LI>
    </OL>
</P>
<P>
<I>Note: since this class uses a <tt>StringWriter</tt> instance as the
returned <tt>Writer</tt>, the <tt>WriterCache</tt> calls to <tt>close()</tt>
on the instance will be ignored, as this is the documented behavior of the
<tt>StringWriter</tt> class.  Thus, the same <tt>StringWriter</tt> instance
can be sent to the <tt>WriterCache</tt> over and over.</I>
</P>

<section>Creating a TestRunnable</section>
<P>
In order to support the above tests, we'll need a way to generate the text
pattern for a given location, a given number of times.  This is where the
GroboUtil extension for multi-threaded testing comes into play.  We create
another inner class to generate the <tt>WriterCache</tt> calls by extending
the <tt>TestRunnable</tt> class:
<!--
    
    static class WriteText extends TestRunnable
    {
        private WriterCache wc;
        private int count;
        private String textPattern;
        private String location;
        private int sleepTime;
        public WriteText( WriterCache wc, int count, String pattern,
            String loc, int delay )
        {
            this.wc = wc;
            this.count = count;
            this.textPattern = pattern;
            this.location = loc;
            this.sleepTime = delay;
        }
        
        public void runTest() throws Throwable
        {
            for (int i = 0; i < this.count; ++i)
            {
                Thread.sleep( this.sleepTime );
                this.wc.writeToFile( this.location, this.textPattern );
            }
        }
    }
-->
<PRE CLASS="JavaCode"><SPAN CLASS="gutter">31:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax6">static</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax8">class</SPAN><SPAN CLASS="syntax125"> </SPAN>WriteText<SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax6">extends</SPAN><SPAN CLASS="syntax125"> </SPAN>TestRunnable
<SPAN CLASS="gutter">32:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax11">{</SPAN>
<SPAN CLASS="gutter">33:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax6">private</SPAN><SPAN CLASS="syntax125"> </SPAN>WriterCache<SPAN CLASS="syntax125"> </SPAN>wc;
<SPAN CLASS="gutter">34:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax6">private</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax8">int</SPAN><SPAN CLASS="syntax125"> </SPAN>count;
<SPAN CLASS="gutterH">35:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax6">private</SPAN><SPAN CLASS="syntax125"> </SPAN>String<SPAN CLASS="syntax125"> </SPAN>textPattern;
<SPAN CLASS="gutter">36:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax6">private</SPAN><SPAN CLASS="syntax125"> </SPAN>String<SPAN CLASS="syntax125"> </SPAN>location;
<SPAN CLASS="gutter">37:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax6">private</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax8">int</SPAN><SPAN CLASS="syntax125"> </SPAN>sleepTime;
<SPAN CLASS="gutter">38:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax6">public</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax9">WriteText</SPAN>(<SPAN CLASS="syntax125"> </SPAN>WriterCache<SPAN CLASS="syntax125"> </SPAN>wc,<SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax8">int</SPAN><SPAN CLASS="syntax125"> </SPAN>count,<SPAN CLASS="syntax125"> </SPAN>String<SPAN CLASS="syntax125"> </SPAN>pattern,
<SPAN CLASS="gutter">39:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN>String<SPAN CLASS="syntax125"> </SPAN>loc,<SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax8">int</SPAN><SPAN CLASS="syntax125"> </SPAN>delay<SPAN CLASS="syntax125"> </SPAN>)
<SPAN CLASS="gutterH">40:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax11">{</SPAN>
<SPAN CLASS="gutter">41:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax4">this</SPAN>.wc<SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax11">=</SPAN><SPAN CLASS="syntax125"> </SPAN>wc;
<SPAN CLASS="gutter">42:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax4">this</SPAN>.count<SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax11">=</SPAN><SPAN CLASS="syntax125"> </SPAN>count;
<SPAN CLASS="gutter">43:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax4">this</SPAN>.textPattern<SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax11">=</SPAN><SPAN CLASS="syntax125"> </SPAN>pattern;
<SPAN CLASS="gutter">44:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax4">this</SPAN>.location<SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax11">=</SPAN><SPAN CLASS="syntax125"> </SPAN>loc;
<SPAN CLASS="gutterH">45:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax4">this</SPAN>.sleepTime<SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax11">=</SPAN><SPAN CLASS="syntax125"> </SPAN>delay;
<SPAN CLASS="gutter">46:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax11">}</SPAN>
<SPAN CLASS="gutter">47:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN>
<SPAN CLASS="gutter">48:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax6">public</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax8">void</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax9">runTest</SPAN>()<SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax6">throws</SPAN><SPAN CLASS="syntax125"> </SPAN>Throwable
<SPAN CLASS="gutter">49:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax11">{</SPAN>
<SPAN CLASS="gutterH">50:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax6">for</SPAN><SPAN CLASS="syntax125"> </SPAN>(<SPAN CLASS="syntax8">int</SPAN><SPAN CLASS="syntax125"> </SPAN>i<SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax11">=</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax12">0</SPAN>;<SPAN CLASS="syntax125"> </SPAN>i<SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax11">&lt;</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax4">this</SPAN>.count;<SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax11">+</SPAN><SPAN CLASS="syntax11">+</SPAN>i)
<SPAN CLASS="gutter">51:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax11">{</SPAN>
<SPAN CLASS="gutter">52:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN>Thread.<SPAN CLASS="syntax9">sleep</SPAN>(<SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax4">this</SPAN>.sleepTime<SPAN CLASS="syntax125"> </SPAN>);
<SPAN CLASS="gutter">53:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax4">this</SPAN>.wc.<SPAN CLASS="syntax9">writeToFile</SPAN>(<SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax4">this</SPAN>.location,<SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax4">this</SPAN>.textPattern<SPAN CLASS="syntax125"> </SPAN>);
<SPAN CLASS="gutter">54:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax11">}</SPAN>
<SPAN CLASS="gutterH">55:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax11">}</SPAN>
<SPAN CLASS="gutter">56:</SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax125"> </SPAN><SPAN CLASS="syntax11">}</SPAN>
<SPAN CLASS="gutter">57:</SPAN><SPAN CLASS="syntax11">}</SPAN>
</PRE>
The <tt>void runTest()</tt> method must be implemented by concrete subclasses of
<tt>TestRunnable</tt>: this is the equivalent of the standard Java interface
<tt>Runnable</tt>'s <tt>void run()</tt> method, but properly wrapped for
testing.
</P>

<section>Running Several Tasks In Parallel</section>
<P>
This allows us to begin writing the test for "TextPattern".  This test says that
we need to write to two different streams with different text in parallel.
Then, we need to ensure that the correct amount of data was written to each,
and that no inappropriate data was written.  In this test, we'll have one
<tt>WriteText</tt> instance write to location "0" with text "0" (we'll call this
thread 0), and another write to location "1" with text "1" (we'll call this
thread 1).  To vary things up, thread 0 will write 10 times with a 50
millisecond wait, and thread 1 will write 12 times with a 20 millisecond wait.
The test ends up looking like:
<!--
    public void testTextPattern() throws Throwable
    {
        MyWriterFactor mwf = new MyWriterFactory();
        WriterCache wc = new WriterCache( mwf );
        TestRunnable tcs[] = {
                new WriteText( wc, 10, "0", "0", 50 ),
                new WriteText( wc, 12, "1", "1", 20 )
            };
        MultiThreadedTestRunner mttr =
            new MultiThreadedTestRunner( tcs );
        mttr.runTestRunnables( 2 * 60 * 1000 );
        String s0 = mwf.nameToStream.get( "0" ).toString();
        String s1 = mwf.nameToStream.get( "1" ).toString();
        assertEquals( "Data corruption: stream 0", 10,
            s0.length() );
        assertEquals( "Data corruption: stream 1", 12,
            s1.length() );
        assertTrue( "Incorrect data written to stream 0.",
            s0.indexOf( '1' ) < 0 );
        assertTrue( "Incorrect data written to stream 1.",
            s1.indexOf( '0' ) < 0 );
    }
-->
<PRE CLASS="JavaCode"><SPAN CLASS="gutter">59:</SPAN>    <SPAN CLASS="syntax6">public</SPAN> <SPAN CLASS="syntax10">void</SPAN> <SPAN CLASS="syntax6">testTextPattern</SPAN>() <SPAN CLASS="syntax8">throws</SPAN> Throwable
<SPAN CLASS="gutterH">60:</SPAN>    <SPAN CLASS="syntax18">{</SPAN>
<SPAN CLASS="gutter">61:</SPAN>        MyWriterFactor mwf <SPAN CLASS="syntax18">=</SPAN> <SPAN CLASS="syntax8">new</SPAN> <SPAN CLASS="syntax6">MyWriterFactory</SPAN>();
<SPAN CLASS="gutter">62:</SPAN>        WriterCache wc <SPAN CLASS="syntax18">=</SPAN> <SPAN CLASS="syntax8">new</SPAN> <SPAN CLASS="syntax6">WriterCache</SPAN>( mwf );
<SPAN CLASS="gutter">63:</SPAN>        TestRunnable tcs[] <SPAN CLASS="syntax18">=</SPAN> <SPAN CLASS="syntax18">{</SPAN>
<SPAN CLASS="gutter">64:</SPAN>                <SPAN CLASS="syntax8">new</SPAN> <SPAN CLASS="syntax6">WriteText</SPAN>( wc, <SPAN CLASS="syntax5">10</SPAN>, <SPAN CLASS="syntax13">&quot;</SPAN><SPAN CLASS="syntax13">0</SPAN><SPAN CLASS="syntax13">&quot;</SPAN>, <SPAN CLASS="syntax13">&quot;</SPAN><SPAN CLASS="syntax13">0</SPAN><SPAN CLASS="syntax13">&quot;</SPAN>, <SPAN CLASS="syntax5">50</SPAN> ),
<SPAN CLASS="gutterH">65:</SPAN>                <SPAN CLASS="syntax8">new</SPAN> <SPAN CLASS="syntax6">WriteText</SPAN>( wc, <SPAN CLASS="syntax5">12</SPAN>, <SPAN CLASS="syntax13">&quot;</SPAN><SPAN CLASS="syntax13">1</SPAN><SPAN CLASS="syntax13">&quot;</SPAN>, <SPAN CLASS="syntax13">&quot;</SPAN><SPAN CLASS="syntax13">1</SPAN><SPAN CLASS="syntax13">&quot;</SPAN>, <SPAN CLASS="syntax5">20</SPAN> )
<SPAN CLASS="gutter">66:</SPAN>            <SPAN CLASS="syntax18">}</SPAN>;
<SPAN CLASS="gutter">67:</SPAN>        MultiThreadedTestRunner mttr <SPAN CLASS="syntax18">=</SPAN>
<SPAN CLASS="gutter">68:</SPAN>            <SPAN CLASS="syntax8">new</SPAN> <SPAN CLASS="syntax6">MultiThreadedTestRunner</SPAN>( tcs );
<SPAN CLASS="gutter">69:</SPAN>        mttr.<SPAN CLASS="syntax6">runTestRunnables</SPAN>( <SPAN CLASS="syntax5">2</SPAN> <SPAN CLASS="syntax18">*</SPAN> <SPAN CLASS="syntax5">60</SPAN> <SPAN CLASS="syntax18">*</SPAN> <SPAN CLASS="syntax5">1000</SPAN> );
<SPAN CLASS="gutterH">70:</SPAN>        String s0 <SPAN CLASS="syntax18">=</SPAN> mwf.nameToStream.<SPAN CLASS="syntax6">get</SPAN>( <SPAN CLASS="syntax13">&quot;</SPAN><SPAN CLASS="syntax13">0</SPAN><SPAN CLASS="syntax13">&quot;</SPAN> ).<SPAN CLASS="syntax6">toString</SPAN>();
<SPAN CLASS="gutter">71:</SPAN>        String s1 <SPAN CLASS="syntax18">=</SPAN> mwf.nameToStream.<SPAN CLASS="syntax6">get</SPAN>( <SPAN CLASS="syntax13">&quot;</SPAN><SPAN CLASS="syntax13">1</SPAN><SPAN CLASS="syntax13">&quot;</SPAN> ).<SPAN CLASS="syntax6">toString</SPAN>();
<SPAN CLASS="gutter">72:</SPAN>        <SPAN CLASS="syntax6">assertEquals</SPAN>( <SPAN CLASS="syntax13">&quot;</SPAN><SPAN CLASS="syntax13">Data</SPAN><SPAN CLASS="syntax13"> </SPAN><SPAN CLASS="syntax13">corruption:</SPAN><SPAN CLASS="syntax13"> </SPAN><SPAN CLASS="syntax13">stream</SPAN><SPAN CLASS="syntax13"> </SPAN><SPAN CLASS="syntax13">0</SPAN><SPAN CLASS="syntax13">&quot;</SPAN>, <SPAN CLASS="syntax5">10</SPAN>,
<SPAN CLASS="gutter">73:</SPAN>            s0.<SPAN CLASS="syntax6">length</SPAN>() );
<SPAN CLASS="gutter">74:</SPAN>        <SPAN CLASS="syntax6">assertEquals</SPAN>( <SPAN CLASS="syntax13">&quot;</SPAN><SPAN CLASS="syntax13">Data</SPAN><SPAN CLASS="syntax13"> </SPAN><SPAN CLASS="syntax13">corruption:</SPAN><SPAN CLASS="syntax13"> </SPAN><SPAN CLASS="syntax13">stream</SPAN><SPAN CLASS="syntax13"> </SPAN><SPAN CLASS="syntax13">1</SPAN><SPAN CLASS="syntax13">&quot;</SPAN>, <SPAN CLASS="syntax5">12</SPAN>,
<SPAN CLASS="gutterH">75:</SPAN>            s1.<SPAN CLASS="syntax6">length</SPAN>() );
<SPAN CLASS="gutter">76:</SPAN>        <SPAN CLASS="syntax6">assertTrue</SPAN>( <SPAN CLASS="syntax13">&quot;</SPAN><SPAN CLASS="syntax13">Incorrect</SPAN><SPAN CLASS="syntax13"> </SPAN><SPAN CLASS="syntax13">data</SPAN><SPAN CLASS="syntax13"> </SPAN><SPAN CLASS="syntax13">written</SPAN><SPAN CLASS="syntax13"> </SPAN><SPAN CLASS="syntax13">to</SPAN><SPAN CLASS="syntax13"> </SPAN><SPAN CLASS="syntax13">stream</SPAN><SPAN CLASS="syntax13"> </SPAN><SPAN CLASS="syntax13">0.</SPAN><SPAN CLASS="syntax13">&quot;</SPAN>,
<SPAN CLASS="gutter">77:</SPAN>            s0.<SPAN CLASS="syntax6">indexOf</SPAN>( <SPAN CLASS="syntax13">'</SPAN><SPAN CLASS="syntax13">1</SPAN><SPAN CLASS="syntax13">'</SPAN> ) <SPAN CLASS="syntax18">&lt;</SPAN> <SPAN CLASS="syntax5">0</SPAN> );
<SPAN CLASS="gutter">78:</SPAN>        <SPAN CLASS="syntax6">assertTrue</SPAN>( <SPAN CLASS="syntax13">&quot;</SPAN><SPAN CLASS="syntax13">Incorrect</SPAN><SPAN CLASS="syntax13"> </SPAN><SPAN CLASS="syntax13">data</SPAN><SPAN CLASS="syntax13"> </SPAN><SPAN CLASS="syntax13">written</SPAN><SPAN CLASS="syntax13"> </SPAN><SPAN CLASS="syntax13">to</SPAN><SPAN CLASS="syntax13"> </SPAN><SPAN CLASS="syntax13">stream</SPAN><SPAN CLASS="syntax13"> </SPAN><SPAN CLASS="syntax13">1.</SPAN><SPAN CLASS="syntax13">&quot;</SPAN>,
<SPAN CLASS="gutter">79:</SPAN>            s1.<SPAN CLASS="syntax6">indexOf</SPAN>( <SPAN CLASS="syntax13">'</SPAN><SPAN CLASS="syntax13">0</SPAN><SPAN CLASS="syntax13">'</SPAN> ) <SPAN CLASS="syntax18">&lt;</SPAN> <SPAN CLASS="syntax5">0</SPAN> );
<SPAN CLASS="gutterH">80:</SPAN>    <SPAN CLASS="syntax18">}</SPAN>
<SPAN CLASS="gutter">81:</SPAN>
</PRE>
Lines 61-62 initialize the <tt>WriterCache</tt> instance with our test factory.
Line 63 creates our list of parallel tasks to execute, which are instances of
our <tt>WriteText</tt> class above.
</P>
<P>
Line 64 creates an instance of the
<tt>MultiThreadedTestRunner</tt> class, a utility class which handles the
creation, execution, and termination of the threads which run the
<tt>TestRunnable</tt> instances.  Line 65 invokes the utility method to run the
tasks.  The argument to this method specifies the maximum amount
of time (in milliseconds) to let the threads run before killing them and marking
the execution as a failure (through <tt>junit.famework.Assert</tt>).  If any
of the <tt>TestRunnable</tt> instances dies due to an exception (including an
Assert failure), then all of the running threads are terminated, and the
<tt>runTestRunnables</tt> method rethrows the underlying exception.
</P>
<P>
The remainder of the test ensures that only the correct data was placed where
it was intended to go.
</P>

<section>Parallel Monitoring of Threaded Access</section>
<P>
Now we can move on to the OpenWriterCount test.  As of GroboTestingJUnit
version 1.2.0, there is a very simple way to add a thread that monitors the
status of the common object while other threads manipulate its state.
</P>
<P>
The monitors are in a separate group from the runners, as the monitors are
intended to loop over a set of checks until all the runners have completed.
Before this functionality was added, the monitor runners had to communicate
with the other runners to learn when the runners were finished.  Now, the
<tt>MultiThreadedTestRunner</tt> class handles this communication.
</P>
<P>
We create another runnable class, but this time subclassing from
<tt>TestMonitorRunnable</tt>.  This frees the new class from having to perform
the proper looping and detection of when the runners have completed.
In order to use this added functionality, the subclasses overload the
<tt>runMonitor()</tt> method instead of the <tt>runTest()</tt> method.
<!--
    public static class WriterCountMonitor extends TestMonitorRunnable
    {
        private WriterCache wc;
        public WriterCountMonitor( WriterCache wc )
        {
            this.wc = wc;
        }
        
        public void runMonitor() throws Throwable
        {
            int open = this.wc.getNumberWritersOpen();
            assertTrue(
                "Invalid number of open writers.",
                open == 0 || open == 1 );
        }
    }
-->
<PRE CLASS="JavaCode"><SPAN CLASS="gutter">83:</SPAN>    <SPAN CLASS="syntax8">public</SPAN> <SPAN CLASS="syntax8">static</SPAN> <SPAN CLASS="syntax10">class</SPAN> WriterCountMonitor <SPAN CLASS="syntax8">extends</SPAN> TestMonitorRunnable
<SPAN CLASS="gutter">84:</SPAN>    <SPAN CLASS="syntax18">{</SPAN>
<SPAN CLASS="gutterH">85:</SPAN>        <SPAN CLASS="syntax8">private</SPAN> WriterCache wc;
<SPAN CLASS="gutter">86:</SPAN>        <SPAN CLASS="syntax8">public</SPAN> <SPAN CLASS="syntax6">WriterCountMonitor</SPAN>( WriterCache wc )
<SPAN CLASS="gutter">87:</SPAN>        <SPAN CLASS="syntax18">{</SPAN>
<SPAN CLASS="gutter">88:</SPAN>            <SPAN CLASS="syntax14">this</SPAN>.wc <SPAN CLASS="syntax18">=</SPAN> wc;
<SPAN CLASS="gutter">89:</SPAN>        <SPAN CLASS="syntax18">}</SPAN>
<SPAN CLASS="gutterH">90:</SPAN>        
<SPAN CLASS="gutter">91:</SPAN>        <SPAN CLASS="syntax8">public</SPAN> <SPAN CLASS="syntax10">void</SPAN> <SPAN CLASS="syntax6">runMonitor</SPAN>() <SPAN CLASS="syntax8">throws</SPAN> Throwable
<SPAN CLASS="gutter">92:</SPAN>        <SPAN CLASS="syntax18">{</SPAN>
<SPAN CLASS="gutter">93:</SPAN>            <SPAN CLASS="syntax10">int</SPAN> open <SPAN CLASS="syntax18">=</SPAN> <SPAN CLASS="syntax14">this</SPAN>.wc.<SPAN CLASS="syntax6">getNumberWritersOpen</SPAN>();
<SPAN CLASS="gutter">94:</SPAN>            <SPAN CLASS="syntax6">assertTrue</SPAN>(
<SPAN CLASS="gutterH">95:</SPAN>                <SPAN CLASS="syntax13">&quot;</SPAN><SPAN CLASS="syntax13">Invalid</SPAN><SPAN CLASS="syntax13"> </SPAN><SPAN CLASS="syntax13">number</SPAN><SPAN CLASS="syntax13"> </SPAN><SPAN CLASS="syntax13">of</SPAN><SPAN CLASS="syntax13"> </SPAN><SPAN CLASS="syntax13">open</SPAN><SPAN CLASS="syntax13"> </SPAN><SPAN CLASS="syntax13">writers.</SPAN><SPAN CLASS="syntax13">&quot;</SPAN>,
<SPAN CLASS="gutter">96:</SPAN>                open <SPAN CLASS="syntax18">=</SPAN><SPAN CLASS="syntax18">=</SPAN> <SPAN CLASS="syntax5">0</SPAN> <SPAN CLASS="syntax18">|</SPAN><SPAN CLASS="syntax18">|</SPAN> open <SPAN CLASS="syntax18">=</SPAN><SPAN CLASS="syntax18">=</SPAN> <SPAN CLASS="syntax5">1</SPAN> );
<SPAN CLASS="gutter">97:</SPAN>        <SPAN CLASS="syntax18">}</SPAN>
<SPAN CLASS="gutter">98:</SPAN>    <SPAN CLASS="syntax18">}</SPAN>
<SPAN CLASS="gutter">99:</SPAN>
</PRE>
</P>
<P>
The monitors are added to the <tt>MultiThreadedTestRunner</tt> through
a second argument in another constructor.  For our test, in order to have
high confidence that threading errors causing the writer open count to
be invalid (not 1 or 0), we need to have a sufficient number of threaded
access on the object-under-test.  So, we generate 30 runners to iterate
500 times each over the writer.
<!--
    public void testOpenWriterCount() throws Throwable
    {
        int runnerCount = 30;
        int iterations = 500;
        MyWriterFactor mwf = new MyWriterFactory();
        WriterCache wc = new WriterCache( mwf );
        TestRunnable tcs[] = new TestRunnable[ runnerCount ];
        for (int i = 0; i < runnerCount; ++i)
        {
            tcs[i] = new WriteText( wc, 500, ""+(char)i,
                ""+(char)i, 50 );
        }
        TestRunnable monitors[] = {
                new WriterCountMonitor( wc );
            };
        MultiThreadedTestRunner mttr =
            new MultiThreadedTestRunner( tcs, monitors );
        mttr.runTestRunnables( 10 * 60 * 1000 );
        
        // verify streams
        for (int i = 0; i < runnerCount; ++i)
        {
            String s = mwf.nameToString.get( ""+(char)i ).toString();
            assertEquals( "Data corruption: stream "+i,
                500, s.length() );
        }
    }
-->
<PRE CLASS="JavaCode"><SPAN CLASS="gutter">101:</SPAN>    <SPAN CLASS="syntax8">public</SPAN> <SPAN CLASS="syntax10">void</SPAN> <SPAN CLASS="syntax6">testOpenWriterCount</SPAN>() <SPAN CLASS="syntax8">throws</SPAN> Throwable
<SPAN CLASS="gutter">102:</SPAN>    <SPAN CLASS="syntax18">{</SPAN>
<SPAN CLASS="gutter">103:</SPAN>        <SPAN CLASS="syntax10">int</SPAN> runnerCount <SPAN CLASS="syntax18">=</SPAN> <SPAN CLASS="syntax5">30</SPAN>;
<SPAN CLASS="gutter">104:</SPAN>        <SPAN CLASS="syntax10">int</SPAN> iterations <SPAN CLASS="syntax18">=</SPAN> <SPAN CLASS="syntax5">500</SPAN>;
<SPAN CLASS="gutterH">105:</SPAN>        MyWriterFactor mwf <SPAN CLASS="syntax18">=</SPAN> <SPAN CLASS="syntax8">new</SPAN> <SPAN CLASS="syntax6">MyWriterFactory</SPAN>();
<SPAN CLASS="gutter">106:</SPAN>        WriterCache wc <SPAN CLASS="syntax18">=</SPAN> <SPAN CLASS="syntax8">new</SPAN> <SPAN CLASS="syntax6">WriterCache</SPAN>( mwf );
<SPAN CLASS="gutter">107:</SPAN>        TestRunnable tcs[] <SPAN CLASS="syntax18">=</SPAN> <SPAN CLASS="syntax8">new</SPAN> TestRunnable[ runnerCount ];
<SPAN CLASS="gutter">108:</SPAN>        <SPAN CLASS="syntax8">for</SPAN> (<SPAN CLASS="syntax10">int</SPAN> i <SPAN CLASS="syntax18">=</SPAN> <SPAN CLASS="syntax5">0</SPAN>; i <SPAN CLASS="syntax18">&lt;</SPAN> runnerCount; <SPAN CLASS="syntax18">+</SPAN><SPAN CLASS="syntax18">+</SPAN>i)
<SPAN CLASS="gutter">109:</SPAN>        <SPAN CLASS="syntax18">{</SPAN>
<SPAN CLASS="gutterH">110:</SPAN>            tcs[i] <SPAN CLASS="syntax18">=</SPAN> <SPAN CLASS="syntax8">new</SPAN> <SPAN CLASS="syntax6">WriteText</SPAN>( wc, <SPAN CLASS="syntax5">500</SPAN>, <SPAN CLASS="syntax13">&quot;</SPAN><SPAN CLASS="syntax13">&quot;</SPAN><SPAN CLASS="syntax18">+</SPAN>(<SPAN CLASS="syntax10">char</SPAN>)i,
<SPAN CLASS="gutter">111:</SPAN>                <SPAN CLASS="syntax13">&quot;</SPAN><SPAN CLASS="syntax13">&quot;</SPAN><SPAN CLASS="syntax18">+</SPAN>(<SPAN CLASS="syntax10">char</SPAN>)i, <SPAN CLASS="syntax5">50</SPAN> );
<SPAN CLASS="gutter">112:</SPAN>        <SPAN CLASS="syntax18">}</SPAN>
<SPAN CLASS="gutter">113:</SPAN>        TestRunnable monitors[] <SPAN CLASS="syntax18">=</SPAN> <SPAN CLASS="syntax18">{</SPAN>
<SPAN CLASS="gutter">114:</SPAN>                <SPAN CLASS="syntax8">new</SPAN> <SPAN CLASS="syntax6">WriterCountMonitor</SPAN>( wc );
<SPAN CLASS="gutterH">115:</SPAN>            <SPAN CLASS="syntax18">}</SPAN>;
<SPAN CLASS="gutter">116:</SPAN>        MultiThreadedTestRunner mttr <SPAN CLASS="syntax18">=</SPAN>
<SPAN CLASS="gutter">117:</SPAN>            <SPAN CLASS="syntax8">new</SPAN> <SPAN CLASS="syntax6">MultiThreadedTestRunner</SPAN>( tcs, monitors );
<SPAN CLASS="gutter">118:</SPAN>        mttr.<SPAN CLASS="syntax6">runTestRunnables</SPAN>( <SPAN CLASS="syntax5">10</SPAN> <SPAN CLASS="syntax18">*</SPAN> <SPAN CLASS="syntax5">60</SPAN> <SPAN CLASS="syntax18">*</SPAN> <SPAN CLASS="syntax5">1000</SPAN> );
<SPAN CLASS="gutter">119:</SPAN>        
<SPAN CLASS="gutterH">120:</SPAN>        <SPAN CLASS="syntax2">//</SPAN><SPAN CLASS="syntax2"> </SPAN><SPAN CLASS="syntax2">verify</SPAN><SPAN CLASS="syntax2"> </SPAN><SPAN CLASS="syntax2">streams</SPAN>
<SPAN CLASS="gutter">121:</SPAN>        <SPAN CLASS="syntax8">for</SPAN> (<SPAN CLASS="syntax10">int</SPAN> i <SPAN CLASS="syntax18">=</SPAN> <SPAN CLASS="syntax5">0</SPAN>; i <SPAN CLASS="syntax18">&lt;</SPAN> runnerCount; <SPAN CLASS="syntax18">+</SPAN><SPAN CLASS="syntax18">+</SPAN>i)
<SPAN CLASS="gutter">122:</SPAN>        <SPAN CLASS="syntax18">{</SPAN>
<SPAN CLASS="gutter">123:</SPAN>            String s <SPAN CLASS="syntax18">=</SPAN> mwf.nameToString.<SPAN CLASS="syntax6">get</SPAN>( <SPAN CLASS="syntax13">&quot;</SPAN><SPAN CLASS="syntax13">&quot;</SPAN><SPAN CLASS="syntax18">+</SPAN>(<SPAN CLASS="syntax10">char</SPAN>)i ).<SPAN CLASS="syntax6">toString</SPAN>();
<SPAN CLASS="gutter">124:</SPAN>            <SPAN CLASS="syntax6">assertEquals</SPAN>( <SPAN CLASS="syntax13">&quot;</SPAN><SPAN CLASS="syntax13">Data</SPAN><SPAN CLASS="syntax13"> </SPAN><SPAN CLASS="syntax13">corruption:</SPAN><SPAN CLASS="syntax13"> </SPAN><SPAN CLASS="syntax13">stream</SPAN><SPAN CLASS="syntax13"> </SPAN><SPAN CLASS="syntax13">&quot;</SPAN><SPAN CLASS="syntax18">+</SPAN>i,
<SPAN CLASS="gutterH">125:</SPAN>                <SPAN CLASS="syntax5">500</SPAN>, s.<SPAN CLASS="syntax6">length</SPAN>() );
<SPAN CLASS="gutter">126:</SPAN>        <SPAN CLASS="syntax18">}</SPAN>
<SPAN CLASS="gutter">127:</SPAN>    <SPAN CLASS="syntax18">}</SPAN>
<SPAN CLASS="gutter">128:</SPAN>
</PRE>
</P>
<P>
So, while this test runs the 30 runners, each writing 500 times, the
monitor runs concurrently, analyzing the status of the <tt>WriterCache</tt>
instance to ensure its integrety.
</P>
<P>
As one would expect, both of these tests expose serious synchronization
flaws in the <tt>WriterCache</tt> implementation.
</P>

<section>Things To Look Out For</section>
<P>
This package isn't without its pitfalls.  Here's a checklist to review
to ensure that your tests comply with the MTTR caveats.
<OL>
    <LI>
    Never directly run <tt>TestRunnable</tt> instances.  They are designed
    to only be run in threads generated by the <tt>runTestRunnables()</tt>
    method inside the <tt>MultiThreadedTestRunner</tt> class.
    </LI>
    <LI>
    The <tt>TestRunnable</tt> subclasses need to have their
    <tt>runTest()</tt> methods be watchful of <tt>InterruptedException</tt>
    and <tt>Thread.currentThread().isInterrupted()</tt>.  The
    <tt>MultiThreadedTestRunner</tt> prematurely halts the runner threads
    by calling <tt>Thread.interrupt()</tt> on them.  If the threads don't
    terminate themselves within a certain time limit, then
    <tt>MultiThreadedTestRunner</tt> will perform the dangerous
    <tt>Thread.stop()</tt> operation, in order to prevent threads from
    going rogue.  (Future versions may allow the <tt>Thread.stop()</tt>
    call to be disabled.)
    </LI>
</OL>
</P>

<section>Alternatives</section>
<P>
One alternative design to this approach is Greg Vaughn's <tt>TestDecorator</tt>
approach, which is located <a href=
"http://sourceforge.net/tracker/index.php?func=detail&amp;aid=535058&amp;group_id=15278&amp;atid=315278"
>here</a>.  However, it forces the decorated Test instance to create the
threads itself, and does nothing to manage runaway threads.
</P>
</body>
</document>

