Search in sources :

Example 1 with QueryItem

use of org.apache.wiki.api.search.QueryItem in project jspwiki by apache.

the class WikiProviderAdaptersTest method testAttachmentProvider.

@Test
public void testAttachmentProvider() throws Exception {
    final AttachmentProvider attachmentProvider = engine.getManager(AttachmentManager.class).getCurrentProvider();
    final Attachment att11 = new Attachment(engine, "page1", "att11.txt");
    final Attachment att13 = new Attachment(engine, "page1", "att13.txt");
    final QueryItem qi = new QueryItem();
    qi.word = "doesn't matter will be ignored";
    qi.type = QueryItem.REQUESTED;
    Assertions.assertEquals("com.example.providers.TwoXWikiAttachmentProvider", attachmentProvider.getProviderInfo());
    Assertions.assertEquals(2, attachmentProvider.listAttachments(new WikiPage(engine, "page1")).size());
    final byte[] attDataArray = new byte[attachmentProvider.getAttachmentData(att11).available()];
    attachmentProvider.getAttachmentData(att11).read(attDataArray);
    Assertions.assertArrayEquals("blurb".getBytes(StandardCharsets.UTF_8), attDataArray);
    Assertions.assertEquals(0, attachmentProvider.findAttachments(new QueryItem[] { qi }).size());
    Assertions.assertEquals(3, attachmentProvider.listAllChanged(new Date(0L)).size());
    Assertions.assertEquals(att11.getName(), attachmentProvider.getAttachmentInfo(new WikiPage(engine, "page1"), "att11.txt", 0).getName());
    Assertions.assertEquals(1, attachmentProvider.getVersionHistory(att11).size());
    attachmentProvider.putAttachmentData(att13, new ByteArrayInputStream("blorb".getBytes(StandardCharsets.UTF_8)));
    Assertions.assertEquals(3, attachmentProvider.listAttachments(new WikiPage(engine, "page1")).size());
    attachmentProvider.putAttachmentData(att13, new ByteArrayInputStream("blorb".getBytes(StandardCharsets.UTF_8)));
    Assertions.assertEquals(2, attachmentProvider.getVersionHistory(att13).size());
    attachmentProvider.deleteVersion(attachmentProvider.getVersionHistory(att13).get(1));
    Assertions.assertEquals(1, attachmentProvider.getVersionHistory(att13).size());
    attachmentProvider.deleteAttachment(att13);
    Assertions.assertEquals(0, attachmentProvider.getVersionHistory(att13).size());
    Assertions.assertEquals(2, attachmentProvider.listAttachments(new WikiPage(engine, "page1")).size());
    attachmentProvider.moveAttachmentsForPage("page1", "page0");
    Assertions.assertEquals(2, attachmentProvider.listAttachments(new WikiPage(engine, "page0")).size());
    Assertions.assertEquals(0, attachmentProvider.listAttachments(new WikiPage(engine, "page1")).size());
}
Also used : QueryItem(org.apache.wiki.api.search.QueryItem) ByteArrayInputStream(java.io.ByteArrayInputStream) WikiPage(org.apache.wiki.WikiPage) Attachment(org.apache.wiki.attachment.Attachment) AttachmentManager(org.apache.wiki.attachment.AttachmentManager) Date(java.util.Date) AttachmentProvider(org.apache.wiki.api.providers.AttachmentProvider) Test(org.junit.jupiter.api.Test)

Example 2 with QueryItem

use of org.apache.wiki.api.search.QueryItem in project jspwiki by apache.

the class WikiProviderAdaptersTest method testPageProvider.

@Test
public void testPageProvider() throws Exception {
    final PageProvider pageProvider = engine.getManager(PageManager.class).getProvider();
    final QueryItem qi = new QueryItem();
    qi.word = "blablablabla";
    qi.type = QueryItem.REQUESTED;
    Assertions.assertEquals("com.example.providers.TwoXWikiPageProvider", pageProvider.getProviderInfo());
    Assertions.assertEquals(3, pageProvider.getAllChangedSince(new Date(0L)).size());
    Assertions.assertEquals(3, pageProvider.getAllPages().size());
    Assertions.assertEquals(3, pageProvider.getPageCount());
    Assertions.assertTrue(pageProvider.pageExists("page1"));
    Assertions.assertTrue(pageProvider.pageExists("page1", 0));
    pageProvider.movePage("page1", "page0");
    Assertions.assertTrue(pageProvider.pageExists("page0"));
    Assertions.assertFalse(pageProvider.pageExists("page1"));
    pageProvider.putPageText(new WikiPage(engine, "page4"), "bloblobloblo");
    Assertions.assertTrue(pageProvider.pageExists("page4"));
    Assertions.assertEquals(1, pageProvider.findPages(new QueryItem[] { qi }).size());
    pageProvider.putPageText(new WikiPage(engine, "page4"), "blublublublu");
    Assertions.assertEquals(2, pageProvider.getVersionHistory("page4").size());
    Assertions.assertEquals("bloblobloblo", pageProvider.getPageText("page4", 0));
    Assertions.assertEquals("blublublublu", pageProvider.getPageText("page4", 1));
    pageProvider.deleteVersion("page4", 1);
    Assertions.assertEquals(1, pageProvider.getVersionHistory("page4").size());
    pageProvider.deletePage("page4");
    Assertions.assertFalse(pageProvider.pageExists("page4"));
}
Also used : QueryItem(org.apache.wiki.api.search.QueryItem) PageManager(org.apache.wiki.pages.PageManager) PageProvider(org.apache.wiki.api.providers.PageProvider) WikiPage(org.apache.wiki.WikiPage) Date(java.util.Date) Test(org.junit.jupiter.api.Test)

