Testing StaxUpdateRequestHandler with tests written for XmlUpdateRequestHandler

* Added three test subclasses to test updating with StaxUpdateRequestHandler rather then XmlUpdateRequestHandler
* Made the XmlUpdateRequestHandler in TestHarness public so it could be changed after initialization
* Fixed new DocumentBuilder toDocument() impl so it passes tests (copy fields were not handled properly)

git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@548958 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Ryan McKinley 2007-06-20 06:46:23 +00:00
parent be08e28414
commit 69095c5de0
7 changed files with 150 additions and 7 deletions

View File

@ -61,7 +61,7 @@ import org.apache.solr.update.DeleteUpdateCommand;
* </requestHandler>
*
*/
public class StaxUpdateRequestHandler extends RequestHandlerBase
public class StaxUpdateRequestHandler extends XmlUpdateRequestHandler
{
public static Logger log = Logger.getLogger(StaxUpdateRequestHandler.class.getName());

View File

@ -169,14 +169,15 @@ public class DocumentBuilder {
* @since solr 1.3
*/
public static Document toDocument( SolrInputDocument doc, IndexSchema schema )
{
{
Document out = new Document();
// Load fields from SolrDocument to Document
for( String name : doc.getFieldNames() ) {
SchemaField sfield = schema.getField(name);
SchemaField sfield = schema.getFieldOrNull(name);
Float b = doc.getBoost( name );
float boost = (b==null) ? 1.0f : b.floatValue();
boolean used = false;
// Make sure it has the correct number
Collection<Object> vals = doc.getFieldValues( name );
@ -186,17 +187,45 @@ public class DocumentBuilder {
sfield.getName() + ": " +vals.toString() );
}
SchemaField[] destArr = schema.getCopyFields(name);
// load each field value
for( Object v : vals ) {
String val = null;
if( v instanceof Date && sfield.getType() instanceof DateField ) {
// HACK -- date conversion
if( sfield != null && v instanceof Date && sfield.getType() instanceof DateField ) {
DateField df = (DateField)sfield.getType();
val = df.toInternal( (Date)v )+'Z';
}
else if (v != null) {
val = v.toString();
}
out.add( sfield.createField( val, boost ) );
if( sfield != null ) {
used = true;
Field f = sfield.createField( val, boost );
if( f != null ) { // null fields are not added
out.add( f );
}
}
// Add the copy fields
for( SchemaField sf : destArr ) {
// check if the copy field is a multivalued or not
if( !sf.multiValued() && out.get( sf.getName() ) != null ) {
throw new SolrException( SolrException.ErrorCode.BAD_REQUEST,
"ERROR: multiple values encountered for non multiValued copy field " +
sf.getName() + ": " +val );
}
used = true;
Field f = sf.createField( val, boost );
if( f != null ) { // null fields are not added
out.add( f );
}
}
// In lucene, the boost for a given field is the product of the
// document boost and *all* boosts on values of that field.
@ -204,8 +233,14 @@ public class DocumentBuilder {
// first field.
boost = 1.0f;
}
// make sure the field was used somehow...
if( !used ) {
throw new SolrException( SolrException.ErrorCode.BAD_REQUEST,"ERROR:unknown field '" + name + "'");
}
}
// Now validate required fields or add default values
// fields with default values are defacto 'required'
for (SchemaField field : schema.getRequiredFields()) {

View File

@ -26,6 +26,7 @@ import org.apache.solr.request.LocalSolrQueryRequest;
import org.apache.solr.request.QueryResponseWriter;
import org.apache.solr.request.SolrQueryRequest;
import org.apache.solr.request.SolrQueryResponse;
import org.apache.solr.request.SolrRequestHandler;
import org.apache.solr.schema.IndexSchema;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
@ -68,7 +69,7 @@ public class TestHarness {
private SolrCore core;
private XPath xpath = XPathFactory.newInstance().newXPath();
private DocumentBuilder builder;
XmlUpdateRequestHandler updater;
public XmlUpdateRequestHandler updater;
/**
* Assumes "solrconfig.xml" is the config file to use, and
@ -126,7 +127,6 @@ public class TestHarness {
StringWriter writer = new StringWriter(32000);
updater.doLegacyUpdate(req, writer);
return writer.toString();
}

View File

@ -0,0 +1,19 @@
package org.apache.solr.handler;
import org.apache.solr.BasicFunctionalityTest;
/**
* Temporary test to duplicate behavior for the StaxUpdateRequestHandler
*
* When the XmlUpdateRequestHandler is replaced, this should go away
*/
public class TestStaxUpdateHandler1 extends BasicFunctionalityTest
{
@Override
public void setUp() throws Exception {
super.setUp();
h.updater = new StaxUpdateRequestHandler();
h.updater.init( null );
}
}

View File

@ -0,0 +1,19 @@
package org.apache.solr.handler;
import org.apache.solr.ConvertedLegacyTest;
/**
* Temporary test to duplicate behavior for the StaxUpdateRequestHandler
*
* When the XmlUpdateRequestHandler is replaced, this should go away
*/
public class TestStaxUpdateHandler2 extends ConvertedLegacyTest
{
@Override
public void setUp() throws Exception {
super.setUp();
h.updater = new StaxUpdateRequestHandler();
h.updater.init( null );
}
}

View File

@ -0,0 +1,19 @@
package org.apache.solr.handler;
import org.apache.solr.DisMaxRequestHandlerTest;
/**
* Temporary test to duplicate behavior for the StaxUpdateRequestHandler
*
* When the XmlUpdateRequestHandler is replaced, this should go away
*/
public class TestStaxUpdateHandler3 extends DisMaxRequestHandlerTest
{
@Override
public void setUp() throws Exception {
super.setUp();
h.updater = new StaxUpdateRequestHandler();
h.updater.init( null );
}
}

View File

@ -0,0 +1,51 @@
/**
* 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.update;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.SolrInputDocument;
import org.apache.solr.core.SolrCore;
import org.apache.solr.util.AbstractSolrTestCase;
/**
*
* @author ryan
*
*/
public class DocumentBuilderTest extends AbstractSolrTestCase {
@Override public String getSchemaFile() { return "schema.xml"; }
@Override public String getSolrConfigFile() { return "solrconfig.xml"; }
public void testBuildDocument() throws Exception
{
SolrCore core = SolrCore.getSolrCore();
// undefined field
try {
SolrInputDocument doc = new SolrInputDocument();
doc.setField( "unknown field", 12345 );
DocumentBuilder.toDocument( doc, core.getSchema() );
fail( "should throw an error" );
}
catch( SolrException ex ) {
assertEquals( "should be bad request", 400, ex.code() );
}
}
}