Apache Lucene open-source search software
SOLR-227 -- rather then quietly overwrite an item in a Map<String,xxx>, this checks if an old value exists and adds a severe error if so.
Ryan McKinleyc9a8deb0d4ac
diff --git a/src/java/org/apache/solr/core/RequestHandlers.java b/src/java/org/apache/solr/core/RequestHandlers.javaindex a7832827ba5..3dca2340396 100644--- a/src/java/org/apache/solr/core/RequestHandlers.java+++ b/src/java/org/apache/solr/core/RequestHandlers.java@@ -156,8 +156,10 @@ final class RequestHandlers {SolrRequestHandler old = register( name, handler );if( old != null ) {- // TODO: SOLR-179?- log.warning( "multiple handlers registered on the same path! ignoring: "+old );+ String msg = "multiple handlers registered on the same path! ignoring: "+old;+ Throwable t = new SolrException( 500, msg );+ SolrConfig.severeErrors.add( t );+ SolrException.logOnce(log,null,t);}names.put( name, args );}diff --git a/src/java/org/apache/solr/schema/IndexSchema.java b/src/java/org/apache/solr/schema/IndexSchema.javaindex 3584169216d..d6775cfdf88 100644--- a/src/java/org/apache/solr/schema/IndexSchema.java+++ b/src/java/org/apache/solr/schema/IndexSchema.java@@ -339,7 +339,15 @@ public final class IndexSchema {ft.setArgs(this, DOMUtil.toMapExcept(attrs,"name","class"));- fieldTypes.put(ft.typeName,ft);+ FieldType old = fieldTypes.put(ft.typeName,ft);+ if( old != null ) {+ String msg = "[schema.xml] Duplicate fieldType definition for '"+ + ft.typeName + "' ignoring: "+old.toString();++ Throwable t = new SolrException( 500, msg );+ SolrException.logOnce(log,null,t);+ SolrConfig.severeErrors.add( t );+ }log.finest("fieldtype defined: " + ft);}@@ -373,7 +381,16 @@ public final class IndexSchema {SchemaField f = SchemaField.create(name,ft,args);if (node.getNodeName().equals("field")) {- fields.put(f.getName(),f);+ SchemaField old = fields.put(f.getName(),f);+ if( old != null ) {+ String msg = "[schema.xml] Duplicate field definition for '"+ + f.getName() + "' ignoring: "+old.toString();++ Throwable t = new SolrException( 500, msg );+ SolrException.logOnce(log,null,t);+ SolrConfig.severeErrors.add( t );+ }+log.fine("field defined: " + f);if( f.getDefaultValue() != null ) {log.fine(name+" contains default value: " + f.getDefaultValue());@@ -384,8 +401,24 @@ public final class IndexSchema {requiredFields.add(f);}} else if (node.getNodeName().equals("dynamicField")) {- dFields.add(new DynamicField(f));- log.fine("dynamic field defined: " + f);+ // make sure nothing else has the same path+ boolean dup = false;+ for( DynamicField df : dFields ) {+ if( df.regex.equals( f.name ) ) {+ String msg = "[schema.xml] Duplicate DynamicField definition for '"+ + f.getName() + "' ignoring: "+f.toString();++ Throwable t = new SolrException( 500, msg );+ SolrException.logOnce(log,null,t);+ SolrConfig.severeErrors.add( t );+ dup = true;+ break;+ }+ }+ if( !dup ) {+ dFields.add(new DynamicField(f));+ log.fine("dynamic field defined: " + f);+ }} else {// we should never get herethrow new RuntimeException("Unknown field type");diff --git a/src/test/org/apache/solr/schema/BadIndexSchemaTest.java b/src/test/org/apache/solr/schema/BadIndexSchemaTest.javanew file mode 100644index 00000000000..39a5b58df1d--- /dev/null+++ b/src/test/org/apache/solr/schema/BadIndexSchemaTest.java@@ -0,0 +1,86 @@+/**+ * 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.schema;++import java.util.LinkedList;+import java.util.List;++import org.apache.solr.core.SolrConfig;+import org.apache.solr.core.SolrCore;+import org.apache.solr.util.AbstractSolrTestCase;++/**+ * @author ryan+ */+public class BadIndexSchemaTest extends AbstractSolrTestCase {++ @Override public String getSchemaFile() { return "bad-schema.xml"; }+ @Override public String getSolrConfigFile() { return "solrconfig.xml"; }++ @Override+ public void setUp() throws Exception {+ super.setUp();+ }++ @Override+ public void tearDown() throws Exception {+ super.tearDown();+ }+++ private Throwable findErrorWithSubstring( List<Throwable> err, String v )+ {+ for( Throwable t : err ) {+ if( t.getMessage().indexOf( v ) > 0 ) {+ return t;+ }+ }+ return null;+ }+++ public void testSevereErrorsForDuplicateNames()+ {+ SolrCore core = SolrCore.getSolrCore();+ IndexSchema schema = core.getSchema();++ for( Throwable t : SolrConfig.severeErrors ) {+ System.out.println( "ERROR:"+t.getMessage() );+ }++ assertEquals( 3, SolrConfig.severeErrors.size() );++ List<Throwable> err = new LinkedList<Throwable>();+ err.addAll( SolrConfig.severeErrors );++ Throwable t = findErrorWithSubstring( err, "*_twice" );+ assertNotNull( t );+ err.remove( t );++ t = findErrorWithSubstring( err, "ftAgain" );+ assertNotNull( t );+ err.remove( t );++ t = findErrorWithSubstring( err, "fAgain" );+ assertNotNull( t );+ err.remove( t );++ // make sure thats all of them+ assertTrue( err.isEmpty() );+ }+}diff --git a/src/test/test-files/solr/conf/bad-schema.xml b/src/test/test-files/solr/conf/bad-schema.xmlnew file mode 100644index 00000000000..6a14bdb72b6--- /dev/null+++ b/src/test/test-files/solr/conf/bad-schema.xml@@ -0,0 +1,45 @@+<?xml version="1.0" ?>+<!--+ 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.+-->++<schema name="test" version="1.0">+ <types>++ <fieldType name="ftAgain" class="solr.IntField"/>+ <fieldType name="ftAgain" class="solr.IntField"/>++ <!-- this ones is ok -->+ <fieldtype name="text" class="solr.TextField" />++ </types>+++ <fields>+ <field name="id" type="text" indexed="true" stored="true" multiValued="false" required="false"/>++ <field name="fAgain" type="text" indexed="true" stored="true"/>+ <field name="fAgain" type="text" indexed="true" stored="true"/>++ <dynamicField name="*_twice" type="text" indexed="true" stored="true"/>+ <dynamicField name="*_twice" type="text" indexed="true" stored="true"/>++ </fields>++ <defaultSearchField>id</defaultSearchField>+ <uniqueKey>id</uniqueKey>++</schema>