FileDocCategorySizeDatePackage
LinkTestSuite.javaAPI DocExample6851Wed Feb 18 10:12:50 GMT 2004com.develop.ss

LinkTestSuite

public class LinkTestSuite extends TestSuite

Fields Summary
com.meterware.httpunit.WebConversation
conversation
Set
linksAlreadyFollowed
Set
linksNotFollowed
Set
linkPrefixesToFollow
HashSet
linkPrefixesToAvoid
private String
linksNotFollowedOutputFileName
private int
maxLinks
private IndexWriter
writer
private String
initialLink
private static String[]
EMPTY_STRINGARRAY
Constructors Summary
public LinkTestSuite(String indexPath, int maxLinks, String skippedLinksOutputFileName)

    this.maxLinks = maxLinks;
    this.linksNotFollowedOutputFileName = skippedLinksOutputFileName;
    writer = new IndexWriter(indexPath, new StandardAnalyzer(), true);
  
Methods Summary
public voidavoidPrefixes(java.lang.String[] prefixesToAvoid)

    for (int i = 0; i < prefixesToAvoid.length; i++) {
      String s = prefixesToAvoid[i];
      LinkTest.log.info("Avoid links prefixed with " + s);
      linkPrefixesToAvoid.add(new URL(s));
    }
  
voidconsiderNewLink(java.lang.String linkFrom, com.meterware.httpunit.WebLink newLink)

    URL url = null;
    url = newLink.getRequest().getURL();
    if (shouldFollowLink(url)) {
      if (linksAlreadyFollowed.add(url.toExternalForm())) {
        if (linksAlreadyFollowed.size() > maxLinks) {
          linksAlreadyFollowed.remove(url.toExternalForm());
          throw new Error("Max links exceeded " + maxLinks);
        }
        if (shouldNotFollowLink(url)) {
          LinkTest.log.info("Not following " + url.toExternalForm() + " from " + linkFrom);
        } else {
          LinkTest.log.info("Following " + url.toExternalForm() + " from " + linkFrom);
          addTest(new LinkTest(url.toString(),conversation, this));
        }
      }
    } else {
      ignoreLink(url, linkFrom);
    }
  
public voidexecute()

        junit.textui.TestRunner.run(this);
    
public voidfollowPrefixes(java.lang.String[] prefixesToFollow)

    for (int i = 0; i < prefixesToFollow.length; i++) {
      String s = prefixesToFollow[i];
      LinkTest.log.info("Following links prefixed with " + s);
      linkPrefixesToFollow.add(new URL(s));
    }
  
public java.lang.StringgetInitialLink()


     
    return initialLink;
  
public org.apache.lucene.index.IndexWritergetWriter()

    return writer;
  
private voidignoreLink(java.net.URL url, java.lang.String linkFrom)

    String status = "Ignoring " + url.toExternalForm() + " from " + linkFrom;
    linksNotFollowed.add(status);
    LinkTest.log.fine(status);
  
public voidinitAvoidPrefixesFromSystemProperties()

    String avoidPrefixes = System.getProperty("com.develop.ss.AvoidLinks");
    if (avoidPrefixes == null || avoidPrefixes.length() == 0) return;
    String[] prefixes = avoidPrefixes.split(" ");
    if (prefixes != null && prefixes.length != 0) {
      avoidPrefixes(prefixes);
    }
  
public voidinitFollowPrefixesFromSystemProperties()

    String followPrefixes = System.getProperty("com.develop.ss.FollowLinks");
    if (followPrefixes == null || followPrefixes.length() == 0) return;
    String[] prefixes = followPrefixes.split(" ");
    if (prefixes != null && prefixes.length != 0) {
      followPrefixes(prefixes);
    }
  
private booleanmatchesDownToPathPrefix(java.net.URL matchBase, java.net.URL newLink)

    return matchBase.getHost().equals(newLink.getHost()) &&
              matchBase.getPort() == newLink.getPort() &&
              matchBase.getProtocol().equals(newLink.getProtocol()) &&
              newLink.getPath().startsWith(matchBase.getPath());
  
public voidrun(junit.framework.TestResult result)

    super.run(result);
    try {
      writeStringSet(linksNotFollowedOutputFileName, linksNotFollowed);
    } catch (Exception e) {
      e.printStackTrace();
    }
    try {
      writer.optimize();
      writer.close();
    } catch (Exception e) {
      e.printStackTrace();
    }
  
public voidsetInitialLink(java.lang.String initialLink)

    if ((initialLink == null) || (initialLink.length() == 0)) {
      throw new Error("Must specify a non-null initialLink");
    }
    linkPrefixesToFollow.add(new URL(initialLink));
    this.initialLink = initialLink;
    LinkTest.log.info("Initial link is " + initialLink);
    addTest(new LinkTest(initialLink,conversation,this));
  
booleanshouldFollowLink(java.net.URL newLink)

    for (Iterator iterator = linkPrefixesToFollow.iterator(); iterator.hasNext();) {
      URL u = (URL) iterator.next();
      if (matchesDownToPathPrefix(u, newLink)) {
        return true;
      }
    }
    return false;
  
booleanshouldNotFollowLink(java.net.URL newLink)

    for (Iterator iterator = linkPrefixesToAvoid.iterator(); iterator.hasNext();) {
      URL u = (URL) iterator.next();
      if (matchesDownToPathPrefix(u, newLink)) {
        return true;
      }
    }
    return false;
  
public static junit.framework.Testsuite()


        
    String indexPath = System.getProperty("com.develop.ss.Index", "index.1");
    String initialLink = System.getProperty("com.develop.ss.InitialLink");
    int maxLinks = Integer.MAX_VALUE;
    String maxLinksString = System.getProperty("com.develop.ss.MaxLinks");
    if (maxLinksString != null) {
      maxLinks = Integer.parseInt(maxLinksString);
    }
    LinkTestSuite suite = new LinkTestSuite(indexPath, maxLinks, "linksNotFollowed.txt");
    suite.setInitialLink(initialLink);
    suite.initFollowPrefixesFromSystemProperties();
    suite.initAvoidPrefixesFromSystemProperties();
    return suite;
  
private voidwriteStringSet(java.lang.String s, java.util.Set skippedPaths)

    FileOutputStream fos = new FileOutputStream(s);
    PrintStream ps = new PrintStream(fos);
    Iterator it = skippedPaths.iterator();
    while (it.hasNext()) {
      ps.println(it.next());
    }
    fos.flush();
    fos.close();