Update of /cvsroot/nutch/playground/src/test/net/nutch/searcher
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv10313/src/test/net/nutch/searcher

Added Files:
        TestQuery.java TestHitDetails.java 
Log Message:
intial commit

--- NEW FILE: TestQuery.java ---
/* Copyright (c) 2003 The Nutch Organization.  All rights reserved.   */
/* Use subject to the conditions in http://www.nutch.org/LICENSE.txt. */

package net.nutch.searcher;

import java.io.*;
import junit.framework.TestCase;
import java.util.Arrays;
import net.nutch.analysis.NutchAnalysis;

public class TestQuery extends TestCase {
  public TestQuery(String name) { super(name); }

  public void testRequiredTerm() throws Exception {
    Query query = new Query();
    query.addRequiredTerm("bobo");
    testQuery(query, "bobo");
  }

  public void testProhibitedTerm() throws Exception {
    Query query = new Query();
    query.addProhibitedTerm("bobo");
    testQuery(query, "-bobo");
  }

  public void testRequiredPhrase() throws Exception {
    Query query = new Query();
    query.addRequiredPhrase(new String[] {"bobo", "bogo"});
    testQuery(query, "\"bobo bogo\"");
  }

  public void testProhibitedPhrase() throws Exception {
    Query query = new Query();
    query.addProhibitedPhrase(new String[] {"bobo", "bogo"});
    testQuery(query, "-\"bobo bogo\"");
  }

  public void testComplex() throws Exception {
    Query query = new Query();
    query.addRequiredTerm("bobo");
    query.addProhibitedTerm("bono");
    query.addRequiredPhrase(new String[] {"bobo", "bogo"});
    query.addProhibitedPhrase(new String[] {"bogo", "bobo"});
    testQuery(query, "bobo -bono \"bobo bogo\" -\"bogo bobo\"");
  }

  public static void testQuery(Query query, String string) throws Exception {
    testQueryToString(query, string);
    testQueryParser(query, string);
    testQueryIO(query, string);
  }

  public static void testQueryToString(Query query, String string) {
    assertEquals(query.toString(), string);
  }

  public static void testQueryParser(Query query, String string)
    throws Exception {
    Query after = NutchAnalysis.parseQuery(string);
    assertEquals(after, query);
    assertEquals(after.toString(), string);
  }

  public static void testQueryIO(Query query, String string) throws Exception {
    ByteArrayOutputStream oBuf = new ByteArrayOutputStream();
    DataOutputStream out = new DataOutputStream(oBuf);
    query.write(out);

    ByteArrayInputStream iBuf = new ByteArrayInputStream(oBuf.toByteArray());
    DataInputStream in = new DataInputStream(iBuf);

    Query after = Query.read(in);

    assertEquals(after, query);
  }

  public void testQueryTerms() throws Exception {
    testQueryTerms("foo bar", new String[] {"foo", "bar"});
    testQueryTerms("\"foo bar\"", new String[] {"foo", "bar"});
    testQueryTerms("\"foo bar\" baz", new String[] {"foo", "bar", "baz"});
  }

  public static void testQueryTerms(String query, String[] terms)
    throws Exception {
    assertTrue(Arrays.equals(NutchAnalysis.parseQuery(query).getTerms(),
                             terms));
  }

  public static void main(String[] args) throws Exception {
    TestQuery test = new TestQuery("test");
    test.testComplex();
  }

}

--- NEW FILE: TestHitDetails.java ---
/* Copyright (c) 2003 The Nutch Organization.  All rights reserved.   */
/* Use subject to the conditions in http://www.nutch.org/LICENSE.txt. */

package net.nutch.searcher;

import java.io.*;
import net.nutch.io.*;
import junit.framework.TestCase;

public class TestHitDetails extends TestCase {
  public TestHitDetails(String name) { super(name); }

  public void testHitDetails() throws Exception {
    final int length = 3;
    final String[] fields = new String[] {"a", "b", "c" };
    final String[] values = new String[] { "foo", "bar", "baz" };

    HitDetails before = new HitDetails(fields, values);

    DataOutputBuffer dob = new DataOutputBuffer();
    before.write(dob);

    DataInputBuffer dib = new DataInputBuffer();
    dib.reset(dob.getData(), dob.getLength());

    HitDetails after = HitDetails.read(dib);

    assertEquals(length, after.getLength());
    for (int i = 0; i < length; i++) {
      assertEquals(fields[i], after.getField(i));
      assertEquals(values[i], after.getValue(i));
      assertEquals(values[i], after.getValue(fields[i]));
    }
  }
}



-------------------------------------------------------
The SF.Net email is sponsored by EclipseCon 2004
Premiere Conference on Open Tools Development and Integration
See the breadth of Eclipse activity. February 3-5 in Anaheim, CA.
http://www.eclipsecon.org/osdn
_______________________________________________
Nutch-cvs mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/nutch-cvs

Reply via email to