FileDocCategorySizeDatePackage
LinkTest.javaAPI DocExample2303Wed Feb 18 10:12:50 GMT 2004com.develop.ss

LinkTest.java

package com.develop.ss;

import junit.framework.TestCase;
import junit.framework.TestSuite;

import java.util.Set;
import java.util.HashSet;
import java.util.logging.Logger;
import java.util.logging.LogManager;
import java.util.logging.Level;
import java.io.IOException;

import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebResponse;
import com.meterware.httpunit.WebLink;
import com.meterware.httpunit.HttpNotFoundException;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.demo.html.HTMLParser;
import org.xml.sax.SAXException;

public class LinkTest extends TestCase {
  private WebConversation conversation;
  private LinkTestSuite suite;
  static Logger log = Logger.getLogger("com.develop.ss");

  public LinkTest(String name, WebConversation conversation, LinkTestSuite suite) {
    super(name);
    if ((name == null) || (conversation == null) || (suite == null)) {
      throw new IllegalArgumentException("LinkTest constructor requires non-null args");
    }
    this.conversation = conversation;
    this.suite = suite;
  }

  protected void runTest() throws Exception {
    WebResponse response = null;
    try {
      response = conversation.getResponse(getName());
    } catch (HttpNotFoundException hnfe) {
      fail("HTTP " + hnfe.getResponseCode() + " " + getName());
    }
    if (!isHTML(response)) {
      return;
    }
    addToIndex(response);
    WebLink[] links = response.getLinks();
    for (int i = 0; i < links.length; i++) {
      WebLink link = links[i];
      suite.considerNewLink(getName(), link);
    }

  }

  private void addToIndex(WebResponse response) throws SAXException, IOException, InterruptedException {
    Document d = new Document();
    HTMLParser parser = new HTMLParser(response.getInputStream());
    d.add(Field.UnIndexed("url", response.getURL().toExternalForm()));
    d.add(Field.UnIndexed("summary", parser.getSummary()));
    d.add(Field.Text("title", parser.getTitle()));
    d.add(Field.Text("contents", parser.getReader()));
    suite.getWriter().addDocument(d);
  }

  private boolean isHTML(WebResponse response) {
    return response.getContentType().equals("text/html");
  }


}