View Javadoc

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