devdaily home | apple | java | perl | unix | directory | blog

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

package tests;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import junit.framework.TestCase;

import org.stringtree.factory.Fetcher;
import org.stringtree.factory.StringFetcher;
import org.stringtree.factory.memory.MapStringRepository;
import org.stringtree.factory.memory.MapTractRepository;
import org.stringtree.juicer.JuicerConvertHelper;
import org.stringtree.juicer.formatter.ExternalFormatter;
import org.stringtree.juicer.string.IgnoreCommentLineStringFilter;
import org.stringtree.juicer.string.JoinLinesStringFilter;
import org.stringtree.juicer.string.SplitLinesStringFilter;
import org.stringtree.juicer.string.StringFilter;
import org.stringtree.juicer.string.StringPipeline;
import org.stringtree.juicer.string.StringStringSource;
import org.stringtree.juicer.tract.JoinTractFilter;
import org.stringtree.juicer.tract.ReplaceTokenTractFilter;
import org.stringtree.juicer.tract.StringTractSource;
import org.stringtree.juicer.tract.TokenFinderTractFilter;
import org.stringtree.juicer.tract.TractFilter;
import org.stringtree.juicer.tract.TractFilterStringFetcher;
import org.stringtree.juicer.tract.TractPipeline;
import org.stringtree.util.FileReadingUtils;
import org.stringtree.util.FileWritingUtils;
import org.stringtree.util.StringUtils;
import org.stringtree.util.tract.Tract;

import wiki.WikiFormatterContext;

public class UserLevelTest 
	extends TestCase
{
	public void testIgnoreComments()
	{
		StringPipeline tube = new StringPipeline(new StringFilter[]
		{
			new SplitLinesStringFilter(),
			new IgnoreCommentLineStringFilter(),
			new JoinLinesStringFilter()
		});
		tube.connectSource(new StringStringSource("hello\n#there\n \nworld\n"));

		assertEquals("strip comment lines", "hello\nworld\n", tube.nextString());
	}

	private TractPipeline helloWorldPipeline()
	{
		Map map = new HashMap();
		map.put("hello", "world");
		
		TractPipeline tube = new TractPipeline(new TractFilter[]
		{
			new TokenFinderTractFilter(),
			new ReplaceTokenTractFilter(map),
			new JoinTractFilter()
		});
		return tube;
	}
	
	public void testReplaceTokens()
	{
		TractPipeline tube = helloWorldPipeline();

		tube.connectSource(new StringTractSource(" @hello@\n#there\n \nworld\n"));

		assertEquals("replace tokens", " world\n#there\n \nworld\n", JuicerConvertHelper.nextString(tube));
	}
	
	public void testPipelineStringFactory()
	{
		TractPipeline tube = helloWorldPipeline();

		StringFetcher fac = new TractFilterStringFetcher(tube);
		String result = fac.get(" @hello@\n#there\n \nworld\n");

		assertEquals("pipeline", " world\n#there\n \nworld\n", result);
	}
	
	public void testWikiTransform()
		throws IOException
	{
		MapTractRepository pages = new MapTractRepository();
		pages.put("FrontPage", new Tract("xx"));
		pages.put("MonospaceIndentationProblem", new Tract("yy"));
		
		MapStringRepository remotes = new MapStringRepository();
		remotes.put("Wiki", "http://c2.com/cgi/wiki?");
		remotes.put("MeatBall", "http://www.usemod.com/cgi-bin/mb.pl?");
		
		Fetcher context = new WikiFormatterContext(pages, remotes); 

		compareWiki(new ExternalFormatter(FileReadingUtils.readFile("wiki.transform"), context), "file");
	}
	
	private void compareWiki(StringFetcher wiki, String title)
		throws IOException
	{
		String input = FileReadingUtils.readFile("wiki-before.txt");
		assertFalse("wiki example page input is blank", StringUtils.isBlank(input));
		String expected = FileReadingUtils.readFile("wiki-after.html");
		assertFalse("wiki example page expected output is blank", StringUtils.isBlank(expected));
		String actual = wiki.get(input);
		assertFalse("wiki example page actual output is blank", StringUtils.isBlank(actual));

FileWritingUtils.writeFile(title + "-actual.html", actual);
		assertEquals("wiki page example", expected, actual);
	}
}




Copyright 1998-2008 Alvin Alexander
All Rights Reserved.
 
devdaily.com is based in louisville, kentucky, and this web site is hosted by godaddy.com