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.

git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@536653 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Ryan McKinley 2007-05-09 20:53:31 +00:00
parent 46428da0dc
commit c9a8deb0d4
4 changed files with 172 additions and 6 deletions

View File

@ -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 );
}

View File

@ -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 here
throw new RuntimeException("Unknown field type");

View File

@ -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() );
}
}

View File

@ -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>