Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 5d393bab052f5a1fb9db0eb03b5c86096cea38dd (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*******************************************************************************
 * Copyright (c) 2011, 2016 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Alexander Kurtakov - Bug 460787
 *******************************************************************************/

package org.eclipse.ua.tests.help.search;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;

import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexFormatTooOldException;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.NIOFSDirectory;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.help.internal.base.BaseHelpSystem;
import org.eclipse.help.internal.search.AnalyzerDescriptor;
import org.eclipse.help.internal.search.PluginIndex;
import org.eclipse.help.internal.search.QueryBuilder;
import org.eclipse.help.internal.search.SearchIndexWithIndexingProgress;
import org.eclipse.ua.tests.plugin.UserAssistanceTestPlugin;
import org.junit.Test;
import org.osgi.framework.Bundle;

/**
 * Verify that older versions of the index can be read by this
 * version of Eclipse.
 *
 * How to maintain this test - if when upgrading to a new version
 * of Lucene one of the IndexReadable tests fails you need to
 * make the following changes:
 * 1. Change the corresponding Compatible() test to expect a result of false
 * 2. Comment out the failing test
 * 3. Change the help system to recognize that version of Lucene as being incompatible
 */

public class PrebuiltIndexCompatibility {

	/**
	 * Test index built with Lucene 1.9.1
	 */
	@Test(expected = IndexFormatTooOldException.class)
	public void test1_9_1_IndexUnReadable() throws Exception {
		checkReadable("data/help/searchindex/index191");
	}

	/**
	 * Test index built with Lucene 2.9.1
	 */
	@Test(expected = IndexFormatTooOldException.class)
	public void test2_9_1_IndexUnReadable() throws Exception {
		checkReadable("data/help/searchindex/index291");
	}

	/**
	 * Test index built with Lucene 3.5.0
	 */
	@Test(expected = IndexFormatTooOldException.class)
	public void test3_5_0_IndexUnReadable() throws Exception {
		checkReadable("data/help/searchindex/index350");
	}

	/**
	 * Test index built with Lucene 6.1.0
	 */
	@Test(expected = IllegalArgumentException.class)
	public void test6_1_0_IndexUnReadable() throws Exception {
		checkReadable("data/help/searchindex/index610");
	}

	/**
	 * Test index built with Lucene 7.0.0
	 */
	@Test
	public void test7_0_0_IndexReadable() throws Exception {
		checkReadable("data/help/searchindex/index700");
	}

	/**
	 ** Test compatibility of Lucene 1.9.1 index with current Lucene
	 */
	@Test
	public void test1_9_1Compatible()
	{
		checkCompatible("data/help/searchindex/index191", false);
	}

	/**
	 ** Test compatibility of Lucene 2.9.1 index with current Lucene
	 */
	@Test
	public void test2_9_1Compatible()
	{
		checkCompatible("data/help/searchindex/index291", false);
	}

	/**
	 ** Test compatibility of Lucene 3.5.0 index with current Lucene
	 */
	@Test
	public void test3_5_0Compatible() {
		checkCompatible("data/help/searchindex/index350", false);
	}

	/**
	 ** Test compatibility of Lucene 6.1.0 index with current Lucene
	 */
	@Test
	public void test6_1_0Compatible() {
		checkCompatible("data/help/searchindex/index610", false);
	}

	/**
	 ** Test compatibility of Lucene 7.0.0 index with current Lucene
	 */
	@Test
	public void test7_0_0Compatible() {
		checkCompatible("data/help/searchindex/index700", true);
	}

	@Test
	public void test1_9_1LuceneCompatible()
	{
		checkLuceneCompatible("1.9.1", false);
	}

	@Test
	public void test1_4_103NotLuceneCompatible()
	{
		checkLuceneCompatible("1.4.103", false);
	}

	@Test
	public void test2_9_1LuceneCompatible()
	{
		checkLuceneCompatible("2.9.1", false);
	}

	@Test
	public void test3_5_0LuceneCompatible() {
		checkLuceneCompatible("3.5.0", false);
	}

	@Test
	public void test6_1_0LuceneCompatible() {
		checkLuceneCompatible("6.1.0", false);
	}

	@Test
	public void test7_0_0LuceneCompatible() {
		checkLuceneCompatible("7.0.0", true);
	}

	@Test
	public void testPluginIndexEqualToItself() {
		PluginIndex index = createPluginIndex("data/help/searchindex/index610");
		assertTrue(index.equals(index));
	}

	/**
	 * Verify that if the paths and plugins are the same two PluginIndex objects are equal
	 */
	@Test
	public void testPluginIndexEquality() {
		PluginIndex index1a = createPluginIndex("data/help/searchindex/index610");
		PluginIndex index1b = createPluginIndex("data/help/searchindex/index610");
		assertTrue(index1a.equals(index1b));
	}

	/**
	 * Verify that if the paths and plugins are the same two PluginIndex objects are equal
	 */
	@Test
	public void testPluginIndexHash() {
		PluginIndex index1a = createPluginIndex("data/help/searchindex/index610");
		PluginIndex index1b = createPluginIndex("data/help/searchindex/index610");
		assertEquals(index1a.hashCode(), index1b.hashCode());
	}

	/**
	 * Verify that if the paths are different two PluginIndex objects are not equal
	 */
	@Test
	public void testPluginIndexInequality() {
		PluginIndex index1 = createPluginIndex("data/help/searchindex/index610");
		PluginIndex index2 = createPluginIndex("data/help/searchindex/index350");
		assertFalse(index1.equals(index2));
	}

    /*
     * Verifies that a prebuilt index can be searched
     */
	private void checkReadable(String indexPath) throws IOException,
			CorruptIndexException {
		Path path = new Path(indexPath);
		Bundle bundle = UserAssistanceTestPlugin.getDefault().getBundle();
		URL url = FileLocator.find(bundle, path, null);
		URL resolved = FileLocator.resolve(url);
		if ("file".equals(resolved.getProtocol())) { //$NON-NLS-1$
			String filePath = resolved.getFile();
			QueryBuilder queryBuilder = new QueryBuilder("eclipse", new AnalyzerDescriptor("en-us"));
			Query luceneQuery = queryBuilder.getLuceneQuery(new ArrayList<String>() , false);
			IndexSearcher searcher;
			try (Directory luceneDirectory = new NIOFSDirectory(new File(filePath).toPath())) {
				searcher = new IndexSearcher(DirectoryReader.open(luceneDirectory));
				TopDocs hits = searcher.search(luceneQuery, 500);
				assertTrue(hits.totalHits >= 1);
			}
		} else {
			fail("Cannot resolve to file protocol");
		}
	}

	/*
	 * Tests the isCompatible method in PluginIndex
	 */
	private void checkCompatible(String versionDirectory, boolean expected) {
		PluginIndex pluginIndex = createPluginIndex(versionDirectory);
		Path path = new Path(versionDirectory);
		assertEquals(expected, pluginIndex.isCompatible(UserAssistanceTestPlugin.getDefault().getBundle(), path));
	}

	public PluginIndex createPluginIndex(String versionDirectory) {
		PluginIndex pluginIndex;
		SearchIndexWithIndexingProgress index = BaseHelpSystem.getLocalSearchManager().getIndex("en_us".toString());
		BaseHelpSystem.getLocalSearchManager().ensureIndexUpdated(
				new NullProgressMonitor(),
				index);
		pluginIndex = new PluginIndex("org.eclipse.ua.tests", "data/help/searchindex/" + versionDirectory, index);
		return pluginIndex;
	}

	/*
	 * Tests the isLuceneCompatible method in SearchIndex
	 */
	private void checkLuceneCompatible(String version, boolean expected) {
		SearchIndexWithIndexingProgress index = BaseHelpSystem.getLocalSearchManager().getIndex("en_us".toString());
		BaseHelpSystem.getLocalSearchManager().ensureIndexUpdated(
				new NullProgressMonitor(),
				index);
		assertEquals(expected, index.isLuceneCompatible(version));
	}

}

Back to the top