1 package net.javacoding.jspider.functional.specific.fetch; 2 3 import junit.framework.TestCase; 4 import net.javacoding.jspider.JSpider; 5 import net.javacoding.jspider.functional.TestingConstants; 6 import net.javacoding.jspider.core.util.config.ConfigurationFactory; 7 import net.javacoding.jspider.core.util.config.JSpiderConfiguration; 8 import net.javacoding.jspider.mockobjects.plugin.JUnitEventSink; 9 10 import java.net.URL ; 11 12 15 public class FetchTest extends TestCase { 16 17 protected JUnitEventSink sink; 18 protected JSpiderConfiguration config; 19 20 23 public FetchTest ( ) { 24 super ( "FetchTest "); 25 } 26 27 31 protected void setUp() throws Exception { 32 System.err.println("setUp"); 33 config = ConfigurationFactory.getConfiguration(ConfigurationFactory.CONFIG_UNITTEST); 34 sink = JUnitEventSink.getInstance(); 35 } 36 37 41 protected void tearDown() throws Exception { 42 System.err.println("tearDown"); 43 ConfigurationFactory.cleanConfiguration(); 44 sink.reset(); 45 } 46 47 50 public void testSimpleFetch ( ) throws Exception { 51 52 URL url = new URL ( "http://" + TestingConstants.HOST + "/testcases/specific/fetch/testSimpleFetch.html" ); 53 54 JSpider jspider = new JSpider ( url ); 55 jspider.start ( ); 56 57 testEventCount(net.javacoding.jspider.api.event.engine.SpideringStartedEvent.class,1); 58 testEventCount(net.javacoding.jspider.api.event.engine.SpideringSummaryEvent.class,1); 59 testEventCount(net.javacoding.jspider.api.event.engine.SpideringStoppedEvent.class,1); 60 61 testEventCount(net.javacoding.jspider.api.event.site.SiteDiscoveredEvent.class,1); 62 testEventCount(net.javacoding.jspider.api.event.site.RobotsTXTMissingEvent.class,0); 63 testEventCount(net.javacoding.jspider.api.event.site.RobotsTXTFetchedEvent.class,1); 64 testEventCount(net.javacoding.jspider.api.event.site.RobotsTXTFetchErrorEvent.class,0); 65 66 testEventCount(net.javacoding.jspider.api.event.resource.ResourceDiscoveredEvent.class,2); 67 testEventCount(net.javacoding.jspider.api.event.resource.ResourceFetchedEvent.class,2); 68 testEventCount(net.javacoding.jspider.api.event.resource.ResourceFetchErrorEvent.class,0); 69 testEventCount(net.javacoding.jspider.api.event.resource.ResourceForbiddenEvent.class,0); 70 testEventCount(net.javacoding.jspider.api.event.resource.ResourceIgnoredForFetchingEvent.class,0); 71 testEventCount(net.javacoding.jspider.api.event.resource.ResourceIgnoredForParsingEvent.class,0); 72 testEventCount(net.javacoding.jspider.api.event.resource.ResourceReferenceDiscoveredEvent.class,0); 73 testEventCount(net.javacoding.jspider.api.event.resource.ResourceParsedEvent.class,1); 74 } 75 76 public void testFetchUnexisting ( ) throws Exception { 77 78 URL url = new URL ( "http://" + TestingConstants.HOST + "/testcases/specific/fetch/unexisting.html" ); 79 80 JSpider jspider = new JSpider ( url ); 81 jspider.start ( ); 82 83 testEventCount(net.javacoding.jspider.api.event.engine.SpideringStartedEvent.class,1); 84 testEventCount(net.javacoding.jspider.api.event.engine.SpideringSummaryEvent.class,1); 85 testEventCount(net.javacoding.jspider.api.event.engine.SpideringStoppedEvent.class,1); 86 87 testEventCount(net.javacoding.jspider.api.event.site.SiteDiscoveredEvent.class,1); 88 testEventCount(net.javacoding.jspider.api.event.site.RobotsTXTMissingEvent.class,0); 89 testEventCount(net.javacoding.jspider.api.event.site.RobotsTXTFetchedEvent.class,1); 90 testEventCount(net.javacoding.jspider.api.event.site.RobotsTXTFetchErrorEvent.class,0); 91 92 testEventCount(net.javacoding.jspider.api.event.resource.ResourceDiscoveredEvent.class,2); 93 testEventCount(net.javacoding.jspider.api.event.resource.ResourceFetchedEvent.class,1); 94 testEventCount(net.javacoding.jspider.api.event.resource.ResourceFetchErrorEvent.class,1); 95 testEventCount(net.javacoding.jspider.api.event.resource.ResourceForbiddenEvent.class,0); 96 testEventCount(net.javacoding.jspider.api.event.resource.ResourceIgnoredForFetchingEvent.class,0); 97 testEventCount(net.javacoding.jspider.api.event.resource.ResourceIgnoredForParsingEvent.class,0); 98 testEventCount(net.javacoding.jspider.api.event.resource.ResourceReferenceDiscoveredEvent.class,0); 99 testEventCount(net.javacoding.jspider.api.event.resource.ResourceParsedEvent.class,0); 100 } 101 102 public void testFetchRedirect ( ) throws Exception { 103 URL url = new URL ( "http://" + TestingConstants.HOST + "/testcases/specific/fetch/testFetchRedirect.php" ); 104 105 JSpider jspider = new JSpider ( url ); 106 jspider.start ( ); 107 108 testEventCount(net.javacoding.jspider.api.event.engine.SpideringStartedEvent.class,1); 109 testEventCount(net.javacoding.jspider.api.event.engine.SpideringSummaryEvent.class,1); 110 testEventCount(net.javacoding.jspider.api.event.engine.SpideringStoppedEvent.class,1); 111 112 testEventCount(net.javacoding.jspider.api.event.site.SiteDiscoveredEvent.class,1); 113 testEventCount(net.javacoding.jspider.api.event.site.RobotsTXTMissingEvent.class,0); 114 testEventCount(net.javacoding.jspider.api.event.site.RobotsTXTFetchedEvent.class,1); 115 testEventCount(net.javacoding.jspider.api.event.site.RobotsTXTFetchErrorEvent.class,0); 116 117 testEventCount(net.javacoding.jspider.api.event.resource.ResourceDiscoveredEvent.class,3); 118 testEventCount(net.javacoding.jspider.api.event.resource.ResourceFetchedEvent.class,3); 119 testEventCount(net.javacoding.jspider.api.event.resource.ResourceFetchErrorEvent.class,0); 120 testEventCount(net.javacoding.jspider.api.event.resource.ResourceForbiddenEvent.class,0); 121 testEventCount(net.javacoding.jspider.api.event.resource.ResourceIgnoredForFetchingEvent.class,0); 122 testEventCount(net.javacoding.jspider.api.event.resource.ResourceIgnoredForParsingEvent.class,0); 123 testEventCount(net.javacoding.jspider.api.event.resource.ResourceReferenceDiscoveredEvent.class,1); 124 testEventCount(net.javacoding.jspider.api.event.resource.ResourceParsedEvent.class,2); 125 } 126 127 public void testFetch500 ( ) throws Exception { 128 129 URL url = new URL ( "http://" + TestingConstants.HOST + "/testcases/specific/fetch/testFetch500.php" ); 130 131 JSpider jspider = new JSpider ( url ); 132 jspider.start ( ); 133 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 138 testEventCount(net.javacoding.jspider.api.event.site.SiteDiscoveredEvent.class,1); 139 testEventCount(net.javacoding.jspider.api.event.site.RobotsTXTMissingEvent.class,0); 140 testEventCount(net.javacoding.jspider.api.event.site.RobotsTXTFetchedEvent.class,1); 141 testEventCount(net.javacoding.jspider.api.event.site.RobotsTXTFetchErrorEvent.class,0); 142 143 testEventCount(net.javacoding.jspider.api.event.resource.ResourceDiscoveredEvent.class,2); 144 testEventCount(net.javacoding.jspider.api.event.resource.ResourceFetchedEvent.class,1); 145 testEventCount(net.javacoding.jspider.api.event.resource.ResourceFetchErrorEvent.class,1); 146 testEventCount(net.javacoding.jspider.api.event.resource.ResourceForbiddenEvent.class,0); 147 testEventCount(net.javacoding.jspider.api.event.resource.ResourceIgnoredForFetchingEvent.class,0); 148 testEventCount(net.javacoding.jspider.api.event.resource.ResourceIgnoredForParsingEvent.class,0); 149 testEventCount(net.javacoding.jspider.api.event.resource.ResourceReferenceDiscoveredEvent.class,0); 150 testEventCount(net.javacoding.jspider.api.event.resource.ResourceParsedEvent.class,0); 151 } 152 153 public void testFetch404 ( ) throws Exception { 154 155 URL url = new URL ( "http://" + TestingConstants.HOST + "/testcases/specific/fetch/testFetch404.php" ); 156 157 JSpider jspider = new JSpider ( url ); 158 jspider.start ( ); 159 160 testEventCount(net.javacoding.jspider.api.event.engine.SpideringStartedEvent.class,1); 161 testEventCount(net.javacoding.jspider.api.event.engine.SpideringSummaryEvent.class,1); 162 testEventCount(net.javacoding.jspider.api.event.engine.SpideringStoppedEvent.class,1); 163 164 testEventCount(net.javacoding.jspider.api.event.site.SiteDiscoveredEvent.class,1); 165 testEventCount(net.javacoding.jspider.api.event.site.RobotsTXTMissingEvent.class,0); 166 testEventCount(net.javacoding.jspider.api.event.site.RobotsTXTFetchedEvent.class,1); 167 testEventCount(net.javacoding.jspider.api.event.site.RobotsTXTFetchErrorEvent.class,0); 168 169 testEventCount(net.javacoding.jspider.api.event.resource.ResourceDiscoveredEvent.class,2); 170 testEventCount(net.javacoding.jspider.api.event.resource.ResourceFetchedEvent.class,1); 171 testEventCount(net.javacoding.jspider.api.event.resource.ResourceFetchErrorEvent.class,1); 172 testEventCount(net.javacoding.jspider.api.event.resource.ResourceForbiddenEvent.class,0); 173 testEventCount(net.javacoding.jspider.api.event.resource.ResourceIgnoredForFetchingEvent.class,0); 174 testEventCount(net.javacoding.jspider.api.event.resource.ResourceIgnoredForParsingEvent.class,0); 175 testEventCount(net.javacoding.jspider.api.event.resource.ResourceReferenceDiscoveredEvent.class,0); 176 testEventCount(net.javacoding.jspider.api.event.resource.ResourceParsedEvent.class,0); 177 } 178 179 public void testFetchNullSizeResource ( ) throws Exception { 180 181 URL url = new URL ( "http://" + TestingConstants.HOST + "/testcases/specific/fetch/testFetchNullSizeResource.html" ); 182 183 JSpider jspider = new JSpider ( url ); 184 jspider.start ( ); 185 186 testEventCount(net.javacoding.jspider.api.event.engine.SpideringStartedEvent.class,1); 187 testEventCount(net.javacoding.jspider.api.event.engine.SpideringSummaryEvent.class,1); 188 testEventCount(net.javacoding.jspider.api.event.engine.SpideringStoppedEvent.class,1); 189 190 testEventCount(net.javacoding.jspider.api.event.site.SiteDiscoveredEvent.class,1); 191 testEventCount(net.javacoding.jspider.api.event.site.RobotsTXTMissingEvent.class,0); 192 testEventCount(net.javacoding.jspider.api.event.site.RobotsTXTFetchedEvent.class,1); 193 testEventCount(net.javacoding.jspider.api.event.site.RobotsTXTFetchErrorEvent.class,0); 194 195 testEventCount(net.javacoding.jspider.api.event.resource.ResourceDiscoveredEvent.class,2); 196 testEventCount(net.javacoding.jspider.api.event.resource.ResourceFetchedEvent.class,2); 197 testEventCount(net.javacoding.jspider.api.event.resource.ResourceFetchErrorEvent.class,0); 198 testEventCount(net.javacoding.jspider.api.event.resource.ResourceForbiddenEvent.class,0); 199 testEventCount(net.javacoding.jspider.api.event.resource.ResourceIgnoredForFetchingEvent.class,0); 200 testEventCount(net.javacoding.jspider.api.event.resource.ResourceIgnoredForParsingEvent.class,0); 201 testEventCount(net.javacoding.jspider.api.event.resource.ResourceReferenceDiscoveredEvent.class,0); 202 testEventCount(net.javacoding.jspider.api.event.resource.ResourceParsedEvent.class,1); 203 } 204 205 protected void testEventCount ( Class eventClass, int expectedCount ) { 206 assertEquals(eventClass.getName(), expectedCount, sink.getEventCount(eventClass)); 207 } 208 209 } 210 211 | Popular Tags |