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

搜索引擎

开发平台:

Java

  1. package net.javacoding.jspider.functional.specific.robotstxt;
  2. import junit.framework.TestCase;
  3. import net.javacoding.jspider.JSpider;
  4. import net.javacoding.jspider.functional.TestingConstants;
  5. import net.javacoding.jspider.core.util.config.ConfigurationFactory;
  6. import net.javacoding.jspider.core.util.config.JSpiderConfiguration;
  7. import net.javacoding.jspider.mockobjects.OverridingJSpiderConfiguration;
  8. import net.javacoding.jspider.mockobjects.OverridingPropertySet;
  9. import net.javacoding.jspider.mockobjects.plugin.JUnitEventSink;
  10. import java.net.URL;
  11. /**
  12.  * $Id: RobotsTXTTest.java,v 1.4 2003/04/10 16:19:24 vanrogu Exp $
  13.  */
  14. public class RobotsTXTTest extends TestCase {
  15.     protected JUnitEventSink sink;
  16.     protected JSpiderConfiguration config;
  17.     protected JSpiderConfiguration config2;
  18.     /**
  19.      * Public constructor giving a name to the test.
  20.      */
  21.     public RobotsTXTTest ( ) {
  22.         super ( "RobotsTXTTest ");
  23.     }
  24.     /**
  25.      * JUnit's overridden setUp method
  26.      * @throws java.lang.Exception in case something fails during setup
  27.      */
  28.     protected void setUp() throws Exception {
  29.         System.err.println("setUp");
  30.         config = ConfigurationFactory.getConfiguration(ConfigurationFactory.CONFIG_UNITTEST);
  31.         config2 = new OverridingJSpiderConfiguration ( config );
  32.         ((OverridingPropertySet)config2.getJSpiderConfiguration()).setValue("jspider.userAgent", "JSpiderUnitTest");
  33.         ConfigurationFactory.setConfiguration(config);
  34.         sink = JUnitEventSink.getInstance();
  35.     }
  36.     /**
  37.      * JUnit's overridden tearDown method
  38.      * @throws java.lang.Exception in case something fails during tearDown
  39.      */
  40.     protected void tearDown() throws Exception {
  41.         System.err.println("tearDown");
  42.         ConfigurationFactory.cleanConfiguration();
  43.         sink.reset();
  44.     }
  45.     public void testAllowedNormalUserAgent ( ) throws Exception {
  46.         URL url = new URL ( "http://" + TestingConstants.HOST + "/testcases/specific/robotstxt/resource.html" );
  47.         ConfigurationFactory.setConfiguration(config);
  48.         JSpider jspider = new JSpider ( url );
  49.         jspider.start ( );
  50.         testEventCount(net.javacoding.jspider.api.event.engine.SpideringStartedEvent.class,1);
  51.         testEventCount(net.javacoding.jspider.api.event.engine.SpideringSummaryEvent.class,1);
  52.         testEventCount(net.javacoding.jspider.api.event.engine.SpideringStoppedEvent.class,1);
  53.         testEventCount(net.javacoding.jspider.api.event.site.SiteDiscoveredEvent.class,1);
  54.         testEventCount(net.javacoding.jspider.api.event.site.RobotsTXTMissingEvent.class,0);
  55.         testEventCount(net.javacoding.jspider.api.event.site.RobotsTXTFetchedEvent.class,1);
  56.         testEventCount(net.javacoding.jspider.api.event.site.RobotsTXTFetchErrorEvent.class,0);
  57.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceDiscoveredEvent.class,2);
  58.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceFetchedEvent.class,2);
  59.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceFetchErrorEvent.class,0);
  60.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceForbiddenEvent.class,0);
  61.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceIgnoredForFetchingEvent.class,0);
  62.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceIgnoredForParsingEvent.class,0);
  63.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceReferenceDiscoveredEvent.class,0);
  64.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceParsedEvent.class,1);
  65.     }
  66.     public void testDisallowedResourceNormalUserAgent ( ) throws Exception {
  67.         URL url = new URL ( "http://" + TestingConstants.HOST + "/testcases/specific/robotstxt/disallowedResource1.html" );
  68.         ConfigurationFactory.setConfiguration(config);
  69.         JSpider jspider = new JSpider ( url );
  70.         jspider.start ( );
  71.         testEventCount(net.javacoding.jspider.api.event.engine.SpideringStartedEvent.class,1);
  72.         testEventCount(net.javacoding.jspider.api.event.engine.SpideringSummaryEvent.class,1);
  73.         testEventCount(net.javacoding.jspider.api.event.engine.SpideringStoppedEvent.class,1);
  74.         testEventCount(net.javacoding.jspider.api.event.site.SiteDiscoveredEvent.class,1);
  75.         testEventCount(net.javacoding.jspider.api.event.site.RobotsTXTMissingEvent.class,0);
  76.         testEventCount(net.javacoding.jspider.api.event.site.RobotsTXTFetchedEvent.class,1);
  77.         testEventCount(net.javacoding.jspider.api.event.site.RobotsTXTFetchErrorEvent.class,0);
  78.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceDiscoveredEvent.class,2);
  79.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceFetchedEvent.class,1);
  80.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceFetchErrorEvent.class,0);
  81.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceForbiddenEvent.class,1);
  82.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceIgnoredForFetchingEvent.class,0);
  83.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceIgnoredForParsingEvent.class,0);
  84.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceReferenceDiscoveredEvent.class,0);
  85.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceParsedEvent.class,0);
  86.     }
  87.     public void testDisallowedFolderNormalUserAgent ( ) throws Exception {
  88.         URL url = new URL ( "http://" + TestingConstants.HOST + "/testcases/specific/robotstxt/disallowedFolder1/resource.html" );
  89.         ConfigurationFactory.setConfiguration(config);
  90.         JSpider jspider = new JSpider ( url );
  91.         jspider.start ( );
  92.         testEventCount(net.javacoding.jspider.api.event.engine.SpideringStartedEvent.class,1);
  93.         testEventCount(net.javacoding.jspider.api.event.engine.SpideringSummaryEvent.class,1);
  94.         testEventCount(net.javacoding.jspider.api.event.engine.SpideringStoppedEvent.class,1);
  95.         testEventCount(net.javacoding.jspider.api.event.site.SiteDiscoveredEvent.class,1);
  96.         testEventCount(net.javacoding.jspider.api.event.site.RobotsTXTMissingEvent.class,0);
  97.         testEventCount(net.javacoding.jspider.api.event.site.RobotsTXTFetchedEvent.class,1);
  98.         testEventCount(net.javacoding.jspider.api.event.site.RobotsTXTFetchErrorEvent.class,0);
  99.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceDiscoveredEvent.class,2);
  100.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceFetchedEvent.class,1);
  101.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceFetchErrorEvent.class,0);
  102.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceForbiddenEvent.class,1);
  103.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceIgnoredForFetchingEvent.class,0);
  104.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceIgnoredForParsingEvent.class,0);
  105.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceReferenceDiscoveredEvent.class,0);
  106.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceParsedEvent.class,0);
  107.     }
  108.     public void testSometimesAllowedResourceNormalUserAgent ( ) throws Exception {
  109.         URL url = new URL ( "http://" + TestingConstants.HOST + "/testcases/specific/robotstxt/disallowedResource2.html" );
  110.         ConfigurationFactory.setConfiguration(config);
  111.         JSpider jspider = new JSpider ( url );
  112.         jspider.start ( );
  113.         testEventCount(net.javacoding.jspider.api.event.engine.SpideringStartedEvent.class,1);
  114.         testEventCount(net.javacoding.jspider.api.event.engine.SpideringSummaryEvent.class,1);
  115.         testEventCount(net.javacoding.jspider.api.event.engine.SpideringStoppedEvent.class,1);
  116.         testEventCount(net.javacoding.jspider.api.event.site.SiteDiscoveredEvent.class,1);
  117.         testEventCount(net.javacoding.jspider.api.event.site.RobotsTXTMissingEvent.class,0);
  118.         testEventCount(net.javacoding.jspider.api.event.site.RobotsTXTFetchedEvent.class,1);
  119.         testEventCount(net.javacoding.jspider.api.event.site.RobotsTXTFetchErrorEvent.class,0);
  120.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceDiscoveredEvent.class,2);
  121.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceFetchedEvent.class,2);
  122.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceFetchErrorEvent.class,0);
  123.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceForbiddenEvent.class,0);
  124.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceIgnoredForFetchingEvent.class,0);
  125.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceIgnoredForParsingEvent.class,0);
  126.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceReferenceDiscoveredEvent.class,0);
  127.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceParsedEvent.class,1);
  128.     }
  129.     public void testSometimesAllowedFolderNormalUserAgent ( ) throws Exception {
  130.         URL url = new URL ( "http://" + TestingConstants.HOST + "/testcases/specific/robotstxt/disallowedFolder2/resource.html" );
  131.         ConfigurationFactory.setConfiguration(config);
  132.         JSpider jspider = new JSpider ( url );
  133.         jspider.start ( );
  134.         testEventCount(net.javacoding.jspider.api.event.engine.SpideringStartedEvent.class,1);
  135.         testEventCount(net.javacoding.jspider.api.event.engine.SpideringSummaryEvent.class,1);
  136.         testEventCount(net.javacoding.jspider.api.event.engine.SpideringStoppedEvent.class,1);
  137.         testEventCount(net.javacoding.jspider.api.event.site.SiteDiscoveredEvent.class,1);
  138.         testEventCount(net.javacoding.jspider.api.event.site.RobotsTXTMissingEvent.class,0);
  139.         testEventCount(net.javacoding.jspider.api.event.site.RobotsTXTFetchedEvent.class,1);
  140.         testEventCount(net.javacoding.jspider.api.event.site.RobotsTXTFetchErrorEvent.class,0);
  141.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceDiscoveredEvent.class,2);
  142.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceFetchedEvent.class,2);
  143.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceFetchErrorEvent.class,0);
  144.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceForbiddenEvent.class,0);
  145.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceIgnoredForFetchingEvent.class,0);
  146.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceIgnoredForParsingEvent.class,0);
  147.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceReferenceDiscoveredEvent.class,0);
  148.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceParsedEvent.class,1);
  149.     }
  150.     public void testAllowedTestUserAgent ( ) throws Exception {
  151.         URL url = new URL ( "http://" + TestingConstants.HOST + "/testcases/specific/robotstxt/resource.html" );
  152.         ConfigurationFactory.setConfiguration(config2);
  153.         JSpider jspider = new JSpider ( url );
  154.         jspider.start ( );
  155.         testEventCount(net.javacoding.jspider.api.event.engine.SpideringStartedEvent.class,1);
  156.         testEventCount(net.javacoding.jspider.api.event.engine.SpideringSummaryEvent.class,1);
  157.         testEventCount(net.javacoding.jspider.api.event.engine.SpideringStoppedEvent.class,1);
  158.         testEventCount(net.javacoding.jspider.api.event.site.SiteDiscoveredEvent.class,1);
  159.         testEventCount(net.javacoding.jspider.api.event.site.RobotsTXTMissingEvent.class,0);
  160.         testEventCount(net.javacoding.jspider.api.event.site.RobotsTXTFetchedEvent.class,1);
  161.         testEventCount(net.javacoding.jspider.api.event.site.RobotsTXTFetchErrorEvent.class,0);
  162.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceDiscoveredEvent.class,2);
  163.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceFetchedEvent.class,2);
  164.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceFetchErrorEvent.class,0);
  165.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceForbiddenEvent.class,0);
  166.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceIgnoredForFetchingEvent.class,0);
  167.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceIgnoredForParsingEvent.class,0);
  168.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceReferenceDiscoveredEvent.class,0);
  169.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceParsedEvent.class,1);
  170.     }
  171.     public void testDisallowedResourceTestUserAgent ( ) throws Exception {
  172.         URL url = new URL ( "http://" + TestingConstants.HOST + "/testcases/specific/robotstxt/disallowedResource2.html" );
  173.         ConfigurationFactory.setConfiguration(config2);
  174.         JSpider jspider = new JSpider ( url );
  175.         jspider.start ( );
  176.         testEventCount(net.javacoding.jspider.api.event.engine.SpideringStartedEvent.class,1);
  177.         testEventCount(net.javacoding.jspider.api.event.engine.SpideringSummaryEvent.class,1);
  178.         testEventCount(net.javacoding.jspider.api.event.engine.SpideringStoppedEvent.class,1);
  179.         testEventCount(net.javacoding.jspider.api.event.site.SiteDiscoveredEvent.class,1);
  180.         testEventCount(net.javacoding.jspider.api.event.site.RobotsTXTMissingEvent.class,0);
  181.         testEventCount(net.javacoding.jspider.api.event.site.RobotsTXTFetchedEvent.class,1);
  182.         testEventCount(net.javacoding.jspider.api.event.site.RobotsTXTFetchErrorEvent.class,0);
  183.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceDiscoveredEvent.class,2);
  184.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceFetchedEvent.class,1);
  185.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceFetchErrorEvent.class,0);
  186.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceForbiddenEvent.class,1);
  187.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceIgnoredForFetchingEvent.class,0);
  188.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceIgnoredForParsingEvent.class,0);
  189.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceReferenceDiscoveredEvent.class,0);
  190.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceParsedEvent.class,0);
  191.     }
  192.     public void testDisallowedFolderTestUserAgent ( ) throws Exception {
  193.         URL url = new URL ( "http://" + TestingConstants.HOST + "/testcases/specific/robotstxt/disallowedFolder2/resource.html" );
  194.         ConfigurationFactory.setConfiguration(config2);
  195.         JSpider jspider = new JSpider ( url );
  196.         jspider.start ( );
  197.         testEventCount(net.javacoding.jspider.api.event.engine.SpideringStartedEvent.class,1);
  198.         testEventCount(net.javacoding.jspider.api.event.engine.SpideringSummaryEvent.class,1);
  199.         testEventCount(net.javacoding.jspider.api.event.engine.SpideringStoppedEvent.class,1);
  200.         testEventCount(net.javacoding.jspider.api.event.site.SiteDiscoveredEvent.class,1);
  201.         testEventCount(net.javacoding.jspider.api.event.site.RobotsTXTMissingEvent.class,0);
  202.         testEventCount(net.javacoding.jspider.api.event.site.RobotsTXTFetchedEvent.class,1);
  203.         testEventCount(net.javacoding.jspider.api.event.site.RobotsTXTFetchErrorEvent.class,0);
  204.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceDiscoveredEvent.class,2);
  205.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceFetchedEvent.class,1);
  206.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceFetchErrorEvent.class,0);
  207.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceForbiddenEvent.class,1);
  208.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceIgnoredForFetchingEvent.class,0);
  209.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceIgnoredForParsingEvent.class,0);
  210.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceReferenceDiscoveredEvent.class,0);
  211.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceParsedEvent.class,0);
  212.     }
  213.     public void testSometimesAllowedResourceTestUserAgent ( ) throws Exception {
  214.         URL url = new URL ( "http://" + TestingConstants.HOST + "/testcases/specific/robotstxt/disallowedResource1.html" );
  215.         ConfigurationFactory.setConfiguration(config2);
  216.         JSpider jspider = new JSpider ( url );
  217.         jspider.start ( );
  218.         testEventCount(net.javacoding.jspider.api.event.engine.SpideringStartedEvent.class,1);
  219.         testEventCount(net.javacoding.jspider.api.event.engine.SpideringSummaryEvent.class,1);
  220.         testEventCount(net.javacoding.jspider.api.event.engine.SpideringStoppedEvent.class,1);
  221.         testEventCount(net.javacoding.jspider.api.event.site.SiteDiscoveredEvent.class,1);
  222.         testEventCount(net.javacoding.jspider.api.event.site.RobotsTXTMissingEvent.class,0);
  223.         testEventCount(net.javacoding.jspider.api.event.site.RobotsTXTFetchedEvent.class,1);
  224.         testEventCount(net.javacoding.jspider.api.event.site.RobotsTXTFetchErrorEvent.class,0);
  225.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceDiscoveredEvent.class,2);
  226.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceFetchedEvent.class,2);
  227.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceFetchErrorEvent.class,0);
  228.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceForbiddenEvent.class,0);
  229.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceIgnoredForFetchingEvent.class,0);
  230.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceIgnoredForParsingEvent.class,0);
  231.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceReferenceDiscoveredEvent.class,0);
  232.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceParsedEvent.class,1);
  233.     }
  234.     public void testSometimesAllowedFolderTestUserAgent ( ) throws Exception {
  235.         URL url = new URL ( "http://" + TestingConstants.HOST + "/testcases/specific/robotstxt/disallowedFolder1/resource.html" );
  236.         ConfigurationFactory.setConfiguration(config2);
  237.         JSpider jspider = new JSpider ( url );
  238.         jspider.start ( );
  239.         testEventCount(net.javacoding.jspider.api.event.engine.SpideringStartedEvent.class,1);
  240.         testEventCount(net.javacoding.jspider.api.event.engine.SpideringSummaryEvent.class,1);
  241.         testEventCount(net.javacoding.jspider.api.event.engine.SpideringStoppedEvent.class,1);
  242.         testEventCount(net.javacoding.jspider.api.event.site.SiteDiscoveredEvent.class,1);
  243.         testEventCount(net.javacoding.jspider.api.event.site.RobotsTXTMissingEvent.class,0);
  244.         testEventCount(net.javacoding.jspider.api.event.site.RobotsTXTFetchedEvent.class,1);
  245.         testEventCount(net.javacoding.jspider.api.event.site.RobotsTXTFetchErrorEvent.class,0);
  246.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceDiscoveredEvent.class,2);
  247.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceFetchedEvent.class,2);
  248.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceFetchErrorEvent.class,0);
  249.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceForbiddenEvent.class,0);
  250.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceIgnoredForFetchingEvent.class,0);
  251.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceIgnoredForParsingEvent.class,0);
  252.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceReferenceDiscoveredEvent.class,0);
  253.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceParsedEvent.class,1);
  254.     }
  255.     protected void testEventCount ( Class eventClass, int expectedCount  ) {
  256.         assertEquals(eventClass.getName(), expectedCount, sink.getEventCount(eventClass));
  257.     }
  258. }