    1   /* ConfiguredDecideRuleTest
    2    *
    3    * Created on Apr 4, 2005
    4    *
    5    * Copyright (C) 2005 Internet Archive.
    6    *
    7    * This file is part of the Heritrix web crawler (crawler.archive.org).
    8    *
    9    * Heritrix is free software; you can redistribute it and/or modify
    10   * it under the terms of the GNU Lesser Public License as published by
    11   * the Free Software Foundation; either version 2.1 of the License, or
    12   * any later version.
    13   *
    14   * Heritrix is distributed in the hope that it will be useful,
    15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    17   * GNU Lesser Public License for more details.
    18   *
    19   * You should have received a copy of the GNU Lesser Public License
    20   * along with Heritrix; if not, write to the Free Software
    21   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    22   */
    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  /***
    40   * @author stack
    41   * @version $Date: 2005-04-05 01:12:11 +0000 (Tue, 05 Apr 2005) $, $Revision: 3318 $
    42   */
    43  public class ConfiguredDecideRuleTest extends TmpDirTestCase {
    44      /***
    45       * Gets setup by {@link #setUp()}.
    46       */
    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          // Create a new ConfigureDecideRule instance and add it to a MapType
    56          // (I can change MapTypes after instantiation).  The chosen MapType
    57          // is the rules canonicalization rules list.
    58          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 }