SOLR-2708: Further DocumentObjectBinder cleanup

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1159083 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Christopher John Male 2011-08-18 08:29:26 +00:00
parent 04bd9ae73a
commit 3a9bdecab0
2 changed files with 134 additions and 127 deletions

View File

@ -260,14 +260,18 @@ public class DocumentObjectBinder {
* and <code>Map<String, List<Object>></code> for a dynamic field. The key is all matching fieldName's.
*/
@SuppressWarnings("unchecked")
private Object getFieldValue(SolrDocument sdoc){
Object fieldValue = sdoc.getFieldValue(name);
private Object getFieldValue(SolrDocument solrDocument) {
Object fieldValue = solrDocument.getFieldValue(name);
if (fieldValue != null) {
//this is not a dynamic field. so return te value
//this is not a dynamic field. so return the value
return fieldValue;
}
if (dynamicFieldNamePatternMatcher == null) {
return null;
}
//reading dynamic field values
if (dynamicFieldNamePatternMatcher != null) {
Map<String, Object> allValuesMap = null;
List allValuesList = null;
if (isContainedInMap) {
@ -276,9 +280,9 @@ public class DocumentObjectBinder {
allValuesList = new ArrayList();
}
for (String field : sdoc.getFieldNames()) {
for (String field : solrDocument.getFieldNames()) {
if (dynamicFieldNamePatternMatcher.matcher(field).find()) {
Object val = sdoc.getFieldValue(field);
Object val = solrDocument.getFieldValue(field);
if (val == null) {
continue;
}
@ -315,8 +319,6 @@ public class DocumentObjectBinder {
return allValuesList.isEmpty() ? null : allValuesList;
}
}
return null;
}
<T> void inject(T obj, SolrDocument sdoc) {
Object val = getFieldValue(sdoc);
@ -353,7 +355,6 @@ public class DocumentObjectBinder {
}
private void set(Object obj, Object v) {
if (v != null && type == ByteBuffer.class && v.getClass() == byte[].class) {
v = ByteBuffer.wrap((byte[]) v);

View File

@ -27,6 +27,7 @@ import org.apache.solr.common.SolrInputField;
import org.apache.solr.common.SolrDocument;
import org.apache.solr.common.util.NamedList;
import org.junit.Assert;
import org.junit.Test;
import java.io.StringReader;
import java.util.Arrays;
@ -35,40 +36,43 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class TestDocumentObjectBinder extends LuceneTestCase
{
import static org.junit.Assert.*;
public class TestDocumentObjectBinder extends LuceneTestCase {
public void testSimple() throws Exception {
DocumentObjectBinder binder = new DocumentObjectBinder();
XMLResponseParser parser = new XMLResponseParser();
NamedList<Object> nl = null;
nl = parser.processResponse(new StringReader(xml));
NamedList<Object> nl = parser.processResponse(new StringReader(xml));
QueryResponse res = new QueryResponse(nl, null);
SolrDocumentList solDocList = res.getResults();
List<Item> l = binder.getBeans(Item.class,res.getResults());
Assert.assertEquals(solDocList.size(), l.size());
Assert.assertEquals(solDocList.get(0).getFieldValue("features"), l.get(0).features);
assertEquals(solDocList.size(), l.size());
assertEquals(solDocList.get(0).getFieldValue("features"), l.get(0).features);
Item item = new Item();
item.id = "aaa";
item.categories = new String[] {"aaa", "bbb", "ccc"};
SolrInputDocument out = binder.toSolrInputDocument(item);
Assert.assertEquals( item.id, out.getFieldValue( "id" ) );
assertEquals(item.id, out.getFieldValue("id"));
SolrInputField catfield = out.getField("cat");
Assert.assertEquals( 3, catfield.getValueCount() );
Assert.assertEquals( "[aaa, bbb, ccc]", catfield.getValue().toString() );
assertEquals(3, catfield.getValueCount());
// Test the error on not settable stuff...
NotGettableItem ng = new NotGettableItem();
ng.setInStock( false );
try {
out = binder.toSolrInputDocument( ng );
Assert.fail( "Should throw an error" );
}
catch( RuntimeException ex ) {
// ok -- this should happen...
List<String> catValues = (List<String>) catfield.getValue();
assertEquals("aaa", catValues.get(0));
assertEquals("bbb", catValues.get(1));
assertEquals("ccc", catValues.get(2));
}
@Test(expected = BindingException.class)
public void testNoGetterError() {
NotGettableItem notGettableItem = new NotGettableItem();
notGettableItem.setInStock(false);
new DocumentObjectBinder().toSolrInputDocument(notGettableItem);
}
public void testSingleVal4Array() {
DocumentObjectBinder binder = new DocumentObjectBinder();
SolrDocumentList solDocList = new SolrDocumentList();
@ -76,8 +80,7 @@ public class TestDocumentObjectBinder extends LuceneTestCase
solDocList.add(d);
d.setField("cat", "hello");
List<Item> l = binder.getBeans(Item.class, solDocList);
Assert.assertEquals("hello", l.get(0).categories[0]);
assertEquals("hello", l.get(0).categories[0]);
}
public void testDynamicFieldBinding() {
@ -86,23 +89,28 @@ public class TestDocumentObjectBinder extends LuceneTestCase
NamedList<Object> nl = parser.processResponse(new StringReader(xml));
QueryResponse res = new QueryResponse(nl, null);
List<Item> l = binder.getBeans(Item.class,res.getResults());
Assert.assertArrayEquals(new String[]{"Mobile Store","iPod Store","CCTV Store"}, l.get(3).getAllSuppliers());
Assert.assertTrue(l.get(3).supplier.containsKey("supplier_1"));
Assert.assertTrue(l.get(3).supplier.containsKey("supplier_2"));
Assert.assertEquals(2, l.get(3).supplier.size());
Assert.assertEquals("[Mobile Store, iPod Store]", l.get(3).supplier.get("supplier_1").toString());
Assert.assertEquals("[CCTV Store]", l.get(3).supplier.get("supplier_2").toString());
assertArrayEquals(new String[]{"Mobile Store", "iPod Store", "CCTV Store"}, l.get(3).getAllSuppliers());
assertTrue(l.get(3).supplier.containsKey("supplier_1"));
assertTrue(l.get(3).supplier.containsKey("supplier_2"));
assertEquals(2, l.get(3).supplier.size());
List<String> supplierOne = l.get(3).supplier.get("supplier_1");
assertEquals("Mobile Store", supplierOne.get(0));
assertEquals("iPod Store", supplierOne.get(1));
List<String> supplierTwo = l.get(3).supplier.get("supplier_2");
assertEquals("CCTV Store", supplierTwo.get(0));
}
public void testToAndFromSolrDocument()
{
public void testToAndFromSolrDocument() {
Item item = new Item();
item.id = "one";
item.inStock = false;
item.categories = new String[] {"aaa", "bbb", "ccc"};
item.features = Arrays.asList(item.categories);
List<String> supA = Arrays.asList( new String[] { "supA1", "supA2", "supA3" } );
List<String> supB = Arrays.asList( new String[] { "supB1", "supB2", "supB3"});
List<String> supA = Arrays.asList("supA1", "supA2", "supA3");
List<String> supB = Arrays.asList("supB1", "supB2", "supB3");
item.supplier = new HashMap<String, List<String>>();
item.supplier.put("supplier_supA", supA);
item.supplier.put("supplier_supB", supB);
@ -119,21 +127,21 @@ public class TestDocumentObjectBinder extends LuceneTestCase
Item singleOut = binder.getBean(Item.class, ClientUtils.toSolrDocument(doc));
// make sure it came out the same
Assert.assertEquals( item.id, out.id );
Assert.assertEquals( item.inStock, out.inStock );
Assert.assertEquals( item.categories.length, out.categories.length );
Assert.assertEquals( item.features, out.features );
Assert.assertEquals( supA,out.supplier.get("supplier_supA"));
Assert.assertEquals( supB, out.supplier.get("supplier_supB"));
Assert.assertEquals( item.supplier_simple.get("sup_simple_supB"), out.supplier_simple.get("sup_simple_supB"));
assertEquals(item.id, out.id);
assertEquals(item.inStock, out.inStock);
assertEquals(item.categories.length, out.categories.length);
assertEquals(item.features, out.features);
assertEquals(supA, out.supplier.get("supplier_supA"));
assertEquals(supB, out.supplier.get("supplier_supB"));
assertEquals(item.supplier_simple.get("sup_simple_supB"), out.supplier_simple.get("sup_simple_supB"));
Assert.assertEquals( item.id, singleOut.id );
Assert.assertEquals( item.inStock, singleOut.inStock );
Assert.assertEquals( item.categories.length, singleOut.categories.length );
Assert.assertEquals( item.features, singleOut.features );
Assert.assertEquals( supA, singleOut.supplier.get("supplier_supA"));
Assert.assertEquals( supB, singleOut.supplier.get("supplier_supB"));
Assert.assertEquals( item.supplier_simple.get("sup_simple_supB"), out.supplier_simple.get("sup_simple_supB"));
assertEquals(item.id, singleOut.id);
assertEquals(item.inStock, singleOut.inStock);
assertEquals(item.categories.length, singleOut.categories.length);
assertEquals(item.features, singleOut.features);
assertEquals(supA, singleOut.supplier.get("supplier_supA"));
assertEquals(supB, singleOut.supplier.get("supplier_supB"));
assertEquals(item.supplier_simple.get("sup_simple_supB"), out.supplier_simple.get("sup_simple_supB"));
// put back "out" as Bean, to see if both ways work as you would expect
// but the Field that "allSuppliers" need to be cleared, as it is just for
@ -145,16 +153,15 @@ public class TestDocumentObjectBinder extends LuceneTestCase
docs1.add(ClientUtils.toSolrDocument(doc1));
Item out1 = binder.getBeans(Item.class, docs1).get(0);
Assert.assertEquals( item.id, out1.id );
Assert.assertEquals( item.inStock, out1.inStock );
Assert.assertEquals( item.categories.length, out1.categories.length );
Assert.assertEquals( item.features, out1.features );
assertEquals(item.id, out1.id);
assertEquals(item.inStock, out1.inStock);
assertEquals(item.categories.length, out1.categories.length);
assertEquals(item.features, out1.features);
Assert.assertEquals( item.supplier_simple.get("sup_simple_supB"), out1.supplier_simple.get("sup_simple_supB"));
Assert.assertEquals( supA,out1.supplier.get("supplier_supA"));
Assert.assertEquals( supB, out1.supplier.get("supplier_supB"));
assertEquals(item.supplier_simple.get("sup_simple_supB"), out1.supplier_simple.get("sup_simple_supB"));
assertEquals(supA, out1.supplier.get("supplier_supA"));
assertEquals(supB, out1.supplier.get("supplier_supB"));
}
public static class Item {
@ -173,7 +180,7 @@ public class TestDocumentObjectBinder extends LuceneTestCase
@Field("highway_mileage")
int mwyMileage;
boolean inStock = false;
boolean inStock;
@Field("supplier_*")
Map<String, List<String>> supplier;
@ -198,8 +205,7 @@ public class TestDocumentObjectBinder extends LuceneTestCase
}
// required if you want to fill SolrDocuments with the same annotaion...
public boolean isInStock()
{
public boolean isInStock() {
return inStock;
}
}