View Javadoc

    1   /* CrawlerSettingsTest
    2    *
    3    * $Id: CrawlerSettingsTest.java 4662 2006-09-25 23:45:21Z paul_jack $
    4    *
    5    * Created on Jan 28, 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
    12   * it under the terms of the GNU Lesser Public License as published by
    13   * the Free Software Foundation; either version 2.1 of the License, or
    14   * any later version.
    15   *
    16   * Heritrix is distributed in the hope that it will be useful,
    17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    19   * GNU Lesser Public License for more details.
    20   *
    21   * You should have received a copy of the GNU Lesser Public License
    22   * along with Heritrix; if not, write to the Free Software
    23   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    24   */
    25  package org.archive.crawler.settings;
    26
    27  import java.io.ByteArrayInputStream;
    28  import java.io.ByteArrayOutputStream;
    29  import java.io.IOException;
    30  import java.io.ObjectInputStream;
    31  import java.io.ObjectOutputStream;
    32
    33  import javax.management.AttributeNotFoundException;
    34  import javax.management.MBeanException;
    35  import javax.management.ReflectionException;
    36
    37
    38  /*** Test the CrawlerSettings object
    39   *
    40   * @author John Erik Halse
    41   */
    42  public class CrawlerSettingsTest extends SettingsFrameworkTestCase {
    43
    44      /*
    45       * @see TestCase#setUp()
    46       */
    47      protected void setUp() throws Exception {
    48          super.setUp();
    49      }
    50
    51      /*
    52       * @see TestCase#tearDown()
    53       */
    54      protected void tearDown() throws Exception {
    55          super.tearDown();
    56      }
    57
    58      final public void testAddComplexType() {
    59          ModuleType mod = new ModuleType("name");
    60          DataContainer data = getGlobalSettings().addComplexType(mod);
    61          assertNotNull(data);
    62      }
    63
    64      final public void testGetModule() {
    65          ModuleType mod = new ModuleType("name");
    66          getGlobalSettings().addComplexType(mod);
    67          assertSame(mod, getGlobalSettings().getModule("name"));
    68      }
    69
    70      public void testSerializingSimpleModuleType()
    71      throws IOException, ClassNotFoundException {
    72          ModuleType mt =
    73              new ModuleType("testSerializingSimpleModuleType");
    74          ModuleType mtDeserialized = (ModuleType)serializeDeserialize(mt);
    75          assertEquals(mt.getName(), mtDeserialized.getName());
    76      }
    77
    78      public void testSerializingStringAttributeModuleType()
    79      throws IOException, ClassNotFoundException, AttributeNotFoundException,
    80      MBeanException, ReflectionException {
    81          ModuleType mt =
    82              new ModuleType("testSerializingStringAttributeModuleType");
    83          final String value = "value";
    84          mt.addElementToDefinition(new SimpleType("name", "description",
    85              value));
    86          ModuleType mtDeserialized = (ModuleType)serializeDeserialize(mt);
    87          assertEquals(mt.getName(), mtDeserialized.getName());
    88          assertEquals(value, (String)mtDeserialized.getAttribute("name"));
    89      }
    90
    91      public void testSerializingTextField()
    92      throws IOException, ClassNotFoundException, AttributeNotFoundException,
    93      MBeanException, ReflectionException {
    94          TextField tf = new TextField("testSerializingTextField");
    95          TextField tfDeserialized = (TextField)serializeDeserialize(tf);
    96          assertEquals(tf.toString(), tfDeserialized.toString());
    97      }
    98
    99      protected Object serializeDeserialize(Object obj)
    100     throws IOException, ClassNotFoundException {
    101         ByteArrayOutputStream baos = new ByteArrayOutputStream();
    102         ObjectOutputStream oos = new ObjectOutputStream(baos);
    103         oos.writeObject(obj);
    104         oos.close();
    105         byte [] objectBytes = baos.toByteArray();
    106         ObjectInputStream ois =
    107             new ObjectInputStream(new ByteArrayInputStream(objectBytes));
    108         return ois.readObject();
    109     }
    110 }