package com.xpn.xwiki.plugin.lucene; import com.xpn.xwiki.XWikiException; import com.xpn.xwiki.api.XWiki; import com.xpn.xwiki.doc.XWikiAttachment; import com.xpn.xwiki.doc.XWikiDocument; import com.xpn.xwiki.objects.BaseObject; import com.xpn.xwiki.objects.classes.BaseClass; import com.xpn.xwiki.test.HibernateTestCase; import com.xpn.xwiki.web.XWikiServletURLFactory; import org.apache.commons.io.IOUtils; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.index.Term; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.queryParser.ParseException; import org.apache.lucene.queryParser.QueryParser; import org.apache.lucene.search.*; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.net.URL; import java.util.*; /** * Created by IntelliJ IDEA. * User: ravenees * Date: Jan 10, 2007 * Time: 4:14:45 PM * To change this template use File | Settings | File Templates. */ public class LucenePluginTest extends HibernateTestCase { String author = "XWiki.LudovicDubost"; String indexDir; protected void setUp() throws Exception { super.setUp(); getXWikiContext().setURLFactory(new XWikiServletURLFactory(new URL("http://www.xwiki.org/"), "xwiki/", "bin/")); // Setting the configuration properties to the XWiki Config getXWikiConfig().setProperty("xwiki.plugins.lucene.tempfiledir", "C:\\TempFileDir\\"); getXWikiConfig().setProperty("xwiki.plugins.lucene.indexdir", "C:\\luceneindex\\"); getXWikiConfig().setProperty("xwiki.plugins.lucene.analyzer", "org.apache.lucene.analysis.standard.StandardAnalyzer"); getXWikiConfig().setProperty("xwiki.plugins.lucene.indexinterval", "2"); indexDir = getXWikiConfig().getProperty("xwiki.plugins.lucene.indexdir"); XWikiDocument doc = xwiki.getDocument("XWiki.ArticleClass", context); doc.setContent("Here is some text"); BaseClass bclass = doc.getxWikiClass(); bclass.addTextField("title", "Title", 30); bclass.addTextAreaField("content", "Content", 80, 10); bclass.addTextAreaField("extract", "Extract", 80, 5); bclass.addDateField("date", "CreationDate", "yyyy-mm-dd"); bclass.addStaticListField("category", "Category", 5, true, "categoryone|categorytwo"); bclass.addStaticListField("complexlist", "Complex List", 5, true, "1=valueone|2=valuetwo"); getXWiki().saveDocument(doc, context); } public void sleep(LucenePlugin lpa) throws InterruptedException { int loop = 0; while ((lpa.getQueueSize() > 0) || (loop > 30)) { Thread.sleep(1000); } } /** * @param query * @param virtualWikiNames comma separated list of virtual wiki names * @param languages comma separated list of language codes to search in, may be * null to search all languages * @throws org.apache.lucene.queryParser.ParseException * */ public Query buildQuery(String query, String virtualWikiNames, String languages) throws ParseException { // build a query like this: AND AND // BooleanQuery bQuery = new BooleanQuery(); QueryParser qParser = new QueryParser(IndexFields.FULLTEXT, new StandardAnalyzer()); qParser.setDefaultOperator(QueryParser.Operator.AND); Query parsedQuery = qParser.parse(query); bQuery.add(parsedQuery, BooleanClause.Occur.MUST); if (virtualWikiNames != null && virtualWikiNames.length() > 0) { bQuery.add(buildOredTermQuery(virtualWikiNames, IndexFields.DOCUMENT_WIKI), BooleanClause.Occur.MUST); } if (languages != null && languages.length() > 0) { bQuery.add(buildOredTermQuery(languages, IndexFields.DOCUMENT_LANGUAGE), BooleanClause.Occur.MUST); } return bQuery; } /** * @param values comma separated list of values to look for * @return A query returning documents matching one of the given values in * the given field */ public Query buildOredTermQuery(final String values, final String fieldname) { String[] valueArray = values.split("\\,"); if (valueArray.length > 1) { // build a query like this: OR OR ... BooleanQuery orQuery = new BooleanQuery(); for (int i = 0; i < valueArray.length; i++) { orQuery.add(new TermQuery(new Term(fieldname, valueArray[i].trim())), BooleanClause.Occur.SHOULD); } return orQuery; } // exactly one value, no OR'ed Terms necessary return new TermQuery(new Term(fieldname, valueArray[0])); } /** * This is to add documents to the context * * @param noofdocuments (Number of documents to create and save in ontext) * @param addAttachements (Need to attache the file for the created documents) * @throws XWikiException */ private void addDocuments(int noofdocuments, boolean addAttachements) throws XWikiException { XWikiDocument doc; for (int i = 0; i < noofdocuments; i++) { doc = new XWikiDocument("Main", "TestLuceneDocument" + i); doc.setContent(" Test lucene document only " + i); doc.setAuthor(author); //doc.setDate(new Date(DateUtility.getNextDate(i + 1))); if (addAttachements) { addAttachments(doc, "attachFile" + i + ".txt", "Content for the attachment file " + i); } xwiki.saveDocument(doc, context); } } /** * This is to add given filename with the given content as an attachment file to the document passed on. * * @param doc * @param fileName * @param content * @throws XWikiException */ private void addAttachments(XWikiDocument doc, String fileName, String content) throws XWikiException { XWikiAttachment attachment = new XWikiAttachment(doc, fileName); attachment.setContent(content.getBytes()); attachment.setDate(new Date()); doc.saveAttachmentContent(attachment, context); doc.getAttachmentList().add(attachment); } public void testMimeTypeFiles() throws XWikiException, IOException, InterruptedException { String dirName = getXWikiConfig().getProperty("xwiki.plugins.lucene.tempfiledir"); XWikiDocument doc = new XWikiDocument("Test", "TestMimeType"); doc.setContent("The text extraction sample"); doc.setAuthor(author); File inputFile1 = new File(dirName + "testxls.xls"); FileInputStream fis1 = new FileInputStream(inputFile1); byte[] excelContent = IOUtils.toByteArray(fis1); String text = TextExtractor.getText(excelContent, "application/ms-excel"); assertNotNull("The text should not be null", text); XWikiAttachment attachment1 = new XWikiAttachment(doc, inputFile1.getName()); attachment1.setContent(excelContent); attachment1.setDate(new Date()); doc.saveAttachmentContent(attachment1, context); doc.getAttachmentList().add(attachment1); fis1.close(); File inputFile2 = new File(dirName + "testdoc.doc"); assertNotNull(inputFile2); FileInputStream fis2 = new FileInputStream(inputFile2); byte[] wordContent = IOUtils.toByteArray(fis2); text = TextExtractor.getText(wordContent, "application/msword"); assertNotNull("The text should not be null", text); XWikiAttachment attachment2 = new XWikiAttachment(doc, inputFile2.getName()); attachment2.setContent(wordContent); attachment2.setDate(new Date()); doc.saveAttachmentContent(attachment2, context); doc.getAttachmentList().add(attachment2); fis2.close(); File inputFile3 = new File(dirName + "testppt.ppt"); FileInputStream fis3 = new FileInputStream(inputFile3); byte[] pptContent = IOUtils.toByteArray(fis3); text = TextExtractor.getText(pptContent, "application/ms-powerpoint"); assertNotNull("The text should not be null", text); XWikiAttachment attachment3 = new XWikiAttachment(doc, inputFile3.getName()); attachment3.setContent(pptContent); attachment3.setDate(new Date()); doc.saveAttachmentContent(attachment3, context); doc.getAttachmentList().add(attachment3); fis3.close(); File inputFile4 = new File(dirName + "testpdf.pdf"); FileInputStream fis4 = new FileInputStream(inputFile4); byte[] pdfContent = IOUtils.toByteArray(fis4); text = TextExtractor.getText(pdfContent, "application/pdf"); assertNotNull("The text should not be null", text); XWikiAttachment attachment4 = new XWikiAttachment(doc, inputFile4.getName()); attachment4.setContent(pdfContent); attachment4.setDate(new Date()); doc.saveAttachmentContent(attachment4, context); doc.getAttachmentList().add(attachment4); fis4.close(); xwiki.saveDocument(doc, context); getXWiki().getPluginManager().addPlugin("lucene", "com.xpn.xwiki.plugin.lucene.LucenePlugin", getXWikiContext()); LucenePlugin lpa = (LucenePlugin) xwiki.getPlugin("lucene", context); assertNotNull("The lucene plugin should not be null ", lpa); XWiki xwikiApi = new XWiki(getXWikiContext().getWiki(), context); int count = lpa.rebuildIndex(xwikiApi, context); LucenePluginApi lpApi = (LucenePluginApi) lpa.getPluginApi(lpa, context); //sleep(lpa); int loop = 0; while ((lpa.getQueueSize() > 0) || (loop > 30)) { Thread.sleep(2000); } String theQuery = "Free your knowledge"; SearchResults results = lpApi.getSearchResults(theQuery, "xwiki", "default,en,de", new XWiki(context.getWiki(), getXWikiContext())); assertNotNull("Search results should have not be null", results); assertEquals("The expected should match the actual", 4, results.getHitcount()); List searchResults = results.getResults(); SearchResult searchRes = (SearchResult) searchResults.get(0); assertEquals("The filename should equals the attachment", "testxls.xls", searchRes.getFilename()); assertEquals("The URL should equals the expected", "/xwiki/bin/download/Test/TestMimeType/testxls.xls", searchRes.getUrl()); assertEquals("The doc name should equals the expected", "TestMimeType", searchRes.getName()); assertEquals("The default language should equals the expected", "default", searchRes.getLanguage()); searchRes = (SearchResult) searchResults.get(1); assertEquals("The filename should equals the attachment", "testdoc.doc", searchRes.getFilename()); assertEquals("The URL should equals the expected", "/xwiki/bin/download/Test/TestMimeType/testdoc.doc", searchRes.getUrl()); assertEquals("The doc name should equals the expected", "TestMimeType", searchRes.getName()); assertEquals("The default language should equals the expected", "default", searchRes.getLanguage()); searchRes = (SearchResult) searchResults.get(2); assertEquals("The filename should equals the attachment", "testppt.ppt", searchRes.getFilename()); assertEquals("The URL should equals the expected", "/xwiki/bin/download/Test/TestMimeType/testppt.ppt", searchRes.getUrl()); assertEquals("The doc name should equals the expected", "TestMimeType", searchRes.getName()); assertEquals("The default language should equals the expected", "default", searchRes.getLanguage()); searchRes = (SearchResult) searchResults.get(3); assertEquals("The filename should equals the attachment", "testpdf.pdf", searchRes.getFilename()); assertEquals("The URL should equals the expected", "/xwiki/bin/download/Test/TestMimeType/testpdf.pdf", searchRes.getUrl()); assertEquals("The doc name should equals the expected", "TestMimeType", searchRes.getName()); assertEquals("The default language should equals the expected", "default", searchRes.getLanguage()); theQuery = "knowledge"; results = lpApi.getSearchResults(theQuery, "xwiki", "default,en,de", new XWiki(context.getWiki(), getXWikiContext())); assertNotNull("Search results should have not be null", results); assertEquals("The expected should match the actual", 4, results.getHitcount()); theQuery = "Free"; results = lpApi.getSearchResults(theQuery, "xwiki", "default,en,de", new XWiki(context.getWiki(), getXWikiContext())); assertNotNull("Search results should have not be null", results); assertEquals("The expected should match the actual", 4, results.getHitcount()); theQuery = "your"; results = lpApi.getSearchResults(theQuery, "xwiki", "default,en,de", new XWiki(context.getWiki(), getXWikiContext())); assertNotNull("Search results should have not be null", results); assertEquals("The expected should match the actual", 4, results.getHitcount()); theQuery = "text not indexed"; results = lpApi.getSearchResults(theQuery, "xwiki", "default,en,de", new XWiki(context.getWiki(), getXWikiContext())); assertNotNull("Search results should have not be null", results); assertEquals("The expected should match the actual", 3, results.getHitcount()); theQuery = "String not indexed"; results = lpApi.getSearchResults(theQuery, "xwiki", "default,en,de", new XWiki(context.getWiki(), getXWikiContext())); assertNotNull("Search results should have not be null", results); assertEquals("The expected should match the actual", 1, results.getHitcount()); theQuery = "The Big Faceless PDF Library"; results = lpApi.getSearchResults(theQuery, "xwiki", "default,en,de", new XWiki(context.getWiki(), getXWikiContext())); assertNotNull("Search results should have not be null", results); assertEquals("The expected should match the actual", 1, results.getHitcount()); theQuery = "125464564564564645456456sdfsdfsdfsdfsdfs4564454s6dfsd4f56sdfsd"; results = lpApi.getSearchResults(theQuery, "xwiki", "default,en,de", new XWiki(context.getWiki(), getXWikiContext())); assertNotNull("Search results should have not be null", results); assertEquals("The expected should match the actual", 0, results.getHitcount()); //for the partial word search theQuery = "Translu"; results = lpApi.getSearchResults(theQuery, "xwiki", "default,en,de", new XWiki(context.getWiki(), getXWikiContext())); assertNotNull("Search results should have not be null", results); assertEquals("The expected should match the actual", 0, results.getHitcount()); theQuery = "Translu*"; results = lpApi.getSearchResults(theQuery, "xwiki", "default,en,de", new XWiki(context.getWiki(), getXWikiContext())); assertNotNull("Search results should have not be null", results); assertEquals("The expected should match the actual", 1, results.getHitcount()); searchResults = results.getResults(); searchRes = (SearchResult) searchResults.get(0); assertEquals("The filename should equals the attachment", "testpdf.pdf", searchRes.getFilename()); assertEquals("The URL should equals the expected", "/xwiki/bin/download/Test/TestMimeType/testpdf.pdf", searchRes.getUrl()); assertEquals("The doc name should equals the expected", "TestMimeType", searchRes.getName()); assertEquals("The default language should equals the expected", "default", searchRes.getLanguage()); theQuery = "knowle*"; results = lpApi.getSearchResults(theQuery, "xwiki", "default,en,de", new XWiki(context.getWiki(), getXWikiContext())); assertNotNull("Search results should have not be null", results); assertEquals("The expected should match the actual", 4, results.getHitcount()); searchResults = results.getResults(); searchRes = (SearchResult) searchResults.get(0); assertEquals("The filename should equals the attachment", "testxls.xls", searchRes.getFilename()); assertEquals("The URL should equals the expected", "/xwiki/bin/download/Test/TestMimeType/testxls.xls", searchRes.getUrl()); assertEquals("The doc name should equals the expected", "TestMimeType", searchRes.getName()); assertEquals("The default language should equals the expected", "default", searchRes.getLanguage()); searchRes = (SearchResult) searchResults.get(1); assertEquals("The filename should equals the attachment", "testdoc.doc", searchRes.getFilename()); assertEquals("The URL should equals the expected", "/xwiki/bin/download/Test/TestMimeType/testdoc.doc", searchRes.getUrl()); assertEquals("The doc name should equals the expected", "TestMimeType", searchRes.getName()); assertEquals("The default language should equals the expected", "default", searchRes.getLanguage()); searchRes = (SearchResult) searchResults.get(2); assertEquals("The filename should equals the attachment", "testppt.ppt", searchRes.getFilename()); assertEquals("The URL should equals the expected", "/xwiki/bin/download/Test/TestMimeType/testppt.ppt", searchRes.getUrl()); assertEquals("The doc name should equals the expected", "TestMimeType", searchRes.getName()); assertEquals("The default language should equals the expected", "default", searchRes.getLanguage()); searchRes = (SearchResult) searchResults.get(3); assertEquals("The filename should equals the attachment", "testpdf.pdf", searchRes.getFilename()); assertEquals("The URL should equals the expected", "/xwiki/bin/download/Test/TestMimeType/testpdf.pdf", searchRes.getUrl()); assertEquals("The doc name should equals the expected", "TestMimeType", searchRes.getName()); assertEquals("The default language should equals the expected", "default", searchRes.getLanguage()); theQuery = "Internati*"; results = lpApi.getSearchResults(theQuery, "xwiki", "default,en,de", new XWiki(context.getWiki(), getXWikiContext())); assertNotNull("Search results should have not be null", results); assertEquals("The expected should match the actual", 1, results.getHitcount()); theQuery = "you*"; results = lpApi.getSearchResults(theQuery, "xwiki", "default,en,de", new XWiki(context.getWiki(), getXWikiContext())); assertNotNull("Search results should have not be null", results); assertEquals("The expected should match the actual", 4, results.getHitcount()); theQuery = "f*"; results = lpApi.getSearchResults(theQuery, "xwiki", "default,en,de", new XWiki(context.getWiki(), getXWikiContext())); assertNotNull("Search results should have not be null", results); assertEquals("The expected should match the actual", 4, results.getHitcount()); theQuery = "123-1234-12345*"; results = lpApi.getSearchResults(theQuery, "xwiki", "default,en,de", new XWiki(context.getWiki(), getXWikiContext())); assertNotNull("Search results should have not be null", results); assertEquals("The expected should match the actual", 0, results.getHitcount()); } /** * Testing Lucene indexing of the documents along with attachments * * @throws XWikiException */ public void testDocumentWithAttachmentsIndexing() throws XWikiException, InterruptedException { XWikiDocument document; int expectedCount = 6; Collection docNames; addDocuments(expectedCount, true); getXWiki().getPluginManager().addPlugin("lucene", "com.xpn.xwiki.plugin.lucene.LucenePlugin", getXWikiContext()); LucenePlugin lpa = (LucenePlugin) xwiki.getPlugin("lucene", context); assertNotNull("The Lucene plugin reference is :", lpa); com.xpn.xwiki.XWiki xwiki = context.getWiki(); XWiki xwikiApi = new XWiki(getXWikiContext().getWiki(), context); docNames = xwiki.getStore().searchDocuments("", context); for (Iterator iterator = docNames.iterator(); iterator.hasNext();) { document = (XWikiDocument) iterator.next(); expectedCount += document.getAttachmentList().size(); } int result = lpa.rebuildIndex(xwikiApi, context); Thread.sleep(8000); //sleep(lpa); assertEquals(expectedCount + 1, result); } /** * Testing Lucene indexing of the documents only * * @throws XWikiException */ public void testDocumentIndexingWithXWikiContext() throws XWikiException, InterruptedException { getXWiki().getPluginManager().addPlugin("lucene", "com.xpn.xwiki.plugin.lucene.LucenePlugin", getXWikiContext()); addDocuments(6, false); LucenePlugin lpa = (LucenePlugin) xwiki.getPlugin("lucene", context); com.xpn.xwiki.XWiki xwiki = context.getWiki(); XWiki xwikiApi = new XWiki(getXWikiContext().getWiki(), context); LucenePluginApi lpApi = (LucenePluginApi) lpa.getPluginApi(lpa, context); assertNotNull("The Lucene plugin reference is :", lpa); int count = lpa.rebuildIndex(xwikiApi, context); int loop = 0; while ((lpa.getQueueSize() > 0) || (loop > 30)) { Thread.sleep(1000); } //sleep(lpa); assertEquals("The expected count is :", 7, count); String theQuery = IndexFields.DOCUMENT_DATE + ":" + DateUtility.getCurrentDate(); SearchResults results = lpApi.getSearchResults(theQuery, "xwiki", "default,en,de", new XWiki(context.getWiki(), getXWikiContext())); assertNotNull("Search results shoould have not returned null", results); assertEquals("(Objetct Search)There search should match exactly the expected value(One)", 7, results.getResults().size()); theQuery = IndexFields.DOCUMENT_DATE + ":[" + DateUtility.getCurrentDate() + " TO " + DateUtility.getCurrentDate() + "]"; results = lpApi.getSearchResults(theQuery, "xwiki", "default,en,de", new XWiki(context.getWiki(), getXWikiContext())); assertNotNull("Search results shoould have not returned null", results); assertEquals("(Objetct Search)There search should match exactly the expected value(Two)", 7, results.getResults().size()); theQuery = IndexFields.DOCUMENT_DATE + ":{" + DateUtility.getCurrentDate() + " TO " + DateUtility.getCurrentDate() + "}"; results = lpApi.getSearchResults(theQuery, "xwiki", "default,en,de", new XWiki(context.getWiki(), getXWikiContext())); assertNotNull("Search results shoould have not returned null", results); assertEquals("(Objetct Search)There search should match exactly the expected value(Three)", 0, results.getResults().size()); theQuery = IndexFields.DOCUMENT_DATE + ":{" + DateUtility.getCurrentDate() + " TO " + DateUtility.getNextDate(4) + "}"; results = lpApi.getSearchResults(theQuery, "xwiki", "default,en,de", new XWiki(context.getWiki(), getXWikiContext())); assertNotNull("Search results shoould have not returned null", results); assertEquals("(Objetct Search)There search should match exactly the expected value(Four)", 0, results.getResults().size()); theQuery = IndexFields.DOCUMENT_DATE + ":{" + DateUtility.getPreviousDate(4) + " TO " + DateUtility.getCurrentDate() + "}"; results = lpApi.getSearchResults(theQuery, "xwiki", "default,en,de", new XWiki(context.getWiki(), getXWikiContext())); assertNotNull("Search results shoould have not returned null", results); assertEquals("(Objetct Search)There search should match exactly the expected value(Five)", 0, results.getResults().size()); theQuery = IndexFields.DOCUMENT_DATE + ":{" + DateUtility.getPreviousDate() + " TO " + DateUtility.getNextDate() + "}"; results = lpApi.getSearchResults(theQuery, "xwiki", "default,en,de", new XWiki(context.getWiki(), getXWikiContext())); assertNotNull("Search results shoould have not returned null", results); assertEquals("(Objetct Search)There search should match exactly the expected value(Six)", 7, results.getResults().size()); theQuery = IndexFields.DOCUMENT_DATE + ":[" + DateUtility.getPreviousDate(5) + " TO " + DateUtility.getCurrentDate() + "]"; results = lpApi.getSearchResults(theQuery, "xwiki", "default,en,de", new XWiki(context.getWiki(), getXWikiContext())); assertNotNull("Search results shoould have not returned null", results); assertEquals("(Objetct Search)There search should match exactly the expected value(Seven)", 7, results.getResults().size()); theQuery = IndexFields.DOCUMENT_DATE + ":[" + DateUtility.getCurrentDate() + " TO " + DateUtility.getNextDate(5) + "]"; results = lpApi.getSearchResults(theQuery, "xwiki", "default,en,de", new XWiki(context.getWiki(), getXWikiContext())); assertNotNull("Search results shoould have not returned null", results); assertEquals("(Objetct Search)There search should match exactly the expected value(Eight)", 7, results.getResults().size()); theQuery = IndexFields.DOCUMENT_DATE + ":[" + DateUtility.getPreviousDate() + " TO " + DateUtility.getNextDate() + "]"; results = lpApi.getSearchResults(theQuery, "xwiki", "default,en,de", new XWiki(context.getWiki(), getXWikiContext())); assertNotNull("Search results shoould have not returned null", results); assertEquals("(Objetct Search)There search should match exactly the expected value(Nine)", 7, results.getResults().size()); } public void testXWikiObjectsOfDocument() throws XWikiException, InterruptedException { XWikiDocument doc; // Adding the lucene plugin getXWiki().getPluginManager().addPlugin("lucene", "com.xpn.xwiki.plugin.lucene.LucenePlugin", getXWikiContext()); //adding document to the index doc = xwiki.getDocument("Blog.Article1", context); //doc.setDate(new Date("2006-11-07")); BaseObject baseobj = doc.getObject("XWiki.ArticleClass", true, context); baseobj.set("title", "titleone", context); baseobj.set("content", " test de contentone, just for trying", context); List categoryList = new ArrayList(); categoryList.add("categoryone"); categoryList.add("categorytwo"); baseobj.set("category", categoryList, context); List complexList = new ArrayList(); complexList.add("1"); baseobj.set("date", doc.getDate(), context); baseobj.set("complexlist", complexList, context); baseobj.set("extract", "blogarticleone", context); xwiki.saveDocument(doc, context); doc = xwiki.getDocument("Blog.Article2", context); baseobj = doc.getObject("XWiki.ArticleClass", true, context); baseobj.set("title", "titletwo", context); baseobj.set("content", "categorytwo", context); categoryList = new ArrayList(); categoryList.add("categoryone"); baseobj.set("category", categoryList, context); baseobj.set("date", doc.getDate(), context); baseobj.set("extract", "blogarticletwo", context); xwiki.saveDocument(doc, context); xwiki.flushCache(); Collection docNames = null; LucenePlugin lpa = (LucenePlugin) xwiki.getPlugin("lucene", context); com.xpn.xwiki.XWiki xwiki = context.getWiki(); XWiki xwikiApi = new XWiki(getXWikiContext().getWiki(), context); int count = lpa.rebuildIndex(xwikiApi, context); assertTrue("There should be at least 2 documents to index", (count >= 2)); docNames = xwiki.getStore().searchDocuments("", context); assertEquals(3, docNames.size()); Object[] docNameArray = docNames.toArray(); String docName1 = ((XWikiDocument) docNameArray[0]).getFullName(); String docName2 = ((XWikiDocument) docNameArray[1]).getFullName(); String docName3 = ((XWikiDocument) docNameArray[2]).getFullName(); assertEquals("XWiki.ArticleClass", docName1); assertEquals("Blog.Article1", docName2); assertEquals("Blog.Article2", docName3); LucenePluginApi lpApi = (LucenePluginApi) lpa.getPluginApi(lpa, context); int loop = 0; while ((lpa.getQueueSize() > 0) || (loop > 30)) { Thread.sleep(1000); } //sleep(lpa); assertNotNull("The plugin API should not be null :", lpApi); String theQuery = "XWiki.ArticleClass.category:sdgdfgdfsgdfgd"; SearchResults results = lpApi.getSearchResults(theQuery, "xwiki", "default,en,de", new XWiki(context.getWiki(), getXWikiContext())); assertNotNull("Search results shoould have not returned null", results); assertEquals("(Objetct Search)There search should match exactly the expected value(garbage value)", 0, results.getResults().size()); theQuery = "XWiki.ArticleClass.title:titleone"; results = lpApi.getSearchResults(theQuery, "xwiki", "default,en,de", new XWiki(context.getWiki(), getXWikiContext())); assertNotNull("Search results shoould have not returned null", results); assertEquals("(Objetct Search)The search result should be one (Title)", 1, results.getResults().size()); SearchResult res = (SearchResult) results.getResults().get(0); assertEquals("Wrong document", "Blog.Article1", res.getWeb() + "." + res.getName()); theQuery = "XWiki.ArticleClass.content:contentone"; results = lpApi.getSearchResults(theQuery, "xwiki", "default,en,de", new XWiki(context.getWiki(), getXWikiContext())); assertNotNull("Search results shoould have not returned null", results); assertTrue("(Objetct Search)There search should match exactly the expected value(Content)", results.getResults().size() == 1); theQuery = "XWiki.ArticleClass.category:categoryone"; results = lpApi.getSearchResults(theQuery, "xwiki", "default,en,de", new XWiki(context.getWiki(), getXWikiContext())); assertNotNull("Search results shoould have not returned null", results); assertTrue("(Objetct Search)There search should match exactly the expected value(Category)", results.getResults().size() == 2); theQuery = "XWiki.ArticleClass.category:categorytwo"; results = lpApi.getSearchResults(theQuery, "xwiki", "default,en,de", new XWiki(context.getWiki(), getXWikiContext())); assertNotNull("Search results shoould have not returned null", results); assertTrue("(Objetct Search)There search should match exactly the expected value(Category)", results.getResults().size() == 1); theQuery = "XWiki.ArticleClass.complexlist.value:valueone";//MATCH results = lpApi.getSearchResults(theQuery, "xwiki", "default,en,de", new XWiki(context.getWiki(), getXWikiContext())); assertNotNull("Search results shoould have not returned null", results); assertEquals("(Objetct Search)There search should match exactly the expected value(ComplexList)", 1, results.getResults().size()); theQuery = "XWiki.ArticleClass.complexlist.value:1";//NOT MATCH results = lpApi.getSearchResults(theQuery, "xwiki", "default,en,de", new XWiki(context.getWiki(), getXWikiContext())); assertNotNull("Search results shoould have not returned null", results); assertEquals("(Objetct Search)There search should match exactly the expected value(ComplexList)", 0, results.getResults().size()); theQuery = "XWiki.ArticleClass.complexlist.key:1"; //MATCH results = lpApi.getSearchResults(theQuery, "xwiki", "default,en,de", new XWiki(context.getWiki(), getXWikiContext())); assertNotNull("Search results shoould have not returned null", results); assertEquals("(Objetct Search)There search should match exactly the expected value(ComplexList)", 1, results.getResults().size()); theQuery = "XWiki.ArticleClass.complexlist.key:value1"; //NOT MATCH results = lpApi.getSearchResults(theQuery, "xwiki", "default,en,de", new XWiki(context.getWiki(), getXWikiContext())); assertNotNull("Search results shoould have not returned null", results); assertEquals("(Objetct Search)There search should match exactly the expected value(ComplexList)", 0, results.getResults().size()); theQuery = "XWiki.ArticleClass.complexlist:1"; //MATCH results = lpApi.getSearchResults(theQuery, "xwiki", "default,en,de", new XWiki(context.getWiki(), getXWikiContext())); assertNotNull("Search results shoould have not returned null", results); assertEquals("(Objetct Search)There search should match exactly the expected value(ComplexList)", 1, results.getResults().size()); theQuery = "XWiki.ArticleClass.complexlist:valueone"; //MATCH results = lpApi.getSearchResults(theQuery, "xwiki", "default,en,de", new XWiki(context.getWiki(), getXWikiContext())); assertNotNull("Search results shoould have not returned null", results); assertEquals("(Objetct Search)There search should match exactly the expected value(ComplexList)", 1, results.getResults().size()); theQuery = "XWiki.ArticleClass.extract:blogarticletwo"; results = lpApi.getSearchResults(theQuery, "xwiki", "default,en,de", new XWiki(context.getWiki(), getXWikiContext())); assertNotNull("Search results shoould have not returned null", results); assertTrue("(Objetct Search)There search should match exactly the expected value(Extract)", results.getResults().size() == 1); String date = DateUtility.getCurrentDate(DateUtility.getCurrentDate(DateUtility.DATE_FORMAT_ONE)); theQuery = "XWiki.ArticleClass.date:" + date; //MATCH results = lpApi.getSearchResults(theQuery, "xwiki", "default,en,de", new XWiki(context.getWiki(), getXWikiContext())); assertNotNull("Search results shoould have not returned null", results); assertEquals("(Objetct Search)There search should match exactly the expected value(Date))", 2, results.getResults().size()); //for fulltext search theQuery = "categoryone"; results = lpApi.getSearchResults(theQuery, "xwiki", "default,en,de", new XWiki(context.getWiki(), getXWikiContext())); assertTrue("(Full Text)There should be atleast one docuement in the search", results.getResults().size() == 2); String currentDate = DateUtility.getCurrentDate(DateUtility.getCurrentDate(DateUtility.DATE_FORMAT_ONE)); String previousDate = null; String nextDate = null; theQuery = "XWiki.ArticleClass.date:" + currentDate + "*"; results = lpApi.getSearchResults(theQuery, "xwiki", "default,en,de", new XWiki(context.getWiki(), getXWikiContext())); assertNotNull("Search results shoould have not returned null", results); assertEquals("(Objetct Search)There search should match exactly the expected value(Date)", 2, results.getResults().size()); theQuery = "XWiki.ArticleClass.date:[" + currentDate + " TO " + currentDate + "]"; results = lpApi.getSearchResults(theQuery, "xwiki", "default,en,de", new XWiki(context.getWiki(), getXWikiContext())); assertNotNull("Search results shoould have not returned null", results); assertEquals("(Objetct Search)There search should match exactly the expected value(Date)", 2, results.getResults().size()); nextDate = DateUtility.getCurrentDate(DateUtility.getNextDate(3)); theQuery = "XWiki.ArticleClass.date:[" + currentDate + " TO " + nextDate + "]"; results = lpApi.getSearchResults(theQuery, "xwiki", "default,en,de", new XWiki(context.getWiki(), getXWikiContext())); assertNotNull("Search results shoould have not returned null", results); assertEquals("(Objetct Search)There search should match exactly the expected value(Date)", 2, results.getResults().size()); nextDate = DateUtility.getCurrentDate(DateUtility.getNextDate(3)); theQuery = "XWiki.ArticleClass.date:" + nextDate + "*"; results = lpApi.getSearchResults(theQuery, "xwiki", "default,en,de", new XWiki(context.getWiki(), getXWikiContext())); assertNotNull("Search results shoould have not returned null", results); assertEquals("(Objetct Search)There search should match exactly the expected value(Date)", 0, results.getResults().size()); previousDate = DateUtility.getCurrentDate(DateUtility.getPreviousDate(3)); theQuery = "XWiki.ArticleClass.date:[" + previousDate + " TO " + currentDate + "]"; results = lpApi.getSearchResults(theQuery, "xwiki", "default,en,de", new XWiki(context.getWiki(), getXWikiContext())); assertNotNull("Search results shoould have not returned null", results); assertEquals("(Objetct Search)There search should match exactly the expected value(Date)", 2, results.getResults().size()); previousDate = DateUtility.getCurrentDate(DateUtility.getPreviousDate(3)); theQuery = "XWiki.ArticleClass.date:" + previousDate + "*"; results = lpApi.getSearchResults(theQuery, "xwiki", "default,en,de", new XWiki(context.getWiki(), getXWikiContext())); assertNotNull("Search results shoould have not returned null", results); assertEquals("(Objetct Search)There search should match exactly the expected value(Date)", 0, results.getResults().size()); theQuery = "XWiki.ArticleClass.date:{" + currentDate + " TO " + currentDate + "}"; results = lpApi.getSearchResults(theQuery, "xwiki", "default,en,de", new XWiki(context.getWiki(), getXWikiContext())); assertNotNull("Search results shoould have not returned null", results); assertEquals("(Objetct Search)There search should match exactly the expected value(Date)", 0, results.getResults().size()); nextDate = DateUtility.getCurrentDate(DateUtility.getNextDate(3)); theQuery = "XWiki.ArticleClass.date:{" + currentDate + " TO " + nextDate + "}"; results = lpApi.getSearchResults(theQuery, "xwiki", "default,en,de", new XWiki(context.getWiki(), getXWikiContext())); assertNotNull("Search results shoould have not returned null", results); assertEquals("(Objetct Search)There search should match exactly the expected value(Date)", 0, results.getResults().size()); } protected void tearDown() { getXWiki().getPluginManager().removePlugin("com.xpn.xwiki.plugin.lucene.LucenePlugin"); super.tearDown(); } }