diff --git a/src/java/org/apache/solr/common/SolrDocument.java b/src/java/org/apache/solr/common/SolrDocument.java index 6f99bf12fef..9b7661799f1 100644 --- a/src/java/org/apache/solr/common/SolrDocument.java +++ b/src/java/org/apache/solr/common/SolrDocument.java @@ -19,6 +19,7 @@ package org.apache.solr.common; import java.io.Serializable; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; @@ -83,12 +84,17 @@ public class SolrDocument implements Serializable, Iterable c = new ArrayList( arr.length ); - for( Object o : arr ) { - c.add( o ); + value = Arrays.asList( (Object[])value ); + } + else if( value instanceof Collection ) { + // nothing + } + else if( value instanceof Iterable ) { + ArrayList lst = new ArrayList(); + for( Object o : (Iterable)value ) { + lst.add( o ); } - value = c; + value = lst; } _fields.put(name, value); } @@ -201,18 +207,6 @@ public class SolrDocument implements Serializable, Iterable put(String key, Collection value) { - setField( key, value ); - return null; - } - - /** Remove the field Value */ - public Collection remove(Object key) { - removeFields( (String)key ); - return null; - } - // Easily Supported methods public boolean containsKey(Object key) { return _fields.containsKey( key ); } public Set keySet() { return _fields.keySet(); } @@ -225,6 +219,8 @@ public class SolrDocument implements Serializable, Iterable>> entrySet() {throw new UnsupportedOperationException();} public void putAll(Map> t) {throw new UnsupportedOperationException();} public Collection> values() {throw new UnsupportedOperationException();} + public Collection put(String key, Collection value) {throw new UnsupportedOperationException();} + public Collection remove(Object key) {throw new UnsupportedOperationException();} }; } @@ -238,18 +234,6 @@ public class SolrDocument implements Serializable, Iterable keySet() { return _fields.keySet(); } @@ -262,6 +246,8 @@ public class SolrDocument implements Serializable, Iterable> entrySet() {throw new UnsupportedOperationException();} public void putAll(Map t) {throw new UnsupportedOperationException();} public Collection values() {throw new UnsupportedOperationException();} - }; + public Collection put(String key, Object value) {throw new UnsupportedOperationException();} + public Collection remove(Object key) {throw new UnsupportedOperationException();} + }; } } diff --git a/src/test/org/apache/solr/common/SolrDocumentTest.java b/src/test/org/apache/solr/common/SolrDocumentTest.java index e380c11139f..108e13a5bd6 100644 --- a/src/test/org/apache/solr/common/SolrDocumentTest.java +++ b/src/test/org/apache/solr/common/SolrDocumentTest.java @@ -20,6 +20,7 @@ package org.apache.solr.common; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.Iterator; import java.util.List; import org.apache.solr.common.SolrDocument; @@ -80,38 +81,83 @@ public class SolrDocumentTest extends TestCase public void testUnsupportedStuff() { SolrDocument doc = new SolrDocument(); + + try { doc.getFieldValueMap().clear(); fail( "should be unsupported!" ); } catch( UnsupportedOperationException ex ){} + try { doc.getFieldValueMap().containsValue( null ); fail( "should be unsupported!" ); } catch( UnsupportedOperationException ex ){} + try { doc.getFieldValueMap().entrySet(); fail( "should be unsupported!" ); } catch( UnsupportedOperationException ex ){} + try { doc.getFieldValueMap().putAll( null ); fail( "should be unsupported!" ); } catch( UnsupportedOperationException ex ){} + try { doc.getFieldValueMap().values(); fail( "should be unsupported!" ); } catch( UnsupportedOperationException ex ){} + try { doc.getFieldValueMap().remove( "key" ); fail( "should be unsupported!" ); } catch( UnsupportedOperationException ex ){} + try { doc.getFieldValueMap().put( "key", "value" ); fail( "should be unsupported!" ); } catch( UnsupportedOperationException ex ){} - try { doc.getFieldValueMap().clear(); fail( "should be unsupported!" ); } catch( Exception ex ){} - try { doc.getFieldValueMap().containsValue( null ); fail( "should be unsupported!" ); } catch( Exception ex ){} - try { doc.getFieldValueMap().entrySet(); fail( "should be unsupported!" ); } catch( Exception ex ){} - try { doc.getFieldValueMap().putAll( null ); fail( "should be unsupported!" ); } catch( Exception ex ){} - try { doc.getFieldValueMap().values(); fail( "should be unsupported!" ); } catch( Exception ex ){} + try { doc.getFieldValuesMap().clear(); fail( "should be unsupported!" ); } catch( UnsupportedOperationException ex ){} + try { doc.getFieldValuesMap().containsValue( null ); fail( "should be unsupported!" ); } catch( UnsupportedOperationException ex ){} + try { doc.getFieldValuesMap().entrySet(); fail( "should be unsupported!" ); } catch( UnsupportedOperationException ex ){} + try { doc.getFieldValuesMap().putAll( null ); fail( "should be unsupported!" ); } catch( UnsupportedOperationException ex ){} + try { doc.getFieldValuesMap().values(); fail( "should be unsupported!" ); } catch( UnsupportedOperationException ex ){} + try { doc.getFieldValuesMap().remove( "key" ); fail( "should be unsupported!" ); } catch( UnsupportedOperationException ex ){} + try { doc.getFieldValueMap().put( "key", Collections.EMPTY_LIST ); fail( "should be unsupported!" ); } catch( UnsupportedOperationException ex ){} assertEquals( null, doc.getFieldValueMap().get( "aaa" ) ); doc.setField( "aaa", "bbb" ); assertEquals( "bbb", doc.getFieldValueMap().get( "aaa" ) ); - doc.getFieldValueMap().remove( "aaa" ); - assertEquals( null, doc.getFieldValueMap().get( "aaa" ) ); } public void testAddCollections() { - List c0 = new ArrayList(); + final List c0 = new ArrayList(); c0.add( "aaa" ); c0.add( "aaa" ); c0.add( "aaa" ); c0.add( "bbb" ); c0.add( "ccc" ); + c0.add( "ddd" ); SolrDocument doc = new SolrDocument(); doc.addField( "v", c0 ); assertEquals( c0.size(), doc.getFieldValues("v").size() ); + assertEquals( c0.get(0), doc.getFirstValue( "v" ) ); // Same thing with an array Object[] arr = new Object[] { "aaa", "aaa", "aaa", 10, 'b' }; doc = new SolrDocument(); - doc.addField( "v", c0 ); + doc.addField( "v", arr ); assertEquals( arr.length, doc.getFieldValues("v").size() ); + // try the same thing with 'setField' + doc.setField( "v", arr ); + assertEquals( arr.length, doc.getFieldValues("v").size() ); + + doc.clear(); + assertEquals( 0, doc.getFieldNames().size() ); + + Iterable iter = new Iterable() { + public Iterator iterator() { + return c0.iterator(); + } + }; + doc.addField( "v", iter ); + assertEquals( c0.size(), doc.getFieldValues("v").size() ); + // do it again to get twice the size... + doc.addField( "v", iter ); + assertEquals( c0.size()*2, doc.getFieldValues("v").size() ); + + // An empty list: + doc.setField( "empty", new ArrayList() ); + assertNull( doc.getFirstValue( "empty" ) ); + + // Try the JSTL accessor functions... + assertFalse( doc.getFieldValueMap().isEmpty() ); + assertFalse( doc.getFieldValuesMap().isEmpty() ); + assertEquals( 2, doc.getFieldValueMap().size() ); + assertEquals( 2, doc.getFieldValuesMap().size() ); + assertTrue( doc.getFieldValueMap().containsKey( "v" ) ); + assertTrue( doc.getFieldValuesMap().containsKey( "v" ) ); + assertTrue( doc.getFieldValueMap().keySet().contains( "v" ) ); + assertTrue( doc.getFieldValuesMap().keySet().contains( "v" ) ); + assertFalse( doc.getFieldValueMap().containsKey( "g" ) ); + assertFalse( doc.getFieldValuesMap().containsKey( "g" ) ); + assertFalse( doc.getFieldValueMap().keySet().contains( "g" ) ); + assertFalse( doc.getFieldValuesMap().keySet().contains( "g" ) ); } public void testDuplicate()