1 package net.javacoding.jspider.mod.rule; 2 3 import junit.framework.TestCase; 4 import net.javacoding.jspider.mockobjects.OverridingPropertySet; 5 import net.javacoding.jspider.api.model.Decision; 6 import net.javacoding.jspider.spi.Rule; 7 import net.javacoding.jspider.core.util.config.ConfigurationFactory; 8 9 import java.net.URL ; 10 11 14 public class MaxResourcesPerSiteRuleTest extends TestCase { 15 16 protected OverridingPropertySet config; 17 18 public MaxResourcesPerSiteRuleTest ( ) { 19 super ( "MaxResourcesPerSiteRuleTest" ); 20 } 21 22 protected void setUp() throws Exception { 23 ConfigurationFactory.getConfiguration(ConfigurationFactory.CONFIG_UNITTEST); 24 config = new OverridingPropertySet ( null ); 25 } 26 27 public void testSimple ( ) throws Exception { 28 int max = 1; 29 int times = 1; 30 String urlString = "http://j-spider.sourceforge.net/test.html"; 31 int expected = Decision.RULE_ACCEPT; 32 33 applyTest(max, urlString, times, expected ); 34 } 35 36 public void testSimpleViolation ( ) throws Exception { 37 int max = 1; 38 int times = 2; 39 String urlString = "http://j-spider.sourceforge.net/test.html"; 40 int expected = Decision.RULE_IGNORE; 41 42 applyTest(max, urlString, times, expected ); 43 } 44 45 public void testZeroAllowed ( ) throws Exception { 46 int max = 0; 47 int times = 1; 48 String urlString = "http://j-spider.sourceforge.net/test.html"; 49 int expected = Decision.RULE_IGNORE; 50 51 applyTest(max, urlString, times, expected ); 52 } 53 54 public void testTwoSites ( ) throws Exception { 55 URL url1 = new URL ("http://j-spider.sourceforge.net/index.html"); 56 URL url2 = new URL ("http://www.somehost.com/index.html"); 57 URL url3 = new URL ("http://j-spider.sourceforge.net/test.html"); 58 59 int max = 3; 60 61 config.setValue(MaxResourcesPerSiteRule.MAX, new Integer (max)); 62 Rule rule = new MaxResourcesPerSiteRule(config) ; 63 Decision decision = null; 64 decision = rule.apply(null, null, url1); assertEquals("decision not as expected", Decision.RULE_ACCEPT, decision.getDecision()); 66 decision = rule.apply(null, null, url2); assertEquals("decision not as expected", Decision.RULE_ACCEPT, decision.getDecision()); 68 decision = rule.apply(null, null, url3); assertEquals("decision not as expected", Decision.RULE_ACCEPT, decision.getDecision()); 70 decision = rule.apply(null, null, url1); assertEquals("decision not as expected", Decision.RULE_ACCEPT, decision.getDecision()); 72 decision = rule.apply(null, null, url2); assertEquals("decision not as expected", Decision.RULE_ACCEPT, decision.getDecision()); 74 decision = rule.apply(null, null, url3); assertEquals("decision not as expected", Decision.RULE_IGNORE, decision.getDecision()); 76 } 77 78 public void applyTest ( int max, String urlString, int times, int lastExpected ) throws Exception { 79 config.setValue(MaxResourcesPerSiteRule.MAX, new Integer (max)); 80 81 URL url = new URL (urlString); 82 Rule rule = new MaxResourcesPerSiteRule(config) ; 83 Decision decision = null; 84 for ( int i = 0; i < times; i++ ) { 85 decision = rule.apply(null, null, url); 86 } 87 assertEquals("final decision not as expected", lastExpected, decision.getDecision()); 88 } 89 90 } 91 | Popular Tags |