1 23 package org.archive.crawler.selftest; 24 25 26 import java.io.File ; 27 import java.io.FileNotFoundException ; 28 import java.io.IOException ; 29 import java.util.ArrayList ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 33 import javax.management.AttributeNotFoundException ; 34 import javax.management.MBeanException ; 35 import javax.management.ReflectionException ; 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 59 public class SelfTestCase extends TestCase 60 { 61 64 protected static final String SELFTEST = "SelfTest"; 65 66 private static CrawlJob crawlJob = null; 67 private static File crawlJobDir = null; 68 private static File [] arcFile = null; 69 private static String selftestURL = null; 70 71 74 private static File logsDir = null; 75 76 79 private static boolean initialized = false; 80 81 84 private static File htdocs = null; 85 86 87 93 private static ARCReader [] readReader = null; 94 95 100 private static List [] metaDatas; 101 102 103 public SelfTestCase() 104 { 105 super(); 106 } 107 108 public SelfTestCase(String testName) 109 { 110 super(testName); 111 } 112 113 116 protected void setUp() throws Exception 117 { 118 if (!initialized) 119 { 120 throw new Exception ("SelfTestCase.initialize() not called" + 121 " before running of first test."); 122 } 123 super.setUp(); 124 } 125 126 133 private static Object testNonNull(Object obj) 134 throws NullPointerException 135 { 136 if (obj == null) 137 { 138 throw new NullPointerException (obj.toString()); 139 } 140 return obj; 141 } 142 143 150 private static String testNonNullNonEmpty(String str) 151 throws IllegalArgumentException , NullPointerException 152 { 153 if (((String )testNonNull(str)).length() <= 0) 154 { 155 throw new IllegalArgumentException ("Passed string " + str + 156 " is empty."); 157 } 158 return str; 159 } 160 161 168 private static File testNonNullExists(File file) 169 throws FileNotFoundException 170 { 171 if (!((File )testNonNull(file)).exists()) 172 { 173 throw new FileNotFoundException (file.getAbsolutePath()); 174 } 175 return file; 176 } 177 178 191 public static synchronized void initialize(final String url, 192 final CrawlJob job, final File jobDir, final File docs) 193 throws IOException , AttributeNotFoundException , MBeanException , 194 ReflectionException , InterruptedException 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 CrawlOrder crawlOrder = 205 (CrawlOrder)testNonNull(job.getSettingsHandler().getOrder()); 206 String diskPath = (String )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 (diskPath); 211 } else { 212 SelfTestCase.logsDir = 213 (diskPath != null && diskPath.length() > 0)? 214 new File (jobDir, diskPath): jobDir; 215 } 216 testNonNullExists(SelfTestCase.logsDir); 217 ComplexType arcWriterProcessor = 220 crawlOrder.getSettingsHandler().getModule("Archiver"); 221 String arcDirStr = (String )((StringList)arcWriterProcessor. 222 getAttribute(ARCWriterProcessor.ATTR_PATH)).get(0); 223 File arcDir = null; 224 if (arcDirStr != null && arcDirStr.length() > 0 && 225 arcDirStr.startsWith(File.separator)) { 226 arcDir = new File (arcDirStr); 227 } else { 228 arcDir = (arcDirStr != null && arcDirStr.length() > 0)? 229 new File (SelfTestCase.logsDir, arcDirStr): SelfTestCase.logsDir; 230 } 231 testNonNullExists(arcDir); 232 String prefix = testNonNullNonEmpty((String )arcWriterProcessor. 233 getAttribute(ARCWriterProcessor.ATTR_PREFIX)); 234 File [] arcs = FileUtils.getFilesWithPrefix(arcDir, prefix); 235 241 SelfTestCase.readReader = new ARCReader[arcs.length]; 242 SelfTestCase.arcFile = new File [arcs.length]; 243 SelfTestCase.metaDatas = new List [arcs.length]; 244 for (int i = 0; i < arcs.length; i++) { 245 File 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 256 protected static File [] getArcFiles() { 257 return arcFile; 258 } 259 260 263 protected static File getCrawlJobDir() 264 { 265 return SelfTestCase.crawlJobDir; 266 } 267 268 271 protected static File getLogsDir() 272 { 273 return SelfTestCase.logsDir; 274 } 275 276 283 protected static ARCReader [] getReadReaders() { 284 return SelfTestCase.readReader; 285 } 286 287 291 protected static List [] getMetaDatas() { 292 return SelfTestCase.metaDatas; 293 } 294 295 298 public static String getSelftestURL() 299 { 300 return SelfTestCase.selftestURL; 301 } 302 303 307 public static String getSelftestURLWithTrailingSlash() 308 { 309 return selftestURL.endsWith("/")? selftestURL: selftestURL + "/"; 310 } 311 312 317 public String getTestName() 318 { 319 String 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 331 public static File getHtdocs() 332 { 333 return SelfTestCase.htdocs; 334 } 335 336 339 public static CrawlJob getCrawlJob() 340 { 341 return crawlJob; 342 } 343 344 350 public boolean filesExist(List files) 351 { 352 boolean result = true; 353 for (Iterator i = files.iterator(); i.hasNext();) 354 { 355 if (!fileExists((File )i.next())) 356 { 357 result = false; 358 break; 359 } 360 } 361 return result; 362 } 363 364 373 public boolean fileExists(File file) 374 { 375 File testDir = new File (getHtdocs(), getTestName()); 376 File fileOnDisk = new File (testDir, file.getPath()); 377 return fileOnDisk.exists(); 378 } 379 380 388 public void testFilesInArc(List <File > files) 389 { 390 testFilesInArc(files, filesFoundInArc()); 391 } 392 393 402 public void testFilesInArc(List <File > files, List <File > 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 415 protected List <File > filesFoundInArc() { 416 String baseURL = getSelftestURLWithTrailingSlash(); 417 if (baseURL.endsWith(getTestName() + '/')) { 418 baseURL = baseURL.substring(0, baseURL.length() - 1); 421 } else { 422 baseURL += getTestName(); 423 } 424 List [] metaDatas = getMetaDatas(); 425 ARCRecordMetaData metaData = null; 426 List <File > filesFound = new ArrayList <File >(); 427 for (int mdi = 0; mdi < metaDatas.length; mdi++) { 428 List list = metaDatas[mdi]; 429 for (final Iterator i = list.iterator(); i.hasNext();) { 430 metaData = (ARCRecordMetaData) i.next(); 431 String url = metaData.getUrl(); 432 if (url.startsWith(baseURL) 433 && metaData.getMimetype().equalsIgnoreCase("text/html")) { 434 String fileName = url.substring(baseURL.length()); 435 if (fileName.startsWith("/")) { 436 fileName = fileName.substring(1); 437 } 438 if (fileName != null && fileName.length() > 0) { 439 File f = new File (fileName); 440 if (!filesFound.contains(f)) { 441 filesFound.add(new File (fileName)); 443 } 444 } 445 } 446 } 447 } 448 return filesFound; 449 } 450 } 451 | Popular Tags |