1 23 package org.archive.crawler.deciderules; 24 25 import java.io.File ; 26 27 import javax.management.Attribute ; 28 import javax.management.AttributeNotFoundException ; 29 import javax.management.InvalidAttributeValueException ; 30 import javax.management.MBeanException ; 31 import javax.management.ReflectionException ; 32 33 import org.archive.crawler.datamodel.CrawlOrder; 34 import org.archive.crawler.settings.MapType; 35 import org.archive.crawler.settings.SettingsHandler; 36 import org.archive.crawler.settings.XMLSettingsHandler; 37 import org.archive.util.TmpDirTestCase; 38 39 43 public class ConfiguredDecideRuleTest extends TmpDirTestCase { 44 47 private ConfiguredDecideRule rule = null; 48 49 protected void setUp() throws Exception { 50 super.setUp(); 51 final String name = this.getClass().getName(); 52 SettingsHandler settingsHandler = new XMLSettingsHandler( 53 new File (getTmpDir(), name + ".order.xml")); 54 settingsHandler.initialize(); 55 this.rule = (ConfiguredDecideRule)((MapType)settingsHandler.getOrder(). 59 getAttribute(CrawlOrder.ATTR_RULES)).addElement(settingsHandler. 60 getSettingsObject(null), new ConfiguredDecideRule(name)); 61 } 62 63 public void testDefault() { 64 Object decision = rule.decisionFor(new Object ()); 65 assertTrue("Wrong answer " + decision, decision == DecideRule.ACCEPT); 66 } 67 68 public void testACCEPT() 69 throws AttributeNotFoundException , InvalidAttributeValueException , 70 MBeanException , ReflectionException { 71 runTest(DecideRule.ACCEPT); 72 } 73 74 public void testPASS() 75 throws AttributeNotFoundException , MBeanException , ReflectionException { 76 String exceptionMessage = null; 77 try { 78 runTest(DecideRule.PASS); 79 } catch(InvalidAttributeValueException e) { 80 exceptionMessage = e.getMessage(); 81 } 82 assertNotNull("Did not get expected exception", exceptionMessage); 83 } 84 85 public void testREJECT() 86 throws AttributeNotFoundException , InvalidAttributeValueException , 87 MBeanException , ReflectionException { 88 runTest(DecideRule.REJECT); 89 } 90 91 protected void runTest(String expectedResult) 92 throws AttributeNotFoundException , InvalidAttributeValueException , 93 MBeanException , ReflectionException { 94 configure(expectedResult); 95 Object decision = rule.decisionFor(new Object ()); 96 assertTrue("Expected " + expectedResult + " but got answer " + 97 decision, decision == expectedResult); 98 } 99 100 protected void configure(String setting) 101 throws AttributeNotFoundException , InvalidAttributeValueException , 102 MBeanException , ReflectionException { 103 this.rule.setAttribute( 104 new Attribute (ConfiguredDecideRule.ATTR_DECISION, setting)); 105 } 106 } 107 | Popular Tags |