Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6e335ce99d1272ce20764d60596b96187e99c28a (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
/*******************************************************************************
 * Copyright (c) 2008, 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
 *******************************************************************************/

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

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.help.internal.base.BaseHelpSystem;
import org.eclipse.help.internal.search.ISearchQuery;
import org.eclipse.help.internal.search.SearchHit;
import org.eclipse.help.internal.search.SearchQuery;
import org.eclipse.help.internal.search.SearchResults;
import org.junit.Assert;

public class SearchTestUtils {

	public static void searchAllLocales(String searchWord, String[] hrefs) {
		searchOneLocale(searchWord, hrefs, "en");
		searchOneLocale(searchWord, hrefs, "de");
	}

	public static void searchOneLocale(String searchWord, String[] hrefs, String nl) {
		String unexpected = searchForExpectedResults(searchWord, hrefs, nl);
		if (unexpected != null) {
			Assert.fail(unexpected);
		}
	}

	/**
	 * @return null if the expected results are returned, otherwise a string describing
	 * any discrepancies
	 */
	public static String searchForExpectedResults(String searchWord,
			String[] hrefs, String nl) {
		final Set<String> hrefsToFind = new HashSet<>();
		final Set<String> unexpectedHrefs = new HashSet<>();
		hrefsToFind.addAll(Arrays.asList(hrefs));

		SearchHit[] hits;
		hits = getSearchHits(searchWord, nl);
		for (SearchHit hit : hits) {
			String href = hit.getHref();
			// ignore query params
			int index = href.indexOf('?');
			if (index != -1) {
				href = href.substring(0, index);
			}
			if (hrefsToFind.contains(href)) {
				hrefsToFind.remove(href);
			}
			else {
				unexpectedHrefs.add(href);
			}
		}

		if (!hrefsToFind.isEmpty() || !unexpectedHrefs.isEmpty()) {
			StringBuffer buf = new StringBuffer();
			buf.append("While searching for: " + searchWord + ",\n");
			if (!hrefsToFind.isEmpty()) {
				buf.append("Some of the expected results were not found:\n");
				Iterator<String> iter = hrefsToFind.iterator();
				while (iter.hasNext()) {
					String missedHref = iter.next();
					buf.append(missedHref + "\n");
				}
			}
			if (!unexpectedHrefs.isEmpty()) {
				if (!hrefsToFind.isEmpty()) {
					buf.append("\nAlso,\n");
				}
				buf.append("Found some unexpected search results:\n");
				Iterator<String> iter = unexpectedHrefs.iterator();
				while (iter.hasNext()) {
					String unexpectedHref = iter.next();
					buf.append(unexpectedHref + "\n");
				}
			}
			return buf.toString();
		}
		return null;
	}

	public static SearchHit[] getSearchHits(String searchWord, String nl) {
		SearchHit[] hits;
		ISearchQuery query = new SearchQuery(searchWord, false, new ArrayList<String>(), nl);
		SearchResults collector = new SearchResults(null, 500, nl);
		BaseHelpSystem.getSearchManager().search(query, collector, new NullProgressMonitor());
		hits = collector.getSearchHits();
		return hits;
	}

}

Back to the top