1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
46
47 protected void setUp() throws Exception {
48 super.setUp();
49 }
50
51
52
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 }