SOLR-361 : refactor solrj tests so they are more extendable

git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@577394 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Ryan McKinley 2007-09-19 17:31:05 +00:00
parent c364ddd185
commit 533f5cb7a5
4 changed files with 290 additions and 235 deletions

View File

@ -18,19 +18,6 @@
package org.apache.solr.client.solrj;
import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import junit.framework.Assert;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.client.solrj.util.ClientUtils;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.util.XML;
import org.apache.solr.util.AbstractSolrTestCase;
/**
@ -52,212 +39,7 @@ abstract public class SolrExampleTestBase extends AbstractSolrTestCase
protected abstract SolrServer getSolrServer();
/**
* query the example
* Create a new solr server
*/
public void testExampleConfig() throws Exception
{
SolrServer server = getSolrServer();
// Empty the database...
server.deleteByQuery( "*:*" );// delete everything!
// Now add something...
SolrInputDocument doc = new SolrInputDocument();
String docID = "1112211111";
doc.addField( "id", docID, 1.0f );
doc.addField( "name", "my name!", 1.0f );
Assert.assertEquals( null, doc.getField("foo") );
Assert.assertTrue(doc.getField("name").getValue() != null );
UpdateResponse upres = server.add( doc );
System.out.println( "ADD:"+upres.getResponse() );
Assert.assertEquals(0, upres.getStatus());
upres = server.commit( true, true );
System.out.println( "COMMIT:"+upres.getResponse() );
Assert.assertEquals(0, upres.getStatus());
upres = server.optimize( true, true );
System.out.println( "OPTIMIZE:"+upres.getResponse() );
Assert.assertEquals(0, upres.getStatus());
SolrQuery query = new SolrQuery();
query.setQuery( "id:"+docID );
QueryResponse response = server.query( query );
Assert.assertEquals(docID, response.getResults().get(0).getFieldValue("id") );
// Now add a few docs for facet testing...
List<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
SolrInputDocument doc2 = new SolrInputDocument();
doc2.addField( "id", "2", 1.0f );
doc2.addField( "inStock", true, 1.0f );
doc2.addField( "price", 2, 1.0f );
doc2.addField( "timestamp", new java.util.Date(), 1.0f );
docs.add(doc2);
SolrInputDocument doc3 = new SolrInputDocument();
doc3.addField( "id", "3", 1.0f );
doc3.addField( "inStock", false, 1.0f );
doc3.addField( "price", 3, 1.0f );
doc3.addField( "timestamp", new java.util.Date(), 1.0f );
docs.add(doc3);
SolrInputDocument doc4 = new SolrInputDocument();
doc4.addField( "id", "4", 1.0f );
doc4.addField( "inStock", true, 1.0f );
doc4.addField( "price", 4, 1.0f );
doc4.addField( "timestamp", new java.util.Date(), 1.0f );
docs.add(doc4);
SolrInputDocument doc5 = new SolrInputDocument();
doc5.addField( "id", "5", 1.0f );
doc5.addField( "inStock", false, 1.0f );
doc5.addField( "price", 5, 1.0f );
doc5.addField( "timestamp", new java.util.Date(), 1.0f );
docs.add(doc5);
upres = server.add( docs );
System.out.println( "ADD:"+upres.getResponse() );
Assert.assertEquals(0, upres.getStatus());
upres = server.commit( true, true );
System.out.println( "COMMIT:"+upres.getResponse() );
Assert.assertEquals(0, upres.getStatus());
upres = server.optimize( true, true );
System.out.println( "OPTIMIZE:"+upres.getResponse() );
Assert.assertEquals(0, upres.getStatus());
query = new SolrQuery("*:*");
query.addFacetQuery("price:[* TO 2]");
query.addFacetQuery("price:[2 TO 4]");
query.addFacetQuery("price:[5 TO *]");
query.addFacetField("inStock");
query.addFacetField("price");
query.addFacetField("timestamp");
query.removeFilterQuery("inStock:true");
response = server.query( query );
Assert.assertEquals(0, response.getStatus());
Assert.assertEquals(5, response.getResults().getNumFound() );
Assert.assertEquals(3, response.getFacetQuery().size());
Assert.assertEquals(2, response.getFacetField("inStock").getValueCount());
Assert.assertEquals(4, response.getFacetField("price").getValueCount());
// test a second query, test making a copy of the main query
SolrQuery query2 = query.getCopy();
query2.addFilterQuery("inStock:true");
response = server.query( query2 );
Assert.assertEquals(1, query2.getFilterQueries().length);
Assert.assertEquals(0, response.getStatus());
Assert.assertEquals(2, response.getResults().getNumFound() );
Assert.assertFalse(query.getFilterQueries() == query2.getFilterQueries());
}
/**
* query the example
*/
public void testAddRetrieve() throws Exception
{
SolrServer server = getSolrServer();
// Empty the database...
server.deleteByQuery( "*:*" );// delete everything!
// Now add something...
SolrInputDocument doc1 = new SolrInputDocument();
doc1.addField( "id", "id1", 1.0f );
doc1.addField( "name", "doc1", 1.0f );
doc1.addField( "price", 10 );
SolrInputDocument doc2 = new SolrInputDocument();
doc2.addField( "id", "id2", 1.0f );
doc2.addField( "name", "doc2", 1.0f );
doc2.addField( "price", 20 );
Collection<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
docs.add( doc1 );
docs.add( doc2 );
// Add the documents
server.add( docs );
server.commit();
SolrQuery query = new SolrQuery();
query.setQuery( "*:*" );
query.addSortField( "price", SolrQuery.ORDER.asc );
QueryResponse rsp = server.query( query );
Assert.assertEquals( 2, rsp.getResults().getNumFound() );
System.out.println( rsp.getResults() );
// Now do it again
server.add( docs );
server.commit();
rsp = server.query( query );
Assert.assertEquals( 2, rsp.getResults().getNumFound() );
System.out.println( rsp.getResults() );
}
protected void assertNumFound( String query, int num ) throws SolrServerException, IOException
{
QueryResponse rsp = getSolrServer().query( new SolrQuery( query ) );
if( num != rsp.getResults().getNumFound() ) {
fail( "expected: "+num +" but had: "+rsp.getResults().getNumFound() + " :: " + rsp.getResults() );
}
}
public void testAddDelete() throws Exception
{
SolrServer server = getSolrServer();
// Empty the database...
server.deleteByQuery( "*:*" );// delete everything!
SolrInputDocument[] doc = new SolrInputDocument[3];
for( int i=0; i<3; i++ ) {
doc[i] = new SolrInputDocument();
doc[i].setField( "id", i + " & 222", 1.0f );
}
String id = (String) doc[0].getField( "id" ).getFirstValue();
server.add( doc[0] );
server.commit();
assertNumFound( "*:*", 1 ); // make sure it got in
// make sure it got in there
server.deleteById( id );
server.commit();
assertNumFound( "*:*", 0 ); // make sure it got out
// add it back
server.add( doc[0] );
server.commit();
assertNumFound( "*:*", 1 ); // make sure it got in
server.deleteByQuery( "id:\""+ClientUtils.escapeQueryChars(id)+"\"" );
server.commit();
assertNumFound( "*:*", 0 ); // make sure it got out
// Add two documents
for( SolrInputDocument d : doc ) {
server.add( d );
}
server.commit();
assertNumFound( "*:*", 3 ); // make sure it got in
// should be able to handle multiple delete commands in a single go
StringWriter xml = new StringWriter();
xml.append( "<delete>" );
for( SolrInputDocument d : doc ) {
xml.append( "<id>" );
XML.escapeCharData( (String)d.getField( "id" ).getFirstValue(), xml );
xml.append( "</id>" );
}
xml.append( "</delete>" );
DirectXmlUpdateRequest up = new DirectXmlUpdateRequest( "/update", xml.toString() );
server.request( up );
server.commit();
assertNumFound( "*:*", 0 ); // make sure it got out
}
protected abstract SolrServer createNewSolrServer();
}

