KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > archive > crawler > deciderules > ConfiguredDecideRuleTest


1 /* ConfiguredDecideRuleTest
2  *
3  * Created on Apr 4, 2005
4  *
5  * Copyright (C) 2005 Internet Archive.
6  *
7  * This file is part of the Heritrix web crawler (crawler.archive.org).
8  *
9  * Heritrix is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU Lesser Public License as published by
11  * the Free Software Foundation; either version 2.1 of the License, or
12  * any later version.
13  *
14  * Heritrix is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Lesser Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser Public License
20  * along with Heritrix; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */

23 package org.archive.crawler.deciderules;
24
25 import java.io.File JavaDoc;
26
27 import javax.management.Attribute JavaDoc;
28 import javax.management.AttributeNotFoundException JavaDoc;
29 import javax.management.InvalidAttributeValueException JavaDoc;
30 import javax.management.MBeanException JavaDoc;
31 import javax.management.ReflectionException JavaDoc;
32
33 import org.archive.crawler.datamodel.CrawlOrder;
34 import org.archive.crawler.settings.MapType;
35 import org.archive.crawler.settings.SettingsHandler;
36 import org.archive.crawler.settings.XMLSettingsHandler;
37 import org.archive.util.TmpDirTestCase;
38
39 /**
40  * @author stack
41  * @version $Date: 2005/04/05 01:12:10 $, $Revision: 1.1 $
42  */

43 public class ConfiguredDecideRuleTest extends TmpDirTestCase {
44     /**
45      * Gets setup by {@link #setUp()}.
46      */

47     private ConfiguredDecideRule rule = null;
48     
49     protected void setUp() throws Exception JavaDoc {
50         super.setUp();
51         final String JavaDoc name = this.getClass().getName();
52         SettingsHandler settingsHandler = new XMLSettingsHandler(
53             new File JavaDoc(getTmpDir(), name + ".order.xml"));
54         settingsHandler.initialize();
55         // Create a new ConfigureDecideRule instance and add it to a MapType
56
// (I can change MapTypes after instantiation). The chosen MapType
57
// is the rules canonicalization rules list.
58
this.rule = (ConfiguredDecideRule)((MapType)settingsHandler.getOrder().
59             getAttribute(CrawlOrder.ATTR_RULES)).addElement(settingsHandler.
60                 getSettingsObject(null), new ConfiguredDecideRule(name));
61     }
62     
63     public void testDefault() {
64         Object JavaDoc decision = rule.decisionFor(new Object JavaDoc());
65         assertTrue("Wrong answer " + decision, decision == DecideRule.ACCEPT);
66     }
67     
68     public void testACCEPT()
69     throws AttributeNotFoundException JavaDoc, InvalidAttributeValueException JavaDoc,
70     MBeanException JavaDoc, ReflectionException JavaDoc {
71         runTest(DecideRule.ACCEPT);
72     }
73     
74     public void testPASS()
75     throws AttributeNotFoundException JavaDoc, MBeanException JavaDoc, ReflectionException JavaDoc {
76         String JavaDoc exceptionMessage = null;
77         try {
78             runTest(DecideRule.PASS);
79         } catch(InvalidAttributeValueException JavaDoc e) {
80             exceptionMessage = e.getMessage();
81         }
82         assertNotNull("Did not get expected exception", exceptionMessage);
83     }
84     
85     public void testREJECT()
86     throws AttributeNotFoundException JavaDoc, InvalidAttributeValueException JavaDoc,
87     MBeanException JavaDoc, ReflectionException JavaDoc {
88         runTest(DecideRule.REJECT);
89     }
90     
91     protected void runTest(String JavaDoc expectedResult)
92     throws AttributeNotFoundException JavaDoc, InvalidAttributeValueException JavaDoc,
93     MBeanException JavaDoc, ReflectionException JavaDoc {
94         configure(expectedResult);
95         Object JavaDoc decision = rule.decisionFor(new Object JavaDoc());
96         assertTrue("Expected " + expectedResult + " but got answer " +
97             decision, decision == expectedResult);
98     }
99     
100     protected void configure(String JavaDoc setting)
101     throws AttributeNotFoundException JavaDoc, InvalidAttributeValueException JavaDoc,
102     MBeanException JavaDoc, ReflectionException JavaDoc {
103         this.rule.setAttribute(
104             new Attribute JavaDoc(ConfiguredDecideRule.ATTR_DECISION, setting));
105     }
106 }
107
Popular Tags