org.apache.solr.util
Class TestHarness

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

public class TestHarness
extends 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:$

Nested Class Summary
 class TestHarness.LocalRequestFactory
          A Factory that generates LocalSolrQueryRequest objects using a specified set of default options.
 
Field Summary
protected  CoreContainer container
           
 XmlUpdateRequestHandler updater
           
 
Constructor Summary
TestHarness(String dataDirectory)
          Assumes "solrconfig.xml" is the config file to use, and "schema.xml" is the schema path to use.
TestHarness(String coreName, CoreContainer.Initializer init)
           
TestHarness(String dataDirectory, SolrConfig solrConfig, IndexSchema indexSchema)
           
TestHarness(String dataDirectory, SolrConfig solrConfig, String schemaFile)
           
TestHarness(String dataDirectory, String schemaFile)
          Assumes "solrconfig.xml" is the config file to use.
TestHarness(String dataDirectory, String configFile, String schemaFile)
           
 
Method Summary
 void appendSimpleDoc(StringBuffer buf, String... fieldsAndValues)
          Deprecated. see appendSimpleDoc(StringBuilder, String...)
 void appendSimpleDoc(StringBuilder buf, 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 StringBuilder
 String checkUpdateStatus(String xml, String code)
          Validates that an "update" (add, commit or optimize) results in success.
 void close()
          Shuts down and frees any resources
static String commit(String... args)
          Helper that returns an <commit> String with optional key/val pairs.
static SolrConfig createConfig(String confFile)
           
static String deleteById(String id)
          Generates a delete by id xml string
static String deleteByQuery(String q)
          Generates a delete by query xml string
 SolrCore getCore()
           
 CoreContainer getCoreContainer()
           
 TestHarness.LocalRequestFactory getRequestFactory(String qtype, int start, int limit)
           
 TestHarness.LocalRequestFactory getRequestFactory(String qtype, int start, int limit, Map<String,String> args)
           
 TestHarness.LocalRequestFactory getRequestFactory(String qtype, int start, int limit, String... args)
          0 and Even numbered args are keys, Odd numbered args are values.
static StringBuffer makeSimpleDoc(String... fieldsAndValues)
          A helper that creates an xml <doc> containing all of the fields and values specified
static String optimize(String... args)
          Helper that returns an <optimize> String with optional key/val pairs.
 String query(SolrQueryRequest req)
          Processes a "query" using a user constructed SolrQueryRequest
 String query(String handler, SolrQueryRequest req)
          Processes a "query" using a user constructed SolrQueryRequest
 SolrQueryResponse queryAndResponse(String handler, SolrQueryRequest req)
           
 String update(String xml)
          Deprecated. The better approach is to instantiate an Updatehandler directly
 String validateAddDoc(String... fieldsAndValues)
          Validates that an add of a single document results in success.
 String validateErrorUpdate(String xml)
          Validates that an "update" (add, commit or optimize) results in success.
 String validateQuery(SolrQueryRequest req, String... tests)
          Validates a "query" response against an array of XPath test strings
 String validateUpdate(String xml)
          Validates that an "update" (add, commit or optimize) results in success.
 String validateXPath(String xml, 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
 

Field Detail

container

protected CoreContainer container

updater

public XmlUpdateRequestHandler updater
Constructor Detail

TestHarness

public TestHarness(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(String dataDirectory,
                   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(String dataDirectory,
                   String configFile,
                   String schemaFile)
Parameters:
dataDirectory - path for index data, will not be cleaned up
configFile - solrconfig filename
schemaFile - schema filename

TestHarness

public TestHarness(String dataDirectory,
                   SolrConfig solrConfig,
                   String schemaFile)
Parameters:
dataDirectory - path for index data, will not be cleaned up
solrConfig - solronfig instance
schemaFile - schema filename

TestHarness

public TestHarness(String dataDirectory,
                   SolrConfig solrConfig,
                   IndexSchema indexSchema)
Parameters:
dataDirectory - path for index data, will not be cleaned up
solrConfig - solrconfig instance
indexSchema - schema instance

TestHarness

public TestHarness(String coreName,
                   CoreContainer.Initializer init)
Method Detail

createConfig

public static SolrConfig createConfig(String confFile)

getCoreContainer

public CoreContainer getCoreContainer()

getCore

public SolrCore getCore()

update

@Deprecated
public String update(String xml)
Deprecated. The better approach is to instantiate an Updatehandler directly

Processes an "update" (add, commit or optimize) and returns the response as a String.

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

validateUpdate

public String validateUpdate(String xml)
                      throws 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 successful, otherwise the XML response to the update
Throws:
SAXException

validateErrorUpdate

public String validateErrorUpdate(String xml)
                           throws 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 successful, otherwise the XML response to the update
Throws:
SAXException

checkUpdateStatus

public String checkUpdateStatus(String xml,
                                String code)
                         throws 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 successful, otherwise the XML response to the update
Throws:
SAXException

validateAddDoc

public String validateAddDoc(String... fieldsAndValues)
                      throws XPathExpressionException,
                             SAXException,
                             IOException
Validates that an add of a single document results in success.

Parameters:
fieldsAndValues - Odds are field names, Evens are values
Returns:
null if successful, otherwise the XML response to the update
Throws:
XPathExpressionException
SAXException
IOException
See Also:
appendSimpleDoc(java.lang.StringBuilder, java.lang.String...)

validateQuery

public String validateQuery(SolrQueryRequest req,
                            String... tests)
                     throws IOException,
                            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:
Exception - any exception in the response.
IOException - if there is a problem writing the XML
See Also:
LocalSolrQueryRequest

query

public String query(SolrQueryRequest req)
             throws IOException,
                    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:
Exception - any exception in the response.
IOException - if there is a problem writing the XML
See Also:
LocalSolrQueryRequest

query

public String query(String handler,
                    SolrQueryRequest req)
             throws IOException,
                    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:
Exception - any exception in the response.
IOException - if there is a problem writing the XML
See Also:
LocalSolrQueryRequest

queryAndResponse

public SolrQueryResponse queryAndResponse(String handler,
                                          SolrQueryRequest req)
                                   throws Exception
Throws:
Exception

validateXPath

public String validateXPath(String xml,
                            String... tests)
                     throws XPathExpressionException,
                            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:
XPathExpressionException
SAXException

close

public void close()
Shuts down and frees any resources


appendSimpleDoc

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

Throws:
IOException

appendSimpleDoc

public void appendSimpleDoc(StringBuffer buf,
                            String... fieldsAndValues)
                     throws IOException
Deprecated. see appendSimpleDoc(StringBuilder, String...)

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:
IOException

makeSimpleDoc

public static StringBuffer makeSimpleDoc(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 String deleteByQuery(String q)
Generates a delete by query xml string

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

deleteById

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

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

optimize

public static String optimize(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 String commit(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(String qtype,
                                                         int start,
                                                         int limit)

getRequestFactory

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


getRequestFactory

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


Copyright © 2010 Apache Software Foundation. All Rights Reserved.