001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.search.unit.test;
016    
017    import com.liferay.portal.kernel.dao.orm.QueryUtil;
018    import com.liferay.portal.kernel.search.Document;
019    import com.liferay.portal.kernel.search.Field;
020    import com.liferay.portal.kernel.search.Hits;
021    import com.liferay.portal.kernel.search.IndexSearcher;
022    import com.liferay.portal.kernel.search.IndexWriter;
023    import com.liferay.portal.kernel.search.Query;
024    import com.liferay.portal.kernel.search.QueryConfig;
025    import com.liferay.portal.kernel.search.SearchContext;
026    import com.liferay.portal.kernel.search.generic.TermQueryImpl;
027    import com.liferay.portal.kernel.test.util.RandomTestUtil;
028    import com.liferay.portal.kernel.util.StringUtil;
029    
030    import org.junit.After;
031    import org.junit.Assume;
032    import org.junit.Before;
033    
034    /**
035     * @author Miguel Angelo Caldas Gallindo
036     */
037    public abstract class BaseIndexingTestCase {
038    
039            public BaseIndexingTestCase() {
040                    Class<?> clazz = this.getClass();
041    
042                    _entryClassName = StringUtil.toLowerCase(clazz.getSimpleName());
043            }
044    
045            @Before
046            public void setUp() throws Exception {
047                    _documentFixture.setUp();
048    
049                    _indexingFixture = createIndexingFixture();
050    
051                    Assume.assumeTrue(_indexingFixture.isSearchEngineAvailable());
052    
053                    _indexingFixture.setUp();
054    
055                    _indexSearcher = _indexingFixture.getIndexSearcher();
056                    _indexWriter = _indexingFixture.getIndexWriter();
057            }
058    
059            @After
060            public void tearDown() throws Exception {
061                    if (!_indexingFixture.isSearchEngineAvailable()) {
062                            return;
063                    }
064    
065                    _documentFixture.tearDown();
066    
067                    _indexWriter.deleteEntityDocuments(
068                            createSearchContext(), _entryClassName);
069    
070                    _indexingFixture.tearDown();
071            }
072    
073            public interface DocumentCreationHelper {
074    
075                    public void populate(Document document);
076    
077            }
078    
079            protected static SearchContext createSearchContext() {
080                    SearchContext searchContext = new SearchContext();
081    
082                    searchContext.setCompanyId(COMPANY_ID);
083                    searchContext.setEnd(QueryUtil.ALL_POS);
084                    searchContext.setGroupIds(new long[] {GROUP_ID});
085    
086                    QueryConfig queryConfig = new QueryConfig();
087    
088                    queryConfig.setHighlightEnabled(false);
089                    queryConfig.setHitsProcessingEnabled(true);
090                    queryConfig.setScoreEnabled(false);
091    
092                    searchContext.setQueryConfig(queryConfig);
093    
094                    searchContext.setStart(QueryUtil.ALL_POS);
095    
096                    return searchContext;
097            }
098    
099            protected void addDocument(DocumentCreationHelper documentCreationHelper)
100                    throws Exception {
101    
102                    Document document = DocumentFixture.newDocument(
103                            COMPANY_ID, GROUP_ID, _entryClassName);
104    
105                    documentCreationHelper.populate(document);
106    
107                    _indexWriter.addDocument(createSearchContext(), document);
108            }
109    
110            protected abstract IndexingFixture createIndexingFixture() throws Exception;
111    
112            protected Hits search(SearchContext searchContext) throws Exception {
113                    Query query = new TermQueryImpl(
114                            Field.ENTRY_CLASS_NAME, _entryClassName);
115    
116                    return _indexSearcher.search(searchContext, query);
117            }
118    
119            protected static final long COMPANY_ID = RandomTestUtil.randomLong();
120    
121            protected static final long GROUP_ID = RandomTestUtil.randomLong();
122    
123            private final DocumentFixture _documentFixture = new DocumentFixture();
124            private final String _entryClassName;
125            private IndexingFixture _indexingFixture;
126            private IndexSearcher _indexSearcher;
127            private IndexWriter _indexWriter;
128    
129    }