Example 3 with QueryItem

use of org.apache.wiki.api.search.QueryItem in project jspwiki by apache.

the class SearchAdapterTest method testOldQueryItemfrom.

@Test
public void testOldQueryItemfrom() {
    final QueryItem qi = new QueryItem();
    qi.type = 1;
    qi.word = "word";
    final org.apache.wiki.search.QueryItem old = SearchAdapter.oldQueryItemfrom(qi);
    Assertions.assertEquals(qi.type, old.type);
    Assertions.assertEquals(qi.word, old.word);
}
Also used : QueryItem(org.apache.wiki.api.search.QueryItem) Test(org.junit.jupiter.api.Test)

Example 4 with QueryItem

use of org.apache.wiki.api.search.QueryItem in project jspwiki by apache.

the class BasicSearchProvider method parseQuery.

/**
 *  Parses a query into something that we can use.
 *
 *  @param query A query string.
 *  @return A parsed array.
 */
public QueryItem[] parseQuery(final String query) {
    final StringTokenizer st = new StringTokenizer(query, " \t,");
    final QueryItem[] items = new QueryItem[st.countTokens()];
    int word = 0;
    log.debug("Expecting " + items.length + " items");
    // Parse incoming search string
    while (st.hasMoreTokens()) {
        log.debug("Item " + word);
        String token = st.nextToken().toLowerCase();
        items[word] = new QueryItem();
        switch(token.charAt(0)) {
            case '+':
                items[word].type = QueryItem.REQUIRED;
                token = token.substring(1);
                log.debug("Required word: " + token);
                break;
            case '-':
                items[word].type = QueryItem.FORBIDDEN;
                token = token.substring(1);
                log.debug("Forbidden word: " + token);
                break;
            default:
                items[word].type = QueryItem.REQUESTED;
                log.debug("Requested word: " + token);
                break;
        }
        items[word++].word = token;
    }
    return items;
}
Also used : QueryItem(org.apache.wiki.api.search.QueryItem) StringTokenizer(java.util.StringTokenizer)

Example 5 with QueryItem

use of org.apache.wiki.api.search.QueryItem in project jspwiki by apache.

the class WikiPageAdapterProvider method findPages.

/**
 * {@inheritDoc}
 */
@Override
public Collection<SearchResult> findPages(final QueryItem[] query) {
    final org.apache.wiki.search.QueryItem[] queryItems = Arrays.stream(query).map(SearchAdapter::oldQueryItemfrom).toArray(org.apache.wiki.search.QueryItem[]::new);
    final Collection<org.apache.wiki.search.SearchResult> results = provider.findPages(queryItems);
    return results.stream().map(SearchAdapter::newSearchResultFrom).collect(Collectors.toCollection(() -> new TreeSet<>(new SearchResultComparator())));
}
Also used : QueryItem(org.apache.wiki.api.search.QueryItem) TreeSet(java.util.TreeSet) SearchResultComparator(org.apache.wiki.search.SearchResultComparator) SearchResult(org.apache.wiki.api.search.SearchResult)

Aggregations

QueryItem (org.apache.wiki.api.search.QueryItem)5 Test (org.junit.jupiter.api.Test)3 Date (java.util.Date)2 WikiPage (org.apache.wiki.WikiPage)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 StringTokenizer (java.util.StringTokenizer)1 TreeSet (java.util.TreeSet)1 AttachmentProvider (org.apache.wiki.api.providers.AttachmentProvider)1 PageProvider (org.apache.wiki.api.providers.PageProvider)1 SearchResult (org.apache.wiki.api.search.SearchResult)1 Attachment (org.apache.wiki.attachment.Attachment)1 AttachmentManager (org.apache.wiki.attachment.AttachmentManager)1 PageManager (org.apache.wiki.pages.PageManager)1 SearchResultComparator (org.apache.wiki.search.SearchResultComparator)1