RobotsTXTLineTest.java
上传用户:qing5858
上传日期:2015-10-27
资源大小:6056k
文件大小:6k
源码类别:

搜索引擎

开发平台:

Java

  1. package net.javacoding.jspider.core.util.html;
  2. import junit.framework.TestCase;
  3. import java.net.URL;
  4. /**
  5.  * $Id: RobotsTXTLineTest.java,v 1.1 2003/02/11 17:33:04 vanrogu Exp $
  6.  */
  7. public class RobotsTXTLineTest extends TestCase {
  8.     public RobotsTXTLineTest ( ) {
  9.         super ( "RobotsTXTLineTest" );
  10.     }
  11.     public void testSimpleAllow ( ) {
  12.         RobotsTXTLine l = RobotsTXTLine.parse("allow: index.html");
  13.         assertNotNull("line returned for correct info is null", l);
  14.         String resource = l.getResourceURI();
  15.         int type = l.getType ( );
  16.         assertEquals("type parsed incorrect", RobotsTXTLine.ROBOTSTXT_RULE_ALLOW,  type);
  17.         assertEquals("resourceURI parsed incorrect", "index.html", resource);
  18.     }
  19.     public void testSimpleAllowCaseSensitivity ( ) {
  20.         RobotsTXTLine l = RobotsTXTLine.parse("AlLoW: index.html");
  21.         assertNotNull("line returned for correct info is null", l);
  22.         String resource = l.getResourceURI();
  23.         int type = l.getType ( );
  24.         assertEquals("type parsed incorrect", RobotsTXTLine.ROBOTSTXT_RULE_ALLOW,  type);
  25.         assertEquals("resourceURI parsed incorrect", "index.html", resource);
  26.     }
  27.     public void testSimpleDisallow ( ) {
  28.         RobotsTXTLine l = RobotsTXTLine.parse("disallow: index.html");
  29.         assertNotNull("line returned for correct info is null", l);
  30.         String resource = l.getResourceURI();
  31.         int type = l.getType ( );
  32.         assertEquals("type parsed incorrect", RobotsTXTLine.ROBOTSTXT_RULE_DISALLOW,  type);
  33.         assertEquals("resourceURI parsed incorrect", "index.html", resource);
  34.     }
  35.     public void testSimpleDisallowCaseSensitivity ( ) {
  36.         RobotsTXTLine l = RobotsTXTLine.parse("dIsAlLoW: index.html");
  37.         assertNotNull("line returned for correct info is null", l);
  38.         String resource = l.getResourceURI();
  39.         int type = l.getType ( );
  40.         assertEquals("type parsed incorrect", RobotsTXTLine.ROBOTSTXT_RULE_DISALLOW,  type);
  41.         assertEquals("resourceURI parsed incorrect", "index.html", resource);
  42.     }
  43.     public void testSimpleAllowWithoutSpace ( ) {
  44.         RobotsTXTLine l = RobotsTXTLine.parse("allow:index.html");
  45.         assertNotNull("line returned for correct info is null", l);
  46.         String resource = l.getResourceURI();
  47.         int type = l.getType ( );
  48.         assertEquals("type parsed incorrect", RobotsTXTLine.ROBOTSTXT_RULE_ALLOW,  type);
  49.         assertEquals("resourceURI parsed incorrect", "index.html", resource);
  50.     }
  51.     public void testSimpleDisallowWithoutSpace ( ) {
  52.         RobotsTXTLine l = RobotsTXTLine.parse("disallow:index.html");
  53.         assertNotNull("line returned for correct info is null", l);
  54.         String resource = l.getResourceURI();
  55.         int type = l.getType ( );
  56.         assertEquals("type parsed incorrect", RobotsTXTLine.ROBOTSTXT_RULE_DISALLOW,  type);
  57.         assertEquals("resourceURI parsed incorrect", "index.html", resource);
  58.     }
  59.     public void testErroneousAllowDisallow ( ) {
  60.         RobotsTXTLine l = RobotsTXTLine.parse("alow:index.html");
  61.         assertNull("line returned for incorrect info is not null", l);
  62.     }
  63.     public void testEmptyString ( ) {
  64.         RobotsTXTLine l = RobotsTXTLine.parse("");
  65.         assertNull("line returned for empty string is not null", l);
  66.     }
  67.     public void testOnlyAllow ( ) {
  68.         RobotsTXTLine l = RobotsTXTLine.parse("allow:");
  69.         assertNull("line returned for 'allow:' string is not null", l);
  70.     }
  71.     public void testOnlyDisAllow ( ) {
  72.         RobotsTXTLine l = RobotsTXTLine.parse("disallow:");
  73.         assertNull("line returned for 'disallow:' string is not null", l);
  74.     }
  75.     public void testNullString ( ) {
  76.         RobotsTXTLine l = RobotsTXTLine.parse(null);
  77.         assertNull("line returned for null string is not null", l);
  78.     }
  79.     public void testSimpleMatch ( ) throws Exception {
  80.         URL url = new URL ( "http://j-spider.sourceforge.net/index.html" );
  81.         RobotsTXTLine line = new RobotsTXTLine("/index.html", RobotsTXTLine.ROBOTSTXT_RULE_DISALLOW);
  82.         boolean matches = line.matches(url);
  83.         assertTrue ( "simple match didn't work", matches);
  84.     }
  85.     public void testSimpleMatchWithFolder ( ) throws Exception {
  86.         URL url = new URL ( "http://j-spider.sourceforge.net/manual/index.html" );
  87.         RobotsTXTLine line = new RobotsTXTLine("/manual", RobotsTXTLine.ROBOTSTXT_RULE_DISALLOW);
  88.         boolean matches = line.matches(url);
  89.         assertTrue ( "simple match didn't work", matches);
  90.     }
  91.     public void testSimpleMatchCaseSensitivity ( ) throws Exception {
  92.         URL url = new URL ( "http://j-spider.sourceforge.net/index.HTML" );
  93.         RobotsTXTLine line = new RobotsTXTLine("/index.html", RobotsTXTLine.ROBOTSTXT_RULE_DISALLOW);
  94.         boolean matches = line.matches(url);
  95.         assertFalse ( "cases were different, yet a match was given", matches);
  96.     }
  97.     public void testSimpleNoMatch ( ) throws Exception {
  98.         URL url = new URL ( "http://j-spider.sourceforge.net/index.htm" );
  99.         RobotsTXTLine line = new RobotsTXTLine("/index.html", RobotsTXTLine.ROBOTSTXT_RULE_DISALLOW);
  100.         boolean matches = line.matches(url);
  101.         assertFalse ( "simple nomatch didn't work", matches);
  102.     }
  103.     public void testRootMatch ( ) throws Exception {
  104.         URL url = new URL ( "http://j-spider.sourceforge.net" );
  105.         RobotsTXTLine line = new RobotsTXTLine("/", RobotsTXTLine.ROBOTSTXT_RULE_DISALLOW);
  106.         boolean matches = line.matches(url);
  107.         assertTrue ( "root match didn't work", matches);
  108.     }
  109.     public void testRootMatchWithTrailingSlash ( ) throws Exception {
  110.         URL url = new URL ( "http://j-spider.sourceforge.net/" );
  111.         RobotsTXTLine line = new RobotsTXTLine("/", RobotsTXTLine.ROBOTSTXT_RULE_DISALLOW);
  112.         boolean matches = line.matches(url);
  113.         assertTrue ( "root match didn't work", matches);
  114.     }
  115. }