KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > archive > crawler > selftest > SelfTestCase


1 /* SelfTestCase
2  *
3  * Created on Feb 4, 2004
4  *
5  * Copyright (C) 2004 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.selftest;
24
25
26 import java.io.File JavaDoc;
27 import java.io.FileNotFoundException JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
32
33 import javax.management.AttributeNotFoundException JavaDoc;
34 import javax.management.MBeanException JavaDoc;
35 import javax.management.ReflectionException JavaDoc;
36
37 import junit.framework.TestCase;
38
39 import org.archive.crawler.admin.CrawlJob;
40 import org.archive.crawler.datamodel.CrawlOrder;
41 import org.archive.crawler.settings.ComplexType;
42 import org.archive.crawler.settings.StringList;
43 import org.archive.crawler.writer.ARCWriterProcessor;
44 import org.archive.io.arc.ARCReader;
45 import org.archive.io.arc.ARCReaderFactory;
46 import org.archive.io.arc.ARCRecordMetaData;
47 import org.archive.util.FileUtils;
48
49
50 /**
51  * Base class for integrated selftest unit tests.
52  *
53  * Has utility for integrated selftest such as location of selftest generated
54  * arc file.
55  *
56  * @author stack
57  * @version $Id: SelfTestCase.java,v 1.25.4.1 2007/01/13 01:31:26 stack-sf Exp $
58  */

