FitNesse integration

Micah Martin <[email protected]> Wed, 20 Aug 2003 15:12:58 -0500
Newsgroups gmane.comp.programming.tools.fit.devel
Message-ID <AF07776DB5D1D211BC0700A0C9E9237FAFC6E2@objectmentor>
Code is worth a thousand words.

This code should express my intent.  
Using the Listener idea Ward suggested I implemented the chunking ability
(in Java).  This required only minor changes to the Fixture class; although
I did rip out the inner Counts class and put it in it's own file.  Two new
classes for this are FixtureListener (Interface) and NullFixtureListener
which is used by default in Fixture

I also wrote a new implementation of FitFilter.  I call it FitServer though
becuase thats what it feels like.  It will handle multiple test executions
and represents all the functionallity I've mentioned to integrate with
FitNesse. 

Of course I'm interested in comments.

Micah

Here is the FitServer code... the rest of the source is available at
http://fitnesse.org/files/fitSource.zip.  (The attachment was too big to
post)

public class FitServer
{
	public static final DecimalFormat format = new
DecimalFormat("0000000000");

	public String input;
	public Parse tables;
	public Fixture fixture = new Fixture();
	Counts counts = new Counts();

	public static void main(String argv[])
	{
		new FitServer().run(argv);
	}

	public void run(String argv[])
	{
		args(argv);
		process();
		exit();
	}

	public void process()
	{
		fixture.listner = new TablePrintingFixtureListener();
		try
		{
			int size = 1;
			while( (size = readSize()) != 0)
			{
				String document = readDocument(size);
				tables = new Parse(document);
				newFixture().doTables(tables);
				counts.tally(fixture.counts);
			}
		}
		catch(Exception e)
		{
			exception(e);
		}
	}

	private Fixture newFixture()
	{
		fixture = new Fixture();
		fixture.listner = new TablePrintingFixtureListener();
		return fixture;
	}

	public void args(String[] argv)
	{
		if(argv.length != 0)
		{
			System.err.println("usage: java fit.FitServer");
			System.exit(-1);
		}
	}

	private int readSize() throws Exception
	{
		byte[] bytes = new byte[10];
		System.in.read(bytes);
    Number n = format.parse(new String(bytes));
		return n.intValue();
	}

	private String readDocument(int size) throws Exception
	{
		byte[] bytes = new byte[size];
		System.in.read(bytes);
		return new String(bytes);
	}

	protected void exception(Exception e)
	{
		tables = new Parse("body", "Unable to parse input. Input
ignored.", null, null);
		fixture.exception(tables, e);
	}

	protected void exit()
	{
		System.exit(counts.wrong + counts.exceptions);
	}

	class TablePrintingFixtureListener implements FixtureListener
	{
		public void tableFinished(Parse table)
		{
			try
			{
				ByteArrayOutputStream byteBuffer = new
ByteArrayOutputStream();
				PrintWriter writer = new
PrintWriter(byteBuffer);
				Parse more = table.more;
				table.more = null;
				if(table.trailer == null)
					table.trailer = "";
				table.print(writer);
				table.more = more;
				writer.close();

				byte[] bytes = byteBuffer.toByteArray();
	
System.out.write(format.format(bytes.length).getBytes());
				System.out.write(bytes);
				System.out.flush();
			}
			catch(IOException e)
			{
				e.printStackTrace();
			}
		}

		public void tablesFinished(Counts count)
		{
			try
			{
	
System.out.write(format.format(0).getBytes());
	
System.out.write(format.format(count.right).getBytes());
	
System.out.write(format.format(count.wrong).getBytes());
	
System.out.write(format.format(count.ignores).getBytes());
	
System.out.write(format.format(count.exceptions).getBytes());
				System.out.flush();
			}
			catch(IOException e)
			{
				e.printStackTrace();
			}
		}
	}
}
   

-------------------------------------------------------------------
Micah Martin  -  [email protected]
Software Developer  -  Object Mentor, Inc.
www.objectmentor.com
 



-------------------------------------------------------------------
Micah Martin  -  [email protected]
Software Developer  -  Object Mentor, Inc.
www.objectmentor.com