org.apache.solr.util
Class TestHarness

java.lang.Object
  extended by org.apache.solr.util.TestHarness

public class TestHarness
extends java.lang.Object

This class provides a simple harness that may be useful when writing testcases.

This class lives in the main source tree (and not in the test source tree), so that it will be included with even the most minimal solr distribution, in order to encourage plugin writers to create unit tests for their plugins.

Version:
$Id:$
Author:
hossman

Nested Class Summary
 class TestHarness.LocalRequestFactory
          A Factory that generates LocalSolrQueryRequest objects using a specified set of default options.
 
Constructor Summary
TestHarness(java.lang.String dataDirectory)
          Assumes "solrconfig.xml" is the config file to use, and "schema.xml" is the schema path to use.
TestHarness(java.lang.String dataDirectory, java.lang.String schemaFile)
          Assumes "solrconfig.xml" is the config file to use.
TestHarness(java.lang.String dataDirectory, java.lang.String confFile, java.lang.String schemaFile)
           
 
Method Summary
 void appendSimpleDoc(java.lang.StringBuffer buf, java.lang.String... fieldsAndValues)
          A helper that adds an xml <doc> containing all of the fields and values specified (odds are fields, evens are values) to a StringBuffer.
 java.lang.String checkUpdateStatus(java.lang.String xml, java.lang.String code)
          Validates that an "update" (add, commit or optimize) results in success.
 void close()
          Shuts down and frees any resources
static java.lang.String commit(java.lang.String... args)
          Helper that returns an <commit> String with optional key/val pairs.
static java.lang.String deleteById(java.lang.String id)
          Generates a delete by id xml string
static java.lang.String deleteByQuery(java.lang.String q)
          Generates a delete by query xml string
 SolrCore getCore()
           
 TestHarness.LocalRequestFactory getRequestFactory(java.lang.String qtype, int start, int limit)
           
 TestHarness.LocalRequestFactory getRequestFactory(java.lang.String qtype, int start, int limit, java.util.Map<java.lang.String,java.lang.String> args)
           
 TestHarness.LocalRequestFactory getRequestFactory(java.lang.String qtype, int start, int limit, java.lang.String... args)
          0 and Even numbered args are keys, Odd numbered args are values.
static java.lang.StringBuffer makeSimpleDoc(java.lang.String... fieldsAndValues)
          A helper that creates an xml <doc> containing all of the fields and values specified
static java.lang.String optimize(java.lang.String... args)
          Helper that returns an <optimize> String with optional key/val pairs.
 java.lang.String query(SolrQueryRequest req)
          Processes a "query" using a user constructed SolrQueryRequest
 java.lang.String query(java.lang.String handler, SolrQueryRequest req)
          Processes a "query" using a user constructed SolrQueryRequest
 java.lang.String update(java.lang.String xml)
          Deprecated. 
 java.lang.String validateAddDoc(java.lang.String... fieldsAndValues)
          Validates that an add of a single document results in success.
 java.lang.String validateErrorUpdate(java.lang.String xml)
          Validates that an "update" (add, commit or optimize) results in success.
 java.lang.String validateQuery(SolrQueryRequest req, java.lang.String... tests)
          Validates a "query" response against an array of XPath test strings
 java.lang.String validateUpdate(java.lang.String xml)
          Validates that an "update" (add, commit or optimize) results in success.
 java.lang.String validateXPath(java.lang.String xml, java.lang.String... tests)
          A helper method which valides a String against an array of XPath test strings.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

TestHarness

public TestHarness(java.lang.String dataDirectory)
Assumes "solrconfig.xml" is the config file to use, and "schema.xml" is the schema path to use.

Parameters:
dataDirectory - path for index data, will not be cleaned up

TestHarness

public TestHarness(java.lang.String dataDirectory,
                   java.lang.String schemaFile)
Assumes "solrconfig.xml" is the config file to use.

Parameters:
dataDirectory - path for index data, will not be cleaned up
schemaFile - path of schema file

TestHarness

public TestHarness(java.lang.String dataDirectory,
                   java.lang.String confFile,
                   java.lang.String schemaFile)
Parameters:
dataDirectory - path for index data, will not be cleaned up
confFile - solrconfig filename
schemaFile - schema filename
Method Detail

update

@Deprecated
public java.lang.String update(java.lang.String xml)
Deprecated. 

Processes an "update" (add, commit or optimize) and returns the response as a String. The better approach is to instanciate a Updatehandler directly

Parameters:
xml - The XML of the update
Returns:
The XML response to the update

validateUpdate

public java.lang.String validateUpdate(java.lang.String xml)
                                throws org.xml.sax.SAXException
Validates that an "update" (add, commit or optimize) results in success. :TODO: currently only deals with one add/doc at a time, this will need changed if/when SOLR-2 is resolved

Parameters:
xml - The XML of the update
Returns:
null if succesful, otherwise the XML response to the update
Throws:
org.xml.sax.SAXException

validateErrorUpdate

public java.lang.String validateErrorUpdate(java.lang.String xml)
                                     throws org.xml.sax.SAXException
Validates that an "update" (add, commit or optimize) results in success. :TODO: currently only deals with one add/doc at a time, this will need changed if/when SOLR-2 is resolved

