/*
 * Created on 9-Apr-2005
*/
import junit.framework.TestCase;
import edu.uiuc.webcoord.*;

import java.io.*;

/**
 * @author Marc Gagnon
 */
public class TestFetchHTML extends TestCase {

	public static void main(String[] args) {
	}

	/*
	 * @see TestCase#setUp()
	 */
	protected void setUp() throws Exception {
		super.setUp();
	}

	/*
	 * @see TestCase#tearDown()
	 */
	protected void tearDown() throws Exception {
		super.tearDown();
	}

	/**
	 * Constructor for TestFetchHTML.
	 * @param arg0
	 */
	public TestFetchHTML(String arg0) {
		super(arg0);
	}

	public void testGetHTMLStringValidURL() {
		String strHTMLTest = "";
		
		strHTMLTest = FetchHTML.getHTMLString("http://www.uiuc.edu");
		assertTrue(strHTMLTest.contains("<title>University of Illinois"));
	}
	
	public void testGetHTMLStringMalformedURL() {
		String strHTMLTest = "";
		
		PrintStream console = System.out;
		ByteArrayOutputStream arrayOut = new ByteArrayOutputStream();
		PrintStream consolePrint = new PrintStream(arrayOut);
		
		System.setOut(consolePrint);
		
		strHTMLTest = FetchHTML.getHTMLString("www.uiuc.ca");
		assertTrue(arrayOut.toString().contains("URL malformed"));
		
		System.setOut(console);
	}
	
	public void testGetHTMLStringUnknownHost() {
		String strHTMLTest = "";
		
		PrintStream console = System.out;
		ByteArrayOutputStream arrayOut = new ByteArrayOutputStream();
		PrintStream consolePrint = new PrintStream(arrayOut);
		
		System.setOut(consolePrint);
		
		strHTMLTest = FetchHTML.getHTMLString("http://www.plo.ca");
		assertTrue(arrayOut.toString().contains("Could not connect to host"));
		
		System.setOut(console);
	}
	
	public void testMainEmptyArg() {
		PrintStream console = System.out;
		
		ByteArrayOutputStream arrayOut = new ByteArrayOutputStream();
		PrintStream consolePrint = new PrintStream(arrayOut);
		
		System.setOut(consolePrint);
		
		String[] arguments = {""};
		
		FetchHTML.main(arguments);
		assertTrue(arrayOut.toString().contains("Usage"));
	
		System.setOut(console);
	}
	
	public void testMainTooManyArg() {
		PrintStream console = System.out;
		
		ByteArrayOutputStream arrayOut = new ByteArrayOutputStream();
		PrintStream consolePrint = new PrintStream(arrayOut);
		
		System.setOut(consolePrint);
		
		String[] arguments = {"-F","-U","-F"};
		
		FetchHTML.main(arguments);
		assertTrue(arrayOut.toString().contains("Usage"));
	
		System.setOut(console);
	}
	
	public void testMainBadOption() {
		PrintStream console = System.out;
		
		ByteArrayOutputStream arrayOut = new ByteArrayOutputStream();
		PrintStream consolePrint = new PrintStream(arrayOut);
		
		System.setOut(consolePrint);
		
		String[] arguments = {"-K","test"};
		
		FetchHTML.main(arguments);
		assertTrue(arrayOut.toString().contains("Usage"));
	
		System.setOut(console);
	}
	
	public void testMainValidFile() {
		PrintStream console = System.out;
		
		ByteArrayOutputStream arrayOut = new ByteArrayOutputStream();
		PrintStream consolePrint = new PrintStream(arrayOut);
		
		System.setOut(consolePrint);
		
		String[] arguments = {"-F","lib/doc/banking.html"};
		
		FetchHTML.main(arguments);
		assertTrue(arrayOut.toString().contains("Fetching html from file"));
	
		System.setOut(console);
	}
	
	public void testMainInvalidFile() {
		PrintStream console = System.out;
		
		ByteArrayOutputStream arrayOut = new ByteArrayOutputStream();
		PrintStream consolePrint = new PrintStream(arrayOut);
		
		System.setOut(consolePrint);
		
		String[] arguments = {"-F","lib/doc/banking.htmly"};
		
		FetchHTML.main(arguments);
		assertTrue(arrayOut.toString().contains("The input file lib/doc/banking.htmly does not exist"));
	
		System.setOut(console);
	}
	
	public void testNoBody() {
		PrintStream console = System.out;
		
		ByteArrayOutputStream arrayOut = new ByteArrayOutputStream();
		PrintStream consolePrint = new PrintStream(arrayOut);
		
		System.setOut(consolePrint);
		
		HTMLDocument doc = HTMLParser.parseHTML("<html><head><title>test</title></head></html>");
		
		assertTrue(arrayOut.toString().contains("Body tag not found"));
	
		System.setOut(console);
	}
}
