SOLR-193 -- increaseing code coverage on SolrDocument. Fixing some missing logic on adding vs. setting an Iterable field

git-svn-id: https://svn.apache.org/repos/asf/lucene/solr/trunk@663723 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Ryan McKinley 2008-06-05 20:10:28 +00:00
parent 993226e54f
commit 44ce0b1bee
2 changed files with 71 additions and 39 deletions

View File

@ -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<Map.Entry<String, Ob
public void setField(String name, Object value)
{
if( value instanceof Object[] ) {
Object[] arr = (Object[])value;
Collection<Object> c = new ArrayList<Object>( 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<Object> lst = new ArrayList<Object>();
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<Map.Entry<String, Ob
return getFieldValues( (String)key );
}
/** Set the field Value */
public Collection<Object> put(String key, Collection<Object> value) {
setField( key, value );
return null;
}
/** Remove the field Value */
public Collection<Object> remove(Object key) {
removeFields( (String)key );
return null;
}
// Easily Supported methods
public boolean containsKey(Object key) { return _fields.containsKey( key ); }
public Set<String> keySet() { return _fields.keySet(); }
@ -225,6 +219,8 @@ public class SolrDocument implements Serializable, Iterable<Map.Entry<String, Ob
public Set<java.util.Map.Entry<String, Collection<Object>>> entrySet() {throw new UnsupportedOperationException();}
public void putAll(Map<? extends String, ? extends Collection<Object>> t) {throw new UnsupportedOperationException();}
public Collection<Collection<Object>> values() {throw new UnsupportedOperationException();}
public Collection<Object> put(String key, Collection<Object> value) {throw new UnsupportedOperationException();}
public Collection<Object> remove(Object key) {throw new UnsupportedOperationException();}
};
}
@ -238,18 +234,6 @@ public class SolrDocument implements Serializable, Iterable<Map.Entry<String, Ob
return getFirstValue( (String)key );
}
/** Set the field Value */
public Object put(String key, Object value) {
setField( key, value );
return null;
}
/** Remove the field Value */
public Object remove(Object key) {
removeFields( (String)key );
return null;
}
// Easily Supported methods
public boolean containsKey(Object key) { return _fields.containsKey( key ); }
public Set<String> keySet() { return _fields.keySet(); }
@ -262,6 +246,8 @@ public class SolrDocument implements Serializable, Iterable<Map.Entry<String, Ob
public Set<java.util.Map.Entry<String, Object>> entrySet() {throw new UnsupportedOperationException();}
public void putAll(Map<? extends String, ? extends Object> t) {throw new UnsupportedOperationException();}
public Collection<Object> values() {throw new UnsupportedOperationException();}
};
public Collection<Object> put(String key, Object value) {throw new UnsupportedOperationException();}
public Collection<Object> remove(Object key) {throw new UnsupportedOperationException();}
};
}
}

View File

@ -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;
@ -81,37 +82,82 @@ public class SolrDocumentTest extends TestCase
{
SolrDocument doc = new SolrDocument();
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.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.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<String> c0 = new ArrayList<String>();
final List<String> c0 = new ArrayList<String>();
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<String>() );
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()