Parameters:
xml - The XML of the update
Returns:
null if succesful, otherwise the XML response to the update
Throws:
org.xml.sax.SAXException

checkUpdateStatus

public java.lang.String checkUpdateStatus(java.lang.String xml,
                                          java.lang.String code)
                                   throws org.xml.sax.SAXException
Validates that an "update" (add, commit or optimize) results in success. :TODO: currently only deals with one add/doc at a time, this will need changed if/when SOLR-2 is resolved

Parameters:
xml - The XML of the update
Returns:
null if succesful, otherwise the XML response to the update
Throws:
org.xml.sax.SAXException

validateAddDoc

public java.lang.String validateAddDoc(java.lang.String... fieldsAndValues)
                                throws javax.xml.xpath.XPathExpressionException,
                                       org.xml.sax.SAXException,
                                       java.io.IOException
Validates that an add of a single document results in success.

Parameters:
fieldsAndValues - Odds are field names, Evens are values
Returns:
null if succesful, otherwise the XML response to the update
Throws:
javax.xml.xpath.XPathExpressionException
org.xml.sax.SAXException
java.io.IOException
See Also:
appendSimpleDoc(java.lang.StringBuffer, java.lang.String...)

validateQuery

public java.lang.String validateQuery(SolrQueryRequest req,
                                      java.lang.String... tests)
                               throws java.io.IOException,
                                      java.lang.Exception
Validates a "query" response against an array of XPath test strings

Parameters:
req - the Query to process
Returns:
null if all good, otherwise the first test that fails.
Throws:
java.lang.Exception - any exception in the response.
java.io.IOException - if there is a problem writing the XML
See Also:
LocalSolrQueryRequest

query

public java.lang.String query(SolrQueryRequest req)
                       throws java.io.IOException,
                              java.lang.Exception
Processes a "query" using a user constructed SolrQueryRequest

Parameters:
req - the Query to process, will be closed.
Returns:
The XML response to the query
Throws:
java.lang.Exception - any exception in the response.
java.io.IOException - if there is a problem writing the XML
See Also:
LocalSolrQueryRequest

query

public java.lang.String query(java.lang.String handler,
                              SolrQueryRequest req)
                       throws java.io.IOException,
                              java.lang.Exception
Processes a "query" using a user constructed SolrQueryRequest

Parameters:
handler - the name of the request handler to process the request
req - the Query to process, will be closed.
Returns:
The XML response to the query
Throws:
java.lang.Exception - any exception in the response.
java.io.IOException - if there is a problem writing the XML
See Also:
LocalSolrQueryRequest

validateXPath

public java.lang.String validateXPath(java.lang.String xml,
                                      java.lang.String... tests)
                               throws javax.xml.xpath.XPathExpressionException,
                                      org.xml.sax.SAXException
A helper method which valides a String against an array of XPath test strings.

Parameters:
xml - The xml String to validate
tests - Array of XPath strings to test (in boolean mode) on the xml
Returns:
null if all good, otherwise the first test that fails.
Throws:
javax.xml.xpath.XPathExpressionException
org.xml.sax.SAXException

getCore

public SolrCore getCore()

close

public void close()
Shuts down and frees any resources


appendSimpleDoc

public void appendSimpleDoc(java.lang.StringBuffer buf,
                            java.lang.String... fieldsAndValues)
                     throws java.io.IOException
A helper that adds an xml <doc> containing all of the fields and values specified (odds are fields, evens are values) to a StringBuffer.

Throws:
java.io.IOException

makeSimpleDoc

public static java.lang.StringBuffer makeSimpleDoc(java.lang.String... fieldsAndValues)
A helper that creates an xml <doc> containing all of the fields and values specified

Parameters:
fieldsAndValues - 0 and Even numbered args are fields names odds are field values.

deleteByQuery

public static java.lang.String deleteByQuery(java.lang.String q)
Generates a delete by query xml string

Parameters:
q - Query that has not already been xml escaped

deleteById

public static java.lang.String deleteById(java.lang.String id)
Generates a delete by id xml string

Parameters:
id - ID that has not already been xml escaped

optimize

public static java.lang.String optimize(java.lang.String... args)
Helper that returns an <optimize> String with optional key/val pairs.

Parameters:
args - 0 and Even numbered args are params, Odd numbered args are values.

commit

public static java.lang.String commit(java.lang.String... args)
Helper that returns an <commit> String with optional key/val pairs.

Parameters:
args - 0 and Even numbered args are params, Odd numbered args are values.

getRequestFactory

public TestHarness.LocalRequestFactory getRequestFactory(java.lang.String qtype,
                                                         int start,
                                                         int limit)

getRequestFactory

public TestHarness.LocalRequestFactory getRequestFactory(java.lang.String qtype,
                                                         int start,
                                                         int limit,
                                                         java.lang.String... args)
0 and Even numbered args are keys, Odd numbered args are values.


getRequestFactory

public TestHarness.LocalRequestFactory getRequestFactory(java.lang.String qtype,
                                                         int start,
                                                         int limit,
                                                         java.util.Map<java.lang.String,java.lang.String> args)


Copyright © 2006 - 2009 The Apache Software Foundation