PageRenderTime 65ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 1ms

/solr/solrj/src/test/org/apache/solr/client/solrj/beans/TestDocumentObjectBinder.java

https://bitbucket.org/jmblair/lucene-solr
Java | 260 lines | 193 code | 46 blank | 21 comment | 0 complexity | 6d74130bae20200cb772a0328db7af16 MD5 | raw file
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.solr.client.solrj.beans;
  18. import org.apache.lucene.util.LuceneTestCase;
  19. import org.apache.solr.client.solrj.impl.XMLResponseParser;
  20. import org.apache.solr.client.solrj.response.QueryResponse;
  21. import org.apache.solr.client.solrj.util.ClientUtils;
  22. import org.apache.solr.common.SolrDocumentList;
  23. import org.apache.solr.common.SolrInputDocument;
  24. import org.apache.solr.common.SolrInputField;
  25. import org.apache.solr.common.SolrDocument;
  26. import org.apache.solr.common.util.NamedList;
  27. import org.junit.Assert;
  28. import org.junit.Test;
  29. import java.io.StringReader;
  30. import java.util.Arrays;
  31. import java.util.Date;
  32. import java.util.HashMap;
  33. import java.util.List;
  34. import java.util.Map;
  35. import static org.junit.Assert.*;
  36. public class TestDocumentObjectBinder extends LuceneTestCase {
  37. public void testSimple() throws Exception {
  38. DocumentObjectBinder binder = new DocumentObjectBinder();
  39. XMLResponseParser parser = new XMLResponseParser();
  40. NamedList<Object> nl = parser.processResponse(new StringReader(xml));
  41. QueryResponse res = new QueryResponse(nl, null);
  42. SolrDocumentList solDocList = res.getResults();
  43. List<Item> l = binder.getBeans(Item.class,res.getResults());
  44. assertEquals(solDocList.size(), l.size());
  45. assertEquals(solDocList.get(0).getFieldValue("features"), l.get(0).features);
  46. Item item = new Item();
  47. item.id = "aaa";
  48. item.categories = new String[] {"aaa", "bbb", "ccc"};
  49. SolrInputDocument out = binder.toSolrInputDocument(item);
  50. assertEquals(item.id, out.getFieldValue("id"));
  51. SolrInputField catfield = out.getField("cat");
  52. assertEquals(3, catfield.getValueCount());
  53. List<String> catValues = (List<String>) catfield.getValue();
  54. assertEquals("aaa", catValues.get(0));
  55. assertEquals("bbb", catValues.get(1));
  56. assertEquals("ccc", catValues.get(2));
  57. }
  58. @Test(expected = BindingException.class)
  59. public void testNoGetterError() {
  60. NotGettableItem notGettableItem = new NotGettableItem();
  61. notGettableItem.setInStock(false);
  62. new DocumentObjectBinder().toSolrInputDocument(notGettableItem);
  63. }
  64. public void testSingleVal4Array() {
  65. DocumentObjectBinder binder = new DocumentObjectBinder();
  66. SolrDocumentList solDocList = new SolrDocumentList();
  67. SolrDocument d = new SolrDocument();
  68. solDocList.add(d);
  69. d.setField("cat", "hello");
  70. List<Item> l = binder.getBeans(Item.class, solDocList);
  71. assertEquals("hello", l.get(0).categories[0]);
  72. }
  73. public void testDynamicFieldBinding() {
  74. DocumentObjectBinder binder = new DocumentObjectBinder();
  75. XMLResponseParser parser = new XMLResponseParser();
  76. NamedList<Object> nl = parser.processResponse(new StringReader(xml));
  77. QueryResponse res = new QueryResponse(nl, null);
  78. List<Item> l = binder.getBeans(Item.class,res.getResults());
  79. assertArrayEquals(new String[]{"Mobile Store", "iPod Store", "CCTV Store"}, l.get(3).getAllSuppliers());
  80. assertTrue(l.get(3).supplier.containsKey("supplier_1"));
  81. assertTrue(l.get(3).supplier.containsKey("supplier_2"));
  82. assertEquals(2, l.get(3).supplier.size());
  83. List<String> supplierOne = l.get(3).supplier.get("supplier_1");
  84. assertEquals("Mobile Store", supplierOne.get(0));
  85. assertEquals("iPod Store", supplierOne.get(1));
  86. List<String> supplierTwo = l.get(3).supplier.get("supplier_2");
  87. assertEquals("CCTV Store", supplierTwo.get(0));
  88. }
  89. public void testToAndFromSolrDocument() {
  90. Item item = new Item();
  91. item.id = "one";
  92. item.inStock = false;
  93. item.categories = new String[] {"aaa", "bbb", "ccc"};
  94. item.features = Arrays.asList(item.categories);
  95. List<String> supA = Arrays.asList("supA1", "supA2", "supA3");
  96. List<String> supB = Arrays.asList("supB1", "supB2", "supB3");
  97. item.supplier = new HashMap<String, List<String>>();
  98. item.supplier.put("supplier_supA", supA);
  99. item.supplier.put("supplier_supB", supB);
  100. item.supplier_simple = new HashMap<String, String>();
  101. item.supplier_simple.put("sup_simple_supA", "supA_val");
  102. item.supplier_simple.put("sup_simple_supB", "supB_val");
  103. DocumentObjectBinder binder = new DocumentObjectBinder();
  104. SolrInputDocument doc = binder.toSolrInputDocument(item);
  105. SolrDocumentList docs = new SolrDocumentList();
  106. docs.add(ClientUtils.toSolrDocument(doc));
  107. Item out = binder.getBeans(Item.class, docs).get(0);
  108. Item singleOut = binder.getBean(Item.class, ClientUtils.toSolrDocument(doc));
  109. // make sure it came out the same
  110. assertEquals(item.id, out.id);
  111. assertEquals(item.inStock, out.inStock);
  112. assertEquals(item.categories.length, out.categories.length);
  113. assertEquals(item.features, out.features);
  114. assertEquals(supA, out.supplier.get("supplier_supA"));
  115. assertEquals(supB, out.supplier.get("supplier_supB"));
  116. assertEquals(item.supplier_simple.get("sup_simple_supB"), out.supplier_simple.get("sup_simple_supB"));
  117. assertEquals(item.id, singleOut.id);
  118. assertEquals(item.inStock, singleOut.inStock);
  119. assertEquals(item.categories.length, singleOut.categories.length);
  120. assertEquals(item.features, singleOut.features);
  121. assertEquals(supA, singleOut.supplier.get("supplier_supA"));
  122. assertEquals(supB, singleOut.supplier.get("supplier_supB"));
  123. assertEquals(item.supplier_simple.get("sup_simple_supB"), out.supplier_simple.get("sup_simple_supB"));
  124. // put back "out" as Bean, to see if both ways work as you would expect
  125. // but the Field that "allSuppliers" need to be cleared, as it is just for
  126. // retrieving data, not to post data
  127. out.allSuppliers = null;
  128. SolrInputDocument doc1 = binder.toSolrInputDocument(out);
  129. SolrDocumentList docs1 = new SolrDocumentList();
  130. docs1.add(ClientUtils.toSolrDocument(doc1));
  131. Item out1 = binder.getBeans(Item.class, docs1).get(0);
  132. assertEquals(item.id, out1.id);
  133. assertEquals(item.inStock, out1.inStock);
  134. assertEquals(item.categories.length, out1.categories.length);
  135. assertEquals(item.features, out1.features);
  136. assertEquals(item.supplier_simple.get("sup_simple_supB"), out1.supplier_simple.get("sup_simple_supB"));
  137. assertEquals(supA, out1.supplier.get("supplier_supA"));
  138. assertEquals(supB, out1.supplier.get("supplier_supB"));
  139. }
  140. public static class Item {
  141. @Field
  142. String id;
  143. @Field("cat")
  144. String[] categories;
  145. @Field
  146. List<String> features;
  147. @Field
  148. Date timestamp;
  149. @Field("highway_mileage")
  150. int mwyMileage;
  151. boolean inStock;
  152. @Field("supplier_*")
  153. Map<String, List<String>> supplier;
  154. @Field("sup_simple_*")
  155. Map<String, String> supplier_simple;
  156. private String[] allSuppliers;
  157. @Field("supplier_*")
  158. public void setAllSuppliers(String[] allSuppliers) {
  159. this.allSuppliers = allSuppliers;
  160. }
  161. public String[] getAllSuppliers() {
  162. return this.allSuppliers;
  163. }
  164. @Field
  165. public void setInStock(Boolean b) {
  166. inStock = b;
  167. }
  168. // required if you want to fill SolrDocuments with the same annotaion...
  169. public boolean isInStock() {
  170. return inStock;
  171. }
  172. }
  173. public static class NotGettableItem {
  174. @Field
  175. String id;
  176. private boolean inStock;
  177. private String aaa;
  178. @Field
  179. public void setInStock(Boolean b) {
  180. inStock = b;
  181. }
  182. public String getAaa() {
  183. return aaa;
  184. }
  185. @Field
  186. public void setAaa(String aaa) {
  187. this.aaa = aaa;
  188. }
  189. }
  190. public static final String xml =
  191. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
  192. "<response>" +
  193. "<lst name=\"responseHeader\"><int name=\"status\">0</int><int name=\"QTime\">0</int><lst name=\"params\"><str name=\"start\">0</str><str name=\"q\">*:*\n" +
  194. "</str><str name=\"version\">2.2</str><str name=\"rows\">4</str></lst></lst><result name=\"response\" numFound=\"26\" start=\"0\"><doc><arr name=\"cat\">" +
  195. "<str>electronics</str><str>hard drive</str></arr><arr name=\"features\"><str>7200RPM, 8MB cache, IDE Ultra ATA-133</str>" +
  196. "<str>NoiseGuard, SilentSeek technology, Fluid Dynamic Bearing (FDB) motor</str></arr><str name=\"id\">SP2514N</str>" +
  197. "<bool name=\"inStock\">true</bool><str name=\"manu\">Samsung Electronics Co. Ltd.</str><str name=\"name\">Samsung SpinPoint P120 SP2514N - hard drive - 250 GB - ATA-133</str>" +
  198. "<int name=\"popularity\">6</int><float name=\"price\">92.0</float><str name=\"sku\">SP2514N</str><date name=\"timestamp\">2008-04-16T10:35:57.078Z</date></doc>" +
  199. "<doc><arr name=\"cat\"><str>electronics</str><str>hard drive</str></arr><arr name=\"features\"><str>SATA 3.0Gb/s, NCQ</str><str>8.5ms seek</str>" +
  200. "<str>16MB cache</str></arr><str name=\"id\">6H500F0</str><bool name=\"inStock\">true</bool><str name=\"manu\">Maxtor Corp.</str>" +
  201. "<str name=\"name\">Maxtor DiamondMax 11 - hard drive - 500 GB - SATA-300</str><int name=\"popularity\">6</int><float name=\"price\">350.0</float>" +
  202. "<str name=\"sku\">6H500F0</str><date name=\"timestamp\">2008-04-16T10:35:57.109Z</date></doc><doc><arr name=\"cat\"><str>electronics</str>" +
  203. "<str>connector</str></arr><arr name=\"features\"><str>car power adapter, white</str></arr><str name=\"id\">F8V7067-APL-KIT</str>" +
  204. "<bool name=\"inStock\">false</bool><str name=\"manu\">Belkin</str><str name=\"name\">Belkin Mobile Power Cord for iPod w/ Dock</str>" +
  205. "<int name=\"popularity\">1</int><float name=\"price\">19.95</float><str name=\"sku\">F8V7067-APL-KIT</str>" +
  206. "<date name=\"timestamp\">2008-04-16T10:35:57.140Z</date><float name=\"weight\">4.0</float></doc><doc>" +
  207. "<arr name=\"cat\"><str>electronics</str><str>connector</str></arr><arr name=\"features\">" +
  208. "<str>car power adapter for iPod, white</str></arr><str name=\"id\">IW-02</str><bool name=\"inStock\">false</bool>" +
  209. "<str name=\"manu\">Belkin</str><str name=\"name\">iPod &amp; iPod Mini USB 2.0 Cable</str>" +
  210. "<int name=\"popularity\">1</int><float name=\"price\">11.5</float><str name=\"sku\">IW-02</str>" +
  211. "<str name=\"supplier_1\">Mobile Store</str><str name=\"supplier_1\">iPod Store</str><str name=\"supplier_2\">CCTV Store</str>" +
  212. "<date name=\"timestamp\">2008-04-16T10:35:57.140Z</date><float name=\"weight\">2.0</float></doc></result>\n" +
  213. "</response>";
  214. }