001: /* MapTypeTest
002: *
003: * $Id: MapTypeTest.java 4662 2006-09-25 23:45:21Z paul_jack $
004: *
005: * Created on Jan 29, 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.util.ArrayList;
028: import java.util.Iterator;
029: import java.util.List;
030:
031: import javax.management.Attribute;
032: import javax.management.AttributeNotFoundException;
033: import javax.management.InvalidAttributeValueException;
034: import javax.management.MBeanAttributeInfo;
035: import javax.management.MBeanException;
036: import javax.management.ReflectionException;
037:
038: import org.archive.crawler.datamodel.CrawlOrder;
039: import org.archive.crawler.prefetch.Preselector;
040:
041: /** JUnit tests for MapType
042: *
043: * @author John Erik Halse
044: *
045: */
046: public class MapTypeTest extends SettingsFrameworkTestCase {
047:
048: /*
049: * @see TmpDirTestCase#setUp()
050: */
051: protected void setUp() throws Exception {
052: super .setUp();
053: }
054:
055: /*
056: * @see TmpDirTestCase#tearDown()
057: */
058: protected void tearDown() throws Exception {
059: super .tearDown();
060: }
061:
062: /** Test different aspects of manipulating a MapType for the global
063: * settings.
064: *
065: * @throws InvalidAttributeValueException
066: * @throws AttributeNotFoundException
067: */
068: public void testAddRemoveSizeGlobal()
069: throws InvalidAttributeValueException,
070: AttributeNotFoundException, MBeanException,
071: ReflectionException {
072:
073: MapType map = (MapType) getSettingsHandler().getOrder()
074: .getAttribute(CrawlOrder.ATTR_PRE_FETCH_PROCESSORS);
075:
076: assertTrue("Map should be empty", map.isEmpty(null));
077: assertEquals("Map should be empty", map.size(null), 0);
078:
079: ModuleType module = new Preselector("testModule");
080: assertSame("Did not return added element", map.addElement(null,
081: module), module);
082: assertFalse("Map should contain a element", map.isEmpty(null));
083: assertEquals("Map should contain a element", map.size(null), 1);
084:
085: assertSame("Did not return removed element", map.removeElement(
086: null, "testModule"), module);
087: assertTrue("Map should be empty", map.isEmpty(null));
088: assertEquals("Map should be empty", map.size(null), 0);
089: }
090:
091: /** Test different aspects of manipulating a MapType for the per domain
092: * settings.
093: *
094: * @throws InvalidAttributeValueException
095: * @throws AttributeNotFoundException
096: * @throws MBeanException
097: * @throws ReflectionException
098: */
099: public void testAddRemoveSizeHost()
100: throws InvalidAttributeValueException,
101: AttributeNotFoundException, MBeanException,
102: ReflectionException {
103:
104: MapType map = (MapType) getSettingsHandler().getOrder()
105: .getAttribute(CrawlOrder.ATTR_HTTP_HEADERS);
106:
107: MBeanAttributeInfo atts[] = map.getMBeanInfo().getAttributes();
108: for (int i = 0; i < atts.length; i++) {
109: map.removeElement(getGlobalSettings(), atts[i].getName());
110: }
111:
112: assertTrue("Map should be empty", map
113: .isEmpty(getPerHostSettings()));
114: assertEquals("Map should be empty", 0, map
115: .size(getPerHostSettings()));
116:
117: ModuleType module1 = new Preselector("testModule1");
118: ModuleType module2 = new Preselector("testModule2");
119: ModuleType module3 = new Preselector("testModule3");
120:
121: assertSame("Did not return added element", module1, map
122: .addElement(getGlobalSettings(), module1));
123:
124: assertSame("Did not return added element", module2, map
125: .addElement(getPerHostSettings(), module2));
126:
127: assertSame("Did not return added element", module3, map
128: .addElement(getPerHostSettings(), module3));
129:
130: assertFalse("Map should contain elements", map
131: .isEmpty(getPerHostSettings()));
132: assertEquals("Wrong number of elements", 3, map
133: .size(getPerHostSettings()));
134: assertEquals("Wrong number of elements", 1, map
135: .size(getGlobalSettings()));
136:
137: module1.setAttribute(getPerHostSettings(), new SimpleType(
138: "enabled", "desc", new Boolean(false)));
139: checkOrder(getGlobalSettings(), new Type[] { module1 }, map);
140: checkOrder(getPerHostSettings(), new Type[] { module1, module2,
141: module3 }, map);
142:
143: assertSame("Did not return removed element", map.removeElement(
144: getGlobalSettings(), "testModule1"), module1);
145:
146: assertSame("Did not return removed element", map.removeElement(
147: getPerHostSettings(), "testModule2"), module2);
148:
149: assertSame("Did not return removed element", map.removeElement(
150: getPerHostSettings(), "testModule3"), module3);
151:
152: assertTrue("Map should be empty", map
153: .isEmpty(getPerHostSettings()));
154: assertEquals("Map should be empty", 0, map
155: .size(getPerHostSettings()));
156: }
157:
158: public void testMoveElementUp() throws AttributeNotFoundException,
159: MBeanException, ReflectionException,
160: InvalidAttributeValueException {
161: MapType map = (MapType) getSettingsHandler().getOrder()
162: .getAttribute(CrawlOrder.ATTR_PRE_FETCH_PROCESSORS);
163:
164: ModuleType module1 = new Preselector("testModule1");
165: ModuleType module2 = new Preselector("testModule2");
166: ModuleType module3 = new Preselector("testModule3");
167: map.addElement(null, module1);
168: map.addElement(null, module2);
169: map.addElement(null, module3);
170:
171: Type modules[] = new Type[] { module1, module2, module3 };
172: checkOrder(null, modules, map);
173:
174: assertTrue(map.moveElementUp(null, "testModule2"));
175:
176: modules = new Type[] { module2, module1, module3 };
177: checkOrder(null, modules, map);
178:
179: assertFalse(map.moveElementUp(null, "testModule2"));
180:
181: modules = new Type[] { module2, module1, module3 };
182: checkOrder(null, modules, map);
183: }
184:
185: public void testMoveElementDown()
186: throws InvalidAttributeValueException,
187: AttributeNotFoundException, MBeanException,
188: ReflectionException {
189: MapType map = (MapType) getSettingsHandler().getOrder()
190: .getAttribute(CrawlOrder.ATTR_PRE_FETCH_PROCESSORS);
191:
192: ModuleType module1 = new Preselector("testModule1");
193: ModuleType module2 = new Preselector("testModule2");
194: ModuleType module3 = new Preselector("testModule3");
195: map.addElement(null, module1);
196: map.addElement(null, module2);
197: map.addElement(null, module3);
198:
199: Type modules[] = new Type[] { module1, module2, module3 };
200: checkOrder(null, modules, map);
201:
202: assertTrue(map.moveElementDown(null, "testModule2"));
203:
204: modules = new Type[] { module1, module3, module2 };
205: checkOrder(null, modules, map);
206:
207: assertFalse(map.moveElementDown(null, "testModule2"));
208:
209: modules = new Type[] { module1, module3, module2 };
210: checkOrder(null, modules, map);
211: }
212:
213: /** Helper method for checking that elements are in a certain order after
214: * maipulating them.
215: *
216: * @param settings
217: * @param modules
218: * @param map
219: * @throws AttributeNotFoundException
220: * @throws MBeanException
221: * @throws ReflectionException
222: */
223: public void checkOrder(CrawlerSettings settings, Type[] modules,
224: MapType map) throws AttributeNotFoundException,
225: MBeanException, ReflectionException {
226:
227: settings = settings == null ? map.globalSettings() : settings;
228:
229: MBeanAttributeInfo atts[] = map.getMBeanInfo(settings)
230: .getAttributes();
231: assertEquals("AttributeInfo wrong length", modules.length,
232: atts.length);
233: for (int i = 0; i < atts.length; i++) {
234: assertEquals("AttributeInfo in wrong order", modules[i]
235: .getValue(), map.getAttribute(settings, atts[i]
236: .getName()));
237: }
238:
239: Iterator it = map.iterator(settings);
240: int i = 0;
241: while (it.hasNext()) {
242: assertEquals("Iterator in wrong order", modules[i]
243: .getValue(), ((Attribute) it.next()).getValue());
244: i++;
245: }
246: assertEquals("Iterator wrong length", modules.length, i);
247: }
248:
249: public void testGetDefaultValue()
250: throws AttributeNotFoundException, MBeanException,
251: ReflectionException {
252: MapType map = (MapType) getSettingsHandler().getOrder()
253: .getAttribute(CrawlOrder.ATTR_HTTP_HEADERS);
254:
255: assertSame(map.getDefaultValue(), map);
256: }
257:
258: public void testGetLegalValues() throws AttributeNotFoundException,
259: MBeanException, ReflectionException {
260: MapType map = (MapType) getSettingsHandler().getOrder()
261: .getAttribute(CrawlOrder.ATTR_HTTP_HEADERS);
262:
263: assertNull(map.getLegalValues());
264: }
265:
266: /*
267: * Test for Object getValue()
268: */
269: public void testGetValue() throws AttributeNotFoundException,
270: MBeanException, ReflectionException {
271: MapType map = (MapType) getSettingsHandler().getOrder()
272: .getAttribute(CrawlOrder.ATTR_HTTP_HEADERS);
273:
274: assertSame(map.getValue(), map);
275: }
276:
277: /* Test for getAttribute
278: *
279: */
280: public void testGetAttribute() throws AttributeNotFoundException,
281: MBeanException, ReflectionException,
282: InvalidAttributeValueException {
283: MapType map = (MapType) getSettingsHandler().getOrder()
284: .getAttribute(CrawlOrder.ATTR_HTTP_HEADERS);
285:
286: SimpleType type1 = new SimpleType("testType1", "description",
287: "value");
288: SimpleType type2 = new SimpleType("testType2", "description",
289: "value");
290: map.addElement(getGlobalSettings(), type1);
291: map.addElement(getPerDomainSettings(), type2);
292: assertEquals(type1.getValue(), map.getAttribute(
293: getPerHostSettings(), "testType1"));
294: assertEquals(type2.getValue(), map.getAttribute(
295: getPerHostSettings(), "testType2"));
296: try {
297: map.getAttribute(getGlobalSettings(), "testType2");
298: fail();
299: } catch (AttributeNotFoundException e) {
300: // OK
301: }
302: }
303:
304: public void testListAttributes() throws AttributeNotFoundException,
305: MBeanException, ReflectionException,
306: InvalidAttributeValueException {
307: MapType map = (MapType) getSettingsHandler().getOrder()
308: .getAttribute(CrawlOrder.ATTR_HTTP_HEADERS);
309:
310: List<Attribute> atts = new ArrayList<Attribute>();
311: for (Iterator it = map.iterator(null); it.hasNext();) {
312: atts.add(new SimpleType("", "", ((Attribute) it.next())
313: .getValue()));
314: }
315:
316: SimpleType type1 = new SimpleType("testType1", "description",
317: "value");
318: SimpleType type2 = new SimpleType("testType2", "description",
319: "value");
320: map.addElement(getGlobalSettings(), type1);
321: map.addElement(getPerDomainSettings(), type2);
322: getSettingsHandler().writeSettingsObject(getGlobalSettings());
323: getSettingsHandler()
324: .writeSettingsObject(getPerDomainSettings());
325:
326: atts.add(type1);
327: atts.add(type2);
328: Type modules[] = (Type[]) atts.toArray(new Type[0]);
329: checkOrder(getPerHostSettings(), modules, map);
330:
331: XMLSettingsHandler newHandler = new XMLSettingsHandler(
332: getOrderFile());
333: newHandler.initialize();
334: CrawlerSettings newPer = newHandler
335: .getSettingsObject(getPerDomainSettings().getScope());
336:
337: checkOrder(newPer, modules, map);
338: }
339:
340: }
|