2 * Copyright 2000-2012 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.psi.resolve
18 import com.intellij.openapi.application.ex.PathManagerEx
19 import com.intellij.openapi.vfs.LocalFileSystem
20 import com.intellij.psi.JavaPsiFacade
21 import com.intellij.psi.search.GlobalSearchScope
22 import com.intellij.testFramework.PsiTestUtil
23 import com.intellij.testFramework.fixtures.JavaCodeInsightFixtureTestCase
28 class ResolveInLibrariesTest extends JavaCodeInsightFixtureTestCase {
30 public void "test prefer current library when navigation from its source"() {
31 def lib = LocalFileSystem.getInstance().refreshAndFindFileByPath(PathManagerEx.getTestDataPath() + "/../../../lib")
32 def nanoJar = lib.children.find { it.name.startsWith("nanoxml") }
33 def nanoSrc = lib.findChild("src").children.find { it.name.startsWith("nanoxml") }
35 def jarCopy = myFixture.copyFileToProject(nanoJar.path, 'lib/nanoJar.jar')
36 def srcCopy = myFixture.copyFileToProject(nanoSrc.path, 'lib/nanoSrc.zip')
38 PsiTestUtil.addLibrary(myModule, 'nano1', lib.path, ["/$nanoJar.name!/"] as String[], ["/src/$nanoSrc.name!/"] as String[])
39 PsiTestUtil.addLibrary(myModule, 'nano2', jarCopy.parent.path, ["/$jarCopy.name!/"] as String[], ["/$srcCopy.name!/"] as String[])
41 def parsers = JavaPsiFacade.getInstance(project).findClasses('net.n3.nanoxml.IXMLParser', GlobalSearchScope.allScope(project))
42 assert parsers.size() == 2
44 def file0 = parsers[0].navigationElement.containingFile
45 assert file0.virtualFile.path.startsWith(nanoSrc.path)
46 assert file0.findReferenceAt(file0.text.indexOf('IXMLReader reader')).resolve().navigationElement.containingFile.virtualFile.path.startsWith(nanoSrc.path)
48 def file1 = parsers[1].navigationElement.containingFile
49 assert file1.virtualFile.path.startsWith(srcCopy.path)
50 assert file1.findReferenceAt(file1.text.indexOf('IXMLReader reader')).resolve().navigationElement.containingFile.virtualFile.path.startsWith(srcCopy.path)
54 public void "test inheritance transitivity"() {
55 def lib = LocalFileSystem.getInstance().refreshAndFindFileByPath(PathManagerEx.getTestDataPath() + "/../../../lib")
56 def protoJar = lib.children.find { it.name.startsWith("protobuf") }
58 def jarCopy = myFixture.copyFileToProject(protoJar.path, 'lib/protoJar.jar')
60 PsiTestUtil.addLibrary(myModule, 'proto1', lib.path, ["/$protoJar.name!/"] as String[], [] as String[])
61 PsiTestUtil.addLibrary(myModule, 'proto2', jarCopy.parent.path, ["/$jarCopy.name!/"] as String[], [] as String[])
63 def scope = GlobalSearchScope.allScope(project)
65 def bottoms = JavaPsiFacade.getInstance(project).findClasses('com.google.protobuf.AbstractMessage', scope)
66 assert bottoms.size() == 2
68 def middles = JavaPsiFacade.getInstance(project).findClasses('com.google.protobuf.AbstractMessageLite', scope)
69 assert middles.size() == 2
71 def intfs = JavaPsiFacade.getInstance(project).findClasses('com.google.protobuf.MessageLite', scope)
72 assert intfs.size() == 2
75 assert middles[i].isInheritor(intfs[i], true)
76 assert bottoms[i].isInheritor(intfs[i], true)
77 assert bottoms[i].isInheritor(middles[i], true)
80 for (deep in [false, true]) {
82 assert !middles[i].isInheritor(intfs[1-i], deep)
83 assert !bottoms[i].isInheritor(intfs[1-i], deep)
84 assert !bottoms[i].isInheritor(middles[1-i], deep)