View Javadoc

1   /* SettingsFrameworkTestCase
2    *
3    * $Id: SettingsFrameworkTestCase.java 6082 2008-12-09 02:03:13Z gojomo $
4    *
5    * Created on Feb 2, 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.io.File;
28  
29  import javax.management.Attribute;
30  
31  import org.archive.crawler.datamodel.CrawlOrder;
32  import org.archive.crawler.datamodel.CrawlURI;
33  import org.archive.crawler.datamodel.ServerCache;
34  import org.archive.crawler.settings.Constraint.FailedCheck;
35  import org.archive.net.UURIFactory;
36  import org.archive.util.TmpDirTestCase;
37  
38  /*** Set up a couple of settings to test different functions of the settings
39   * framework.
40   *
41   * @author John Erik Halse
42   */
43  public abstract class SettingsFrameworkTestCase extends TmpDirTestCase implements
44          ValueErrorHandler {
45      private File orderFile;
46      private File settingsDir;
47      private CrawlerSettings globalSettings;
48      private CrawlerSettings perDomainSettings;
49      private CrawlerSettings perHostSettings;
50      protected XMLSettingsHandler settingsHandler;
51      private CrawlURI unMatchedURI;
52      private CrawlURI matchDomainURI;
53      private CrawlURI matchHostURI;
54  
55      /*
56       * @see TmpDirTestCase#setUp()
57       */
58      protected void setUp() throws Exception {
59          super.setUp();
60          cleanUpOldFiles("SETTINGS"); // preemptive cleanup just in case
61          orderFile = new File(getTmpDir(), "SETTINGS_order.xml");
62          String settingsDirName = "SETTINGS_per_host_settings";
63          settingsDir = new File(orderFile, settingsDirName);
64          settingsHandler = new XMLSettingsHandler(orderFile);
65          settingsHandler.initialize();
66          settingsHandler.getOrder().setAttribute(
67            new Attribute(CrawlOrder.ATTR_SETTINGS_DIRECTORY, settingsDirName));
68  
69          globalSettings = settingsHandler.getSettingsObject(null);
70          perDomainSettings = settingsHandler.getOrCreateSettingsObject("archive.org");
71          perHostSettings = settingsHandler.getOrCreateSettingsObject("www.archive.org");
72  
73          new ServerCache(getSettingsHandler());
74  
75          unMatchedURI = new CrawlURI(
76              UURIFactory.getInstance("http://localhost.com/index.html"));
77  
78          matchDomainURI = new CrawlURI(
79              UURIFactory.getInstance("http://audio.archive.org/index.html"));
80  
81          matchHostURI = new CrawlURI(
82              UURIFactory.getInstance("http://www.archive.org/index.html"));
83  
84          // Write legit email and url so we avoid warnings if tests are reading
85          // and writing order files.
86          MapType httpHeaders = (MapType)globalSettings.
87              getModule(CrawlOrder.ATTR_NAME).
88                  getAttribute(CrawlOrder.ATTR_HTTP_HEADERS);
89          httpHeaders.setAttribute(globalSettings,
90              new Attribute(CrawlOrder.ATTR_USER_AGENT,
91                  "unittest (+http://testing.one.two.three)"));
92          httpHeaders.setAttribute(globalSettings,
93                  new Attribute(CrawlOrder.ATTR_FROM,
94                      "unittestingtesting@one.two.three"));
95      }
96  
97      /*
98       * @see TmpDirTestCase#tearDown()
99       */
100     protected void tearDown() throws Exception {
101         super.tearDown();
102         cleanUpOldFiles("SETTINGS");
103     }
104 
105     /***
106      * @return global settings
107      */
108     public CrawlerSettings getGlobalSettings() {
109         return globalSettings;
110     }
111 
112     /***
113      * @return per domain settings
114      */
115     public CrawlerSettings getPerDomainSettings() {
116         return perDomainSettings;
117     }
118 
119     /***
120      * @return per host settings
121      */
122     public CrawlerSettings getPerHostSettings() {
123         return perHostSettings;
124     }
125 
126     /***
127      * @return settings handler
128      */
129     public XMLSettingsHandler getSettingsHandler() {
130         return settingsHandler;
131     }
132 
133     /***
134      * @return the order file
135      */
136     public File getOrderFile() {
137         return orderFile;
138     }
139 
140     /***
141      * @return the settings directory
142      */
143     public File getSettingsDir() {
144         return settingsDir;
145     }
146 
147     /***
148      * @return a uri matching the domain settings
149      */
150     public CrawlURI getMatchDomainURI() {
151         return matchDomainURI;
152     }
153 
154     /***
155      * @return a uri matching the per host settings
156      */
157     public CrawlURI getMatchHostURI() {
158         return matchHostURI;
159     }
160 
161     /***
162      * @return a uri that doesn't match any settings object except globals.
163      */
164     public CrawlURI getUnMatchedURI() {
165         return unMatchedURI;
166     }
167 
168     /* (non-Javadoc)
169      * @see org.archive.crawler.settings.ValueErrorHandler#handleValueError(org.archive.crawler.settings.Constraint.FailedCheck)
170      */
171     public void handleValueError(FailedCheck error) {
172         // TODO Auto-generated method stub
173     }
174 
175 }