View Javadoc

    1   /* OverrideTest
    2    *
    3    * $Id: OverrideTest.java 2168 2004-05-28 22:33:09Z stack-sf $
    4    *
    5    * Created on Feb 20, 2004
    6    *
    7    * Copyright (C) 2004 Internet Archive.
    8    *
    9    * This file is part of the Heritrix web crawler (crawler.archive.org).
    10   *
    11   * Heritrix is free software; you can redistribute it and/or modify it under the
    12   * terms of the GNU Lesser Public License as published by the Free Software
    13   * Foundation; either version 2.1 of the License, or any later version.
    14   *
    15   * Heritrix is distributed in the hope that it will be useful, but WITHOUT ANY
    16   * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
    17   * A PARTICULAR PURPOSE. See the GNU Lesser Public License for more details.
    18   *
    19   * You should have received a copy of the GNU Lesser Public License along with
    20   * Heritrix; if not, write to the Free Software Foundation, Inc., 59 Temple
    21   * Place, Suite 330, Boston, MA 02111-1307 USA
    22   */
    23  package org.archive.crawler.settings;
    24
    25  import javax.management.AttributeNotFoundException;
    26  import javax.management.InvalidAttributeValueException;
    27  import javax.management.MBeanException;
    28  import javax.management.ReflectionException;
    29
    30  import org.archive.crawler.datamodel.CrawlOrder;
    31  import org.archive.crawler.framework.Processor;
    32
    33  /***
    34   * Test the concept of overrides.
    35   *
    36   * As this test is testing a concept, it involves more than one class to be
    37   * tested. Thus the name of this test doesn't match a class name.
    38   *
    39   * @author John Erik Halse
    40   *
    41   */
    42  public class OverrideTest extends SettingsFrameworkTestCase {
    43
    44      /*
    45       * @see SettingsFrameworkTestCase#setUp()
    46       */
    47      protected void setUp() throws Exception {
    48          super.setUp();
    49      }
    50
    51      /*
    52       * @see SettingsFrameworkTestCase#tearDown()
    53       */
    54      protected void tearDown() throws Exception {
    55          super.tearDown();
    56      }
    57
    58      public void testOverridingOfGlobalAttribute()
    59              throws AttributeNotFoundException, MBeanException,
    60              ReflectionException, InvalidAttributeValueException {
    61
    62          final String MODULE_NAME = "module1";
    63          ModuleType module1 = new ModuleType(MODULE_NAME);
    64          ModuleType module2 = new Processor(MODULE_NAME, "Descr");
    65
    66          // Set up override
    67          MapType headers = (MapType) getSettingsHandler().getOrder()
    68                  .getAttribute(CrawlOrder.ATTR_HTTP_HEADERS);
    69          headers.addElement(getGlobalSettings(), module1);
    70          headers.setAttribute(getPerDomainSettings(), module2);
    71
    72          // Read back values to see if we get the right ones
    73          ModuleType getMod;
    74          getMod = (ModuleType) headers.getAttribute(getGlobalSettings(),
    75                  MODULE_NAME);
    76          assertSame("Wrong global value", module1, getMod);
    77          assertEquals("Wrong class type", module1.getClass().getName(), headers
    78                  .getAttributeInfo(getGlobalSettings(), MODULE_NAME).getType());
    79
    80          getMod = (ModuleType) headers.getAttribute(getPerDomainSettings(),
    81                  MODULE_NAME);
    82          assertSame("Wrong domain value", module2, getMod);
    83          assertEquals("Wrong class type", module2.getClass().getName(), headers
    84                  .getAttributeInfo(getPerDomainSettings(), MODULE_NAME)
    85                  .getType());
    86
    87          getMod = (ModuleType) headers.getAttribute(getPerHostSettings(),
    88                  MODULE_NAME);
    89          assertSame("Wrong host value", module2, getMod);
    90          assertEquals("Wrong class type", module2.getClass().getName(), headers
    91                  .getAttributeInfo(getPerHostSettings(), MODULE_NAME).getType());
    92      }
    93
    94      public void testOverridingOfNonGlobalAttribute()
    95              throws AttributeNotFoundException, MBeanException,
    96              ReflectionException, InvalidAttributeValueException {
    97          final String MODULE_NAME = "module1";
    98          ModuleType module1 = new ModuleType(MODULE_NAME);
    99          ModuleType module2 = new Processor(MODULE_NAME, "Descr");
    100
    101         // Set up override
    102         MapType headers = (MapType) getSettingsHandler().getOrder()
    103                 .getAttribute(CrawlOrder.ATTR_HTTP_HEADERS);
    104         headers.addElement(getPerDomainSettings(), module1);
    105         headers.setAttribute(getPerHostSettings(), module2);
    106
    107         // Read back values to see if we get the right ones
    108         ModuleType getMod;
    109         try {
    110             getMod = (ModuleType) headers.getAttribute(getGlobalSettings(),
    111                     MODULE_NAME);
    112             fail("Global value should not exist");
    113         } catch (AttributeNotFoundException e) {
    114             // OK! this should throw an exception;
    115         }
    116
    117         getMod = (ModuleType) headers.getAttribute(getPerDomainSettings(),
    118                 MODULE_NAME);
    119         assertSame("Wrong domain value", module1, getMod);
    120         assertEquals("Wrong class type", module1.getClass().getName(), headers
    121                 .getAttributeInfo(getPerDomainSettings(), MODULE_NAME)
    122                 .getType());
    123
    124         getMod = (ModuleType) headers.getAttribute(getPerHostSettings(),
    125                 MODULE_NAME);
    126         assertSame("Wrong host value", module2, getMod);
    127         assertEquals("Wrong class type", module2.getClass().getName(), headers
    128                 .getAttributeInfo(getPerHostSettings(), MODULE_NAME).getType());
    129     }
    130
    131 }