Skip to content

armgit / nutch

Public Imported from GitHub
0

Apache Nutch is an extensible and scalable web crawler

Fixed JUnit tests after trunk cleanup.

Piotr Kosiorowski37d3a9764c7d
diff --git a/src/plugin/parse-ext/src/test/org/apache/nutch/parse/ext/TestExtParser.java b/src/plugin/parse-ext/src/test/org/apache/nutch/parse/ext/TestExtParser.java
index b7eb77ff1..ac57b53dd 100644
--- a/src/plugin/parse-ext/src/test/org/apache/nutch/parse/ext/TestExtParser.java
+++ b/src/plugin/parse-ext/src/test/org/apache/nutch/parse/ext/TestExtParser.java
@@ -25,6 +25,9 @@ import org.apache.nutch.parse.Parse;
import org.apache.nutch.parse.ParseUtil;
import org.apache.nutch.parse.ParseException;
+import org.apache.nutch.io.UTF8;
+import org.apache.nutch.crawl.CrawlDatum;
+
import junit.framework.TestCase;
import java.io.File;
@@ -77,7 +80,7 @@ public class TestExtParser extends TestCase {
// get nutch content
Protocol protocol = ProtocolFactory.getProtocol(urlString);
- content = protocol.getProtocolOutput(urlString).getContent();
+ content = protocol.getProtocolOutput(new UTF8(urlString), new CrawlDatum()).getContent();
protocol = null;
}
diff --git a/src/plugin/parse-mspowerpoint/src/test/org/apache/nutch/parse/mspowerpoint/TestMSPowerPointParser.java b/src/plugin/parse-mspowerpoint/src/test/org/apache/nutch/parse/mspowerpoint/TestMSPowerPointParser.java
index dceb18de1..002a8eba5 100644
--- a/src/plugin/parse-mspowerpoint/src/test/org/apache/nutch/parse/mspowerpoint/TestMSPowerPointParser.java
+++ b/src/plugin/parse-mspowerpoint/src/test/org/apache/nutch/parse/mspowerpoint/TestMSPowerPointParser.java
@@ -35,6 +35,9 @@ import org.apache.nutch.protocol.Protocol;
import org.apache.nutch.protocol.ProtocolFactory;
import org.apache.nutch.util.LogFormatter;
+import org.apache.nutch.io.UTF8;
+import org.apache.nutch.crawl.CrawlDatum;
+
/**
* <p>
* Unit tests for MSPowerPointParser.
@@ -103,7 +106,7 @@ public class TestMSPowerPointParser extends TestCase {
System.out.println("Testing file: " + this.urlString + "...");
this.protocol = ProtocolFactory.getProtocol(this.urlString);
- this.content = this.protocol.getProtocolOutput(this.urlString).getContent();
+ this.content = this.protocol.getProtocolOutput(new UTF8(this.urlString), new CrawlDatum()).getContent();
}
/**
diff --git a/src/plugin/parse-msword/src/test/org/apache/nutch/parse/msword/TestMSWordParser.java b/src/plugin/parse-msword/src/test/org/apache/nutch/parse/msword/TestMSWordParser.java
index 475c0d448..bba3ca675 100644
--- a/src/plugin/parse-msword/src/test/org/apache/nutch/parse/msword/TestMSWordParser.java
+++ b/src/plugin/parse-msword/src/test/org/apache/nutch/parse/msword/TestMSWordParser.java
@@ -25,6 +25,9 @@ import org.apache.nutch.parse.Parse;
import org.apache.nutch.parse.ParseUtil;
import org.apache.nutch.parse.ParseException;
+import org.apache.nutch.io.UTF8;
+import org.apache.nutch.crawl.CrawlDatum;
+
import junit.framework.TestCase;
/**
@@ -62,7 +65,7 @@ public class TestMSWordParser extends TestCase {
urlString = "file:" + sampleDir + fileSeparator + sampleFiles[i];
protocol = ProtocolFactory.getProtocol(urlString);
- content = protocol.getProtocolOutput(urlString).getContent();
+ content = protocol.getProtocolOutput(new UTF8(urlString), new CrawlDatum()).getContent();
parse = ParseUtil.parseByParserId("parse-msword",content);
assertTrue(parse.getText().startsWith(expectedText));
diff --git a/src/plugin/parse-pdf/src/test/org/apache/nutch/parse/pdf/TestPdfParser.java b/src/plugin/parse-pdf/src/test/org/apache/nutch/parse/pdf/TestPdfParser.java
index d3ca3ec53..9253816f3 100644
--- a/src/plugin/parse-pdf/src/test/org/apache/nutch/parse/pdf/TestPdfParser.java
+++ b/src/plugin/parse-pdf/src/test/org/apache/nutch/parse/pdf/TestPdfParser.java
@@ -25,6 +25,9 @@ import org.apache.nutch.parse.Parse;
import org.apache.nutch.parse.ParseUtil;
import org.apache.nutch.parse.ParseException;
+import org.apache.nutch.io.UTF8;
+import org.apache.nutch.crawl.CrawlDatum;
+
import junit.framework.TestCase;
/**
@@ -62,7 +65,7 @@ public class TestPdfParser extends TestCase {
urlString = "file:" + sampleDir + fileSeparator + sampleFiles[i];
protocol = ProtocolFactory.getProtocol(urlString);
- content = protocol.getProtocolOutput(urlString).getContent();
+ content = protocol.getProtocolOutput(new UTF8(urlString), new CrawlDatum()).getContent();
parse = ParseUtil.parseByParserId("parse-pdf",content);
int index = parse.getText().indexOf(expectedText);
diff --git a/src/plugin/parse-rss/src/test/org/apache/nutch/parse/rss/TestRSSParser.java b/src/plugin/parse-rss/src/test/org/apache/nutch/parse/rss/TestRSSParser.java
index 93f5a1b88..d5ccec59e 100644
--- a/src/plugin/parse-rss/src/test/org/apache/nutch/parse/rss/TestRSSParser.java
+++ b/src/plugin/parse-rss/src/test/org/apache/nutch/parse/rss/TestRSSParser.java
@@ -27,6 +27,9 @@ import org.apache.nutch.parse.ParseException;
import org.apache.nutch.parse.ParseData;
import org.apache.nutch.parse.Outlink;
+import org.apache.nutch.io.UTF8;
+import org.apache.nutch.crawl.CrawlDatum;
+
import junit.framework.TestCase;
/**
@@ -80,7 +83,7 @@ public class TestRSSParser extends TestCase {
urlString = "file:" + sampleDir + fileSeparator + sampleFiles[i];
protocol = ProtocolFactory.getProtocol(urlString);
- content = protocol.getProtocolOutput(urlString).getContent();
+ content = protocol.getProtocolOutput(new UTF8(urlString), new CrawlDatum()).getContent();
parse = ParseUtil.parseByParserId("parse-rss",content);
//check that there are 3 outlinks:
diff --git a/src/plugin/parse-zip/src/test/org/apache/nutch/parse/zip/TestZipParser.java b/src/plugin/parse-zip/src/test/org/apache/nutch/parse/zip/TestZipParser.java
index 837ccd773..1dc67dbad 100644
--- a/src/plugin/parse-zip/src/test/org/apache/nutch/parse/zip/TestZipParser.java
+++ b/src/plugin/parse-zip/src/test/org/apache/nutch/parse/zip/TestZipParser.java
@@ -25,6 +25,9 @@ import org.apache.nutch.parse.Parse;
import org.apache.nutch.parse.ParseUtil;
import org.apache.nutch.parse.ParseException;
+import org.apache.nutch.io.UTF8;
+import org.apache.nutch.crawl.CrawlDatum;
+
import junit.framework.TestCase;
/**
@@ -62,7 +65,7 @@ public class TestZipParser extends TestCase {
urlString = "file:" + sampleDir + fileSeparator + sampleFiles[i];
protocol = ProtocolFactory.getProtocol(urlString);
- content = protocol.getProtocolOutput(urlString).getContent();
+ content = protocol.getProtocolOutput(new UTF8(urlString), new CrawlDatum()).getContent();
parse = ParseUtil.parseByParserId("parse-zip",content);
assertTrue(parse.getText().equals(expectedText));
}
diff --git a/src/test/org/apache/nutch/db/DBTester.java b/src/test/org/apache/nutch/db/DBTester.java
deleted file mode 100644
index e480f754e..000000000
--- a/src/test/org/apache/nutch/db/DBTester.java
+++ /dev/null
@@ -1,771 +0,0 @@
-/**
- * Copyright 2005 The Apache Software Foundation
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.nutch.db;
-
-import java.io.*;
-import java.util.*;
-
-import org.apache.nutch.db.*;
-import org.apache.nutch.io.*;
-import org.apache.nutch.fs.*;
-import org.apache.nutch.util.*;
-import org.apache.nutch.linkdb.*;
-import org.apache.nutch.pagedb.*;
-
-/***********************************************
- * DBTester runs a test suite against
- * org.apache.nutch.db.IWebDBWriter and org.apache.nutch.db.IWebDBReader.
- *
- * It tests things by repeatedly:
- * 1. Adding new items and editing existing items in the WebDB,
- * 2. Closing down the db
- * 3. Making sure it's still coherent, via WebDBReader
- * 4. Goto 1 a bunch of times.
- * 5. Test the full IWebDBReader API.
- *
- * @author Mike Cafarella
- ***********************************************/
-public class DBTester {
- static int MAX_OUTLINKS = 20;
-
- NutchFileSystem nfs;
- long seed;
- Random rand;
- File webdb;
- int maxPages;
- TreeSet seenLinks = new TreeSet();
- TreeMap md5Hashes = new TreeMap();
- long pageCount = 0, linkCount = 0, totalLinksEver = 0;
- Page pages[];
- Vector outlinks[];
- Hashtable inlinks;
-
- /**
- */
- public DBTester(NutchFileSystem nfs, File dir, int maxPages) throws IOException {
- this(nfs, dir, new Random().nextLong(), maxPages);
- }
-
- /**
- * Create a tester object by passing in the location of the
- * webdb and a few parameters.
- */
- public DBTester(NutchFileSystem nfs, File dir, long seed, int maxPages) throws IOException {
- this.nfs = nfs;
- this.maxPages = maxPages;
- this.webdb = new File(dir, "webdb_test");
- if (webdb.exists()) {
- throw new IOException("File " + webdb + " already exists");
- }
- webdb.mkdirs();
- this.seed = seed;
-
- WebDBWriter.createWebDB(nfs, webdb);
-
- this.rand = new Random(seed);
- System.out.println("-----------------------------------------------");
- System.out.println("DBTester created at " + new Date(System.currentTimeMillis()));
- System.out.println("WebDB: " + webdb);
- System.out.println("Seed: " + seed);
- System.out.println("-----------------------------------------------");
-
-
- //
- // Create structures to hold our model
- // webgraph. As we build our own mini-web,
- // we also make changes to the WebDB. Then we
- // read the WebDB back and check if it matches
- // our model.
- pages = new Page[maxPages];
- outlinks = new Vector[maxPages];
- for (int i = 0; i < outlinks.length; i++) {
- outlinks[i] = new Vector();
- }
- inlinks = new Hashtable();
- }
-
- /**
- * We run a series of tests against the WebDB. We do
- * this by first creating a model of our mini web-graph.
- * We insert this into the webdb, close it, and then
- * read back to make sure it matches our model.
- *
- * Then, we make a series of edits to the graph, making
- * then simultaneously to the IWebDBWriter. Again we
- * close it down, then read it back to see if it matches.
- *
- * We do this repeatedly.
- */
- public void runTest() throws IOException {
- // Round 1.
- // First thing, we create a large number of
- // brand-new pages. We just add them to the
- // webdb. Their interlink-stats are very roughly
- // reflective of the real world.
- System.out.println("CREATING WEB MODEL, CHECKING CONSISTENCY");
- createGraph();
- // Check to see if it's correct.
- checkConsistency();
-
- // Round 2 (repeated)
- //
- // Next, we create a number of edits. We
- // select an existing page with some probability
- // p, and choose a brand-new page with (1 - p).
- // Perform some kind of edit to our model's page,
- // then make the appropriate call to IWebDBWriter.
- //
- // After all that, check the db consistency
- //
- int maxTests = 10;
- for (int i = 1; i <= maxTests; i++) {
- System.out.println("EDIT-CONSISTENCY TEST (" + i + " of " + maxTests + ")");
- makeEdits();
- checkConsistency();
- }
-
- // At this point we're sure that the db is consistent
- // with our own model. But is it self-consistent?
- // The final step is do an API-coverage test.
- System.out.println("API TEST");
- apiTest();
-
- //
- // Finally, we make a bunch of random page deletes from
- // the db, and test to make sure all Pages and Links are
- // removed properly. This tests the "pageGone" scenario.
- //
- System.out.println("DB PAGE-DELETE TEST");
- IWebDBReader db = new WebDBReader(nfs, webdb);
- Vector toRemove = new Vector();
- try {
- for (Enumeration e = db.pages(); e.hasMoreElements(); ) {
- Page p = (Page) e.nextElement();
-
- if (Math.abs(rand.nextInt()) % 100 == 0) {
- toRemove.add(p);
- }
- }
- } finally {
- db.close();
- }
-
- //
- // Remove the randomly-chosen elements
- //
- IWebDBWriter dbwriter = new WebDBWriter(nfs, webdb);
- try {
- for (Enumeration e = toRemove.elements(); e.hasMoreElements(); ) {
- Page p = (Page) e.nextElement();
- dbwriter.deletePage(p.getURL().toString());
- }
- } finally {
- dbwriter.close();
- }
-
- // Test that the Pages and any inlinks are gone
- db = new WebDBReader(nfs, webdb);
- try {
- for (Enumeration e = toRemove.elements(); e.hasMoreElements(); ) {
- Page p = (Page) e.nextElement();
-
- Page result = db.getPage(p.getURL().toString());
- if (result != null) {
- // error
- throw new IOException("Found a Page that should have been deleted: " + result);
- }
-
- Link results[] = db.getLinks(p.getURL());
- if (results.length != 0) {
- // error
- throw new IOException("Should find no inlinks for deleted URL " + p.getURL() + ", but found " + results.length + " of them.");
- }
- }
- } finally {
- db.close();
- }
-
- System.out.println("*** TEST COMPLETE ***");
- }
-
- /**
- * Do away with the database. Only do this if you
- * no longer need the evidence!
- */
- public void cleanup() throws IOException {
- FileUtil.fullyDelete(nfs, webdb);
- }
-
- /**
- * We create the 1st iteration of the web graph. That
- * means no edits or modifications. Just adds.
- */
- private void createGraph() throws IOException {
- IWebDBWriter writer = new WebDBWriter(nfs, webdb);
- try {
- for (int i = 0; i < maxPages; i++) {
- // Make some pages
- pages[i] = createRandomPage();
- writer.addPage(pages[i]);
- pageCount++;
- }
-
- // Make some links that interconnect them
- for (int i = 0; i < maxPages; i++) {
- pages[i].setNumOutlinks(makeOutlinkSet(writer, i));
- }
- } finally {
- writer.close();
- }
- }
-
- /**
- * We make a set of adds, deletes, and mods to the
- * internal web graph. All of these are also applied
- * to the WebDB.
- */
- private void makeEdits() throws IOException {
- IWebDBWriter writer = new WebDBWriter(nfs, webdb);
- try {
- int actions[] = new int[pages.length];
-
- for (int i = 0; i < maxPages; i++) {
- Page curPage = pages[i];
-
- // We will either delete, edit, or leave it alone
- int action = Math.abs(rand.nextInt() % 2);
- actions[i] = action;
- if (action == 0) {
- // Get rid of the page
- Integer hashCount = (Integer) md5Hashes.get(curPage.getMD5());
- if (hashCount.intValue() == 1) {
- md5Hashes.remove(curPage.getMD5());
- } else {
- md5Hashes.put(curPage.getMD5(), new Integer(hashCount.intValue() - 1));
- }
- pages[i] = null;
- writer.deletePage(curPage.getURL().toString());
- linkCount -= outlinks[i].size();
-
- // Delete all the outlinks from our webgraph
- // structures.
- //
- // First, iterate through the list of all the outlinks
- // we're about to get rid of.
- for (Enumeration e = outlinks[i].elements(); e.hasMoreElements(); ) {
- Link curOutlink = (Link) e.nextElement();
-
- // Remove each outlink from the "seenLinks" table.
- seenLinks.remove(curOutlink);
-
- // Remove each outlink from the "inlink" tables.
- // We need to find the target URL's inlinkList,
- // and remove the handle to the curOutlink.
- //
- // First, get the target's inlinkList.
- int removeIndex = -1, pos = 0;
- Vector inlinkList = (Vector) inlinks.get(curOutlink.getURL().toString());
- // Find the position where the curOutlink appears.
- for (Enumeration e2 = inlinkList.elements(); e2.hasMoreElements(); pos++) {
- Link curInlink = (Link) e2.nextElement();
- if (curInlink.getFromID().equals(curOutlink.getFromID())) {
- removeIndex = pos;
- break;
- }
- }
-
- // Remove the curOutlink from the target's inlink list
- if (removeIndex >= 0) {
- inlinkList.removeElementAt(removeIndex);
- }
- }
-
- // Just clear all the links out.
- outlinks[i].clear();
-
- // Create a new one to replace it!
- pages[i] = createRandomPage();
-
- // Will add new links after this loop,
- // once all pages are created.
- } else if (action == 1) {
- // Modify the page's MD5.
- Integer hashCount = (Integer) md5Hashes.get(curPage.getMD5());
- if (hashCount.intValue() == 1) {
- md5Hashes.remove(curPage.getMD5());
- } else {
- md5Hashes.put(curPage.getMD5(), new Integer(hashCount.intValue() - 1));
- }
-
- // We need a unique md5 hash, because
- // otherwise we need to maintain models
- // of page contents. That is too much
- // for now, though might eventually
- // be a good idea.
- MD5Hash md5Hash = null;
- do {
- md5Hash = MD5Hash.digest(createRandomString(Math.abs(rand.nextInt() % 2048)));
- hashCount = (Integer) md5Hashes.get(md5Hash);
- } while (hashCount != null);
-
- md5Hashes.put(md5Hash, new Integer(1));
- pages[i].setMD5(md5Hash);
-
- // We're going to generate new Outlinks.
- // (However, the Page's URL stays the same,
- // so all inlinks to this URL remain untouched.)
- linkCount -= outlinks[i].size();
-
- //
- // Delete all of the outlinks from our webgraph
- // structures.
- //
- // First, iterate through the list of all the outlinks
- // we're about to get rid of.
- for (Enumeration e = outlinks[i].elements(); e.hasMoreElements(); ) {
- Link curOutlink = (Link) e.nextElement();
-
- // Remove each outlink from the "seenLinks" table
- seenLinks.remove(curOutlink);
-
- // Remove each outlink from the "inlink" tables.
- // We need to find the target URL's inlinkList,
- // and remove the handle to the curOutlink.
- //
- // First, get the target's inlinkList
- int removeIndex = -1, pos = 0;
- Vector inlinkList = (Vector) inlinks.get(curOutlink.getURL().toString());
- // Find the position where the curOutlink appears
- for (Enumeration e2 = inlinkList.elements(); e2.hasMoreElements(); pos++) {
- Link curLink = (Link) e2.nextElement();
- if (curLink.getFromID().equals(curOutlink.getFromID())) {
- removeIndex = pos;
- break;
- }
- }
-
- // Remove the curOutlink from the target's inlink list
- if (removeIndex >= 0) {
- inlinkList.removeElementAt(removeIndex);
- }
- }
-
- // Clear all the links out.
- // Set the Page's number of outlinks to zero.
- outlinks[i].clear();
- pages[i].setNumOutlinks(0);
-
- // Will add new links after this loop...
- }
- // Otherwise, leave things alone!
- }
-
- // Now that we've built all the pages, add in the
- // outlinks
- for (int i = 0; i < maxPages; i++) {
- if ((actions[i] == 0) || (actions[i] == 1)) {
- // Make the necessary outlinks for this new page!
- pages[i].setNumOutlinks(makeOutlinkSet(writer, i));
- writer.addPage(pages[i]);
- }
- }
- } finally {
- writer.close();
- }
- }
-
- /**
- * The checkConsistency() function will load in the
- * db from disk, and match it against the in-memory
- * representation.
- */
- private void checkConsistency() throws IOException {
- IWebDBReader reader = new WebDBReader(nfs, webdb);
- try {
- // Make sure counts match.
- if (pageCount != reader.numPages()) {
- throw new IOException("DB claims " + reader.numPages() + " pages, but should be " + pageCount);
- }
-
- if (seenLinks.size() != reader.numLinks()) {
- throw new IOException("DB claims " + reader.numLinks() + " links, but should be " + seenLinks.size() + ". Total links since last checkConsistency: " + totalLinksEver);
- }
-
- // Go through every page....
- for (int i = 0; i < pageCount; i++) {
- // First, check coverage of the page set.
- Page dbPage = reader.getPage(pages[i].getURL().toString());
- if (dbPage == null) {
- throw new IOException("DB could not find page " + pages[i].getURL());
- }
- if (! dbPage.getURL().equals(pages[i].getURL())) {
- throw new IOException("DB's page " + dbPage.getURL() + " should be " + pages[i].getURL());
- }
- if (! dbPage.getMD5().equals(pages[i].getMD5())) {
- throw new IOException("Page " + pages[i].getURL() + " in the DB has an MD5 of " + dbPage.getMD5() + ", but should be " + pages[i].getMD5());
- }
-
- // Next, the outlinks from that page. Go through
- // every one of the links we think it should have,
- // and make sure it is there.
- Link dbOutlinks[] = reader.getLinks(pages[i].getMD5());
- for (Enumeration e = outlinks[i].elements(); e.hasMoreElements(); ) {
- Link curOutlink = (Link) e.nextElement();
- boolean foundLink = false;
- for (int j = 0; j < dbOutlinks.length; j++) {
- if (dbOutlinks[j].compareTo(curOutlink) == 0) {
- foundLink = true;
- break;
- }
- }
- if (! foundLink) {
- throw new IOException("DB did not return Link " + curOutlink + " when asked for all links from " + pages[i].getMD5());
- }
- }
-
- // We also want to test whether there are some
- // links in the DB which should not be there.
- // (Yes, this is caught by the above counting
- // test, but we want to find out *which* urls
- // are the "extra" ones.)
- int numTooMany = 0;
- boolean excessLinks = false;
- for (int j = 0; j < dbOutlinks.length; j++) {
- boolean foundLink = false;
- for (Enumeration e = outlinks[i].elements(); e.hasMoreElements(); ) {
- Link curOutlink = (Link) e.nextElement();
- if (dbOutlinks[j].compareTo(curOutlink) == 0) {
- foundLink = true;
- break;
- }
- }
-
- if (! foundLink) {
- System.out.println("Found excess link in WebDB: " + dbOutlinks[j]);
- excessLinks = true;
- numTooMany++;
- }
- }
- if (excessLinks) {
- throw new IOException("DB has " + numTooMany + " too many outlinks.");
- }
-
-
-
- // Finally, the links *to* that page.
- Vector inlinkList = (Vector) inlinks.get(pages[i].getURL().toString());
- if (inlinkList != null) {
- Link dbInlinks[] = reader.getLinks(pages[i].getURL());
- for (Enumeration e = inlinkList.elements(); e.hasMoreElements(); ) {
- Link curInlink = (Link) e.nextElement();
- boolean foundLink = false;
- for (int j = 0; j < dbInlinks.length; j++) {
- if (dbInlinks[j].compareTo(curInlink) == 0) {
- foundLink = true;
- break;
- }
- }
- if (! foundLink) {
- throw new IOException("DB did not return Link " + curInlink + " when asked for all links to " + pages[i].getURL());
- }
- }
- }
- }
- } finally {
- reader.close();
- }
- totalLinksEver = 0;
- }
-
- /**
- * apiTest() will run through all the methods of
- * IWebDBReader and make sure they give correct
- * answers. We might use the internal model as a
- * source of items that are in the webdb, but we aren't
- * trying to perform a full consistency check - that's
- * done in checkConsistency().
- */
- private void apiTest() throws IOException {
- long urlEnumCount = 0, md5EnumCount = 0, linkEnumCount = 0;
- IWebDBReader reader = new WebDBReader(nfs, webdb);
- try {
- //
- // PAGE OPERATIONS
- //
-
- // 1. Test pages() and numPages()
- System.out.println("Testing IWebDBReader.pages()...");
- Page prevPage = null;
- for (Enumeration e = reader.pages(); e.hasMoreElements(); ) {
- if (prevPage == null) {
- prevPage = (Page) e.nextElement();
- } else {
- Page curPage = (Page) e.nextElement();
- if (! (prevPage.getURL().compareTo(curPage.getURL()) < 0)) {
- throw new IOException("While enumerating by URL, page " + prevPage + " comes before " + curPage);
- }
- prevPage = curPage;
- }
- urlEnumCount++;
- }
- if (urlEnumCount != reader.numPages()) {
- throw new IOException("IWebDBReader call to pages() results in " + urlEnumCount + ", but IWebDBReader reports " + reader.numPages() + " items.");
- }
-
- // 2. Test pagesByMD5().
- System.out.println("Testing IWebDBReader.pagesByMD5()...");
- prevPage = null;
- for (Enumeration e = reader.pagesByMD5(); e.hasMoreElements(); ) {
- if (prevPage == null) {
- prevPage = (Page) e.nextElement();
- } else {
- Page curPage = (Page) e.nextElement();
- if (! (prevPage.compareTo(curPage) < 0)) {
- throw new IOException("While enumerating by MD5, page " + prevPage + " comes before " + curPage);
- }
- prevPage = curPage;
- }
- md5EnumCount++;
- }
- if (md5EnumCount != reader.numPages()) {
- throw new IOException("IWebDBReader call to pagesByMD5() results in " + md5EnumCount + ", but IWebDBReader reports " + reader.numPages() + " items.");
- }
-
- // 3. Test getPage(String) method.
- System.out.println("Testing IWebDBReader.getPage()...");
- for (int i = 0; i < pages.length; i++) {
- Page curPage = pages[i];
- Page resultPage = reader.getPage(curPage.getURL().toString());
-
- if (resultPage == null || (resultPage.compareTo(curPage) != 0)) {
- throw new IOException("Call to IWebDBReader.getPage(" + curPage.getURL() + ") should have returned " + curPage + ", but returned " + resultPage + " instead.");
- }
- }
-
- // 4. Test getPages(MD5Hash)
- System.out.println("Testing IWebDBReader.getPages()...");
- for (Iterator it = md5Hashes.keySet().iterator(); it.hasNext(); ) {
- MD5Hash curHash = (MD5Hash) it.next();
- Page pageSet[] = reader.getPages(curHash);
- int numItems = ((Integer) md5Hashes.get(curHash)).intValue();
- if (pageSet.length != numItems) {
- throw new IOException("There should be " + numItems + " item(s) with MD5Hash " + curHash + " in the db, but IWebDBReader.getPages() reports " + pageSet.length);
- }
- }
-
- // 5. Test pageExists(MD5Hash)
- System.out.println("Testing IWebDBReader.pageExists()...");
- for (int i = 0; i < pages.length; i++) {
- Page curPage = pages[i];
- if (! reader.pageExists(curPage.getMD5())) {
- throw new IOException("IWebDBReader.pageExists() reports that a page with MD5 " + curPage.getMD5() + " is not found. It should be!");
- }
- }
-
-
- //
- // LINK OPERATIONS
- //
-
- // 1. Test links() and numLinks(), and generate a list
- // of items to test later on.
- System.out.println("Testing IWebDBReader.links()...");
- Link prevLink = null;
- for (Enumeration e = reader.links(); e.hasMoreElements(); ) {
- if (prevLink == null) {
- prevLink = (Link) e.nextElement();
- } else {
- Link curLink = (Link) e.nextElement();
- if (! (prevLink.compareTo(curLink) < 0)) {
- throw new IOException("While enumerating by Link, link " + prevLink + " comes before " + curLink);
- }
- prevLink = curLink;
- }
- linkEnumCount++;
- }
- if (linkEnumCount != reader.numLinks()) {
- throw new IOException("IWebDBReader call to links() results in " + linkEnumCount + ", but IWebDBReader reports " + reader.numLinks() + " items.");
- }
-
- // 2. Test getLinks(UTF8)
- System.out.println("Testing IWebDBReader.getLinks(UTF8)...");
- for (int i = 0; i < pages.length; i++) {
- Page curPage = pages[i];
- Vector inlinkList = (Vector) inlinks.get(curPage.getURL().toString());
- Link dbInlinks[] = reader.getLinks(curPage.getURL());
-
- if (inlinkList == null || dbInlinks == null) {
- if ((inlinkList == null || inlinkList.size() == 0) &&
- (dbInlinks.length != 0)) {
- throw new IOException("Call to IWebDBReader.getLinks(" + curPage.getURL()+ ") should return 0 links, but returns " + dbInlinks.length + " instead.");
- }
- } else {
- if (dbInlinks.length != inlinkList.size()) {
- throw new IOException("Call to IWebDBReader.getLinks(" + curPage.getURL() + ") should return " + inlinkList.size() + " inlinks, but returns " + dbInlinks.length + " instead.");
- }
- }
- }
-
- // 3. Test getLinks(MD5Hash)
- System.out.println("Testing IWebDBReader.getLinks(MD5Hash)...");
- for (int i = 0; i < pages.length; i++) {
- Page curPage = pages[i];
- Link dbOutlinks[] = reader.getLinks(curPage.getMD5());
- if (dbOutlinks.length != outlinks[i].size()) {
- throw new IOException("Call to IWebDBReader.getLinks(" + curPage.getMD5() + ") should return " + outlinks[i].size() + " outlinks, but returns " + dbOutlinks.length + " instead.");
- }
- if (dbOutlinks.length != curPage.getNumOutlinks()) {
- throw new IOException("Call to IWebDBReader.getLinks(" + curPage.getMD5() + ") should (according to Page.getNumOutlinks() return " + curPage.getNumOutlinks() + ", but returns " + dbOutlinks.length + " instead.");
- }
- }
- } finally {
- reader.close();
- }
- }
-
- /**
- * Return a string of numChars length. This is good for
- * anchors and URLs.
- */
- private String createRandomString(int numChars) {
- StringBuffer buf = new StringBuffer();
- for (int i = 0; i < numChars; i++) {
- buf.append((char) ('A' + Math.abs(rand.nextInt() % 26)));
- }
- return buf.toString();
- }
-
- /**
- * Internal utility method that manufactures a brand-new
- * novel page.
- */
- private Page createRandomPage() throws IOException {
- String curURL = "http://www.somePage." + createRandomString(20) + ".com/index.html";
- MD5Hash md5Hash = null;
- Integer hashCount = null;
-
- // Keep generating random contents until we have a unique
- // one. otherwise we need to maintain models of page contents.
- // As mentioned, that's too much for now, but maybe someday.
- do {
- md5Hash = MD5Hash.digest(createRandomString(Math.abs(rand.nextInt() % 2048) + 1));
- hashCount = (Integer) md5Hashes.get(md5Hash);
- } while (hashCount != null);
-
- md5Hashes.put(md5Hash, new Integer(1));
- return new Page(curURL, md5Hash);
- }
-
- /**
- * The createClonePage() is used to test duplicate MD5s
- * in our webdb. This is why we worry about tracking MD5s
- * in the md5Hashes table. REMIND - It is not used yet
- */
- private Page createClonePage(Page cloneSrc) throws IOException {
- String curURL = "http://www.somePage." + createRandomString(20) + ".com/index.html";
- MD5Hash md5Hash = cloneSrc.getMD5();
- Integer hashCount = (Integer) md5Hashes.get(md5Hash);
- md5Hashes.put(md5Hash, (hashCount == null) ? new Integer(1) : new Integer(hashCount.intValue() + 1));
- return new Page(curURL, md5Hash);
- }
-
- /**
- * Internal method that makes a Link between two
- * pages.
- */
- private Link createLink(Page src, Page dst) throws IOException {
- UTF8 targetURL = dst.getURL();
- MD5Hash srcMD5 = new MD5Hash();
- srcMD5.set(src.getMD5());
- String linkText = createRandomString(Math.abs(rand.nextInt() % 16) + 1);
- return new Link(srcMD5, src.computeDomainID(), targetURL.toString(), linkText);
- }
-
- /**
- * We make a randomized set of outlinks from the given
- * page to any number of other pages.
- *
- * Returns number of outlinks generated.
- */
- private int makeOutlinkSet(IWebDBWriter writer, int srcIndex) throws IOException {
- // Create the links for this new page!
- int numOutlinks = Math.abs(rand.nextInt() % MAX_OUTLINKS) + 1;
- int numInserted = 0;
- for (int j = 0; j < numOutlinks; j++) {
- int targetPageIndex = Math.abs(rand.nextInt() % (maxPages));
- Page targetPage = pages[targetPageIndex];
- Link lr = createLink(pages[srcIndex], targetPage);
-
- // See if we've made this link before
- if (! seenLinks.contains(lr)) {
- outlinks[srcIndex].add(lr);
- Vector inlinkList = (Vector) inlinks.get(targetPage.getURL().toString());
- if (inlinkList == null) {
- inlinkList = new Vector();
- inlinks.put(targetPage.getURL().toString(), inlinkList);
- }
- inlinkList.add(lr);
- writer.addLink(lr);
-
- linkCount++;
- totalLinksEver++;
- numInserted++;
- seenLinks.add(lr);
- }
- }
- return numInserted;
- }
-
- /**
- * The command-line takes a location to put temporary work
- * files, the number of pages to use in the test set, and
- * (optionally) a seed for the random num-generator.
- */
- public static void main(String argv[]) throws IOException {
- if (argv.length < 2) {
- System.out.println("Usage: java org.apache.nutch.db.DBTester (-local | -ndfs <namenode:port>) <workingdir> <numPages> [-seed <seed>]");
- return;
- }
-
- // Parse args
- int i = 0;
- NutchFileSystem nfs = NutchFileSystem.parseArgs(argv, i);
- try {
- File dbDir = new File(argv[i++]);
- int numPages = Integer.parseInt(argv[i++]);
-
- boolean gotSeed = false;
- long seed = 0;
- for (; i < argv.length; i++) {
- if ("-seed".equals(argv[i])) {
- gotSeed = true;
- seed = Long.parseLong(argv[i+1]);
- i++;
- }
- }
-
- DBTester tester = (gotSeed) ? new DBTester(nfs, dbDir, seed, numPages) : new DBTester(nfs, dbDir, numPages);
- try {
- tester.runTest();
- } finally {
- tester.cleanup();
- }
- } finally {
- nfs.close();
- }
- }
-}
diff --git a/src/test/org/apache/nutch/db/TestWebDB.java b/src/test/org/apache/nutch/db/TestWebDB.java
deleted file mode 100644
index 6f5d27f9d..000000000
--- a/src/test/org/apache/nutch/db/TestWebDB.java
+++ /dev/null
@@ -1,54 +0,0 @@
-/**
- * Copyright 2005 The Apache Software Foundation
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.nutch.db;
-
-import org.apache.nutch.fs.*;
-import org.apache.nutch.util.*;
-
-import junit.framework.TestCase;
-import java.io.*;
-
-/*************************************************
- * This is the unit test for WebDBWriter and
- * WebDBReader. It's really not much more than
- * a nice Junit-compatible wrapper around DBTester.
- *
- *************************************************/
-public class TestWebDB extends TestCase {
-
- /**
- * Build the TestWebDB by calling super's constructor.
- */
- public TestWebDB(String name) {
- super(name);
- }
-
- /**
- * The main test method, invoked by Junit.
- */
- public void testWebDB() throws Exception {
- String testDir = System.getProperty("test.build.data",".");
- File dbDir = new File(testDir, "testDb");
- dbDir.delete();
- dbDir.mkdir();
-
- DBTester tester = new DBTester(new LocalFileSystem(), dbDir, 500);
- tester.runTest();
- tester.cleanup();
- dbDir.delete();
- }
-}
diff --git a/src/test/org/apache/nutch/fetcher/TestFetcher.java b/src/test/org/apache/nutch/fetcher/TestFetcher.java
deleted file mode 100644
index 01aa5f833..000000000
--- a/src/test/org/apache/nutch/fetcher/TestFetcher.java
+++ /dev/null
@@ -1,114 +0,0 @@
-/**
- * Copyright 2005 The Apache Software Foundation
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.nutch.fetcher;
-
-import org.apache.nutch.io.*;
-import org.apache.nutch.db.*;
-import org.apache.nutch.fs.*;
-import org.apache.nutch.util.*;
-import org.apache.nutch.pagedb.*;
-import org.apache.nutch.parse.*;
-import org.apache.nutch.protocol.*;
-
-import java.io.*;
-import java.util.logging.Level;
-import junit.framework.TestCase;
-
-
-public class TestFetcher extends TestCase {
-
- public TestFetcher(String name) {
- super(name);
- }
-
-
- public void testFetcher() throws Exception {
- NutchFileSystem nfs = new LocalFileSystem();
- try {
- String directory = System.getProperty("test.build.data",".");
-
- String fetchListFilename = directory + "/" + FetchListEntry.DIR_NAME;
-
- ArrayFile.Writer testFetchList =
- new ArrayFile.Writer(nfs, fetchListFilename, FetchListEntry.class);
-
- MD5Hash id1 = new MD5Hash(new byte[]{0,0,0,0, 0,0,0,0, 0,0,0,0, 1,2,3,4});
- MD5Hash id2 = new MD5Hash(new byte[]{0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0});
-
- String url1 = "http://sourceforge.net/projects/nutch/";
- String url2 = "http://www.yahoo.com/";
- String url3 = "http://jakarta.apache.org/lucene/";
- String url4 = "http://www.nutch.org/docs/index.html";
- String url5 = "ftp://ftp.redhat.com/";
-
- Page page1 = new Page(url1, id1);
- Page page2 = new Page(url2, id2);
- Page page3 = new Page(url3, id2);
- Page page4 = new Page(url4, id2);
- Page page5 = new Page(url5, id2);
-
- String[] anchors = new String[] {"foo", "bar"};
-
- FetchListEntry fe1 = new FetchListEntry(true, page1, anchors);
- FetchListEntry fe2 = new FetchListEntry(true, page2, anchors);
- FetchListEntry fe3 = new FetchListEntry(true, page3, anchors);
- FetchListEntry fe4 = new FetchListEntry(true, page4, anchors);
- FetchListEntry fe5 = new FetchListEntry(false, page5, anchors);
-
- testFetchList.append(fe1);
- testFetchList.append(fe2);
- testFetchList.append(fe3);
- testFetchList.append(fe4);
- testFetchList.append(fe5);
- testFetchList.close();
-
- Fetcher fetcher = new Fetcher(nfs, directory, true);
- fetcher.setLogLevel(Level.FINE);
- //fetcher.getHttp().setMaxContentLength(4096);
- //fetcher.getHttp().setAgentString("NutchCVS");
-
- fetcher.run();
-
- ArrayFile.Reader fetcher_stripped;
- String stripped = directory + "/" + ParseText.DIR_NAME;
- ParseText s = new ParseText();
- fetcher_stripped = new ArrayFile.Reader(nfs, stripped);
-
- boolean yahoo = false;
- boolean nutch = false;
-
- while (fetcher_stripped.next(s) != null) {
-
- if (s.toString().indexOf("Yahoo!") >= 0)
- yahoo = true;
-
- if (s.toString().indexOf("Nutch") >= 0 )
- nutch = true;
- }
- fetcher_stripped.close();
- assertTrue(yahoo);
- assertTrue(nutch);
- } finally {
- nfs.close();
- }
- }
-}
-
-
-
-
-
diff --git a/src/test/org/apache/nutch/fetcher/TestFetcherOutput.java b/src/test/org/apache/nutch/fetcher/TestFetcherOutput.java
deleted file mode 100644
index dbb64c827..000000000
--- a/src/test/org/apache/nutch/fetcher/TestFetcherOutput.java
+++ /dev/null
@@ -1,43 +0,0 @@
-/**
- * Copyright 2005 The Apache Software Foundation
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.nutch.fetcher;
-
-import java.io.*;
-import org.apache.nutch.io.*;
-import org.apache.nutch.pagedb.*;
-import org.apache.nutch.protocol.ProtocolStatus;
-
-import junit.framework.TestCase;
-
-/** Unit tests for FetcherOutput. */
-public class TestFetcherOutput extends TestCase {
- public TestFetcherOutput(String name) { super(name); }
-
- public void testFetcherOutput() throws Exception {
-
- String[] anchors = new String[] {"foo", "bar"};
-
- FetcherOutput o =
- new FetcherOutput(new FetchListEntry(true, TestPage.getTestPage(),
- anchors),
- TestMD5Hash.getTestHash(), ProtocolStatus.STATUS_SUCCESS);
-
- TestWritable.testWritable(o);
-
- }
-
-}
diff --git a/src/test/org/apache/nutch/pagedb/TestFetchListEntry.java b/src/test/org/apache/nutch/pagedb/TestFetchListEntry.java
deleted file mode 100644
index 3ee64907d..000000000
--- a/src/test/org/apache/nutch/pagedb/TestFetchListEntry.java
+++ /dev/null
@@ -1,42 +0,0 @@
-/**
- * Copyright 2005 The Apache Software Foundation
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.nutch.pagedb;
-
-import java.io.*;
-import org.apache.nutch.io.*;
-import org.apache.nutch.pagedb.*;
-import junit.framework.TestCase;
-
-/** Unit tests for FetchListEntry. */
-public class TestFetchListEntry extends TestCase {
- public TestFetchListEntry(String name) { super(name); }
-
- public void testFetchListEntry() throws Exception {
-
- String[] anchors = new String[] {"foo", "bar"};
-
- FetchListEntry e1 = new FetchListEntry(true, TestPage.getTestPage(),
- anchors);
- TestWritable.testWritable(e1);
-
- FetchListEntry e2 = new FetchListEntry(false, TestPage.getTestPage(),
- anchors);
- TestWritable.testWritable(e2);
-
- }
-
-}
diff --git a/src/test/org/apache/nutch/pagedb/TestPage.java b/src/test/org/apache/nutch/pagedb/TestPage.java
deleted file mode 100644
index 505f2f4dc..000000000
--- a/src/test/org/apache/nutch/pagedb/TestPage.java
+++ /dev/null
@@ -1,70 +0,0 @@
-/**
- * Copyright 2005 The Apache Software Foundation
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.nutch.pagedb;
-
-import java.io.*;
-import java.util.Random;
-import org.apache.nutch.io.*;
-import org.apache.nutch.db.*;
-import junit.framework.TestCase;
-
-/** Unit tests for Page. */
-public class TestPage extends TestCase {
- public TestPage(String name) { super(name); }
-
- public static Page getTestPage() throws Exception {
- return new Page("http://foo.com/", TestMD5Hash.getTestHash());
- }
-
- public void testPage() throws Exception {
- TestWritable.testWritable(getTestPage());
- }
-
- public void testPageCtors() throws Exception {
- Random random = new Random();
- String urlString = "http://foo.com/";
- MD5Hash md5 = MD5Hash.digest(urlString);
- long now = System.currentTimeMillis();
- float score = random.nextFloat();
- float nextScore = random.nextFloat();
-
- Page page = new Page(urlString, md5);
- assertEquals(page.getURL().toString(), urlString);
- assertEquals(page.getMD5(), md5);
-
- page = new Page(urlString, score);
- assertEquals(page.getURL().toString(), urlString);
- assertEquals(page.getMD5(), md5);
- assertEquals(page.getScore(), score, 0.0f);
- assertEquals(page.getNextScore(), score, 0.0f);
-
- page = new Page(urlString, score, now);
- assertEquals(page.getURL().toString(), urlString);
- assertEquals(page.getMD5(), md5);
- assertEquals(page.getNextFetchTime(), now);
- assertEquals(page.getScore(), score, 0.0f);
- assertEquals(page.getNextScore(), score, 0.0f);
-
- page = new Page(urlString, score, nextScore, now);
- assertEquals(page.getURL().toString(), urlString);
- assertEquals(page.getMD5(), md5);
- assertEquals(page.getNextFetchTime(), now);
- assertEquals(page.getScore(), score, 0.0f);
- assertEquals(page.getNextScore(), nextScore, 0.0f);
- }
-
-}
diff --git a/src/test/org/apache/nutch/parse/TestParseText.java b/src/test/org/apache/nutch/parse/TestParseText.java
index 2f6ff40ff..1e4d9fb94 100644
--- a/src/test/org/apache/nutch/parse/TestParseText.java
+++ b/src/test/org/apache/nutch/parse/TestParseText.java
@@ -18,7 +18,6 @@ package org.apache.nutch.parse;
import java.io.*;
import org.apache.nutch.io.*;
-import org.apache.nutch.pagedb.*;
import junit.framework.TestCase;
/** Unit tests for ParseText. */
diff --git a/src/test/org/apache/nutch/tools/TestSegmentMergeTool.java b/src/test/org/apache/nutch/tools/TestSegmentMergeTool.java
deleted file mode 100644
index 9d85fed48..000000000
--- a/src/test/org/apache/nutch/tools/TestSegmentMergeTool.java
+++ /dev/null
@@ -1,277 +0,0 @@
-/**
- * Copyright 2005 The Apache Software Foundation
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.nutch.tools;
-
-import java.io.*;
-import java.util.Properties;
-import java.util.Random;
-
-import org.apache.nutch.db.Page;
-import org.apache.nutch.fetcher.FetcherOutput;
-import org.apache.nutch.io.MD5Hash;
-import org.apache.nutch.fs.*;
-import org.apache.nutch.segment.SegmentReader;
-import org.apache.nutch.segment.SegmentWriter;
-import org.apache.nutch.pagedb.FetchListEntry;
-import org.apache.nutch.parse.Outlink;
-import org.apache.nutch.parse.ParseData;
-import org.apache.nutch.parse.ParseStatus;
-import org.apache.nutch.parse.ParseText;
-import org.apache.nutch.protocol.Content;
-import org.apache.nutch.protocol.ContentProperties;
-import org.apache.nutch.protocol.ProtocolStatus;
-
-import junit.framework.TestCase;
-
-/** Unit tests for SegmentMergeTool methods. */
-public class TestSegmentMergeTool extends TestCase {
-
- protected static final int SEGMENT_CNT = 10;
-
- protected static final int PAGE_CNT = 500;
-
- protected File testDir = null;
-
- public TestSegmentMergeTool(String name) {
- super(name);
- }
-
- /**
- * Create test directory.
- *
- * @see junit.framework.TestCase#setUp()
- */
- protected void setUp() throws Exception {
- super.setUp();
- testDir = File.createTempFile(".smttest", "");
- testDir.delete();
- testDir.mkdirs();
- }
-
- /**
- * Create test segment data.
- *
- * @param dir segment directory
- * @param unique if true, use unique data per segment, otherwise use the same
- * data
- * @throws Exception
- */
- protected void createSegmentData(NutchFileSystem nfs, File dir, boolean unique) throws Exception {
- SegmentWriter sw = new SegmentWriter(nfs, dir, true);
- Random r = new Random(System.currentTimeMillis());
- for (int i = 0; i < PAGE_CNT; i++) {
- String url = "http://www.example.com/page-" + i;
- String rnd = "";
- if (unique) {
- rnd = "/" + System.currentTimeMillis();
- url += rnd;
- }
- url += "/example.html";
- FetchListEntry fle = new FetchListEntry(true, new Page(url, 1.0f), new String[] { "test" + rnd });
- FetcherOutput fo = new FetcherOutput(fle, MD5Hash.digest(url), ProtocolStatus.STATUS_SUCCESS);
- StringBuffer content = new StringBuffer("<html><body><h1>Hello from Page " + i + "</h1>");
- if (unique) {
- content.append("<p>Created at epoch time: " + System.currentTimeMillis() + ", " + r.nextLong() + "</p>");
- }
- for (int k = 0; k < 10; k++) {
- content.append("<p>" + k + " lines of text in the queue, " + k + " lines of text...</p>\n");
- }
- content.append("</body></html>");
- ContentProperties meta = new ContentProperties();
- meta.setProperty("Content-Type", "text/html");
- meta.setProperty("Host", "http://localhost");
- meta.setProperty("Connection", "Keep-alive, close");
- Content co = new Content(url, "http://www.example.com", content.toString().getBytes("UTF-8"), "text/html", meta);
- ParseData pd = new ParseData(ParseStatus.STATUS_SUCCESS, "Hello from Page " + i, new Outlink[0], meta);
- StringBuffer text = new StringBuffer("Hello from Page" + i);
- if (unique) {
- text.append("\nCreated at epoch time: " + System.currentTimeMillis() + ", " + r.nextLong());
- }
- for (int k = 0; k < 10; k++) {
- text.append(k + " lines of text in the queue, " + k + " lines of text...\n");
- }
- ParseText pt = new ParseText(text.toString());
- sw.append(fo, co, pt, pd);
- }
- sw.close();
- }
-
- /**
- * Remove test directory.
- *
- * @see junit.framework.TestCase#tearDown()
- */
- protected void tearDown() throws Exception {
- NutchFileSystem nfs = new LocalFileSystem();
- try {
- super.tearDown();
- try {
- FileUtil.fullyDelete(nfs, testDir);
- } catch (Exception e) {
- System.out.println("NON-FATAL: " + e.getMessage());
- }
- } finally {
- nfs.close();
- }
- }
-
- /**
- * Test merging segments with unique data. The output (merged segment) should
- * contain the number of pages equal exactly to a product of segment count
- * times page count per segment.
- *
- */
- public void testUniqueMerge() throws IOException {
- NutchFileSystem nfs = new LocalFileSystem();
- try {
- File dataDir = new File(testDir, "segments");
- File outSegment = new File(testDir, "output");
- try {
- for (int i = 0; i < SEGMENT_CNT; i++) {
- File f = new File(dataDir, "seg" + i);
- nfs.mkdirs(f);
- createSegmentData(nfs, f, true);
- }
- runTool(dataDir, outSegment);
- SegmentReader sr = new SegmentReader(outSegment.listFiles()[0]);
- assertEquals(SEGMENT_CNT * PAGE_CNT, sr.size);
- sr.close();
- } catch (Throwable e) {
- e.printStackTrace();
- fail(e.getMessage() + ", " + e.getStackTrace());
- }
- } finally {
- nfs.close();
- }
- }
-
- protected void runTool(File dataDir, File outSegment) throws Exception {
- SegmentMergeTool.main(
- new String[] {"-dir", dataDir.toString(), "-o", outSegment.toString(),
- "-ds"});
- }
-
- /**
- * Test merging segments with the same data. The output (merged segment)
- * should contain the number of pages equal exactly to the page count of a
- * single segment.
- *
- */
- public void testSameMerge() throws IOException {
- NutchFileSystem nfs = new LocalFileSystem();
- try {
- File dataDir = new File(testDir, "segments");
- File outSegment = new File(testDir, "output");
- try {
- for (int i = 0; i < SEGMENT_CNT; i++) {
- File f = new File(dataDir, "seg" + i);
- nfs.mkdirs(f);
- createSegmentData(nfs, f, false);
- }
- runTool(dataDir, outSegment);
- SegmentReader sr = new SegmentReader(outSegment.listFiles()[0]);
- assertEquals(PAGE_CNT, sr.size);
- sr.close();
- } catch (Exception e) {
- e.printStackTrace();
- fail(e.getMessage());
- }
- } catch (Throwable ex) {
- ex.printStackTrace();
- fail(ex.getMessage());
- } finally {
- nfs.close();
- }
- }
-
- public void testCorruptSegmentMerge() throws IOException {
- NutchFileSystem nfs = new LocalFileSystem();
- try {
- File dataDir = new File(testDir, "segments");
- File outSegment = new File(testDir, "output");
- try {
- for (int i = 0; i < SEGMENT_CNT; i++) {
- File f = new File(dataDir, "seg" + i);
- nfs.mkdirs(f);
- createSegmentData(nfs, f, true);
- // corrupt some segments in various ways... be creative :-)
- switch (i) {
- case 0:
- // truncate the fetcherOutput data file
- File data = new File(f, FetcherOutput.DIR_NAME);
- data = new File(data, "data");
- RandomAccessFile raf = new RandomAccessFile(data, "rws");
- raf.setLength(raf.length() - raf.length() / 4);
- raf.close();
- break;
- case 1:
- // truncate the Content data file
- data = new File(f, Content.DIR_NAME);
- data = new File(data, "data");
- raf = new RandomAccessFile(data, "rws");
- raf.setLength(raf.length() - raf.length() / 4);
- raf.close();
- break;
- case 2:
- // trash the whole
- // content
- data = new File(f, Content.DIR_NAME);
- new File(data, "data").delete();
- new File(data, "index").delete();
- break;
- case 3:
- // remove the "index" files - this is a very typical symptom for
- // segments created by a crashed fetcher process. Such segments should
- // be automatically fixed and recovered.
- data = new File(f, FetcherOutput.DIR_NAME);
- new File(data, "index").delete();
- data = new File(f, Content.DIR_NAME);
- new File(data, "index").delete();
- data = new File(f, ParseData.DIR_NAME);
- new File(data, "index").delete();
- data = new File(f, ParseText.DIR_NAME);
- new File(data, "index").delete();
- break;
- default:
- // do nothing
- ;
- }
- }
- runTool(dataDir, outSegment);
- SegmentReader sr = new SegmentReader(outSegment.listFiles()[0]);
- // we arrive at this expression as follows:
- // 1. SEGMENT_CNT - 1 : because we trash one whole segment
- // 2. 2 * PAGE_CNT / 4: because for two segments
- // we truncate 1/4 of the data file
- // 3. + 2: because sometimes truncation falls on
- // the boundary of the last entry
- int maxCnt = PAGE_CNT * (SEGMENT_CNT - 1) - 2 * PAGE_CNT / 4 + 2 * (SEGMENT_CNT -1);
- //System.out.println("maxCnt=" + maxCnt + ", sr.size=" + sr.size);
- assertTrue(sr.size < maxCnt);
- sr.close();
- } catch (Exception e) {
- e.printStackTrace();
- fail(e.getMessage());
- }
- } catch (Throwable ex) {
- ex.printStackTrace();
- fail(ex.getMessage());
- } finally {
- nfs.close();
- }
- }
-}