/*
 * refactoring example, unit tests
 * http://www.informit.com/articles/article.aspx?p=167891&seqNum=1
 * "What Is Refactoring?" by William C. Wake.
 *
 */

import java.io.*;
import junit.framework.*;

public class CodeReplacerTest extends TestCase {
  final static String templateContents = 
      "xxx%CODE%yyy%ALTCODE%zzz\n";

  CodeReplacer replacer;

  public CodeReplacerTest(String testName)
    {super(testName);}

  protected void setUp() {
    try {
      replacer = new CodeReplacer(new StringReader(templateContents));
    } catch (Exception ex) {
      fail("CodeReplacer couldn't load");
    }
  }

  public void testTemplateLoadedProperly() {
    assertEquals(templateContents,
		 replacer.sourceTemplate);
  }

  public void testSubstitution() {
    StringWriter stringOut = new StringWriter();
    PrintWriter testOut = new PrintWriter(stringOut);
    String trackingId = "01234567";
        
    try {
      replacer.substitute(trackingId, testOut);
      testOut.close();
    } catch (IOException ex) {
      fail("testSubstitution exception - " + ex);
    }

    assertEquals("xxx01234567yyy01234-567zzz\n",
		 stringOut.toString());
  }

}
