001: /* CrawlerSettingsTest
002: *
003: * $Id: CrawlerSettingsTest.java 4662 2006-09-25 23:45:21Z paul_jack $
004: *
005: * Created on Jan 28, 2004
006: *
007: * Copyright (C) 2004 Internet Archive.
008: *
009: * This file is part of the Heritrix web crawler (crawler.archive.org).
010: *
011: * Heritrix is free software; you can redistribute it and/or modify
012: * it under the terms of the GNU Lesser Public License as published by
013: * the Free Software Foundation; either version 2.1 of the License, or
014: * any later version.
015: *
016: * Heritrix is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
019: * GNU Lesser Public License for more details.
020: *
021: * You should have received a copy of the GNU Lesser Public License
022: * along with Heritrix; if not, write to the Free Software
023: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024: */
025: package org.archive.crawler.settings;
026:
027: import java.io.ByteArrayInputStream;
028: import java.io.ByteArrayOutputStream;
029: import java.io.IOException;
030: import java.io.ObjectInputStream;
031: import java.io.ObjectOutputStream;
032:
033: import javax.management.AttributeNotFoundException;
034: import javax.management.MBeanException;
035: import javax.management.ReflectionException;
036:
037: /** Test the CrawlerSettings object
038: *
039: * @author John Erik Halse
040: */
041: public class CrawlerSettingsTest extends SettingsFrameworkTestCase {
042:
043: /*
044: * @see TestCase#setUp()
045: */
046: protected void setUp() throws Exception {
047: super .setUp();
048: }
049:
050: /*
051: * @see TestCase#tearDown()
052: */
053: protected void tearDown() throws Exception {
054: super .tearDown();
055: }
056:
057: final public void testAddComplexType() {
058: ModuleType mod = new ModuleType("name");
059: DataContainer data = getGlobalSettings().addComplexType(mod);
060: assertNotNull(data);
061: }
062:
063: final public void testGetModule() {
064: ModuleType mod = new ModuleType("name");
065: getGlobalSettings().addComplexType(mod);
066: assertSame(mod, getGlobalSettings().getModule("name"));
067: }
068:
069: public void testSerializingSimpleModuleType() throws IOException,
070: ClassNotFoundException {
071: ModuleType mt = new ModuleType(
072: "testSerializingSimpleModuleType");
073: ModuleType mtDeserialized = (ModuleType) serializeDeserialize(mt);
074: assertEquals(mt.getName(), mtDeserialized.getName());
075: }
076:
077: public void testSerializingStringAttributeModuleType()
078: throws IOException, ClassNotFoundException,
079: AttributeNotFoundException, MBeanException,
080: ReflectionException {
081: ModuleType mt = new ModuleType(
082: "testSerializingStringAttributeModuleType");
083: final String value = "value";
084: mt.addElementToDefinition(new SimpleType("name", "description",
085: value));
086: ModuleType mtDeserialized = (ModuleType) serializeDeserialize(mt);
087: assertEquals(mt.getName(), mtDeserialized.getName());
088: assertEquals(value, (String) mtDeserialized
089: .getAttribute("name"));
090: }
091:
092: public void testSerializingTextField() throws IOException,
093: ClassNotFoundException, AttributeNotFoundException,
094: MBeanException, ReflectionException {
095: TextField tf = new TextField("testSerializingTextField");
096: TextField tfDeserialized = (TextField) serializeDeserialize(tf);
097: assertEquals(tf.toString(), tfDeserialized.toString());
098: }
099:
100: protected Object serializeDeserialize(Object obj)
101: throws IOException, ClassNotFoundException {
102: ByteArrayOutputStream baos = new ByteArrayOutputStream();
103: ObjectOutputStream oos = new ObjectOutputStream(baos);
104: oos.writeObject(obj);
105: oos.close();
106: byte[] objectBytes = baos.toByteArray();
107: ObjectInputStream ois = new ObjectInputStream(
108: new ByteArrayInputStream(objectBytes));
109: return ois.readObject();
110: }
111: }
|