59 public class SelfTestCase extends TestCase
60 {
61     /**
62      * Suffix for selftest classes.
63      */

64     protected static final String JavaDoc SELFTEST = "SelfTest";
65
66     private static CrawlJob crawlJob = null;
67     private static File JavaDoc crawlJobDir = null;
68     private static File JavaDoc [] arcFile = null;
69     private static String JavaDoc selftestURL = null;
70
71     /**
72      * Directory logs are kept in.
73      */

74     private static File JavaDoc logsDir = null;
75
76     /**
77      * Has the static initializer for this class been run.
78      */

79     private static boolean initialized = false;
80
81     /**
82      * The selftest webapp htdocs directory.
83      */

84     private static File JavaDoc htdocs = null;
85
86
87     /**
88      * A reference to an ARCReader on which the validate method has been called.
89      * Can be used to walk the metadata.
90      *
91      * @see org.archive.io.arc.ARCReader#validate()
92      */

93     private static ARCReader [] readReader = null;
94
95     /**
96      * Metadata list from the arc reader.
97      *
98      * Gotten as byproduct of calling validate on the arcreader.
99      */

100     private static List JavaDoc [] metaDatas;
101
102
103     public SelfTestCase()
104     {
105         super();
106     }
107
108     public SelfTestCase(String JavaDoc testName)
109     {
110         super(testName);
111     }
112
113     /* (non-Javadoc)
114      * @see junit.framework.TestCase#setUp()
115      */

116     protected void setUp() throws Exception JavaDoc
117     {
118         if (!initialized)
119         {
120             throw new Exception JavaDoc("SelfTestCase.initialize() not called" +
121                 " before running of first test.");
122         }
123         super.setUp();
124     }
125
126     /**
127      * Test passed object is non-null.
128      *
129      * @param obj Object to test.
130      * @return Passed object.
131      * @throws NullPointerException Thrown if passed object is null.
132      */

133     private static Object JavaDoc testNonNull(Object JavaDoc obj)
134         throws NullPointerException JavaDoc
135     {
136         if (obj == null)
137         {
138             throw new NullPointerException JavaDoc(obj.toString());
139         }
140         return obj;
141     }
142
143     /**
144      * Test non null and not empty.
145      *
146      * @param str String to test.
147      * @return The passed string.
148      * @throws IllegalArgumentException if null or empty string.
149      */

150     private static String JavaDoc testNonNullNonEmpty(String JavaDoc str)
151         throws IllegalArgumentException JavaDoc, NullPointerException JavaDoc
152     {
153         if (((String JavaDoc)testNonNull(str)).length() <= 0)
154         {
155             throw new IllegalArgumentException JavaDoc("Passed string " + str +
156                 " is empty.");
157         }
158         return str;
159     }
160
161     /**
162      * Test nonull and exits.
163      *
164      * @param file File to test.
165      * @return Passed file.
166      * @throws FileNotFoundException passed file doesn't exist.
167      */

168     private static File JavaDoc testNonNullExists(File JavaDoc file)
169         throws FileNotFoundException JavaDoc
170     {
171         if (!((File JavaDoc)testNonNull(file)).exists())
172         {
173             throw new FileNotFoundException JavaDoc(file.getAbsolutePath());
174         }
175         return file;
176     }
177
178     /**
179      * Static initializer.
180      *
181      * Must be called before instantiation of any tests based off this class.
182      *
183      * @param url URL to selftest webapp.
184      * @param job The selftest crawl job.
185      * @param jobDir Job output directory. Has the seed file, the order file
186      * and logs.
187      * @param docs Expanded webapp directory location.
188      *
189      * @throws IOException if nonexistent directories passed.
190      */

191     public static synchronized void initialize(final String JavaDoc url,
192             final CrawlJob job, final File JavaDoc jobDir, final File JavaDoc docs)
193         throws IOException JavaDoc, AttributeNotFoundException JavaDoc, MBeanException JavaDoc,
194             ReflectionException JavaDoc, InterruptedException JavaDoc
195     {
196         testNonNullNonEmpty(url);
197         SelfTestCase.selftestURL = url.endsWith("/")? url: url + "/";
198         SelfTestCase.crawlJob = (CrawlJob)testNonNull(job);
199         SelfTestCase.crawlJobDir = testNonNullExists(jobDir);
200         SelfTestCase.htdocs = testNonNullExists(docs);
201         // Calculate the logs directory. If diskPath is not absolute, then logs
202
// are in the jobs directory under the diskPath subdirectory. Guard
203
// against case where diskPath is empty.
204
CrawlOrder crawlOrder =
205             (CrawlOrder)testNonNull(job.getSettingsHandler().getOrder());
206         String JavaDoc diskPath = (String JavaDoc)crawlOrder.
207             getAttribute(null, CrawlOrder.ATTR_DISK_PATH);
208         if (diskPath != null && diskPath.length() > 0 &&
209             diskPath.startsWith(File.separator)) {
210             SelfTestCase.logsDir = new File JavaDoc(diskPath);
211         } else {
212             SelfTestCase.logsDir =
213                 (diskPath != null && diskPath.length() > 0)?
214                     new File JavaDoc(jobDir, diskPath): jobDir;
215         }
216         testNonNullExists(SelfTestCase.logsDir);
217         // Calculate the arcfile name. Find it in the arcDir. Should only be
218
// one. Then make an instance of ARCReader and call the validate on it.
219
ComplexType arcWriterProcessor =
220             crawlOrder.getSettingsHandler().getModule("Archiver");
221         String JavaDoc arcDirStr = (String JavaDoc)((StringList)arcWriterProcessor.
222             getAttribute(ARCWriterProcessor.ATTR_PATH)).get(0);
223         File JavaDoc arcDir = null;
224         if (arcDirStr != null && arcDirStr.length() > 0 &&
225                 arcDirStr.startsWith(File.separator)) {
226             arcDir = new File JavaDoc(arcDirStr);
227         } else {
228             arcDir = (arcDirStr != null && arcDirStr.length() > 0)?
229                 new File JavaDoc(SelfTestCase.logsDir, arcDirStr): SelfTestCase.logsDir;
230         }
231         testNonNullExists(arcDir);
232         String JavaDoc prefix = testNonNullNonEmpty((String JavaDoc)arcWriterProcessor.
233             getAttribute(ARCWriterProcessor.ATTR_PREFIX));
234         File JavaDoc [] arcs = FileUtils.getFilesWithPrefix(arcDir, prefix);
235         /*
236         if (arcs.length != 1) {
237             throw new IOException("Expected one only arc file. Found" +
238                 " instead " + Integer.toString(arcs.length) + " files.");
239         }
240         */

241         SelfTestCase.readReader = new ARCReader[arcs.length];
242         SelfTestCase.arcFile = new File JavaDoc[arcs.length];
243         SelfTestCase.metaDatas = new List JavaDoc[arcs.length];
244         for (int i = 0; i < arcs.length; i++) {
245             File JavaDoc f = arcs[i];
246             SelfTestCase.arcFile[i] = f;
247             SelfTestCase.readReader[i] = ARCReaderFactory.get(f);
248             SelfTestCase.metaDatas[i] = SelfTestCase.readReader[i].validate();
249         }
250         SelfTestCase.initialized = true;
251     }
252
253     /**
254      * @return Returns the arcDir.
255      */

256     protected static File JavaDoc [] getArcFiles() {
257         return arcFile;
258     }
259
260     /**
261      * @return Returns the jobDir.
262      */

263     protected static File JavaDoc getCrawlJobDir()
264     {
265         return SelfTestCase.crawlJobDir;
266     }
267
268     /**
269      * @return Return the directory w/ logs in it.
270      */

271     protected static File JavaDoc getLogsDir()
272     {
273         return SelfTestCase.logsDir;
274     }
275
276     /**
277      * Returns the selftest read ARCReader.
278      *
279      * The returned ARCReader has been validated. Use it to get at metadata.
280      *
281      * @return Returns the readReader, an ARCReader that has been validated.
282      */

283     protected static ARCReader [] getReadReaders() {
284         return SelfTestCase.readReader;
285     }
286
287     /**
288      * @return Returns list of ARCReader metadatas, the byproduct of calling
289      * validate.
290      */

291     protected static List JavaDoc [] getMetaDatas() {
292         return SelfTestCase.metaDatas;
293     }
294
295     /**
296      * @return Returns the selftestURL.
297      */

298     public static String JavaDoc getSelftestURL()
299     {
300         return SelfTestCase.selftestURL;
301     }
302
303     /**
304      * @return Returns the selftestURL. URL returned is guaranteed to have
305      * a trailing '/'.
306      */

307     public static String JavaDoc getSelftestURLWithTrailingSlash()
308     {
309         return selftestURL.endsWith("/")? selftestURL: selftestURL + "/";
310     }
311
312     /**
313      * Calculates test name by stripping SelfTest from current class name.
314      *
315      * @return The name of the test.
316      */

317     public String JavaDoc getTestName()
318     {
319         String JavaDoc classname = getClass().getName();
320         int selftestIndex = classname.indexOf(SELFTEST);
321         assertTrue("Class name ends with SelfTest", selftestIndex > 0);
322         int lastDotIndex = classname.lastIndexOf('.');
323         assertTrue("Package dot in unexpected location",
324             lastDotIndex + 1 < classname.length() && lastDotIndex > 0);
325         return classname.substring(lastDotIndex + 1, selftestIndex);
326     }
327
328     /**
329      * @return Returns the selftest webappDir.
330      */

331     public static File JavaDoc getHtdocs()
332     {
333         return SelfTestCase.htdocs;
334     }
335
336     /**
337      * @return Returns the crawlJob.
338      */

339     public static CrawlJob getCrawlJob()
340     {
341         return crawlJob;
342     }
343
344     /**
345      * Confirm passed files exist on disk under the test directory.
346      *
347      * @param files Files to test for existence under the test's directory.
348      * @return true if all files exist on disk.
349      */

350     public boolean filesExist(List JavaDoc files)
351     {
352         boolean result = true;
353         for (Iterator JavaDoc i = files.iterator(); i.hasNext();)
354         {
355             if (!fileExists((File JavaDoc)i.next()))
356             {
357                 result = false;
358                 break;
359             }
360         }
361         return result;
362     }
363
364     /**
365      * Confirm passed file exists on disk under the test directory.
366      *
367      * This method takes care of building up the file path under the selftest
368      * webapp. Just pass the file name.
369      *
370      * @param file Name of file to look for.
371      * @return True if file exists.
372      */

373     public boolean fileExists(File JavaDoc file)
374     {
375         File JavaDoc testDir = new File JavaDoc(getHtdocs(), getTestName());
376         File JavaDoc fileOnDisk = new File JavaDoc(testDir, file.getPath());
377         return fileOnDisk.exists();
378     }
379
380     /**
381      * Test passed list were all found in the arc.
382      *
383      * If more or less found, test fails.
384      *
385      * @param files List of files to find in the arc. No other files but these
386      * should be found in the arc.
387      */

388     public void testFilesInArc(List JavaDoc<File JavaDoc> files)
389     {
390         testFilesInArc(files, filesFoundInArc());
391     }
392     
393     /**
394      * Test passed list were all found in the arc.
395      *
396      * If more or less found, test fails.
397      *
398      * @param files List of files to find in the arc. No other files but these
399      * should be found in the arc.
400      * @param foundFiles Files found in the arc.
401      */

402     public void testFilesInArc(List JavaDoc<File JavaDoc> files, List JavaDoc<File JavaDoc> foundFiles)
403     {
404         assertTrue("All files are on disk: " + files, filesExist(files));
405         assertTrue("All found: " + files + ", " + foundFiles,
406             foundFiles.containsAll(files));
407         assertTrue("Same size: " + files + ", " + foundFiles,
408             foundFiles.size() == files.size());
409     }
410
411     /**
412      * Find all files that belong to this test that are mentioned in the arc.
413      * @return List of unique found file File objects.
414      */

415     protected List JavaDoc<File JavaDoc> filesFoundInArc() {
416         String JavaDoc baseURL = getSelftestURLWithTrailingSlash();
417         if (baseURL.endsWith(getTestName() + '/')) {
418             // URL may already end in the test name for case where we're
419
// running one test only. If so, strip back the trailing '/'.
420
baseURL = baseURL.substring(0, baseURL.length() - 1);
421         } else {
422             baseURL += getTestName();
423         }
424         List JavaDoc [] metaDatas = getMetaDatas();
425         ARCRecordMetaData metaData = null;
426         List JavaDoc<File JavaDoc> filesFound = new ArrayList JavaDoc<File JavaDoc>();
427         for (int mdi = 0; mdi < metaDatas.length; mdi++) {
428             List JavaDoc list = metaDatas[mdi];
429             for (final Iterator JavaDoc i = list.iterator(); i.hasNext();) {
430                 metaData = (ARCRecordMetaData) i.next();
431                 String JavaDoc url = metaData.getUrl();
432                 if (url.startsWith(baseURL)
433                         && metaData.getMimetype().equalsIgnoreCase("text/html")) {
434                     String JavaDoc fileName = url.substring(baseURL.length());
435                     if (fileName.startsWith("/")) {
436                         fileName = fileName.substring(1);
437                     }
438                     if (fileName != null && fileName.length() > 0) {
439                         File JavaDoc f = new File JavaDoc(fileName);
440                         if (!filesFound.contains(f)) {
441                             // Don't add duplicates.
442
filesFound.add(new File JavaDoc(fileName));
443                         }
444                     }
445                 }
446             }
447         }
448         return filesFound;
449     }
450 }
451
Popular Tags