move some tests to the platform
[idea/community.git] / java / java-tests / testSrc / com / intellij / psi / LibraryOrderTest.java
1 package com.intellij.psi;
2
3 import com.intellij.JavaTestUtil;
4 import com.intellij.openapi.application.ApplicationManager;
5 import com.intellij.openapi.roots.*;
6 import com.intellij.openapi.roots.libraries.Library;
7 import com.intellij.openapi.roots.libraries.LibraryTable;
8 import com.intellij.openapi.vfs.LocalFileSystem;
9 import com.intellij.openapi.vfs.VirtualFile;
10 import com.intellij.testFramework.PsiTestCase;
11
12 import java.io.File;
13 import java.util.Arrays;
14 import java.util.List;
15
16 /**
17  *  @author dsl
18  */
19 public class LibraryOrderTest extends PsiTestCase {
20
21   public void test1() {
22     setupPaths();
23     checkClassFromLib("test.A", "1");
24
25     final ModifiableRootModel rootModel = ModuleRootManager.getInstance(myModule).getModifiableModel();
26     final OrderEntry[] order = rootModel.getOrderEntries();
27     final int length = order.length;
28     OrderEntry lib2 = order[length - 1];
29     OrderEntry lib1 = order[length - 2];
30     assertTrue(lib1 instanceof LibraryOrderEntry);
31     assertEquals("lib1", ((LibraryOrderEntry) lib1).getLibraryName());
32     assertTrue(lib2 instanceof LibraryOrderEntry);
33     assertEquals("lib2", ((LibraryOrderEntry) lib2).getLibraryName());
34
35     order[length - 1] = lib1;
36     order[length - 2] = lib2;
37     rootModel.rearrangeOrderEntries(order);
38
39     ApplicationManager.getApplication().runWriteAction(new Runnable() {
40       @Override
41       public void run() {
42         rootModel.commit();
43       }
44     }
45     );
46
47     checkClassFromLib("test.A", "2");
48   }
49
50   public void testNavigation() throws Exception {
51     setupPaths();
52     final JavaPsiFacade psiManager = getJavaFacade();
53     final PsiClass classA = psiManager.findClass("test.A");
54     final PsiElement navigationElement = classA.getNavigationElement();
55     assertNotNull(navigationElement);
56     assertTrue(navigationElement != classA);
57     assertEquals("A.java", navigationElement.getContainingFile().getVirtualFile().getName());
58   }
59
60   private void checkClassFromLib(String qualifiedName, String index) {
61     final PsiClass classA = (PsiClass)JavaPsiFacade.getInstance(myProject).findClass(qualifiedName).getNavigationElement();
62     assertNotNull(classA);
63     final PsiMethod[] methodsA = classA.getMethods();
64     assertEquals(1, methodsA.length);
65     assertEquals("methodOfClassFromLib" + index, methodsA[0].getName());
66   }
67
68   public void setupPaths() {
69     final String basePath = JavaTestUtil.getJavaTestDataPath() + "/psi/libraryOrder/";
70
71     final VirtualFile lib1SrcFile = refreshAndFindFile(basePath + "lib1/src");
72     final VirtualFile lib1classes = refreshAndFindFile(basePath + "lib1/classes");
73     final VirtualFile lib2SrcFile = refreshAndFindFile(basePath + "lib2/src");
74     final VirtualFile lib2classes = refreshAndFindFile(basePath + "lib2/classes");
75
76     assertTrue(lib1SrcFile != null);
77     assertTrue(lib2SrcFile != null);
78
79     final ModifiableRootModel rootModel = ModuleRootManager.getInstance(myModule).getModifiableModel();
80     final LibraryTable libraryTable = rootModel.getModuleLibraryTable();
81
82     addLibraryWithSourcePath("lib1", libraryTable, lib1SrcFile, lib1classes);
83     addLibraryWithSourcePath("lib2", libraryTable, lib2SrcFile, lib2classes);
84
85     ApplicationManager.getApplication().runWriteAction(new Runnable() {
86       @Override
87       public void run() {
88         rootModel.commit();
89       }
90     });
91     final List<VirtualFile> list = Arrays.asList(OrderEnumerator.orderEntries(myModule).getClassesRoots());
92     assertTrue(list.contains(lib1classes));
93     assertTrue(list.contains(lib2classes));
94   }
95
96   private VirtualFile refreshAndFindFile(String path) {
97     final File ioLib1Src = new File(path);
98     final VirtualFile lib1SrcFile = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(ioLib1Src);
99     return lib1SrcFile;
100   }
101
102   private void addLibraryWithSourcePath(String name, final LibraryTable libraryTable, final VirtualFile libSource,
103                                         VirtualFile libClasses) {
104     final Library lib = libraryTable.createLibrary(name);
105     final Library.ModifiableModel libModel = lib.getModifiableModel();
106     libModel.addRoot(libClasses, OrderRootType.CLASSES);
107     libModel.addRoot(libSource, OrderRootType.SOURCES);
108     libModel.commit();
109   }
110 }