Apache Lucene open-source search software
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)
Ryan McKinley69095c5de017
diff --git a/src/java/org/apache/solr/handler/StaxUpdateRequestHandler.java b/src/java/org/apache/solr/handler/StaxUpdateRequestHandler.javaindex ce359590f89..57a0a94d93e 100644--- a/src/java/org/apache/solr/handler/StaxUpdateRequestHandler.java+++ b/src/java/org/apache/solr/handler/StaxUpdateRequestHandler.java@@ -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());diff --git a/src/java/org/apache/solr/update/DocumentBuilder.java b/src/java/org/apache/solr/update/DocumentBuilder.javaindex a84b36e121a..4b861acdce1 100644--- a/src/java/org/apache/solr/update/DocumentBuilder.java+++ b/src/java/org/apache/solr/update/DocumentBuilder.java@@ -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 Documentfor( 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 numberCollection<Object> vals = doc.getFieldValues( name );@@ -186,17 +187,45 @@ public class DocumentBuilder {sfield.getName() + ": " +vals.toString() );}+ SchemaField[] destArr = schema.getCopyFields(name);+// load each field valuefor( 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()) {diff --git a/src/java/org/apache/solr/util/TestHarness.java b/src/java/org/apache/solr/util/TestHarness.javaindex d8f3a7ea148..5089ba852e2 100644--- a/src/java/org/apache/solr/util/TestHarness.java+++ b/src/java/org/apache/solr/util/TestHarness.java@@ -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();}diff --git a/src/test/org/apache/solr/handler/TestStaxUpdateHandler1.java b/src/test/org/apache/solr/handler/TestStaxUpdateHandler1.javanew file mode 100644index 00000000000..aba01096ae3--- /dev/null+++ b/src/test/org/apache/solr/handler/TestStaxUpdateHandler1.java@@ -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 );+ }+}diff --git a/src/test/org/apache/solr/handler/TestStaxUpdateHandler2.java b/src/test/org/apache/solr/handler/TestStaxUpdateHandler2.javanew file mode 100644index 00000000000..7a10c553c5c--- /dev/null+++ b/src/test/org/apache/solr/handler/TestStaxUpdateHandler2.java@@ -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 );+ }+}diff --git a/src/test/org/apache/solr/handler/TestStaxUpdateHandler3.java b/src/test/org/apache/solr/handler/TestStaxUpdateHandler3.javanew file mode 100644index 00000000000..1f50c167596--- /dev/null+++ b/src/test/org/apache/solr/handler/TestStaxUpdateHandler3.java@@ -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 );+ }+}diff --git a/src/test/org/apache/solr/update/DocumentBuilderTest.java b/src/test/org/apache/solr/update/DocumentBuilderTest.javanew file mode 100644index 00000000000..784c5ed667f--- /dev/null+++ b/src/test/org/apache/solr/update/DocumentBuilderTest.java@@ -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() );+ }+ }+}