View File

@ -0,0 +1,254 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.solr.client.solrj;
import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import junit.framework.Assert;
import org.apache.solr.client.solrj.response.QueryResponse;
import org.apache.solr.client.solrj.response.UpdateResponse;
import org.apache.solr.client.solrj.util.ClientUtils;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.common.util.XML;
/**
* This should include tests against the example solr config
*
* This lets us try various SolrServer implementations with the same tests.
*
* @version $Id$
* @since solr 1.3
*/
abstract public class SolrExampleTests extends SolrExampleTestBase
{
/**
* query the example
*/
public void testExampleConfig() throws Exception
{
SolrServer server = getSolrServer();
// Empty the database...
server.deleteByQuery( "*:*" );// delete everything!
// Now add something...
SolrInputDocument doc = new SolrInputDocument();
String docID = "1112211111";
doc.addField( "id", docID, 1.0f );
doc.addField( "name", "my name!", 1.0f );
Assert.assertEquals( null, doc.getField("foo") );
Assert.assertTrue(doc.getField("name").getValue() != null );
UpdateResponse upres = server.add( doc );
System.out.println( "ADD:"+upres.getResponse() );
Assert.assertEquals(0, upres.getStatus());
upres = server.commit( true, true );
System.out.println( "COMMIT:"+upres.getResponse() );
Assert.assertEquals(0, upres.getStatus());
upres = server.optimize( true, true );
System.out.println( "OPTIMIZE:"+upres.getResponse() );
Assert.assertEquals(0, upres.getStatus());
SolrQuery query = new SolrQuery();
query.setQuery( "id:"+docID );
QueryResponse response = server.query( query );
Assert.assertEquals(docID, response.getResults().get(0).getFieldValue("id") );
// Now add a few docs for facet testing...
List<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
SolrInputDocument doc2 = new SolrInputDocument();
doc2.addField( "id", "2", 1.0f );
doc2.addField( "inStock", true, 1.0f );
doc2.addField( "price", 2, 1.0f );
doc2.addField( "timestamp", new java.util.Date(), 1.0f );
docs.add(doc2);
SolrInputDocument doc3 = new SolrInputDocument();
doc3.addField( "id", "3", 1.0f );
doc3.addField( "inStock", false, 1.0f );
doc3.addField( "price", 3, 1.0f );
doc3.addField( "timestamp", new java.util.Date(), 1.0f );
docs.add(doc3);
SolrInputDocument doc4 = new SolrInputDocument();
doc4.addField( "id", "4", 1.0f );
doc4.addField( "inStock", true, 1.0f );
doc4.addField( "price", 4, 1.0f );
doc4.addField( "timestamp", new java.util.Date(), 1.0f );
docs.add(doc4);
SolrInputDocument doc5 = new SolrInputDocument();
doc5.addField( "id", "5", 1.0f );
doc5.addField( "inStock", false, 1.0f );
doc5.addField( "price", 5, 1.0f );
doc5.addField( "timestamp", new java.util.Date(), 1.0f );
docs.add(doc5);
upres = server.add( docs );
System.out.println( "ADD:"+upres.getResponse() );
Assert.assertEquals(0, upres.getStatus());
upres = server.commit( true, true );
System.out.println( "COMMIT:"+upres.getResponse() );
Assert.assertEquals(0, upres.getStatus());
upres = server.optimize( true, true );
System.out.println( "OPTIMIZE:"+upres.getResponse() );
Assert.assertEquals(0, upres.getStatus());
query = new SolrQuery("*:*");
query.addFacetQuery("price:[* TO 2]");
query.addFacetQuery("price:[2 TO 4]");
query.addFacetQuery("price:[5 TO *]");
query.addFacetField("inStock");
query.addFacetField("price");
query.addFacetField("timestamp");
query.removeFilterQuery("inStock:true");
response = server.query( query );
Assert.assertEquals(0, response.getStatus());
Assert.assertEquals(5, response.getResults().getNumFound() );
Assert.assertEquals(3, response.getFacetQuery().size());
Assert.assertEquals(2, response.getFacetField("inStock").getValueCount());
Assert.assertEquals(4, response.getFacetField("price").getValueCount());
// test a second query, test making a copy of the main query
SolrQuery query2 = query.getCopy();
query2.addFilterQuery("inStock:true");
response = server.query( query2 );
Assert.assertEquals(1, query2.getFilterQueries().length);
Assert.assertEquals(0, response.getStatus());
Assert.assertEquals(2, response.getResults().getNumFound() );
Assert.assertFalse(query.getFilterQueries() == query2.getFilterQueries());
}
/**
* query the example
*/
public void testAddRetrieve() throws Exception
{
SolrServer server = getSolrServer();
// Empty the database...
server.deleteByQuery( "*:*" );// delete everything!
// Now add something...
SolrInputDocument doc1 = new SolrInputDocument();
doc1.addField( "id", "id1", 1.0f );
doc1.addField( "name", "doc1", 1.0f );
doc1.addField( "price", 10 );
SolrInputDocument doc2 = new SolrInputDocument();
doc2.addField( "id", "id2", 1.0f );
doc2.addField( "name", "doc2", 1.0f );
doc2.addField( "price", 20 );
Collection<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
docs.add( doc1 );
docs.add( doc2 );
// Add the documents
server.add( docs );
server.commit();
SolrQuery query = new SolrQuery();
query.setQuery( "*:*" );
query.addSortField( "price", SolrQuery.ORDER.asc );
QueryResponse rsp = server.query( query );
Assert.assertEquals( 2, rsp.getResults().getNumFound() );
System.out.println( rsp.getResults() );
// Now do it again
server.add( docs );
server.commit();
rsp = server.query( query );
Assert.assertEquals( 2, rsp.getResults().getNumFound() );
System.out.println( rsp.getResults() );
}
protected void assertNumFound( String query, int num ) throws SolrServerException, IOException
{
QueryResponse rsp = getSolrServer().query( new SolrQuery( query ) );
if( num != rsp.getResults().getNumFound() ) {
fail( "expected: "+num +" but had: "+rsp.getResults().getNumFound() + " :: " + rsp.getResults() );
}
}
public void testAddDelete() throws Exception
{
SolrServer server = getSolrServer();
// Empty the database...
server.deleteByQuery( "*:*" );// delete everything!
SolrInputDocument[] doc = new SolrInputDocument[3];
for( int i=0; i<3; i++ ) {
doc[i] = new SolrInputDocument();
doc[i].setField( "id", i + " & 222", 1.0f );
}
String id = (String) doc[0].getField( "id" ).getFirstValue();
server.add( doc[0] );
server.commit();
assertNumFound( "*:*", 1 ); // make sure it got in
// make sure it got in there
server.deleteById( id );
server.commit();
assertNumFound( "*:*", 0 ); // make sure it got out
// add it back
server.add( doc[0] );
server.commit();
assertNumFound( "*:*", 1 ); // make sure it got in
server.deleteByQuery( "id:\""+ClientUtils.escapeQueryChars(id)+"\"" );
server.commit();
assertNumFound( "*:*", 0 ); // make sure it got out
// Add two documents
for( SolrInputDocument d : doc ) {
server.add( d );
}
server.commit();
assertNumFound( "*:*", 3 ); // make sure it got in
// should be able to handle multiple delete commands in a single go
StringWriter xml = new StringWriter();
xml.append( "<delete>" );
for( SolrInputDocument d : doc ) {
xml.append( "<id>" );
XML.escapeCharData( (String)d.getField( "id" ).getFirstValue(), xml );
xml.append( "</id>" );
}
xml.append( "</delete>" );
DirectXmlUpdateRequest up = new DirectXmlUpdateRequest( "/update", xml.toString() );
server.request( up );
server.commit();
assertNumFound( "*:*", 0 ); // make sure it got out
}
}

