@Test public void testXsltNoParameters() { IResource xml = resourceLocator.getFirst("/xml/custom-parameters-not-required.xml"); IResource xslt = resourceLocator.getFirst("/xsl/custom-xslt.xsl"); processingAttributes.put(AttributeUtils.getKey(XmlDocumentSource.class, "xml"), xml); processingAttributes.put(AttributeUtils.getKey(XmlDocumentSource.class, "xslt"), xslt); final int documentCount = runQuery(); assertTransformedDocumentsEqual(documentCount); }
@Test public void testLegacyXml() { IResource xml = resourceLocator.getFirst("/xml/carrot2-apple-computer.xml"); processingAttributes.put(AttributeUtils.getKey(XmlDocumentSource.class, "xml"), xml); processingAttributes.put(AttributeUtils.getKey(XmlDocumentSource.class, "results"), 300); final int documentCount = runQuery(); assertEquals(200, documentCount); assertEquals("apple computer", resultAttributes.get(AttributeNames.QUERY)); }
@Test public void testResultsTruncation() { IResource xml = resourceLocator.getFirst("/xml/carrot2-apple-computer.xml"); processingAttributes.put(AttributeUtils.getKey(XmlDocumentSource.class, "xml"), xml); processingAttributes.put(AttributeNames.RESULTS, 50); processingAttributes.put(AttributeUtils.getKey(XmlDocumentSource.class, "readAll"), false); final int documentCount = runQuery(); assertEquals(50, documentCount); assertEquals("apple computer", resultAttributes.get(AttributeNames.QUERY)); }
@SuppressWarnings("unchecked") @Test public void testReadClusters() { IResource xml = resourceLocator.getFirst("/xml/carrot2-with-clusters.xml"); processingAttributes.put(AttributeUtils.getKey(XmlDocumentSource.class, "xml"), xml); processingAttributes.put(AttributeUtils.getKey(XmlDocumentSource.class, "readClusters"), true); runQuery(); List<Cluster> clusters = (List<Cluster>) resultAttributes.get(AttributeNames.CLUSTERS); assertThat(clusters).isNotEmpty(); }
@Test public void testNoIdsInSourceXml() { IResource xml = resourceLocator.getFirst("/xml/carrot2-no-ids.xml"); processingAttributes.put(AttributeUtils.getKey(XmlDocumentSource.class, "xml"), xml); final int documentCount = runQuery(); assertEquals(2, documentCount); assertEquals(Lists.newArrayList(0, 1), Lists.transform(getDocuments(), DOCUMENT_TO_INT_ID)); }
@Test public void testXsltWithParameters() { IResource xml = resourceLocator.getFirst("/xml/custom-parameters-required.xml"); IResource xslt = resourceLocator.getFirst("/xsl/custom-xslt.xsl"); Map<String, String> xsltParameters = Maps.newHashMap(); xsltParameters.put("id-field", "number"); xsltParameters.put("title-field", "heading"); xsltParameters.put("snippet-field", "snippet"); xsltParameters.put("url-field", "url"); processingAttributes.put(AttributeUtils.getKey(XmlDocumentSource.class, "xml"), xml); processingAttributes.put(AttributeUtils.getKey(XmlDocumentSource.class, "xslt"), xslt); processingAttributes.put( AttributeUtils.getKey(XmlDocumentSource.class, "xsltParameters"), xsltParameters); final int documentCount = runQuery(); assertTransformedDocumentsEqual(documentCount); }
@Test public void testSpeculativeMode() throws Exception { assumeTrue(externalApiTestsEnabled()); processingAttributes.put("search-mode", SearchMode.SPECULATIVE); runAndCheckNoResultsQuery(getSearchEngineMetadata().resultsPerPage + 1); assertEquals( 2, resultAttributes.get(AttributeUtils.getKey(SearchEngineStats.class, "pageRequests"))); }
@Test public void testConservativeMode() throws Exception { assumeTrue(externalApiTestsEnabled()); processingAttributes.put("search-mode", SearchMode.CONSERVATIVE); runAndCheckNoResultsQuery(); assertEquals( 1, resultAttributes.get(AttributeUtils.getKey(SearchEngineStats.class, "pageRequests"))); }
@Test public void testDuplicatedIdsInSourceXml() { IResource xml = resourceLocator.getFirst("/xml/carrot2-duplicated-ids.xml"); processingAttributes.put(AttributeUtils.getKey(XmlDocumentSource.class, "xml"), xml); try { runQuery(); fail(); } catch (ProcessingException e) { assertThat(e.getMessage()).contains("Identifiers must be unique"); } }
@Test public void testGappedIdsInSourceXml() { IResource xml = resourceLocator.getFirst("/xml/carrot2-gapped-ids.xml"); processingAttributes.put(AttributeUtils.getKey(XmlDocumentSource.class, "xml"), xml); try { runQuery(); fail(); } catch (ProcessingException e) { assertThat(e.getMessage()).contains("Null identifiers cannot be mixed with"); } }
@Test public void testOverridingInitializationTimeXslt() { IResource initXslt = resourceLocator.getFirst("/xsl/carrot2-identity.xsl"); initAttributes.put(AttributeUtils.getKey(XmlDocumentSource.class, "xslt"), initXslt); @SuppressWarnings("unchecked") Controller controller = getCachingController(initAttributes); // Run with identity XSLT { IResource xml = resourceLocator.getFirst("/xml/carrot2-test.xml"); processingAttributes.put(AttributeUtils.getKey(XmlDocumentSource.class, "xml"), xml); final int documentCount = runQuery(controller); assertEquals(2, documentCount); assertEquals( Lists.newArrayList("Title 0", "Title 1"), Lists.transform(getDocuments(), DOCUMENT_TO_TITLE)); assertEquals( Lists.newArrayList("Snippet 0", "Snippet 1"), Lists.transform(getDocuments(), DOCUMENT_TO_SUMMARY)); } // Run with swapping XSLT { IResource xml = resourceLocator.getFirst("/xml/carrot2-test.xml"); IResource xslt = resourceLocator.getFirst("/xsl/carrot2-title-snippet-switch.xsl"); processingAttributes.put(AttributeUtils.getKey(XmlDocumentSource.class, "xml"), xml); processingAttributes.put(AttributeUtils.getKey(XmlDocumentSource.class, "xslt"), xslt); final int documentCount = runQuery(controller); assertEquals(2, documentCount); assertEquals( Lists.newArrayList("Snippet 0", "Snippet 1"), Lists.transform(getDocuments(), DOCUMENT_TO_TITLE)); assertEquals( Lists.newArrayList("Title 0", "Title 1"), Lists.transform(getDocuments(), DOCUMENT_TO_SUMMARY)); } }
@Test public void testRemoteUrl() throws MalformedURLException { String base = System.getProperty("carrot2.xml.feed.url.base"); assumeTrue("carrot2.xml.feed.url.base property undefined.", !Strings.isNullOrEmpty(base)); IResource xml = new URLResourceWithParams(new URL(base + "&q=${query}&results=${results}")); final String query = "apple computer"; processingAttributes.put(AttributeUtils.getKey(XmlDocumentSource.class, "xml"), xml); processingAttributes.put(AttributeNames.QUERY, query); processingAttributes.put(AttributeNames.RESULTS, 50); final int documentCount = runQuery(); assertEquals(50, documentCount); assertEquals(query, resultAttributes.get(AttributeNames.QUERY)); }
@SuppressWarnings("unchecked") @Override public void init(IControllerContext context) { super.init(context); this.context = context; synchronized (context) { final String key = AttributeUtils.getKey(getClass(), "openIndexes"); if (context.getAttribute(key) == null) { context.setAttribute(key, Maps.newIdentityHashMap()); context.addListener( new IControllerContextListener() { public void beforeDisposal(IControllerContext context) { closeAllIndexes(); } }); } this.openIndexes = (IdentityHashMap<Directory, IndexSearcher>) context.getAttribute(key); } }