added tests for IDEA-142882
authornik <Nikolay.Chashnikov@jetbrains.com>
Thu, 20 Aug 2015 15:50:59 +0000 (18:50 +0300)
committernik <Nikolay.Chashnikov@jetbrains.com>
Fri, 21 Aug 2015 07:18:20 +0000 (10:18 +0300)
java/java-tests/testSrc/com/intellij/roots/ModuleRootManagerTestCase.java
java/java-tests/testSrc/com/intellij/roots/libraries/LibraryTest.java

index e81abaa8e6e993fa0753d65f36582d1a531f550e..a0c21b482f44d35b71e0ae345a52f32210849641 100644 (file)
@@ -14,6 +14,7 @@ import com.intellij.testFramework.ModuleTestCase;
 import com.intellij.testFramework.PsiTestUtil;
 import com.intellij.util.PathsList;
 import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
 
 import java.io.IOException;
 
@@ -93,10 +94,12 @@ public abstract class ModuleRootManagerTestCase extends ModuleTestCase {
     return output;
   }
 
-  protected Library createLibrary(final String name, final VirtualFile classesRoot, final VirtualFile sourceRoot) {
+  protected Library createLibrary(final String name, final @Nullable VirtualFile classesRoot, final @Nullable VirtualFile sourceRoot) {
     final Library library = LibraryTablesRegistrar.getInstance().getLibraryTable(myProject).createLibrary(name);
     final Library.ModifiableModel model = library.getModifiableModel();
-    model.addRoot(classesRoot, OrderRootType.CLASSES);
+    if (classesRoot != null) {
+      model.addRoot(classesRoot, OrderRootType.CLASSES);
+    }
     if (sourceRoot != null) {
       model.addRoot(sourceRoot, OrderRootType.SOURCES);
     }
index b81cc485d5c54bac5eb9bd8732ac9bf53fcb725e..2d6eab27cbc96eaab851e2818ee4710f968f2fb7 100644 (file)
@@ -5,6 +5,7 @@ import com.intellij.openapi.application.Result;
 import com.intellij.openapi.application.WriteAction;
 import com.intellij.openapi.roots.*;
 import com.intellij.openapi.roots.impl.libraries.LibraryEx;
+import com.intellij.openapi.roots.impl.libraries.LibraryTableBase;
 import com.intellij.openapi.roots.libraries.Library;
 import com.intellij.openapi.roots.libraries.LibraryTable;
 import com.intellij.openapi.roots.libraries.LibraryTablesRegistrar;
@@ -69,6 +70,22 @@ public class LibraryTest extends ModuleRootManagerTestCase {
     assertSameElements(getLibraries(), library);
   }
 
+  public void testFindLibraryByNameAfterRename() {
+    Library a = createLibrary("a", null, null);
+    assertSame(a, getLibraryTable().getLibraryByName("a"));
+    Library.ModifiableModel libraryModel = a.getModifiableModel();
+    libraryModel.setName("b");
+    commit(libraryModel);
+    assertSame(a, getLibraryTable().getLibraryByName("b"));
+  }
+
+  public void testReloadLibraryTable() {
+    ((LibraryTableBase)getLibraryTable()).loadState(new Element("component"));
+    createLibrary("a", null, null);
+    ((LibraryTableBase)getLibraryTable()).loadState(new Element("component").addContent(new Element("library").setAttribute("name", "b")));
+    assertEquals("b", assertOneElement(getLibraryTable().getLibraries()).getName());
+  }
+
   public void testResolveDependencyToRenamedLibrary() {
     Library library = createLibrary("jdom2", getJDomJar(), null);
 
@@ -98,7 +115,7 @@ public class LibraryTest extends ModuleRootManagerTestCase {
   }
 
   public void testNativePathSerialization() {
-    LibraryTable table = LibraryTablesRegistrar.getInstance().getLibraryTable(myProject);
+    LibraryTable table = getLibraryTable();
     Library library = table.createLibrary("native");
     Library.ModifiableModel model = library.getModifiableModel();
     model.addRoot("file://native-lib-root", NativeLibraryOrderRootType.getInstance());
@@ -112,8 +129,13 @@ public class LibraryTest extends ModuleRootManagerTestCase {
       element);
   }
 
+  @NotNull
+  private LibraryTable getLibraryTable() {
+    return LibraryTablesRegistrar.getInstance().getLibraryTable(myProject);
+  }
+
   public void testJarDirectoriesSerialization() {
-    LibraryTable table = LibraryTablesRegistrar.getInstance().getLibraryTable(myProject);
+    LibraryTable table = getLibraryTable();
     Library library = table.createLibrary("jarDirs");
     Library.ModifiableModel model = library.getModifiableModel();
     model.addJarDirectory("file://jar-dir", false, OrderRootType.CLASSES);