Skip to content

Commit 8fe6793

Browse files
committed
refactor search tests
1 parent 7d20870 commit 8fe6793

File tree

16 files changed

+348
-692
lines changed

16 files changed

+348
-692
lines changed

base-test/org.eclipse.jdt.groovy.core.tests.builder/src/org/eclipse/jdt/core/groovy/tests/MockPossibleMatch.java

-37
This file was deleted.

base-test/org.eclipse.jdt.groovy.core.tests.builder/src/org/eclipse/jdt/core/groovy/tests/MockSearchRequestor.java

-70
This file was deleted.

base-test/org.eclipse.jdt.groovy.core.tests.builder/src/org/eclipse/jdt/core/groovy/tests/search/BinarySearchTests.java

+23-39
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import static org.junit.Assert.assertTrue;
2020
import static org.junit.Assert.fail;
2121

22+
import java.util.List;
23+
2224
import org.eclipse.core.runtime.FileLocator;
2325
import org.eclipse.core.runtime.Path;
2426
import org.eclipse.core.runtime.Platform;
@@ -28,13 +30,10 @@
2830
import org.eclipse.jdt.core.IMethod;
2931
import org.eclipse.jdt.core.IType;
3032
import org.eclipse.jdt.core.JavaCore;
31-
import org.eclipse.jdt.core.groovy.tests.MockSearchRequestor;
32-
import org.eclipse.jdt.core.groovy.tests.SimpleProgressMonitor;
3333
import org.eclipse.jdt.core.search.IJavaSearchConstants;
3434
import org.eclipse.jdt.core.search.IJavaSearchScope;
3535
import org.eclipse.jdt.core.search.SearchEngine;
3636
import org.eclipse.jdt.core.search.SearchMatch;
37-
import org.eclipse.jdt.core.search.SearchParticipant;
3837
import org.eclipse.jdt.core.search.SearchPattern;
3938
import org.eclipse.jdt.internal.core.BinaryMember;
4039
import org.junit.After;
@@ -100,8 +99,6 @@ public void setUp() throws Exception {
10099
env.addEntry(project.getFullPath(), JavaCore.newLibraryEntry(libDir.append("binGroovySearch.jar"), libDir.append("binGroovySearchSrc.zip"), null));
101100

102101
javaProject = env.getJavaProject(project.getName());
103-
waitForIndexer(javaProject);
104-
105102
// overwrite the contents vars with the actual contents
106103
groovyClassContents = javaProject.findType("pack.AGroovyClass").getTypeRoot().getBuffer().getContents();
107104
groovyClassContents2 = javaProject.findType("pack.AnotherGroovyClass").getTypeRoot().getBuffer().getContents();
@@ -112,38 +109,32 @@ public void tearDown() throws Exception {
112109
javaProject = null;
113110
}
114111

115-
private MockSearchRequestor performSearch(IJavaElement toSearchFor) throws Exception {
116-
assertTrue("Expected binary member, but got: " + toSearchFor == null ? null : toSearchFor.getClass().getName(), toSearchFor instanceof BinaryMember);
117-
118-
SearchPattern pattern = SearchPattern.createPattern(toSearchFor, IJavaSearchConstants.REFERENCES);
119-
SearchParticipant participant = SearchEngine.getDefaultSearchParticipant();
112+
private List<SearchMatch> performSearch(final IJavaElement javaElement) throws Exception {
113+
assertTrue("Expected binary member, but got: " + javaElement == null ? null : javaElement.getClass().getName(), javaElement instanceof BinaryMember);
114+
SearchPattern pattern = SearchPattern.createPattern(javaElement, IJavaSearchConstants.REFERENCES);
120115
IJavaSearchScope scope = SearchEngine.createJavaSearchScope(new IJavaElement[] {javaProject});
121-
MockSearchRequestor requestor = new MockSearchRequestor();
122-
SimpleProgressMonitor monitor = new SimpleProgressMonitor("Search in project binaries");
123-
124-
new SearchEngine().search(pattern, new SearchParticipant[] {participant}, scope, requestor, monitor);
125-
monitor.waitForCompletion(10);
126-
return requestor;
116+
return search(pattern, scope);
127117
}
128118

129-
private void assertMatches(String toFind, MockSearchRequestor requestor, int allMatches, int firstMatches) {
130-
if (requestor.getMatches().size() != allMatches) {
131-
fail("Expecting " + allMatches + " matches, but found " + requestor.getMatches().size() + "\n" + requestor.printMatches());
119+
private void assertMatches(final String toFind, final List<SearchMatch> matches, final int allMatches, final int firstMatches) {
120+
String matchesString = toString(matches);
121+
if (matches.size() != allMatches) {
122+
fail("Expecting " + allMatches + " matches, but found " + matches.size() + "\n" + matchesString);
132123
}
133124
int currIndex = groovyClassContents.indexOf("def doit") + "def doit".length();
134125
for (int i = 0; i < firstMatches; i += 1) {
135-
SearchMatch match = requestor.getMatches().get(i);
126+
SearchMatch match = matches.get(i);
136127
currIndex = groovyClassContents.indexOf(toFind, currIndex);
137-
assertEquals("Invalid start position in match " + i + "\n" + requestor.printMatches(), currIndex, match.getOffset());
138-
assertEquals("Invalid length in match " + i + "\n" + requestor.printMatches(), toFind.length(), match.getLength());
128+
assertEquals("Invalid start position in match " + i + "\n" + matchesString, currIndex, match.getOffset());
129+
assertEquals("Invalid length in match " + i + "\n" + matchesString, toFind.length(), match.getLength());
139130
currIndex += toFind.length();
140131
}
141132
currIndex = groovyClassContents2.indexOf("def doit") + "def doit".length();
142133
for (int i = firstMatches; i < allMatches; i += 1) {
143-
SearchMatch match = requestor.getMatches().get(i);
134+
SearchMatch match = matches.get(i);
144135
currIndex = groovyClassContents2.indexOf(toFind, currIndex);
145-
assertEquals("Invalid start position in match " + i + "\n" + requestor.printMatches(), currIndex, match.getOffset());
146-
assertEquals("Invalid length in match " + i + "\n" + requestor.printMatches(), toFind.length(), match.getLength());
136+
assertEquals("Invalid start position in match " + i + "\n" + matchesString, currIndex, match.getOffset());
137+
assertEquals("Invalid length in match " + i + "\n" + matchesString, toFind.length(), match.getLength());
147138
currIndex += toFind.length();
148139
}
149140
}
@@ -153,59 +144,52 @@ private void assertMatches(String toFind, MockSearchRequestor requestor, int all
153144
@Test
154145
public void testClassDecl1() throws Exception {
155146
IType type = javaProject.findType("pack.AGroovyClass");
156-
MockSearchRequestor requestor = performSearch(type);
157-
assertMatches("AGroovyClass", requestor, 12, 2);
147+
assertMatches("AGroovyClass", performSearch(type), 12, 2);
158148
}
159149

160150
@Test
161151
public void testClassDecl2() throws Exception {
162152
IType type = javaProject.findType("pack.OtherClass");
163-
MockSearchRequestor requestor = performSearch(type);
164-
assertMatches("OtherClass", requestor, 4, 2);
153+
assertMatches("OtherClass", performSearch(type), 4, 2);
165154
}
166155

167156
@Test
168157
public void testFieldDecl1() throws Exception {
169158
IType type = javaProject.findType("pack.AGroovyClass");
170159
String toFind = "age_1";
171160
IField field = type.getField(toFind);
172-
MockSearchRequestor requestor = performSearch(field);
173-
assertMatches(toFind, requestor, 2, 2); // all was 4, but in binary synthetic accessor is indistinguishable from source method
161+
assertMatches(toFind, performSearch(field), 2, 2); // all was 4, but in binary synthetic accessor is indistinguishable from source method
174162
}
175163

176164
@Test
177165
public void testFieldDecl2() throws Exception {
178166
IType type = javaProject.findType("pack.AGroovyClass");
179167
String toFind = "name_1";
180168
IField field = type.getField(toFind);
181-
MockSearchRequestor requestor = performSearch(field);
182-
assertMatches(toFind, requestor, 2, 2); // all was 4, but in binary synthetic accessor is indistinguishable from source method
169+
assertMatches(toFind, performSearch(field), 2, 2); // all was 4, but in binary synthetic accessor is indistinguishable from source method
183170
}
184171

185172
@Test
186173
public void testMethodDecl() throws Exception {
187174
IType type = javaProject.findType("pack.AGroovyClass");
188175
String toFind = "doit";
189176
IMethod method = type.getMethod(toFind, new String[0]);
190-
MockSearchRequestor requestor = performSearch(method);
191-
assertMatches(toFind, requestor, 4, 2);
177+
assertMatches(toFind, performSearch(method), 4, 2);
192178
}
193179

194180
@Test
195181
public void testFieldRefInInitializer() throws Exception {
196182
IType type = javaProject.findType("pack.AGroovyClass");
197183
String toFind = "fieldInInitializer";
198184
IField method = type.getField(toFind);
199-
MockSearchRequestor requestor = performSearch(method);
200-
assertMatches(toFind, requestor, 1, 1); // all was 2, but in binary synthetic accessor is indistinguishable from source method
185+
assertMatches(toFind, performSearch(method), 1, 1); // all was 2, but in binary synthetic accessor is indistinguishable from source method
201186
}
202187

203188
@Test
204189
public void testMethodRefInInitializer() throws Exception {
205190
IType type = javaProject.findType("pack.AGroovyClass");
206191
String toFind = "referencedInInitializer";
207192
IMethod method = type.getMethod(toFind, new String[0]);
208-
MockSearchRequestor requestor = performSearch(method);
209-
assertMatches(toFind, requestor, 2, 1);
193+
assertMatches(toFind, performSearch(method), 2, 1);
210194
}
211195
}

base-test/org.eclipse.jdt.groovy.core.tests.builder/src/org/eclipse/jdt/core/groovy/tests/search/CategorySearchTests.java

+16-28
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,9 @@
2525
import org.codehaus.jdt.groovy.model.GroovyCompilationUnit;
2626
import org.eclipse.jdt.core.IMethod;
2727
import org.eclipse.jdt.core.JavaModelException;
28-
import org.eclipse.jdt.core.groovy.tests.MockPossibleMatch;
2928
import org.eclipse.jdt.core.search.IJavaSearchConstants;
3029
import org.eclipse.jdt.core.search.SearchMatch;
3130
import org.eclipse.jdt.core.search.SearchPattern;
32-
import org.eclipse.jdt.groovy.search.ITypeRequestor;
33-
import org.eclipse.jdt.groovy.search.TypeInferencingVisitorWithRequestor;
34-
import org.eclipse.jdt.groovy.search.TypeRequestorFactory;
3531
import org.junit.Test;
3632

3733
/**
@@ -143,41 +139,33 @@ public void testCategorySearch6() throws Exception {
143139
doCategorySearchTest(NO_CATEGORY, 0);
144140
}
145141

146-
void doCategorySearchTest(String contents, int numMatches) throws JavaModelException {
142+
//--------------------------------------------------------------------------
143+
144+
private void doCategorySearchTest(final String contents, final int numMatches) throws JavaModelException {
147145
checkMatches(findMatches(contents), numMatches, contents);
148146
}
149147

150-
List<SearchMatch> findMatches(String contents) throws JavaModelException {
148+
private List<SearchMatch> findMatches(final String contents) throws JavaModelException {
151149
GroovyCompilationUnit catUnit = createUnit("Cat", CATEGORY_DEFN);
152150
GroovyCompilationUnit unit = createUnit("Other", contents);
153151
expectingNoProblems();
154152

155-
MockPossibleMatch match = new MockPossibleMatch(unit);
156153
IMethod searchFor = (IMethod) catUnit.getElementAt(CATEGORY_DEFN.indexOf("doNothing"));
157154
assertEquals("Wrong IJavaElement found: " + searchFor, "doNothing", searchFor.getElementName());
158-
SearchPattern pattern = SearchPattern.createPattern(searchFor, IJavaSearchConstants.REFERENCES);
159-
ITypeRequestor typeRequestor = new TypeRequestorFactory().createRequestor(match, pattern, searchRequestor);
160-
TypeInferencingVisitorWithRequestor visitor = factory.createVisitor(match);
161-
visitor.visitCompilationUnit(typeRequestor);
162-
163-
System.out.println("Matches found:\n" + searchRequestor.printMatches());
164-
165-
return searchRequestor.getMatches();
155+
return search(SearchPattern.createPattern(searchFor, IJavaSearchConstants.REFERENCES), unit);
166156
}
167157

168-
void checkMatches(List<SearchMatch> matches, int numExpected, String contents) {
169-
assertEquals("Wrong number matches found:\n" + searchRequestor.printMatches(), numExpected, matches.size());
170-
if (numExpected == 0) {
171-
return;
172-
}
173-
174-
Pattern p = Pattern.compile("doNothing");
175-
Matcher m = p.matcher(contents);
176-
Iterator<SearchMatch> matchIter = matches.iterator();
177-
while (m.find()) {
178-
SearchMatch match = matchIter.next();
179-
assertEquals("Wrong starting location for " + MockPossibleMatch.printMatch(match), m.start(), match.getOffset());
180-
assertEquals("Wrong length for " + MockPossibleMatch.printMatch(match), "doNothing".length(), match.getLength());
158+
private void checkMatches(final List<SearchMatch> matches, final int nExpected, final String contents) {
159+
assertEquals("Wrong number of matches found:\n" + toString(matches), nExpected, matches.size());
160+
if (nExpected > 0) {
161+
Iterator<SearchMatch> it = matches.iterator();
162+
Pattern p = Pattern.compile("doNothing");
163+
Matcher m = p.matcher(contents);
164+
while (m.find()) {
165+
SearchMatch match = it.next();
166+
assertEquals("Wrong starting location for " + toString(match), m.start(), match.getOffset());
167+
assertEquals("Wrong length for " + toString(match), "doNothing".length(), match.getLength());
168+
}
181169
}
182170
}
183171
}

base-test/org.eclipse.jdt.groovy.core.tests.builder/src/org/eclipse/jdt/core/groovy/tests/search/ConstructorReferenceSearchTests.java

+3-8
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import org.eclipse.jdt.core.search.IJavaSearchConstants;
2828
import org.eclipse.jdt.core.search.SearchEngine;
2929
import org.eclipse.jdt.core.search.SearchMatch;
30-
import org.eclipse.jdt.core.search.SearchParticipant;
3130
import org.eclipse.jdt.core.search.SearchPattern;
3231
import org.junit.Test;
3332

@@ -515,12 +514,8 @@ public void testNewifyConstructorReferences4() throws Exception {
515514

516515
//--------------------------------------------------------------------------
517516

518-
List<SearchMatch> searchForReferences(final IMethod method) throws CoreException {
519-
new SearchEngine().search(
520-
SearchPattern.createPattern(method, IJavaSearchConstants.REFERENCES),
521-
new SearchParticipant[] {SearchEngine.getDefaultSearchParticipant()},
522-
SearchEngine.createJavaSearchScope(new IJavaElement[] {JavaCore.create(project)}, false),
523-
searchRequestor, null);
524-
return searchRequestor.getMatches();
517+
private List<SearchMatch> searchForReferences(final IMethod method) throws CoreException {
518+
return search(SearchPattern.createPattern(method, IJavaSearchConstants.REFERENCES),
519+
SearchEngine.createJavaSearchScope(new IJavaElement[] {JavaCore.create(project)}));
525520
}
526521
}

0 commit comments

Comments
 (0)