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

搜索引擎

开发平台:

Java

  1. package net.javacoding.jspider.functional.specific.parse;
  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.plugin.JUnitEventSink;
  8. import java.net.URL;
  9. /**
  10.  * $Id: ParseTest.java,v 1.7 2003/04/11 16:37:09 vanrogu Exp $
  11.  */
  12. public class ParseTest extends TestCase {
  13.     protected JUnitEventSink sink;
  14.     protected JSpiderConfiguration config;
  15.     /**
  16.      * Public constructor giving a name to the test.
  17.      */
  18.     public ParseTest ( ) {
  19.         super ( "ParseTest ");
  20.     }
  21.     /**
  22.      * JUnit's overridden setUp method
  23.      * @throws java.lang.Exception in case something fails during setup
  24.      */
  25.     protected void setUp() throws Exception {
  26.         System.err.println("setUp");
  27.         config = ConfigurationFactory.getConfiguration(ConfigurationFactory.CONFIG_UNITTEST);
  28.         sink = JUnitEventSink.getInstance();
  29.     }
  30.     /**
  31.      * JUnit's overridden tearDown method
  32.      * @throws java.lang.Exception in case something fails during tearDown
  33.      */
  34.     protected void tearDown() throws Exception {
  35.         System.err.println("tearDown");
  36.         ConfigurationFactory.cleanConfiguration();
  37.         sink.reset();
  38.     }
  39.     /**
  40.      * Test a simple parse.
  41.      */
  42.     public void testSimpleParse ( ) throws Exception {
  43.         URL url = new URL ( "http://" + TestingConstants.HOST + "/testcases/specific/parse/testSimpleParse.html" );
  44.         JSpider jspider = new JSpider ( url );
  45.         jspider.start ( );
  46.         testEventCount(net.javacoding.jspider.api.event.engine.SpideringStartedEvent.class,1);
  47.         testEventCount(net.javacoding.jspider.api.event.engine.SpideringSummaryEvent.class,1);
  48.         testEventCount(net.javacoding.jspider.api.event.engine.SpideringStoppedEvent.class,1);
  49.         testEventCount(net.javacoding.jspider.api.event.site.SiteDiscoveredEvent.class,1);
  50.         testEventCount(net.javacoding.jspider.api.event.site.RobotsTXTMissingEvent.class,0);
  51.         testEventCount(net.javacoding.jspider.api.event.site.RobotsTXTFetchedEvent.class,1);
  52.         testEventCount(net.javacoding.jspider.api.event.site.RobotsTXTFetchErrorEvent.class,0);
  53.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceDiscoveredEvent.class,2);
  54.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceFetchedEvent.class,2);
  55.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceFetchErrorEvent.class,0);
  56.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceForbiddenEvent.class,0);
  57.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceIgnoredForFetchingEvent.class,0);
  58.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceIgnoredForParsingEvent.class,0);
  59.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceReferenceDiscoveredEvent.class,6);
  60.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceParsedEvent.class,1);
  61.     }
  62.     public void testSimpleParseWithEMailAddresses ( ) throws Exception {
  63.         URL url = new URL ( "http://" + TestingConstants.HOST + "/testcases/specific/parse/testSimpleParseWithEMailAddresses.html" );
  64.         JSpider jspider = new JSpider ( url );
  65.         jspider.start ( );
  66.         testEventCount(net.javacoding.jspider.api.event.engine.SpideringStartedEvent.class,1);
  67.         testEventCount(net.javacoding.jspider.api.event.engine.SpideringSummaryEvent.class,1);
  68.         testEventCount(net.javacoding.jspider.api.event.engine.SpideringStoppedEvent.class,1);
  69.         testEventCount(net.javacoding.jspider.api.event.site.SiteDiscoveredEvent.class,1);
  70.         testEventCount(net.javacoding.jspider.api.event.site.RobotsTXTMissingEvent.class,0);
  71.         testEventCount(net.javacoding.jspider.api.event.site.RobotsTXTFetchedEvent.class,1);
  72.         testEventCount(net.javacoding.jspider.api.event.site.RobotsTXTFetchErrorEvent.class,0);
  73.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceDiscoveredEvent.class,2);
  74.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceFetchedEvent.class,2);
  75.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceFetchErrorEvent.class,0);
  76.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceForbiddenEvent.class,0);
  77.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceIgnoredForFetchingEvent.class,0);
  78.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceIgnoredForParsingEvent.class,0);
  79.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceReferenceDiscoveredEvent.class,0);
  80.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceParsedEvent.class,1);
  81.         testEventCount(net.javacoding.jspider.api.event.resource.EMailAddressDiscoveredEvent.class,2);
  82.         testEventCount(net.javacoding.jspider.api.event.resource.EMailAddressReferenceDiscoveredEvent.class,3);
  83.     }
  84.     /**
  85.      * Tests parsing of malformed "a href" constructs.
  86.      */
  87.     public void testMalformedParse ( ) throws Exception {
  88.         URL url = new URL ( "http://" + TestingConstants.HOST + "/testcases/specific/parse/testMalformedParse.html" );
  89.         JSpider jspider = new JSpider ( url );
  90.         jspider.start ( );
  91.         testEventCount(net.javacoding.jspider.api.event.engine.SpideringStartedEvent.class,1);
  92.         testEventCount(net.javacoding.jspider.api.event.engine.SpideringSummaryEvent.class,1);
  93.         testEventCount(net.javacoding.jspider.api.event.engine.SpideringStoppedEvent.class,1);
  94.         testEventCount(net.javacoding.jspider.api.event.site.SiteDiscoveredEvent.class,1);
  95.         testEventCount(net.javacoding.jspider.api.event.site.RobotsTXTMissingEvent.class,0);
  96.         testEventCount(net.javacoding.jspider.api.event.site.RobotsTXTFetchedEvent.class,1);
  97.         testEventCount(net.javacoding.jspider.api.event.site.RobotsTXTFetchErrorEvent.class,0);
  98.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceDiscoveredEvent.class,2);
  99.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceFetchedEvent.class,2);
  100.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceFetchErrorEvent.class,0);
  101.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceForbiddenEvent.class,0);
  102.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceIgnoredForFetchingEvent.class,0);
  103.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceIgnoredForParsingEvent.class,0);
  104.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceReferenceDiscoveredEvent.class,11);
  105.         testEventCount(net.javacoding.jspider.api.event.resource.ResourceParsedEvent.class,1);
  106.     }
  107.     protected void testEventCount ( Class eventClass, int expectedCount  ) {
  108.         assertEquals(eventClass.getName(), expectedCount, sink.getEventCount(eventClass));
  109.     }
  110. }