|
28 | 28 | import java.io.InputStreamReader; |
29 | 29 | import java.io.Reader; |
30 | 30 | import java.io.StringReader; |
| 31 | +import java.util.HashSet; |
31 | 32 | import java.util.Set; |
32 | 33 |
|
| 34 | +import javax.xml.bind.JAXBException; |
| 35 | +import javax.xml.bind.annotation.XmlRootElement; |
33 | 36 | import javax.xml.parsers.DocumentBuilder; |
34 | 37 | import javax.xml.parsers.DocumentBuilderFactory; |
35 | 38 | import javax.xml.parsers.ParserConfigurationException; |
|
56 | 59 |
|
57 | 60 | import com.marklogic.client.DatabaseClient; |
58 | 61 | import com.marklogic.client.DatabaseClientFactory; |
| 62 | +import com.marklogic.client.DatabaseClientFactory.Authentication; |
| 63 | +import com.marklogic.client.DatabaseClientFactory.HandleFactoryRegistry; |
59 | 64 | import com.marklogic.client.FailedRequestException; |
60 | 65 | import com.marklogic.client.ForbiddenUserException; |
61 | 66 | import com.marklogic.client.ResourceNotFoundException; |
62 | | -import com.marklogic.client.DatabaseClientFactory.Authentication; |
63 | | -import com.marklogic.client.DatabaseClientFactory.HandleFactoryRegistry; |
64 | 67 | import com.marklogic.client.document.BinaryDocumentManager; |
65 | 68 | import com.marklogic.client.document.TextDocumentManager; |
66 | 69 | import com.marklogic.client.document.XMLDocumentManager; |
67 | 70 | import com.marklogic.client.io.BaseHandle; |
| 71 | +import com.marklogic.client.io.Format; |
| 72 | +import com.marklogic.client.io.JAXBHandle; |
| 73 | +import com.marklogic.client.io.SearchHandle; |
68 | 74 | import com.marklogic.client.io.marker.ContentHandle; |
69 | 75 | import com.marklogic.client.io.marker.ContentHandleFactory; |
| 76 | +import com.marklogic.client.query.DeleteQueryDefinition; |
| 77 | +import com.marklogic.client.query.MatchDocumentSummary; |
| 78 | +import com.marklogic.client.query.QueryDefinition; |
| 79 | +import com.marklogic.client.query.QueryManager; |
70 | 80 |
|
71 | 81 | public class HandleAsTest { |
72 | 82 | @BeforeClass |
@@ -198,19 +208,112 @@ public void testBuiltinReadWrite() |
198 | 208 | assertEquals("StreamReader difference in document read/write as", beforeText, afterText); |
199 | 209 | } |
200 | 210 |
|
| 211 | + @Test |
| 212 | + public void testSearch() throws JAXBException { |
| 213 | + DatabaseClientFactory.Bean clientFactoryBean = makeClientFactory(); |
| 214 | + |
| 215 | + clientFactoryBean.getHandleRegistry().register( |
| 216 | + JAXBHandle.newFactory(Product.class) |
| 217 | + ); |
| 218 | + |
| 219 | + DatabaseClient client = clientFactoryBean.newClient(); |
| 220 | + |
| 221 | + XMLDocumentManager docMgr = client.newXMLDocumentManager(); |
| 222 | + |
| 223 | + QueryManager queryMgr = client.newQueryManager(); |
| 224 | + |
| 225 | + String basedir = "/tmp/jaxb/test/"; |
| 226 | + |
| 227 | + Product product1 = new Product(); |
| 228 | + product1.setName("Widgetry"); |
| 229 | + product1.setIndustry("IT"); |
| 230 | + product1.setDescription("More widgets than you can shake a stick at"); |
| 231 | + |
| 232 | + Product product2 = new Product(); |
| 233 | + product2.setName("AppExcess"); |
| 234 | + product2.setIndustry("IT"); |
| 235 | + product2.setDescription("There's an app for that."); |
| 236 | + |
| 237 | + Product[] products = {product1, product2}; |
| 238 | + |
| 239 | + // setup |
| 240 | + Set<String> prodNames = new HashSet<String>(products.length); |
| 241 | + for (Product product: products) { |
| 242 | + String prodName = product.getName(); |
| 243 | + prodNames.add(prodName); |
| 244 | + String docId = basedir+prodName+".xml"; |
| 245 | + docMgr.writeAs(docId, product); |
| 246 | + } |
| 247 | + |
| 248 | + // test |
| 249 | + String rawQuery = new StringBuilder() |
| 250 | + .append("<search:search xmlns:search=\"http://marklogic.com/appservices/search\">") |
| 251 | + .append("<search:qtext>IT</search:qtext>") |
| 252 | + .append("<search:options>") |
| 253 | + .append("<search:transform-results apply=\"raw\"/> ") |
| 254 | + .append("</search:options>") |
| 255 | + .append("</search:search>") |
| 256 | + .toString(); |
| 257 | + |
| 258 | + QueryDefinition queryDef = |
| 259 | + queryMgr.newRawCombinedQueryDefinitionAs(Format.XML, rawQuery); |
| 260 | + queryDef.setDirectory(basedir); |
| 261 | + |
| 262 | + SearchHandle handle = queryMgr.search(queryDef, new SearchHandle()); |
| 263 | + |
| 264 | + MatchDocumentSummary[] summaries = handle.getMatchResults(); |
| 265 | + assertEquals("raw query should retrieve all products", products.length, summaries.length); |
| 266 | + for (MatchDocumentSummary summary: summaries) { |
| 267 | + Product product = summary.getFirstSnippetAs(Product.class); |
| 268 | + assertTrue("raw product should exist", product != null); |
| 269 | + assertTrue("raw product name should be preserved", prodNames.contains(product.getName())); |
| 270 | + } |
| 271 | + |
| 272 | + rawQuery = new StringBuilder() |
| 273 | + .append("<search:search xmlns:search=\"http://marklogic.com/appservices/search\">") |
| 274 | + .append("<search:qtext>IT</search:qtext>") |
| 275 | + .append("<search:options>") |
| 276 | + .append("<search:extract-metadata>") |
| 277 | + .append("<search:qname elem-ns=\"\" elem-name=\"name\"/>") |
| 278 | + .append("<search:qname elem-ns=\"\" elem-name=\"industry\"/>") |
| 279 | + .append("</search:extract-metadata>") |
| 280 | + .append("</search:options>") |
| 281 | + .append("</search:search>") |
| 282 | + .toString(); |
| 283 | + |
| 284 | + queryDef = |
| 285 | + queryMgr.newRawCombinedQueryDefinitionAs(Format.XML, rawQuery); |
| 286 | + queryDef.setDirectory(basedir); |
| 287 | + |
| 288 | + handle = queryMgr.search(queryDef, new SearchHandle()); |
| 289 | + |
| 290 | + summaries = handle.getMatchResults(); |
| 291 | + assertEquals("metadata query should retrieve all products", products.length, summaries.length); |
| 292 | + for (MatchDocumentSummary summary: summaries) { |
| 293 | + Document productDoc = summary.getMetadataAs(Document.class); |
| 294 | + |
| 295 | + Element name = (Element) productDoc.getElementsByTagName("name").item(0); |
| 296 | + assertTrue("metadata product name should exist", name != null); |
| 297 | + assertTrue("metadata product name should be preserved", prodNames.contains(name.getTextContent())); |
| 298 | + |
| 299 | + Element industry = (Element) productDoc.getElementsByTagName("industry").item(0); |
| 300 | + assertTrue("metadata product industry should exist", industry != null); |
| 301 | + } |
| 302 | + |
| 303 | + // cleanup |
| 304 | + DeleteQueryDefinition deleteDef = queryMgr.newDeleteDefinition(); |
| 305 | + deleteDef.setDirectory(basedir); |
| 306 | + |
| 307 | + queryMgr.delete(deleteDef); |
| 308 | + |
| 309 | + client.release(); |
| 310 | + } |
| 311 | + |
201 | 312 | @Test |
202 | 313 | public void testHandleRegistry() { |
203 | 314 | int[] iterations = {1,2}; |
204 | 315 | for (int i: iterations) { |
205 | | - DatabaseClientFactory.Bean clientFactoryBean = null; |
206 | | - if (i == 2) { |
207 | | - clientFactoryBean = new DatabaseClientFactory.Bean(); |
208 | | - clientFactoryBean.setHost(Common.HOST); |
209 | | - clientFactoryBean.setPort(Common.PORT); |
210 | | - clientFactoryBean.setUser(Common.USERNAME); |
211 | | - clientFactoryBean.setPassword(Common.PASSWORD); |
212 | | - clientFactoryBean.setAuthentication(Authentication.DIGEST); |
213 | | - } |
| 316 | + DatabaseClientFactory.Bean clientFactoryBean = (i == 1) ? null : makeClientFactory(); |
214 | 317 |
|
215 | 318 | HandleFactoryRegistry registry = |
216 | 319 | (i == 1) ? DatabaseClientFactory.getHandleRegistry() |
@@ -268,6 +371,16 @@ public void testHandleRegistry() { |
268 | 371 | } |
269 | 372 | } |
270 | 373 |
|
| 374 | + private DatabaseClientFactory.Bean makeClientFactory() { |
| 375 | + DatabaseClientFactory.Bean clientFactoryBean = new DatabaseClientFactory.Bean(); |
| 376 | + clientFactoryBean.setHost(Common.HOST); |
| 377 | + clientFactoryBean.setPort(Common.PORT); |
| 378 | + clientFactoryBean.setUser(Common.USERNAME); |
| 379 | + clientFactoryBean.setPassword(Common.PASSWORD); |
| 380 | + clientFactoryBean.setAuthentication(Authentication.DIGEST); |
| 381 | + return clientFactoryBean; |
| 382 | + } |
| 383 | + |
271 | 384 | static public class BufferHandle |
272 | 385 | extends BaseHandle<String, String> |
273 | 386 | implements ContentHandle<StringBuilder> { |
@@ -317,4 +430,32 @@ public <C> ContentHandle<C> newHandle(Class<C> type) { |
317 | 430 | return handle; |
318 | 431 | } |
319 | 432 | } |
| 433 | + |
| 434 | + @XmlRootElement |
| 435 | + static public class Product { |
| 436 | + private String name; |
| 437 | + private String industry; |
| 438 | + private String description; |
| 439 | + public Product() { |
| 440 | + super(); |
| 441 | + } |
| 442 | + public String getName() { |
| 443 | + return name; |
| 444 | + } |
| 445 | + public void setName(String name) { |
| 446 | + this.name = name; |
| 447 | + } |
| 448 | + public String getIndustry() { |
| 449 | + return industry; |
| 450 | + } |
| 451 | + public void setIndustry(String industry) { |
| 452 | + this.industry = industry; |
| 453 | + } |
| 454 | + public String getDescription() { |
| 455 | + return description; |
| 456 | + } |
| 457 | + public void setDescription(String description) { |
| 458 | + this.description = description; |
| 459 | + } |
| 460 | + } |
320 | 461 | } |
0 commit comments