Skip to main content
summaryrefslogtreecommitdiffstats
blob: e9f9c3fdedc10363c49019a1d9635884fdfe1839 (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
/*******************************************************************************
 * Copyright (c) 2010 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
 *******************************************************************************/

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

import java.net.URL;

import junit.framework.TestCase;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.help.internal.HelpPlugin;
import org.eclipse.help.internal.base.BaseHelpSystem;
import org.eclipse.help.internal.search.AnalyzerDescriptor;
import org.eclipse.help.internal.search.SearchIndex;
import org.eclipse.help.internal.search.SearchIndexWithIndexingProgress;

public class SearchIndexCreation extends TestCase {
	
	private int mode;
	private AnalyzerDescriptor analyzerDesc;

	protected void setUp() throws Exception {
		BaseHelpSystem.ensureWebappRunning();
        mode = BaseHelpSystem.getMode();
        RemotePreferenceStore.savePreferences();
		RemotePreferenceStore.setMockRemoteServer();
		RemotePreferenceStore.disableErrorPage();
		BaseHelpSystem.setMode(BaseHelpSystem.MODE_INFOCENTER);
		HelpPlugin.getTocManager().getTocs("en");
		analyzerDesc = new AnalyzerDescriptor("en-us");
	}
	
	protected void tearDown() throws Exception {
		RemotePreferenceStore.restorePreferences();
		BaseHelpSystem.setMode(mode);
	}

	public void testSearchIndexMakesNoRemoteCalls() throws Throwable {
		int initialCallCount = MockContentServlet.getCallcount();
		SearchIndexWithIndexingProgress index = new SearchIndexWithIndexingProgress("en-us", analyzerDesc, HelpPlugin
				.getTocManager());
		index.beginAddBatch(true);
		addHrefToIndex(index, "/org.eclipse.ua.tests/data/help/search/test7.html", true);
		addHrefToIndex(index, "/org.eclipse.ua.tests/data/help/search/test8.htm", true);
		addHrefToIndex(index, "/org.eclipse.ua.tests/data/help/search/test77.htm", false); // Does not exist
		index.endAddBatch(false, true);
		int finalCallCount = MockContentServlet.getCallcount();
		assertEquals("Remote server called", 0, finalCallCount - initialCallCount);
	}
	
	public void testSearchIndexMakesNoRemoteCalls2() throws Throwable {
		int initialCallCount = MockContentServlet.getCallcount();
		SearchIndexWithIndexingProgress index = new SearchIndexWithIndexingProgress("en-us", analyzerDesc, HelpPlugin
				.getTocManager());
		index.beginAddBatch(true);
		addHrefToIndex(index, "/org.eclipse.ua.tests/data/help/search/test7.html", true);
		addHrefToIndex(index, "/org.eclipse.ua.tests/data/help/search/test8.htm", true);
		addHrefToIndex(index, "/org.eclipse.ua.tests/data/help/search/test77.htm", false); // Does not exist
		index.endAddBatch(false, true);
		int finalCallCount = MockContentServlet.getCallcount();
		assertEquals("Remote server called", 0, finalCallCount - initialCallCount);
	}
	
	public void testSearchIndexMakesNoRemoteCallsRemotePriority() throws Throwable {
		RemotePreferenceStore.setMockRemotePriority();
		int initialCallCount = MockContentServlet.getCallcount();
		SearchIndexWithIndexingProgress index = new SearchIndexWithIndexingProgress("en-us", analyzerDesc, HelpPlugin
				.getTocManager());
		index.beginAddBatch(true);
		addHrefToIndex(index, "/org.eclipse.ua.tests/data/help/search/test7.html", true);
		addHrefToIndex(index, "/org.eclipse.ua.tests/data/help/search/test8.htm", true);
		addHrefToIndex(index, "/org.eclipse.ua.tests/data/help/search/test77.htm", false); // Does not exist
		index.endAddBatch(false, true);
		int finalCallCount = MockContentServlet.getCallcount();
		assertEquals("Remote server called", 0, finalCallCount - initialCallCount);
	}

	private void addHrefToIndex(SearchIndexWithIndexingProgress index,
			String doc, boolean exists) throws Throwable {
		URL url = SearchIndex.getIndexableURL(index.getLocale(), doc);
		IStatus status = index.addDocument(url.getFile(), url);
		if (exists && !status.isOK()) {
			if (status.getException() != null) {
				throw status.getException();
			}
		    fail(doc + " status = " + status.getMessage());
		}
	}

}

Back to the top