|
| 1 | +package lombok.eclipse.references; |
| 2 | + |
| 3 | +import static org.junit.Assert.*; |
| 4 | + |
| 5 | +import java.util.List; |
| 6 | + |
| 7 | +import org.eclipse.core.runtime.CoreException; |
| 8 | +import org.eclipse.jdt.core.ICompilationUnit; |
| 9 | +import org.eclipse.jdt.core.IJavaElement; |
| 10 | +import org.eclipse.jdt.core.IType; |
| 11 | +import org.eclipse.jdt.core.JavaModelException; |
| 12 | +import org.eclipse.jdt.core.search.IJavaSearchConstants; |
| 13 | +import org.eclipse.jdt.core.search.SearchEngine; |
| 14 | +import org.eclipse.jdt.core.search.SearchMatch; |
| 15 | +import org.eclipse.jdt.core.search.SearchParticipant; |
| 16 | +import org.eclipse.jdt.core.search.SearchPattern; |
| 17 | +import org.eclipse.jdt.internal.corext.refactoring.CollectingSearchRequestor; |
| 18 | +import org.junit.Rule; |
| 19 | +import org.junit.Test; |
| 20 | +import org.junit.runner.RunWith; |
| 21 | + |
| 22 | +import lombok.eclipse.EclipseRunner; |
| 23 | +import lombok.eclipse.SetupSingleFileTest; |
| 24 | + |
| 25 | +@RunWith(EclipseRunner.class) |
| 26 | +public class FindReferencesTest { |
| 27 | + |
| 28 | + @Rule |
| 29 | + public SetupSingleFileTest setup = new SetupSingleFileTest(); |
| 30 | + |
| 31 | + @Test |
| 32 | + public void extensionMethod() throws Exception { |
| 33 | + ICompilationUnit extensionCu = setup.getPackageFragment().getCompilationUnit("Extension.java"); |
| 34 | + IType type = extensionCu.findPrimaryType(); |
| 35 | + List<SearchMatch> firstResult = searchInProject(type.getMethods()[0]); |
| 36 | + assertEquals(firstResult.size(), 2); |
| 37 | + |
| 38 | + ICompilationUnit usageCu = setup.getPackageFragment().getCompilationUnit("Usage.java"); |
| 39 | + List<SearchMatch> secondResult = searchInProject(usageCu.codeSelect(170, 0)[0]); |
| 40 | + assertEquals(secondResult.size(), 2); |
| 41 | + } |
| 42 | + |
| 43 | + private List<SearchMatch> searchInProject(IJavaElement element) throws CoreException, JavaModelException { |
| 44 | + CollectingSearchRequestor requestor = new CollectingSearchRequestor(); |
| 45 | + SearchEngine engine = new SearchEngine(); |
| 46 | + engine.search( |
| 47 | + SearchPattern.createPattern(element, IJavaSearchConstants.ALL_OCCURRENCES, SearchPattern.R_EXACT_MATCH), |
| 48 | + new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, |
| 49 | + SearchEngine.createJavaSearchScope(new IJavaElement[] { setup.getJavaProject() }), |
| 50 | + requestor, |
| 51 | + null |
| 52 | + ); |
| 53 | + |
| 54 | + return requestor.getResults(); |
| 55 | + } |
| 56 | +} |
0 commit comments