Source Code Cross Referenced for DecideRuleSequenceTest.java in  » Web-Crawler » heritrix » org » archive » crawler » deciderules » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Web Crawler » heritrix » org.archive.crawler.deciderules 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /* DecideRuleSequenceTest
002:         * 
003:         * Created on Apr 4, 2005
004:         *
005:         * Copyright (C) 2005 Internet Archive.
006:         * 
007:         * This file is part of the Heritrix web crawler (crawler.archive.org).
008:         * 
009:         * Heritrix is free software; you can redistribute it and/or modify
010:         * it under the terms of the GNU Lesser Public License as published by
011:         * the Free Software Foundation; either version 2.1 of the License, or
012:         * any later version.
013:         * 
014:         * Heritrix is distributed in the hope that it will be useful, 
015:         * but WITHOUT ANY WARRANTY; without even the implied warranty of
016:         * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
017:         * GNU Lesser Public License for more details.
018:         * 
019:         * You should have received a copy of the GNU Lesser Public License
020:         * along with Heritrix; if not, write to the Free Software
021:         * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
022:         */
023:        package org.archive.crawler.deciderules;
024:
025:        import java.io.File;
026:
027:        import javax.management.Attribute;
028:        import javax.management.AttributeNotFoundException;
029:        import javax.management.InvalidAttributeValueException;
030:        import javax.management.MBeanException;
031:        import javax.management.ReflectionException;
032:
033:        import org.apache.commons.httpclient.URIException;
034:        import org.archive.crawler.datamodel.CandidateURI;
035:        import org.archive.crawler.datamodel.CrawlOrder;
036:        import org.archive.crawler.datamodel.CrawlURI;
037:        import org.archive.crawler.filter.ContentTypeRegExpFilter;
038:        import org.archive.crawler.settings.MapType;
039:        import org.archive.crawler.settings.SettingsHandler;
040:        import org.archive.crawler.settings.XMLSettingsHandler;
041:        import org.archive.net.UURI;
042:        import org.archive.net.UURIFactory;
043:        import org.archive.util.SurtPrefixSet;
044:        import org.archive.util.TmpDirTestCase;
045:
046:        /**
047:         * @author stack
048:         * @version $Date: 2007-04-06 01:13:26 +0000 (Fri, 06 Apr 2007) $, $Revision: 5041 $
049:         */
050:        public class DecideRuleSequenceTest extends TmpDirTestCase {
051:            /**
052:             * Gets setup by {@link #setUp()}.
053:             */
054:            private DecideRuleSequence rule = null;
055:
056:            protected void setUp() throws Exception {
057:                super .setUp();
058:                final String name = this .getClass().getName();
059:                SettingsHandler settingsHandler = new XMLSettingsHandler(
060:                        new File(getTmpDir(), name + ".order.xml"));
061:                settingsHandler.initialize();
062:                // Create a new ConfigureDecideRule instance and add it to a MapType
063:                // (I can change MapTypes after instantiation).  The chosen MapType
064:                // is the rules canonicalization rules list.
065:                this .rule = (DecideRuleSequence) ((MapType) settingsHandler
066:                        .getOrder().getAttribute(CrawlOrder.ATTR_RULES))
067:                        .addElement(settingsHandler.getSettingsObject(null),
068:                                new DecideRuleSequence(name));
069:            }
070:
071:            public void testEmptySequence() {
072:                Object decision = this .rule.decisionFor("test");
073:                assertTrue("Expect PASS but got " + decision,
074:                        decision == DecideRule.PASS);
075:            }
076:
077:            public void testSingleACCEPT()
078:                    throws InvalidAttributeValueException {
079:                Object decision = addDecideRule(new AcceptDecideRule("ACCEPT"))
080:                        .decisionFor("test");
081:                assertTrue("Expect ACCEPT but got " + decision,
082:                        decision == DecideRule.ACCEPT);
083:            }
084:
085:            public void testSingleREJECT()
086:                    throws InvalidAttributeValueException {
087:                Object decision = addDecideRule(new RejectDecideRule("REJECT"))
088:                        .decisionFor("test");
089:                assertTrue("Expect REJECT but got " + decision,
090:                        decision == DecideRule.REJECT);
091:            }
092:
093:            public void testSinglePASS() throws InvalidAttributeValueException {
094:                Object decision = addDecideRule(new DecideRule("PASS"))
095:                        .decisionFor("test");
096:                assertTrue("Expect PASS but got " + decision,
097:                        decision == DecideRule.PASS);
098:            }
099:
100:            public void testACCEPTWins() throws InvalidAttributeValueException {
101:                addDecideRule(new DecideRule("PASS1"));
102:                addDecideRule(new RejectDecideRule("REJECT1"));
103:                addDecideRule(new DecideRule("PASS2"));
104:                addDecideRule(new AcceptDecideRule("ACCEPT1"));
105:                addDecideRule(new RejectDecideRule("REJECT2"));
106:                addDecideRule(new DecideRule("PASS3"));
107:                addDecideRule(new AcceptDecideRule("ACCEPT2"));
108:                addDecideRule(new DecideRule("PASS4"));
109:                Object decision = this .rule.decisionFor("test");
110:                assertTrue("Expect ACCEPT but got " + decision,
111:                        decision == DecideRule.ACCEPT);
112:            }
113:
114:            public void testREJECTWins() throws InvalidAttributeValueException {
115:                addDecideRule(new DecideRule("PASS1"));
116:                addDecideRule(new RejectDecideRule("REJECT1"));
117:                addDecideRule(new DecideRule("PASS2"));
118:                addDecideRule(new AcceptDecideRule("ACCEPT1"));
119:                addDecideRule(new RejectDecideRule("REJECT2"));
120:                addDecideRule(new DecideRule("PASS3"));
121:                addDecideRule(new AcceptDecideRule("ACCEPT2"));
122:                addDecideRule(new DecideRule("PASS4"));
123:                addDecideRule(new RejectDecideRule("REJECT3"));
124:                Object decision = this .rule.decisionFor("test");
125:                assertTrue("Expect REJECT but got " + decision,
126:                        decision == DecideRule.REJECT);
127:            }
128:
129:            public void testRegex() throws InvalidAttributeValueException,
130:                    AttributeNotFoundException, MBeanException,
131:                    ReflectionException {
132:                final String regexName = "REGEX";
133:                DecideRule r = addDecideRule(new MatchesRegExpDecideRule(
134:                        regexName));
135:                // Set regex to be match anything that ends in archive.org.
136:                r.setAttribute(new Attribute(
137:                        MatchesRegExpDecideRule.ATTR_REGEXP,
138:                        "^.*\\.archive\\.org"));
139:                Object decision = this .rule.decisionFor("http://google.com");
140:                assertTrue("Expect PASS but got " + decision,
141:                        decision == DecideRule.PASS);
142:                decision = this .rule.decisionFor("http://archive.org");
143:                assertTrue("Expect PASS but got " + decision,
144:                        decision == DecideRule.PASS);
145:                decision = this .rule.decisionFor("http://www.archive.org");
146:                assertTrue("Expect ACCEPT but got " + decision,
147:                        decision == DecideRule.ACCEPT);
148:            }
149:
150:            public void testNotRegex() throws InvalidAttributeValueException,
151:                    AttributeNotFoundException, MBeanException,
152:                    ReflectionException {
153:                final String regexName = "NOT_REGEX";
154:                DecideRule r = addDecideRule(new NotMatchesRegExpDecideRule(
155:                        regexName));
156:                // Set regex to be match anything that ends in archive.org.
157:                r.setAttribute(new Attribute(
158:                        MatchesRegExpDecideRule.ATTR_REGEXP,
159:                        "^.*\\.archive\\.org"));
160:                Object decision = this .rule.decisionFor("http://google.com");
161:                assertTrue("Expect ACCEPT but got " + decision,
162:                        decision == DecideRule.ACCEPT);
163:                decision = this .rule.decisionFor("http://www.archive.org");
164:                assertTrue("Expect PASS but got " + decision,
165:                        decision == DecideRule.PASS);
166:            }
167:
168:            public void testPrerequisite()
169:                    throws InvalidAttributeValueException, URIException {
170:                addDecideRule(new PrerequisiteAcceptDecideRule("PREREQUISITE"));
171:                UURI uuri = UURIFactory.getInstance("http://archive.org");
172:                CandidateURI candidate = new CandidateURI(uuri);
173:                Object decision = this .rule.decisionFor(candidate);
174:                assertTrue("Expect PASS but got " + decision,
175:                        decision == DecideRule.PASS);
176:                candidate = new CandidateURI(uuri, "LLP", null, null);
177:                decision = this .rule.decisionFor(candidate);
178:                assertTrue("Expect ACCEPT but got " + decision,
179:                        decision == DecideRule.ACCEPT);
180:            }
181:
182:            public void testHops() throws InvalidAttributeValueException,
183:                    URIException {
184:                addDecideRule(new TooManyHopsDecideRule("HOPS"));
185:                testHopLimit(TooManyHopsDecideRule.DEFAULT_MAX_HOPS.intValue(),
186:                        'L', DecideRule.PASS, DecideRule.REJECT);
187:            }
188:
189:            public void testTransclusion()
190:                    throws InvalidAttributeValueException, URIException {
191:                addDecideRule(new TransclusionDecideRule("TRANSCLUSION"));
192:                final int max = TransclusionDecideRule.DEFAULT_MAX_TRANS_HOPS
193:                        .intValue();
194:                final char pathExpansion = 'E';
195:                UURI uuri = UURIFactory.getInstance("http://archive.org");
196:                CandidateURI candidate = new CandidateURI(uuri);
197:                Object decision = this .rule.decisionFor(candidate);
198:                assertTrue(
199:                        "Expect " + DecideRule.PASS + " but got " + decision,
200:                        decision == DecideRule.PASS);
201:                StringBuffer path = new StringBuffer(max);
202:                for (int i = 0; i < (max - 1); i++) {
203:                    path.append(pathExpansion);
204:                }
205:                candidate = new CandidateURI(uuri, path.toString(), null, null);
206:                decision = this .rule.decisionFor(candidate);
207:                assertTrue("Expect " + DecideRule.ACCEPT + " but got "
208:                        + decision, decision == DecideRule.ACCEPT);
209:                String pathCopy = path.toString();
210:                path.append(pathExpansion);
211:                candidate = new CandidateURI(uuri, path.toString(), null, null);
212:                decision = this .rule.decisionFor(candidate);
213:                assertTrue("Expect " + DecideRule.ACCEPT + " but got "
214:                        + decision, decision == DecideRule.ACCEPT);
215:                path.append(pathExpansion);
216:                candidate = new CandidateURI(uuri, path.toString(), null, null);
217:                decision = this .rule.decisionFor(candidate);
218:                assertTrue(
219:                        "Expect " + DecideRule.PASS + " but got " + decision,
220:                        decision == DecideRule.PASS);
221:                candidate = new CandidateURI(uuri, pathCopy + 'L', null, null);
222:                decision = this .rule.decisionFor(candidate);
223:                assertTrue(
224:                        "Expect " + DecideRule.PASS + " but got " + decision,
225:                        decision == DecideRule.PASS);
226:            }
227:
228:            public void testPathologicalPath()
229:                    throws InvalidAttributeValueException, URIException {
230:                addDecideRule(new PathologicalPathDecideRule("PATHOLOGICAL"));
231:                final int max = PathologicalPathDecideRule.DEFAULT_REPETITIONS
232:                        .intValue();
233:                String uri = "http://archive.org/";
234:                final String segment = "abc/";
235:                for (int i = 1; i < max; i++) {
236:                    uri = uri + segment;
237:                }
238:                final String baseUri = uri;
239:                UURI uuri = UURIFactory.getInstance(uri);
240:                CandidateURI candidate = new CandidateURI(uuri);
241:                Object decision = this .rule.decisionFor(candidate);
242:                assertTrue(
243:                        "Expect " + DecideRule.PASS + " but got " + decision,
244:                        decision == DecideRule.PASS);
245:                uuri = UURIFactory.getInstance(baseUri + segment);
246:                candidate = new CandidateURI(uuri);
247:                decision = this .rule.decisionFor(candidate);
248:                assertTrue(
249:                        "Expect " + DecideRule.PASS + " but got " + decision,
250:                        decision == DecideRule.PASS);
251:                uuri = UURIFactory.getInstance(baseUri + segment + segment);
252:                candidate = new CandidateURI(uuri);
253:                decision = this .rule.decisionFor(candidate);
254:                assertTrue("Expect " + DecideRule.REJECT + " but got "
255:                        + decision, decision == DecideRule.REJECT);
256:            }
257:
258:            public void testTooManyPathSegments()
259:                    throws InvalidAttributeValueException, URIException {
260:                addDecideRule(new TooManyPathSegmentsDecideRule("SEGMENTS"));
261:                final int max = TooManyPathSegmentsDecideRule.DEFAULT_MAX_PATH_DEPTH
262:                        .intValue();
263:                StringBuffer baseUri = new StringBuffer("http://archive.org");
264:                for (int i = 0; i < max; i++) {
265:                    baseUri.append('/');
266:                    baseUri.append(Integer.toString(i + 1));
267:                }
268:                UURI uuri = UURIFactory.getInstance(baseUri.toString());
269:                CandidateURI candidate = new CandidateURI(uuri);
270:                Object decision = this .rule.decisionFor(candidate);
271:                assertTrue(
272:                        "Expect " + DecideRule.PASS + " but got " + decision,
273:                        decision == DecideRule.PASS);
274:                baseUri.append("/x");
275:                uuri = UURIFactory.getInstance(baseUri.toString());
276:                candidate = new CandidateURI(uuri);
277:                decision = this .rule.decisionFor(candidate);
278:                assertTrue("Expect " + DecideRule.REJECT + " but got "
279:                        + decision, decision == DecideRule.REJECT);
280:            }
281:
282:            public void testMatchesFilePattern()
283:                    throws InvalidAttributeValueException, URIException {
284:                addDecideRule(new MatchesFilePatternDecideRule("FILE_PATTERN"));
285:                StringBuffer baseUri = new StringBuffer("http://archive.org/");
286:                UURI uuri = UURIFactory.getInstance(baseUri.toString()
287:                        + "ms.doc");
288:                CandidateURI candidate = new CandidateURI(uuri);
289:                Object decision = this .rule.decisionFor(candidate);
290:                assertTrue("Expect " + DecideRule.ACCEPT + " but got "
291:                        + decision, decision == DecideRule.ACCEPT);
292:                uuri = UURIFactory.getInstance(baseUri.toString()
293:                        + "index.html");
294:                candidate = new CandidateURI(uuri);
295:                decision = this .rule.decisionFor(candidate);
296:                assertTrue(
297:                        "Expect " + DecideRule.PASS + " but got " + decision,
298:                        decision == DecideRule.PASS);
299:            }
300:
301:            public void testNotMatchesFilePattern()
302:                    throws InvalidAttributeValueException, URIException {
303:                addDecideRule(new NotMatchesFilePatternDecideRule(
304:                        "NOT_FILE_PATTERN"));
305:                StringBuffer baseUri = new StringBuffer("http://archive.org/");
306:                UURI uuri = UURIFactory.getInstance(baseUri.toString()
307:                        + "ms.doc");
308:                CandidateURI candidate = new CandidateURI(uuri);
309:                Object decision = this .rule.decisionFor(candidate);
310:                assertTrue(
311:                        "Expect " + DecideRule.PASS + " but got " + decision,
312:                        decision == DecideRule.PASS);
313:                uuri = UURIFactory.getInstance(baseUri.toString()
314:                        + "index.html");
315:                candidate = new CandidateURI(uuri);
316:                decision = this .rule.decisionFor(candidate);
317:                assertTrue("Expect " + DecideRule.ACCEPT + " but got "
318:                        + decision, decision == DecideRule.ACCEPT);
319:            }
320:
321:            protected void testHopLimit(final int max,
322:                    final char pathExpansion, final String defaultDecision,
323:                    final String overLimitDecision) throws URIException {
324:                UURI uuri = UURIFactory.getInstance("http://archive.org");
325:                CandidateURI candidate = new CandidateURI(uuri);
326:                Object decision = this .rule.decisionFor(candidate);
327:                assertTrue(
328:                        "Expect " + defaultDecision + " but got " + decision,
329:                        decision == defaultDecision);
330:                StringBuffer path = new StringBuffer(max);
331:                for (int i = 0; i < (max - 1); i++) {
332:                    path.append(pathExpansion);
333:                }
334:                candidate = new CandidateURI(uuri, path.toString(), null, null);
335:                decision = this .rule.decisionFor(candidate);
336:                assertTrue(
337:                        "Expect " + defaultDecision + " but got " + decision,
338:                        decision == defaultDecision);
339:                path.append(pathExpansion);
340:                candidate = new CandidateURI(uuri, path.toString(), null, null);
341:                decision = this .rule.decisionFor(candidate);
342:                assertTrue(
343:                        "Expect " + defaultDecision + " but got " + decision,
344:                        decision == defaultDecision);
345:                path.append(pathExpansion);
346:                candidate = new CandidateURI(uuri, path.toString(), null, null);
347:                decision = this .rule.decisionFor(candidate);
348:                assertTrue("Expect " + overLimitDecision + " but got "
349:                        + decision, decision == overLimitDecision);
350:            }
351:
352:            public void testScopePlusOne() throws URIException,
353:                    InvalidAttributeValueException, AttributeNotFoundException,
354:                    MBeanException, ReflectionException {
355:                // first test host scope      
356:                ScopePlusOneDecideRule t = new ScopePlusOneDecideRule("host");
357:                SurtPrefixSet mSet = new SurtPrefixSet();
358:                mSet.add(SurtPrefixSet
359:                        .prefixFromPlain("http://audio.archive.org"));
360:                mSet.convertAllPrefixesToHosts();
361:                t.surtPrefixes = mSet;
362:                DecideRule s = addDecideRule(t);
363:                s.setAttribute(new Attribute(ScopePlusOneDecideRule.ATTR_SCOPE,
364:                        ScopePlusOneDecideRule.HOST));
365:
366:                UURI uuri = UURIFactory
367:                        .getInstance("http://audio.archive.org/examples");
368:                CandidateURI candidate = new CandidateURI(uuri);
369:                Object decision = this .rule.decisionFor(candidate);
370:                assertTrue("URI Expect " + DecideRule.ACCEPT + " for "
371:                        + candidate + " but got " + decision,
372:                        decision == DecideRule.ACCEPT);
373:                UURI uuriOne = UURIFactory
374:                        .getInstance("http://movies.archive.org");
375:                CandidateURI plusOne = new CandidateURI(uuriOne);
376:                plusOne.setVia(uuri);
377:                decision = this .rule.decisionFor(plusOne);
378:                assertTrue("PlusOne Expect " + DecideRule.ACCEPT + " for "
379:                        + plusOne + " with via " + plusOne.flattenVia()
380:                        + " but got " + decision, decision == DecideRule.ACCEPT);
381:                UURI uuriTwo = UURIFactory
382:                        .getInstance("http://sloan.archive.org");
383:                CandidateURI plusTwo = new CandidateURI(uuriTwo);
384:                plusTwo.setVia(uuriOne);
385:                decision = this .rule.decisionFor(plusTwo);
386:                assertTrue("PlusTwo Expect " + DecideRule.PASS + " for "
387:                        + plusTwo + " with via " + plusTwo.flattenVia()
388:                        + " but got " + decision, decision == DecideRule.PASS);
389:
390:                //now test domain scope
391:                ScopePlusOneDecideRule u = new ScopePlusOneDecideRule("domain");
392:                SurtPrefixSet mSet1 = new SurtPrefixSet();
393:                mSet1.add(SurtPrefixSet.prefixFromPlain("archive.org"));
394:                mSet1.convertAllPrefixesToDomains();
395:                u.surtPrefixes = mSet1;
396:                DecideRule v = addDecideRule(u);
397:                v.setAttribute(new Attribute(ScopePlusOneDecideRule.ATTR_SCOPE,
398:                        ScopePlusOneDecideRule.DOMAIN));
399:
400:                decision = this .rule.decisionFor(candidate);
401:                assertTrue("Domain: URI Expect " + DecideRule.ACCEPT + " for "
402:                        + candidate + " but got " + decision,
403:                        decision == DecideRule.ACCEPT);
404:                decision = this .rule.decisionFor(plusOne);
405:                assertTrue("Domain: PlusOne Expect " + DecideRule.ACCEPT
406:                        + " for " + plusOne + " with via "
407:                        + plusOne.flattenVia() + " but got " + decision,
408:                        decision == DecideRule.ACCEPT);
409:                decision = this .rule.decisionFor(plusTwo);
410:                assertTrue("Domain: PlusTwo Expect " + DecideRule.ACCEPT
411:                        + " for " + plusTwo + " with via "
412:                        + plusTwo.flattenVia() + " but got " + decision,
413:                        decision == DecideRule.ACCEPT);
414:                UURI uuriThree = UURIFactory.getInstance("http://sloan.org");
415:                CandidateURI plusThree = new CandidateURI(uuriThree);
416:                plusThree.setVia(uuriTwo);
417:                decision = this .rule.decisionFor(plusThree);
418:                assertTrue("Domain: PlusThree Expect " + DecideRule.ACCEPT
419:                        + " for " + plusThree + " with via "
420:                        + plusThree.flattenVia() + " but got " + decision,
421:                        decision == DecideRule.ACCEPT);
422:                UURI uuriFour = UURIFactory.getInstance("http://example.com");
423:                CandidateURI plusFour = new CandidateURI(uuriFour);
424:                plusFour.setVia(uuriThree);
425:                decision = this .rule.decisionFor(plusFour);
426:                assertTrue("Domain: PlusFour Expect " + DecideRule.PASS
427:                        + " for " + plusFour + " with via "
428:                        + plusFour.flattenVia() + " but got " + decision,
429:                        decision == DecideRule.PASS);
430:            }
431:
432:            public void testFilter() throws InvalidAttributeValueException,
433:                    URIException, AttributeNotFoundException, MBeanException,
434:                    ReflectionException {
435:                FilterDecideRule dr = new FilterDecideRule(
436:                        "FilterDecideRule(ContentTypeRegExpFilter)");
437:                addDecideRule(dr);
438:                StringBuffer baseUri = new StringBuffer();
439:                UURI uuri = UURIFactory.getInstance("http://example.com/foo");
440:                CrawlURI curi = new CrawlURI(uuri);
441:                curi.setContentType("text/html");
442:                Object decision = this .rule.decisionFor(curi);
443:                // default for unconfigured FilterDecideRule is true from (empty)
444:                // filters, then ACCEPT because of true
445:                assertTrue("Expect " + DecideRule.ACCEPT + " but got "
446:                        + decision, decision == DecideRule.ACCEPT);
447:                ContentTypeRegExpFilter filt = new ContentTypeRegExpFilter(
448:                        "ContentTypeRegExpFilter", "app.*");
449:                dr.filters.addElement(null, filt);
450:                decision = this .rule.decisionFor(curi);
451:                // filter should now return false, making decision REJECT
452:                assertTrue("Expect " + DecideRule.REJECT + " but got "
453:                        + decision, decision == DecideRule.REJECT);
454:                curi.setContentType("application/octet-stream");
455:                decision = this .rule.decisionFor(curi);
456:                // filter should now return true, making decision ACCEPT
457:                assertTrue("Expect " + DecideRule.ACCEPT + " but got "
458:                        + decision, decision == DecideRule.ACCEPT);
459:                // change true answer to "PASS"; use String to simulate settings non-identity
460:                dr.setAttribute(new Attribute(
461:                        FilterDecideRule.ATTR_TRUE_DECISION, "PASS"));
462:                decision = this .rule.decisionFor(curi);
463:                assertTrue(
464:                        "Expect " + DecideRule.PASS + " but got " + decision,
465:                        decision == DecideRule.PASS);
466:            }
467:
468:            protected DecideRule addDecideRule(DecideRule dr)
469:                    throws InvalidAttributeValueException {
470:                MapType rules = this .rule.getRules(null);
471:                rules.addElement(null, dr);
472:                return dr;
473:            }
474:
475:            public void testContentTypeMatchesRegexpDecideRule()
476:                    throws Exception {
477:                ContentTypeMatchesRegExpDecideRule dr = new ContentTypeMatchesRegExpDecideRule(
478:                        "CTMREDRtest");
479:                DecideRule v = addDecideRule(dr);
480:
481:                v.setAttribute(new Attribute(
482:                        MatchesRegExpDecideRule.ATTR_REGEXP, "text/html"));
483:                UURI uuri = UURIFactory.getInstance("http://www.archive.org");
484:                CrawlURI crawlUri = new CrawlURI(uuri);
485:
486:                // no content type - let curi pass
487:                Object decision = this .rule.decisionFor(crawlUri);
488:                assertTrue("URI Expect " + DecideRule.PASS + " for " + crawlUri
489:                        + " but got " + decision, decision == DecideRule.PASS);
490:
491:                // non-matching content type - let curi pass
492:                crawlUri.setContentType("application/pdf");
493:                decision = this .rule.decisionFor(crawlUri);
494:                assertTrue("URI Expect " + DecideRule.PASS + " for " + crawlUri
495:                        + " but got " + decision, decision == DecideRule.PASS);
496:
497:                // matching content type - accept curi
498:                crawlUri.setContentType("text/html");
499:                decision = this .rule.decisionFor(crawlUri);
500:                assertTrue("URI Expect " + DecideRule.ACCEPT + " for "
501:                        + crawlUri + " but got " + decision,
502:                        decision == DecideRule.ACCEPT);
503:            }
504:
505:            public void testContentTypeNotMatchesRegexpDecideRule()
506:                    throws Exception {
507:                ContentTypeNotMatchesRegExpDecideRule dr = new ContentTypeNotMatchesRegExpDecideRule(
508:                        "CTNMREDRtest");
509:                DecideRule v = addDecideRule(dr);
510:
511:                v.setAttribute(new Attribute(
512:                        MatchesRegExpDecideRule.ATTR_REGEXP, "text/html"));
513:                UURI uuri = UURIFactory.getInstance("http://www.archive.org");
514:                CrawlURI crawlUri = new CrawlURI(uuri);
515:
516:                // no content type - let curi pass
517:                Object decision = this .rule.decisionFor(crawlUri);
518:                assertTrue("URI Expect " + DecideRule.PASS + " for " + crawlUri
519:                        + " but got " + decision, decision == DecideRule.PASS);
520:
521:                // matching content type - let curi pass
522:                crawlUri.setContentType("text/html");
523:                decision = this .rule.decisionFor(crawlUri);
524:                assertTrue("URI Expect " + DecideRule.PASS + " for " + crawlUri
525:                        + " but got " + decision, decision == DecideRule.PASS);
526:
527:                // non-matching content type - accept curi
528:                crawlUri.setContentType("application/pdf");
529:                decision = this .rule.decisionFor(crawlUri);
530:                assertTrue("URI Expect " + DecideRule.ACCEPT + " for "
531:                        + crawlUri + " but got " + decision,
532:                        decision == DecideRule.ACCEPT);
533:            }
534:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.