View File

@ -17,9 +17,8 @@
package org.apache.solr.client.solrj.embedded;
import org.apache.solr.client.solrj.SolrExampleTestBase;
import org.apache.solr.client.solrj.SolrExampleTests;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.core.SolrCore;
/**
* This runs SolrServer test using
@ -27,7 +26,7 @@ import org.apache.solr.core.SolrCore;
* @version $Id$
* @since solr 1.3
*/
public class TestEmbeddedSolrServer extends SolrExampleTestBase {
public class SolrExampleEmbeddedTest extends SolrExampleTests {
SolrServer server;
@ -36,7 +35,7 @@ public class TestEmbeddedSolrServer extends SolrExampleTestBase {
super.setUp();
// setup the server...
server = new EmbeddedSolrServer( h.getCore() );
server = createNewSolrServer();
}
@Override
@ -44,4 +43,10 @@ public class TestEmbeddedSolrServer extends SolrExampleTestBase {
{
return server;
}
@Override
protected SolrServer createNewSolrServer()
{
return new EmbeddedSolrServer( h.getCore() );
}
}

View File

@ -17,9 +17,11 @@
package org.apache.solr.client.solrj.embedded;
import org.apache.solr.client.solrj.SolrExampleTestBase;
import org.apache.solr.client.solrj.SolrExampleTests;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;
import org.apache.solr.core.SolrCore;
import org.apache.solr.schema.SchemaField;
/**
* TODO? perhaps use:
@ -29,27 +31,22 @@ import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer;
* @version $Id$
* @since solr 1.3
*/
public class TestJettySolrRunner extends SolrExampleTestBase {
public class SolrExampleJettyTest extends SolrExampleTests {
SolrServer server;
JettySolrRunner jetty;
static final int port = 8984; // not 8983
static final String context = "/example";
@Override public void setUp() throws Exception
{
super.setUp();
int port = 8984; // not 8983
String context = "/example";
jetty = new JettySolrRunner( context, port );
jetty.start();
// setup the server...
String url = "http://localhost:"+port+context;
server = new CommonsHttpSolrServer( url );
((CommonsHttpSolrServer)server).setConnectionTimeout(5);
((CommonsHttpSolrServer)server).setDefaultMaxConnectionsPerHost(100);
((CommonsHttpSolrServer)server).setMaxTotalConnections(100);
server = this.createNewSolrServer();
}
@Override public void tearDown() throws Exception
@ -64,4 +61,21 @@ public class TestJettySolrRunner extends SolrExampleTestBase {
{
return server;
}
@Override
protected SolrServer createNewSolrServer()
{
try {
// setup the server...
String url = "http://localhost:"+port+context;
CommonsHttpSolrServer s = new CommonsHttpSolrServer( url );
s.setConnectionTimeout(5);
s.setDefaultMaxConnectionsPerHost(100);
s.setMaxTotalConnections(100);
return s;
}
catch( Exception ex ) {
throw new RuntimeException( ex );
}
}
}