001: package net.javacoding.jspider.functional.specific.fetch;
002:
003: import junit.framework.TestCase;
004: import net.javacoding.jspider.JSpider;
005: import net.javacoding.jspider.functional.TestingConstants;
006: import net.javacoding.jspider.core.util.config.ConfigurationFactory;
007: import net.javacoding.jspider.core.util.config.JSpiderConfiguration;
008: import net.javacoding.jspider.mockobjects.plugin.JUnitEventSink;
009:
010: import java.net.URL;
011:
012: /**
013: * $Id: FetchTest.java,v 1.8 2003/04/10 16:19:21 vanrogu Exp $
014: */
015: public class FetchTest extends TestCase {
016:
017: protected JUnitEventSink sink;
018: protected JSpiderConfiguration config;
019:
020: /**
021: * Public constructor giving a name to the test.
022: */
023: public FetchTest() {
024: super ("FetchTest ");
025: }
026:
027: /**
028: * JUnit's overridden setUp method
029: * @throws java.lang.Exception in case something fails during setup
030: */
031: protected void setUp() throws Exception {
032: System.err.println("setUp");
033: config = ConfigurationFactory
034: .getConfiguration(ConfigurationFactory.CONFIG_UNITTEST);
035: sink = JUnitEventSink.getInstance();
036: }
037:
038: /**
039: * JUnit's overridden tearDown method
040: * @throws java.lang.Exception in case something fails during tearDown
041: */
042: protected void tearDown() throws Exception {
043: System.err.println("tearDown");
044: ConfigurationFactory.cleanConfiguration();
045: sink.reset();
046: }
047:
048: /**
049: * Test a simple fetch.
050: */
051: public void testSimpleFetch() throws Exception {
052:
053: URL url = new URL("http://" + TestingConstants.HOST
054: + "/testcases/specific/fetch/testSimpleFetch.html");
055:
056: JSpider jspider = new JSpider(url);
057: jspider.start();
058:
059: testEventCount(
060: net.javacoding.jspider.api.event.engine.SpideringStartedEvent.class,
061: 1);
062: testEventCount(
063: net.javacoding.jspider.api.event.engine.SpideringSummaryEvent.class,
064: 1);
065: testEventCount(
066: net.javacoding.jspider.api.event.engine.SpideringStoppedEvent.class,
067: 1);
068:
069: testEventCount(
070: net.javacoding.jspider.api.event.site.SiteDiscoveredEvent.class,
071: 1);
072: testEventCount(
073: net.javacoding.jspider.api.event.site.RobotsTXTMissingEvent.class,
074: 0);
075: testEventCount(
076: net.javacoding.jspider.api.event.site.RobotsTXTFetchedEvent.class,
077: 1);
078: testEventCount(
079: net.javacoding.jspider.api.event.site.RobotsTXTFetchErrorEvent.class,
080: 0);
081:
082: testEventCount(
083: net.javacoding.jspider.api.event.resource.ResourceDiscoveredEvent.class,
084: 2);
085: testEventCount(
086: net.javacoding.jspider.api.event.resource.ResourceFetchedEvent.class,
087: 2);
088: testEventCount(
089: net.javacoding.jspider.api.event.resource.ResourceFetchErrorEvent.class,
090: 0);
091: testEventCount(
092: net.javacoding.jspider.api.event.resource.ResourceForbiddenEvent.class,
093: 0);
094: testEventCount(
095: net.javacoding.jspider.api.event.resource.ResourceIgnoredForFetchingEvent.class,
096: 0);
097: testEventCount(
098: net.javacoding.jspider.api.event.resource.ResourceIgnoredForParsingEvent.class,
099: 0);
100: testEventCount(
101: net.javacoding.jspider.api.event.resource.ResourceReferenceDiscoveredEvent.class,
102: 0);
103: testEventCount(
104: net.javacoding.jspider.api.event.resource.ResourceParsedEvent.class,
105: 1);
106: }
107:
108: public void testFetchUnexisting() throws Exception {
109:
110: URL url = new URL("http://" + TestingConstants.HOST
111: + "/testcases/specific/fetch/unexisting.html");
112:
113: JSpider jspider = new JSpider(url);
114: jspider.start();
115:
116: testEventCount(
117: net.javacoding.jspider.api.event.engine.SpideringStartedEvent.class,
118: 1);
119: testEventCount(
120: net.javacoding.jspider.api.event.engine.SpideringSummaryEvent.class,
121: 1);
122: testEventCount(
123: net.javacoding.jspider.api.event.engine.SpideringStoppedEvent.class,
124: 1);
125:
126: testEventCount(
127: net.javacoding.jspider.api.event.site.SiteDiscoveredEvent.class,
128: 1);
129: testEventCount(
130: net.javacoding.jspider.api.event.site.RobotsTXTMissingEvent.class,
131: 0);
132: testEventCount(
133: net.javacoding.jspider.api.event.site.RobotsTXTFetchedEvent.class,
134: 1);
135: testEventCount(
136: net.javacoding.jspider.api.event.site.RobotsTXTFetchErrorEvent.class,
137: 0);
138:
139: testEventCount(
140: net.javacoding.jspider.api.event.resource.ResourceDiscoveredEvent.class,
141: 2);
142: testEventCount(
143: net.javacoding.jspider.api.event.resource.ResourceFetchedEvent.class,
144: 1);
145: testEventCount(
146: net.javacoding.jspider.api.event.resource.ResourceFetchErrorEvent.class,
147: 1);
148: testEventCount(
149: net.javacoding.jspider.api.event.resource.ResourceForbiddenEvent.class,
150: 0);
151: testEventCount(
152: net.javacoding.jspider.api.event.resource.ResourceIgnoredForFetchingEvent.class,
153: 0);
154: testEventCount(
155: net.javacoding.jspider.api.event.resource.ResourceIgnoredForParsingEvent.class,
156: 0);
157: testEventCount(
158: net.javacoding.jspider.api.event.resource.ResourceReferenceDiscoveredEvent.class,
159: 0);
160: testEventCount(
161: net.javacoding.jspider.api.event.resource.ResourceParsedEvent.class,
162: 0);
163: }
164:
165: public void testFetchRedirect() throws Exception {
166: URL url = new URL("http://" + TestingConstants.HOST
167: + "/testcases/specific/fetch/testFetchRedirect.php");
168:
169: JSpider jspider = new JSpider(url);
170: jspider.start();
171:
172: testEventCount(
173: net.javacoding.jspider.api.event.engine.SpideringStartedEvent.class,
174: 1);
175: testEventCount(
176: net.javacoding.jspider.api.event.engine.SpideringSummaryEvent.class,
177: 1);
178: testEventCount(
179: net.javacoding.jspider.api.event.engine.SpideringStoppedEvent.class,
180: 1);
181:
182: testEventCount(
183: net.javacoding.jspider.api.event.site.SiteDiscoveredEvent.class,
184: 1);
185: testEventCount(
186: net.javacoding.jspider.api.event.site.RobotsTXTMissingEvent.class,
187: 0);
188: testEventCount(
189: net.javacoding.jspider.api.event.site.RobotsTXTFetchedEvent.class,
190: 1);
191: testEventCount(
192: net.javacoding.jspider.api.event.site.RobotsTXTFetchErrorEvent.class,
193: 0);
194:
195: testEventCount(
196: net.javacoding.jspider.api.event.resource.ResourceDiscoveredEvent.class,
197: 3);
198: testEventCount(
199: net.javacoding.jspider.api.event.resource.ResourceFetchedEvent.class,
200: 3);
201: testEventCount(
202: net.javacoding.jspider.api.event.resource.ResourceFetchErrorEvent.class,
203: 0);
204: testEventCount(
205: net.javacoding.jspider.api.event.resource.ResourceForbiddenEvent.class,
206: 0);
207: testEventCount(
208: net.javacoding.jspider.api.event.resource.ResourceIgnoredForFetchingEvent.class,
209: 0);
210: testEventCount(
211: net.javacoding.jspider.api.event.resource.ResourceIgnoredForParsingEvent.class,
212: 0);
213: testEventCount(
214: net.javacoding.jspider.api.event.resource.ResourceReferenceDiscoveredEvent.class,
215: 1);
216: testEventCount(
217: net.javacoding.jspider.api.event.resource.ResourceParsedEvent.class,
218: 2);
219: }
220:
221: public void testFetch500() throws Exception {
222:
223: URL url = new URL("http://" + TestingConstants.HOST
224: + "/testcases/specific/fetch/testFetch500.php");
225:
226: JSpider jspider = new JSpider(url);
227: jspider.start();
228:
229: testEventCount(
230: net.javacoding.jspider.api.event.engine.SpideringStartedEvent.class,
231: 1);
232: testEventCount(
233: net.javacoding.jspider.api.event.engine.SpideringSummaryEvent.class,
234: 1);
235: testEventCount(
236: net.javacoding.jspider.api.event.engine.SpideringStoppedEvent.class,
237: 1);
238:
239: testEventCount(
240: net.javacoding.jspider.api.event.site.SiteDiscoveredEvent.class,
241: 1);
242: testEventCount(
243: net.javacoding.jspider.api.event.site.RobotsTXTMissingEvent.class,
244: 0);
245: testEventCount(
246: net.javacoding.jspider.api.event.site.RobotsTXTFetchedEvent.class,
247: 1);
248: testEventCount(
249: net.javacoding.jspider.api.event.site.RobotsTXTFetchErrorEvent.class,
250: 0);
251:
252: testEventCount(
253: net.javacoding.jspider.api.event.resource.ResourceDiscoveredEvent.class,
254: 2);
255: testEventCount(
256: net.javacoding.jspider.api.event.resource.ResourceFetchedEvent.class,
257: 1);
258: testEventCount(
259: net.javacoding.jspider.api.event.resource.ResourceFetchErrorEvent.class,
260: 1);
261: testEventCount(
262: net.javacoding.jspider.api.event.resource.ResourceForbiddenEvent.class,
263: 0);
264: testEventCount(
265: net.javacoding.jspider.api.event.resource.ResourceIgnoredForFetchingEvent.class,
266: 0);
267: testEventCount(
268: net.javacoding.jspider.api.event.resource.ResourceIgnoredForParsingEvent.class,
269: 0);
270: testEventCount(
271: net.javacoding.jspider.api.event.resource.ResourceReferenceDiscoveredEvent.class,
272: 0);
273: testEventCount(
274: net.javacoding.jspider.api.event.resource.ResourceParsedEvent.class,
275: 0);
276: }
277:
278: public void testFetch404() throws Exception {
279:
280: URL url = new URL("http://" + TestingConstants.HOST
281: + "/testcases/specific/fetch/testFetch404.php");
282:
283: JSpider jspider = new JSpider(url);
284: jspider.start();
285:
286: testEventCount(
287: net.javacoding.jspider.api.event.engine.SpideringStartedEvent.class,
288: 1);
289: testEventCount(
290: net.javacoding.jspider.api.event.engine.SpideringSummaryEvent.class,
291: 1);
292: testEventCount(
293: net.javacoding.jspider.api.event.engine.SpideringStoppedEvent.class,
294: 1);
295:
296: testEventCount(
297: net.javacoding.jspider.api.event.site.SiteDiscoveredEvent.class,
298: 1);
299: testEventCount(
300: net.javacoding.jspider.api.event.site.RobotsTXTMissingEvent.class,
301: 0);
302: testEventCount(
303: net.javacoding.jspider.api.event.site.RobotsTXTFetchedEvent.class,
304: 1);
305: testEventCount(
306: net.javacoding.jspider.api.event.site.RobotsTXTFetchErrorEvent.class,
307: 0);
308:
309: testEventCount(
310: net.javacoding.jspider.api.event.resource.ResourceDiscoveredEvent.class,
311: 2);
312: testEventCount(
313: net.javacoding.jspider.api.event.resource.ResourceFetchedEvent.class,
314: 1);
315: testEventCount(
316: net.javacoding.jspider.api.event.resource.ResourceFetchErrorEvent.class,
317: 1);
318: testEventCount(
319: net.javacoding.jspider.api.event.resource.ResourceForbiddenEvent.class,
320: 0);
321: testEventCount(
322: net.javacoding.jspider.api.event.resource.ResourceIgnoredForFetchingEvent.class,
323: 0);
324: testEventCount(
325: net.javacoding.jspider.api.event.resource.ResourceIgnoredForParsingEvent.class,
326: 0);
327: testEventCount(
328: net.javacoding.jspider.api.event.resource.ResourceReferenceDiscoveredEvent.class,
329: 0);
330: testEventCount(
331: net.javacoding.jspider.api.event.resource.ResourceParsedEvent.class,
332: 0);
333: }
334:
335: public void testFetchNullSizeResource() throws Exception {
336:
337: URL url = new URL(
338: "http://"
339: + TestingConstants.HOST
340: + "/testcases/specific/fetch/testFetchNullSizeResource.html");
341:
342: JSpider jspider = new JSpider(url);
343: jspider.start();
344:
345: testEventCount(
346: net.javacoding.jspider.api.event.engine.SpideringStartedEvent.class,
347: 1);
348: testEventCount(
349: net.javacoding.jspider.api.event.engine.SpideringSummaryEvent.class,
350: 1);
351: testEventCount(
352: net.javacoding.jspider.api.event.engine.SpideringStoppedEvent.class,
353: 1);
354:
355: testEventCount(
356: net.javacoding.jspider.api.event.site.SiteDiscoveredEvent.class,
357: 1);
358: testEventCount(
359: net.javacoding.jspider.api.event.site.RobotsTXTMissingEvent.class,
360: 0);
361: testEventCount(
362: net.javacoding.jspider.api.event.site.RobotsTXTFetchedEvent.class,
363: 1);
364: testEventCount(
365: net.javacoding.jspider.api.event.site.RobotsTXTFetchErrorEvent.class,
366: 0);
367:
368: testEventCount(
369: net.javacoding.jspider.api.event.resource.ResourceDiscoveredEvent.class,
370: 2);
371: testEventCount(
372: net.javacoding.jspider.api.event.resource.ResourceFetchedEvent.class,
373: 2);
374: testEventCount(
375: net.javacoding.jspider.api.event.resource.ResourceFetchErrorEvent.class,
376: 0);
377: testEventCount(
378: net.javacoding.jspider.api.event.resource.ResourceForbiddenEvent.class,
379: 0);
380: testEventCount(
381: net.javacoding.jspider.api.event.resource.ResourceIgnoredForFetchingEvent.class,
382: 0);
383: testEventCount(
384: net.javacoding.jspider.api.event.resource.ResourceIgnoredForParsingEvent.class,
385: 0);
386: testEventCount(
387: net.javacoding.jspider.api.event.resource.ResourceReferenceDiscoveredEvent.class,
388: 0);
389: testEventCount(
390: net.javacoding.jspider.api.event.resource.ResourceParsedEvent.class,
391: 1);
392: }
393:
394: protected void testEventCount(Class eventClass, int expectedCount) {
395: assertEquals(eventClass.getName(), expectedCount, sink
396: .getEventCount(eventClass));
397: }
398:
399: }
|