001: /* ConfiguredDecideRuleTest
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.archive.crawler.datamodel.CrawlOrder;
034: import org.archive.crawler.settings.MapType;
035: import org.archive.crawler.settings.SettingsHandler;
036: import org.archive.crawler.settings.XMLSettingsHandler;
037: import org.archive.util.TmpDirTestCase;
038:
039: /**
040: * @author stack
041: * @version $Date: 2005-04-05 01:12:11 +0000 (Tue, 05 Apr 2005) $, $Revision: 3318 $
042: */
043: public class ConfiguredDecideRuleTest extends TmpDirTestCase {
044: /**
045: * Gets setup by {@link #setUp()}.
046: */
047: private ConfiguredDecideRule rule = null;
048:
049: protected void setUp() throws Exception {
050: super .setUp();
051: final String name = this .getClass().getName();
052: SettingsHandler settingsHandler = new XMLSettingsHandler(
053: new File(getTmpDir(), name + ".order.xml"));
054: settingsHandler.initialize();
055: // Create a new ConfigureDecideRule instance and add it to a MapType
056: // (I can change MapTypes after instantiation). The chosen MapType
057: // is the rules canonicalization rules list.
058: this .rule = (ConfiguredDecideRule) ((MapType) settingsHandler
059: .getOrder().getAttribute(CrawlOrder.ATTR_RULES))
060: .addElement(settingsHandler.getSettingsObject(null),
061: new ConfiguredDecideRule(name));
062: }
063:
064: public void testDefault() {
065: Object decision = rule.decisionFor(new Object());
066: assertTrue("Wrong answer " + decision,
067: decision == DecideRule.ACCEPT);
068: }
069:
070: public void testACCEPT() throws AttributeNotFoundException,
071: InvalidAttributeValueException, MBeanException,
072: ReflectionException {
073: runTest(DecideRule.ACCEPT);
074: }
075:
076: public void testPASS() throws AttributeNotFoundException,
077: MBeanException, ReflectionException {
078: String exceptionMessage = null;
079: try {
080: runTest(DecideRule.PASS);
081: } catch (InvalidAttributeValueException e) {
082: exceptionMessage = e.getMessage();
083: }
084: assertNotNull("Did not get expected exception",
085: exceptionMessage);
086: }
087:
088: public void testREJECT() throws AttributeNotFoundException,
089: InvalidAttributeValueException, MBeanException,
090: ReflectionException {
091: runTest(DecideRule.REJECT);
092: }
093:
094: protected void runTest(String expectedResult)
095: throws AttributeNotFoundException,
096: InvalidAttributeValueException, MBeanException,
097: ReflectionException {
098: configure(expectedResult);
099: Object decision = rule.decisionFor(new Object());
100: assertTrue("Expected " + expectedResult + " but got answer "
101: + decision, decision == expectedResult);
102: }
103:
104: protected void configure(String setting)
105: throws AttributeNotFoundException,
106: InvalidAttributeValueException, MBeanException,
107: ReflectionException {
108: this .rule.setAttribute(new Attribute(
109: ConfiguredDecideRule.ATTR_DECISION, setting));
110: }
111: }
|