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
|
/*******************************************************************************
* Copyright (c) 2006, 2015 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.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.junit.Assert;
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.eclipse.help.internal.workingset.AdaptableHelpResource;
import org.eclipse.help.internal.workingset.AdaptableToc;
import org.eclipse.help.internal.workingset.WorkingSet;
import org.eclipse.help.internal.workingset.WorkingSetManager;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Platform;
public class ExtraDirTest extends TestCase {
/*
* Returns an instance of this Test.
*/
public static Test suite() {
return new TestSuite(ExtraDirTest.class);
}
// try without search scope
public void testNoScope()
{
performSearch("iusazemhdv", null, "/org.eclipse.ua.tests/data/help/search/test8.htm");
}
// try with containing toc's search scope
public void testContainingScope()
{
performSearch("iusazemhdv", "/org.eclipse.ua.tests/data/help/toc/root.xml", "/org.eclipse.ua.tests/data/help/search/test8.htm" );
}
// try with another toc's search scope; shouldn't find it
public void testNonContainingScope()
{
performSearch("iusazemhdv", "/org.eclipse.platform.doc.user/toc.xml", null );
}
// extradir doc, no search scope
public void testExtradirNoScope()
{
performSearch("xzopsujjae", null, "/org.eclipse.ua.tests/data/help/search/extraDir/extraDoc1.htm" );
}
// extradir doc, with correct search scope
public void testExtradirCorrectScope()
{
performSearch("xzopsujjae", "/org.eclipse.ua.tests/data/help/toc/root.xml", "/org.eclipse.ua.tests/data/help/search/extraDir/extraDoc1.htm" );
}
// extradir doc, with incorrect search scope
public void testExtradirIncorrectScope()
{
performSearch("xzopsujjae", "/org.eclipse.platform.doc.user/toc.xml", null );
}
// extradir doc (in subdir), no search scope
public void testExtradirSubdirNoScope()
{
performSearch("mrendiqwja", null, "/org.eclipse.ua.tests/data/help/search/extraDir/subDir/extraDoc2.htm" );
}
// extradir doc (in subdir), with correct search scope
public void testExtradirSubDirInScope()
{
performSearch("mrendiqwja", "/org.eclipse.ua.tests/data/help/toc/root.xml", "/org.eclipse.ua.tests/data/help/search/extraDir/subDir/extraDoc2.htm" );
}
// extradir doc (in subdir), with incorrect search scope
public void testExtradirSubDirOutOfScope()
{
performSearch("mrendiqwja", "/org.eclipse.platform.doc.user/toc.xml", null );
}
// extradir doc link_to'ed from another toc (toc3.xml), with incorrect scope
public void testExtradirLinkedOutOfScope()
{
performSearch("kleoiujfpn", "/org.eclipse.platform.doc.user/toc.xml", null );
}
// extradir doc linked into toc2.xml from toc4.xml, no scope
public void testExtradirLinkedNoScope()
{
performSearch("fuqnejwmfh", null, "/org.eclipse.ua.tests/data/help/search/extraDir3/extraDoc.htm" );
}
// extradir doc linked into toc2.xml from toc4.xml, correct scope
public void testExtradirLinkedInScope()
{
performSearch("fuqnejwmfh", "/org.eclipse.ua.tests/data/help/toc/root.xml", "/org.eclipse.ua.tests/data/help/search/extraDir3/extraDoc.htm" );
}
// extradir doc linked into toc2.xml from toc4.xml, incorrect scope
public void testExtradirLinkedWrongScope()
{
performSearch("fuqnejwmfh", "/org.eclipse.platform.doc.user/toc.xml", null );
}
private void performSearch(String searchWord, String scope,
String expectedResults) {
WorkingSet[] workingSets = null;
if (scope != null) {
WorkingSetManager wsm = BaseHelpSystem.getWorkingSetManager();
List<AdaptableToc> tocs = new ArrayList<AdaptableToc>();
StringTokenizer tok = new StringTokenizer(scope, ", \t\n");
while (tok.hasMoreTokens()) {
tocs.add(wsm.getAdaptableToc(tok.nextToken()));
}
workingSets = new WorkingSet[] { wsm.createWorkingSet("testWorkingSet", tocs.toArray(new AdaptableHelpResource[tocs.size()])) };
}
// expected hits
final Set<String> hrefsToFind = new HashSet<String>();
if (expectedResults != null) {
StringTokenizer tok = new StringTokenizer(expectedResults, ", \t\n");
while (tok.hasMoreTokens()) {
hrefsToFind.add(tok.nextToken());
}
}
// run test
final Set<String> unexpectedHrefs = new HashSet<String>();
ISearchQuery query = new SearchQuery(searchWord, false, new ArrayList<String>(), Platform.getNL());
SearchResults collector = new SearchResults(workingSets, 500, Platform.getNL());
BaseHelpSystem.getSearchManager().search(query, collector, new NullProgressMonitor());
SearchHit[] hits = collector.getSearchHits();
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");
}
}
Assert.fail(buf.toString());
}
}
}
|