Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 116af44d0b975d5aaf4c665f901c7ed8d378bf67 (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
/*******************************************************************************
 * Copyright (c) 2000, 2005 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.search.core.tests;


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

import org.junit.Test;

import org.eclipse.search.ui.ISearchQuery;
import org.eclipse.search.ui.ISearchResultListener;
import org.eclipse.search.ui.SearchResultEvent;
import org.eclipse.search.ui.text.AbstractTextSearchResult;
import org.eclipse.search.ui.text.Match;
import org.eclipse.search.ui.text.MatchEvent;

public class TestSearchResult {

	@Test
	public void testAddMatch() {
		ISearchQuery query= new NullQuery();
		AbstractTextSearchResult result= (AbstractTextSearchResult) query.getSearchResult();

		String object= "object"; //$NON-NLS-1$

		Match match1= new Match(object, 0, 0);
		result.addMatch(match1);
		assertEquals(result.getMatchCount(), 1);
		Match match2= new Match(object, 0, 0);
		result.addMatch(match2);
		assertEquals(result.getMatchCount(), 2);
		result.addMatch(match1);
		assertEquals(result.getMatchCount(), 2);
	}

	@Test
	public void testAddMatchDifferentStart() {
		ISearchQuery query= new NullQuery();
		AbstractTextSearchResult result= (AbstractTextSearchResult) query.getSearchResult();

		String object= "object"; //$NON-NLS-1$

		Match match1= new Match(object, 2, 0);
		result.addMatch(match1);
		assertEquals(result.getMatchCount(), 1);
		Match match2= new Match(object, 1, 1);
		result.addMatch(match2);
		Match match3= new Match(object, 0, 2);
		result.addMatch(match3);
		Match[] matches= result.getMatches(object);
		assertTrue("matches[0]", matches[0] == match3);
		assertTrue("matches[1]", matches[1] == match2);
		assertTrue("matches[2]", matches[2] == match1);
	}

	@Test
	public void testAddMatchDifferentStartInOrder() {
		ISearchQuery query= new NullQuery();
		AbstractTextSearchResult result= (AbstractTextSearchResult) query.getSearchResult();

		String object= "object"; //$NON-NLS-1$

		Match match1= new Match(object, 0, 0);
		result.addMatch(match1);
		assertEquals(result.getMatchCount(), 1);
		Match match2= new Match(object, 1, 1);
		result.addMatch(match2);
		Match match3= new Match(object, 2, 2);
		result.addMatch(match3);
		Match[] matches= result.getMatches(object);
		assertTrue("matches[0]", matches[0] == match1);
		assertTrue("matches[1]", matches[1] == match2);
		assertTrue("matches[2]", matches[2] == match3);
	}

	@Test
	public void testAddMatchDifferentLength() {
		ISearchQuery query= new NullQuery();
		AbstractTextSearchResult result= (AbstractTextSearchResult) query.getSearchResult();

		String object= "object"; //$NON-NLS-1$

		Match match1= new Match(object, 1, 1);
		result.addMatch(match1);
		assertEquals(result.getMatchCount(), 1);
		Match match2= new Match(object, 1, 0);
		result.addMatch(match2);
		Match[] matches= result.getMatches(object);
		assertTrue("matches[0]", matches[0] == match2);
		assertTrue("matches[1]", matches[1] == match1);
	}

	@Test
	public void testAddMatchOrderPreserving() {
		ISearchQuery query= new NullQuery();
		AbstractTextSearchResult result= (AbstractTextSearchResult) query.getSearchResult();

		String object= "object"; //$NON-NLS-1$

		Match match1= new Match(object, 1, 0);
		result.addMatch(match1);
		assertEquals(result.getMatchCount(), 1);
		Match match2= new Match(object, 1, 0);
		result.addMatch(match2);
		Match[] matches= result.getMatches(object);
		assertTrue("matches[0]", matches[0] == match1);
		assertTrue("matches[1]", matches[1] == match2);
	}

	@Test
	public void testAddMatches() {
		ISearchQuery query= new NullQuery();
		AbstractTextSearchResult result= (AbstractTextSearchResult) query.getSearchResult();

		String object= "object"; //$NON-NLS-1$

		Match match1= new Match(object, 0, 0);
		Match match2= new Match(object, 0, 0);
		result.addMatches(new Match[] { match1, match2 });
		assertEquals(result.getMatchCount(), 2);
		result.addMatch(match1);
		assertEquals(result.getMatchCount(), 2);
	}

	@Test
	public void testRemoveMatch() {
		ISearchQuery query= new NullQuery();
		AbstractTextSearchResult result= (AbstractTextSearchResult) query.getSearchResult();

		String object= "object"; //$NON-NLS-1$

		Match match1= new Match(object, 0, 0);
		result.addMatch(match1);
		Match match2= new Match(object, 0, 0);
		result.addMatch(match2);
		assertEquals(result.getMatchCount(), 2);

		result.removeMatch(match1);
		assertEquals(result.getMatchCount(), 1);
		result.removeMatch(match1);
		assertEquals(result.getMatchCount(), 1);

	}

	@Test
	public void testRemoveMatches() {
		ISearchQuery query= new NullQuery();
		AbstractTextSearchResult result= (AbstractTextSearchResult) query.getSearchResult();

		String object= "object"; //$NON-NLS-1$

		Match match1= new Match(object, 0, 0);
		Match match2= new Match(object, 0, 0);
		result.addMatches(new Match[] { match1, match2 });
		assertEquals(result.getMatchCount(), 2);

		result.removeMatches(new Match[] { match1, match2 });
		assertEquals(result.getMatchCount(), 0);

	}

	@Test
	public void testMatchEvent() {
		final boolean [] wasAdded= { false };
		final boolean [] wasRemoved= { false };

		ISearchQuery query= new NullQuery();
		AbstractTextSearchResult result= (AbstractTextSearchResult) query.getSearchResult();

		result.addListener(new ISearchResultListener() {
			@Override
			public void searchResultChanged(SearchResultEvent e) {
				if (e instanceof MatchEvent) {
					MatchEvent evt= (MatchEvent) e;
					if (evt.getKind() == MatchEvent.ADDED) {
						wasAdded[0]= true;
					} else {
						wasRemoved[0]= true;
					}
				}
			}
		});

		String object= "object"; //$NON-NLS-1$

		Match match1= new Match(object, 0, 0);
		result.addMatch(match1);
		assertTrue(wasAdded[0]);
		wasAdded[0]= false;
		result.addMatch(match1);
		assertFalse(wasAdded[0]);

		Match match2= new Match(object, 0, 0);
		result.addMatch(match2);
		assertTrue(wasAdded[0]);
		wasAdded[0]= false;

		result.removeMatch(match2);
		assertTrue(wasRemoved[0]);
		wasRemoved[0]= false;

		result.removeMatch(match2);
		assertFalse(wasRemoved[0]);
	}

	@Test
	public void testBatchedMatchEvent() {
		final boolean [] wasAdded= { false };
		final boolean [] wasRemoved= { false };

		ISearchQuery query= new NullQuery();
		AbstractTextSearchResult result= (AbstractTextSearchResult) query.getSearchResult();

		result.addListener(new ISearchResultListener() {
			@Override
			public void searchResultChanged(SearchResultEvent e) {
				if (e instanceof MatchEvent) {
					MatchEvent evt= (MatchEvent) e;
					if (evt.getKind() == MatchEvent.ADDED) {
						wasAdded[0]= true;
					} else {
						wasRemoved[0]= true;
					}
				}
			}
		});

		String object= "object"; //$NON-NLS-1$

		Match match1= new Match(object, 0, 0);
		result.addMatches(new Match[] { match1 });
		assertTrue(wasAdded[0]);
		wasAdded[0]= false;
		result.addMatches(new Match[] { match1 });
		assertFalse(wasAdded[0]);

		Match match2= new Match(object, 0, 0);
		result.addMatches(new Match[] { match2 });
		assertTrue(wasAdded[0]);
		wasAdded[0]= false;

		result.removeMatches(new Match[] { match2 });
		assertTrue(wasRemoved[0]);
		wasRemoved[0]= false;

		result.removeMatches(new Match[] { match2 });
		assertFalse(wasRemoved[0]);
	}
}

Back to the top