View Javadoc

1   /* MapTypeTest
2    *
3    * $Id: MapTypeTest.java 6082 2008-12-09 02:03:13Z gojomo $
4    *
5    * Created on Jan 29, 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.util.ArrayList;
28  import java.util.Iterator;
29  import java.util.List;
30  
31  import javax.management.Attribute;
32  import javax.management.AttributeNotFoundException;
33  import javax.management.InvalidAttributeValueException;
34  import javax.management.MBeanAttributeInfo;
35  import javax.management.MBeanException;
36  import javax.management.ReflectionException;
37  
38  import org.archive.crawler.datamodel.CrawlOrder;
39  import org.archive.crawler.prefetch.Preselector;
40  
41  /*** JUnit tests for MapType
42   *
43   * @author John Erik Halse
44   *
45   */
46  public class MapTypeTest extends SettingsFrameworkTestCase {
47  
48      /*** Test different aspects of manipulating a MapType for the global
49       * settings.
50       *
51       * @throws InvalidAttributeValueException
52       * @throws AttributeNotFoundException
53       */
54      public void testAddRemoveSizeGlobal()
55              throws InvalidAttributeValueException, AttributeNotFoundException,
56              MBeanException, ReflectionException {
57  
58          MapType map = (MapType) getSettingsHandler().getOrder().getAttribute(
59                  CrawlOrder.ATTR_PRE_FETCH_PROCESSORS);
60  
61          assertTrue("Map should be empty", map.isEmpty(null));
62          assertEquals("Map should be empty", map.size(null), 0);
63  
64          ModuleType module = new Preselector("testModule");
65          assertSame("Did not return added element",
66                  map.addElement(null, module), module);
67          assertFalse("Map should contain a element", map.isEmpty(null));
68          assertEquals("Map should contain a element", map.size(null), 1);
69  
70          assertSame("Did not return removed element", map.removeElement(null,
71                  "testModule"), module);
72          assertTrue("Map should be empty", map.isEmpty(null));
73          assertEquals("Map should be empty", map.size(null), 0);
74      }
75  
76      /*** Test different aspects of manipulating a MapType for the per domain
77       * settings.
78       *
79       * @throws InvalidAttributeValueException
80       * @throws AttributeNotFoundException
81       * @throws MBeanException
82       * @throws ReflectionException
83       */
84      public void testAddRemoveSizeHost()
85             throws InvalidAttributeValueException, AttributeNotFoundException,
86                    MBeanException, ReflectionException {
87  
88          MapType map = (MapType) getSettingsHandler().getOrder().getAttribute(
89                  CrawlOrder.ATTR_HTTP_HEADERS);
90  
91          MBeanAttributeInfo atts[] = map.getMBeanInfo().getAttributes();
92          for (int i = 0; i < atts.length; i++) {
93              map.removeElement(getGlobalSettings(), atts[i].getName());
94          }
95  
96          assertTrue("Map should be empty", map.isEmpty(getPerHostSettings()));
97          assertEquals("Map should be empty", 0, map.size(getPerHostSettings()));
98  
99          ModuleType module1 = new Preselector("testModule1");
100         ModuleType module2 = new Preselector("testModule2");
101         ModuleType module3 = new Preselector("testModule3");
102 
103         assertSame("Did not return added element", module1,
104             map.addElement(getGlobalSettings(), module1));
105 
106         assertSame("Did not return added element", module2,
107             map.addElement(getPerHostSettings(), module2));
108 
109         assertSame("Did not return added element", module3,
110             map.addElement(getPerHostSettings(), module3));
111 
112         assertFalse("Map should contain elements",
113             map.isEmpty(getPerHostSettings()));
114         assertEquals("Wrong number of elements", 3,
115             map.size(getPerHostSettings()));
116         assertEquals("Wrong number of elements", 1,
117             map.size(getGlobalSettings()));
118 
119         module1.setAttribute(getPerHostSettings(), new SimpleType("enabled",
120                 "desc", new Boolean(false)));
121         checkOrder(getGlobalSettings(), new Type[] { module1}, map);
122         checkOrder(getPerHostSettings(),
123                 new Type[] { module1, module2, module3}, map);
124 
125         assertSame("Did not return removed element",
126             map.removeElement(getGlobalSettings(), "testModule1"), module1);
127 
128         assertSame("Did not return removed element",
129             map.removeElement(getPerHostSettings(), "testModule2"), module2);
130 
131         assertSame("Did not return removed element",
132             map.removeElement(getPerHostSettings(), "testModule3"), module3);
133 
134         assertTrue("Map should be empty", map.isEmpty(getPerHostSettings()));
135         assertEquals("Map should be empty", 0, map.size(getPerHostSettings()));
136     }
137 
138     public void testMoveElementUp() throws AttributeNotFoundException,
139             MBeanException, ReflectionException, InvalidAttributeValueException {
140         MapType map = (MapType) getSettingsHandler().getOrder().getAttribute(
141                 CrawlOrder.ATTR_PRE_FETCH_PROCESSORS);
142 
143         ModuleType module1 = new Preselector("testModule1");
144         ModuleType module2 = new Preselector("testModule2");
145         ModuleType module3 = new Preselector("testModule3");
146         map.addElement(null, module1);
147         map.addElement(null, module2);
148         map.addElement(null, module3);
149 
150         Type modules[] = new Type[] {module1, module2, module3};
151         checkOrder(null, modules, map);
152 
153         assertTrue(map.moveElementUp(null, "testModule2"));
154 
155         modules = new Type[] {module2, module1, module3};
156         checkOrder(null, modules, map);
157 
158         assertFalse(map.moveElementUp(null, "testModule2"));
159 
160         modules = new Type[] {module2, module1, module3};
161         checkOrder(null, modules, map);
162     }
163 
164     public void testMoveElementDown() throws InvalidAttributeValueException,
165             AttributeNotFoundException, MBeanException, ReflectionException {
166         MapType map = (MapType) getSettingsHandler().getOrder().getAttribute(
167                 CrawlOrder.ATTR_PRE_FETCH_PROCESSORS);
168 
169         ModuleType module1 = new Preselector("testModule1");
170         ModuleType module2 = new Preselector("testModule2");
171         ModuleType module3 = new Preselector("testModule3");
172         map.addElement(null, module1);
173         map.addElement(null, module2);
174         map.addElement(null, module3);
175 
176         Type modules[] = new Type[] {module1, module2, module3};
177         checkOrder(null, modules, map);
178 
179         assertTrue(map.moveElementDown(null, "testModule2"));
180 
181         modules = new Type[] {module1, module3, module2};
182         checkOrder(null, modules, map);
183 
184         assertFalse(map.moveElementDown(null, "testModule2"));
185 
186         modules = new Type[] {module1, module3, module2};
187         checkOrder(null, modules, map);
188     }
189 
190     /*** Helper method for checking that elements are in a certain order after
191      * maipulating them.
192      *
193      * @param settings
194      * @param modules
195      * @param map
196      * @throws AttributeNotFoundException
197      * @throws MBeanException
198      * @throws ReflectionException
199      */
200     public void checkOrder(CrawlerSettings settings, Type[] modules, MapType map)
201            throws AttributeNotFoundException, MBeanException, ReflectionException {
202 
203         settings = settings == null ? map.globalSettings() : settings;
204 
205         MBeanAttributeInfo atts[] = map.getMBeanInfo(settings).getAttributes();
206         assertEquals("AttributeInfo wrong length", modules.length, atts.length);
207         for(int i=0; i<atts.length; i++) {
208             assertEquals("AttributeInfo in wrong order", modules[i].getValue(),
209                 map.getAttribute(settings, atts[i].getName()));
210         }
211 
212         Iterator it = map.iterator(settings);
213         int i = 0;
214         while(it.hasNext()) {
215             assertEquals("Iterator in wrong order", modules[i].getValue(),
216                     ((Attribute) it.next()).getValue());
217             i++;
218         }
219         assertEquals("Iterator wrong length", modules.length, i);
220     }
221 
222     public void testGetDefaultValue() throws AttributeNotFoundException,
223             MBeanException, ReflectionException {
224         MapType map = (MapType) getSettingsHandler().getOrder().getAttribute(
225                 CrawlOrder.ATTR_HTTP_HEADERS);
226 
227         assertSame(map.getDefaultValue(), map);
228     }
229 
230     public void testGetLegalValues() throws AttributeNotFoundException,
231             MBeanException, ReflectionException {
232         MapType map = (MapType) getSettingsHandler().getOrder().getAttribute(
233                 CrawlOrder.ATTR_HTTP_HEADERS);
234 
235         assertNull(map.getLegalValues());
236     }
237 
238     /*
239      * Test for Object getValue()
240      */
241     public void testGetValue() throws AttributeNotFoundException,
242             MBeanException, ReflectionException {
243         MapType map = (MapType) getSettingsHandler().getOrder().getAttribute(
244                 CrawlOrder.ATTR_HTTP_HEADERS);
245 
246         assertSame(map.getValue(), map);
247     }
248 
249     /* Test for getAttribute
250      *
251      */
252     public void testGetAttribute() throws AttributeNotFoundException,
253             MBeanException, ReflectionException, InvalidAttributeValueException {
254         MapType map = (MapType) getSettingsHandler().getOrder().getAttribute(
255                 CrawlOrder.ATTR_HTTP_HEADERS);
256 
257         SimpleType type1 = new SimpleType("testType1", "description", "value");
258         SimpleType type2 = new SimpleType("testType2", "description", "value");
259         map.addElement(getGlobalSettings(), type1);
260         map.addElement(getPerDomainSettings(), type2);
261         assertEquals(type1.getValue(), map.getAttribute(getPerHostSettings(),
262                 "testType1"));
263         assertEquals(type2.getValue(), map.getAttribute(getPerHostSettings(),
264         "testType2"));
265         try {
266             map.getAttribute(getGlobalSettings(), "testType2");
267             fail();
268         } catch (AttributeNotFoundException e) {
269             // OK
270         }
271     }
272 
273     public void testListAttributes() throws AttributeNotFoundException,
274             MBeanException, ReflectionException, InvalidAttributeValueException {
275         MapType map = (MapType) getSettingsHandler().getOrder().getAttribute(
276                 CrawlOrder.ATTR_HTTP_HEADERS);
277 
278         List<Attribute> atts = new ArrayList<Attribute>();
279         for (Iterator it = map.iterator(null); it.hasNext();) {
280             atts.add(new SimpleType("", "", ((Attribute) it.next()).getValue()));
281         }
282 
283         SimpleType type1 = new SimpleType("testType1", "description", "value");
284         SimpleType type2 = new SimpleType("testType2", "description", "value");
285         map.addElement(getGlobalSettings(), type1);
286         map.addElement(getPerDomainSettings(), type2);
287         getSettingsHandler().writeSettingsObject(getGlobalSettings());
288         getSettingsHandler().writeSettingsObject(getPerDomainSettings());
289 
290         atts.add(type1);
291         atts.add(type2);
292         Type modules[] = (Type[]) atts.toArray(new Type[0]);
293         checkOrder(getPerHostSettings(), modules, map);
294 
295         XMLSettingsHandler newHandler = new XMLSettingsHandler(getOrderFile());
296         newHandler.initialize();
297         CrawlerSettings newPer = newHandler.getSettingsObject(getPerDomainSettings().getScope());
298 
299         checkOrder(newPer, modules, map);
300     }
301 
302 }