fixed firing rootsChange event on changes in libraries and SDK (IDEA-121272)

This commit is contained in:
nik
2014-03-17 10:17:52 +04:00
parent ab5d41585e
commit 10659ced26
5 changed files with 51 additions and 14 deletions
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
@@ -16,14 +16,14 @@
package com.intellij.roots;
import com.intellij.ProjectTopics;
import com.intellij.openapi.application.ex.PathManagerEx;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.module.ModuleManager;
import com.intellij.openapi.roots.impl.ModifiableModelCommitter;
import com.intellij.openapi.projectRoots.ProjectJdkTable;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.projectRoots.SdkModificator;
import com.intellij.openapi.projectRoots.impl.JavaSdkImpl;
import com.intellij.openapi.roots.*;
import com.intellij.openapi.roots.impl.ModifiableModelCommitter;
import com.intellij.openapi.roots.libraries.Library;
import com.intellij.openapi.roots.libraries.LibraryTable;
import com.intellij.openapi.roots.libraries.LibraryTablesRegistrar;
@@ -68,16 +68,33 @@ public class RootsChangedTest extends ModuleTestCase {
verifyLibraryTableEditing(globalLibraryTable);
}
public void testProjectLibraryEventsInUncommitedModel() throws Exception {
public void testProjectLibraryEventsInUncommittedModel() throws Exception {
final LibraryTable projectLibraryTable = LibraryTablesRegistrar.getInstance().getLibraryTable(myProject);
verifyLibraryTableEditingInUncommitedModel(projectLibraryTable);
verifyLibraryTableEditingInUncommittedModel(projectLibraryTable);
}
public void testGlobalLibraryEventsInUncommitedModel() throws Exception {
public void testGlobalLibraryEventsInUncommittedModel() throws Exception {
final LibraryTable globalLibraryTable = LibraryTablesRegistrar.getInstance().getLibraryTable();
verifyLibraryTableEditingInUncommitedModel(globalLibraryTable);
verifyLibraryTableEditingInUncommittedModel(globalLibraryTable);
}
public void testEditLibraryForModuleLoadFromXml() throws IOException {
File moduleFile = PathManagerEx.findFileUnderProjectHome("java/java-tests/testData/moduleRootManager/rootsChanged/emptyModule/a.iml", getClass());
Module a = loadModule(moduleFile, true);
assertEventsCount(1);
final Sdk jdk = IdeaTestUtil.getMockJdk17();
ProjectJdkTable.getInstance().addJdk(jdk);
assertEventsCount(0);
ModuleRootModificationUtil.setModuleSdk(a, jdk);
assertEventsCount(1);
final SdkModificator sdkModificator = jdk.getSdkModificator();
sdkModificator.addRoot(getVirtualFile(createTempDirectory()), OrderRootType.CLASSES);
sdkModificator.commitChanges();
assertEventsCount(1);
}
public void testModuleJdkEditing() throws Exception {
final Module moduleA = createModule("a.iml");
@@ -93,9 +110,7 @@ public class RootsChangedTest extends ModuleTestCase {
rootModelA.setSdk(jdk);
rootModelB.setSdk(jdk);
ModifiableRootModel[] rootModels = new ModifiableRootModel[]{rootModelA, rootModelB};
if (rootModels.length > 0) {
ModifiableModelCommitter.multiCommit(rootModels, ModuleManager.getInstance(rootModels[0].getProject()).getModifiableModel());
}
ModifiableModelCommitter.multiCommit(rootModels, ModuleManager.getInstance(rootModels[0].getProject()).getModifiableModel());
assertEventsCount(1);
final SdkModificator sdkModificator = jdk.getSdkModificator();
@@ -192,7 +207,7 @@ public class RootsChangedTest extends ModuleTestCase {
assertEventsCount(1);
}
private void verifyLibraryTableEditingInUncommitedModel(final LibraryTable libraryTable) {
private void verifyLibraryTableEditingInUncommittedModel(final LibraryTable libraryTable) {
final Module moduleA = createModule("a.iml");
final Module moduleB = createModule("b.iml");
assertEventsCount(2);
@@ -96,10 +96,19 @@ public abstract class ModuleTestCase extends IdeaTestCase {
}
protected Module loadModule(final File moduleFile) {
return loadModule(moduleFile, false);
}
protected Module loadModule(final File moduleFile, final boolean loadComponentStates) {
Module module = ApplicationManager.getApplication().runWriteAction(
new Computable<Module>() {
@Override
public Module compute() {
ProjectImpl project = (ProjectImpl)myProject;
boolean oldOptimiseTestLoadSpeed = project.isOptimiseTestLoadSpeed();
if (loadComponentStates) {
project.setOptimiseTestLoadSpeed(false);
}
try {
LocalFileSystem.getInstance().refreshIoFiles(Collections.singletonList(moduleFile));
return ModuleManager.getInstance(myProject).loadModule(moduleFile.getAbsolutePath());
@@ -108,6 +117,9 @@ public abstract class ModuleTestCase extends IdeaTestCase {
LOG.error(e);
return null;
}
finally {
project.setOptimiseTestLoadSpeed(oldOptimiseTestLoadSpeed);
}
}
}
);
@@ -351,7 +351,7 @@ public class ModuleRootManagerImpl extends ModuleRootManager implements ModuleCo
protected void loadState(ModuleRootManagerState object, boolean throwEvent) {
try {
final RootModelImpl newModel = new RootModelImpl(object.getRootModelElement(), this, myProjectRootManager, myFilePointerManager);
final RootModelImpl newModel = new RootModelImpl(object.getRootModelElement(), this, myProjectRootManager, myFilePointerManager, throwEvent);
if (throwEvent) {
makeRootsChange(new Runnable() {
@@ -93,7 +93,7 @@ public class RootModelImpl extends RootModelBase implements ModifiableRootModel
RootModelImpl(@NotNull Element element,
@NotNull ModuleRootManagerImpl moduleRootManager,
ProjectRootManagerImpl projectRootManager,
VirtualFilePointerManager filePointerManager) throws InvalidDataException {
VirtualFilePointerManager filePointerManager, boolean writable) throws InvalidDataException {
myProjectRootManager = projectRootManager;
myFilePointerManager = filePointerManager;
myModuleRootManager = moduleRootManager;
@@ -123,8 +123,7 @@ public class RootModelImpl extends RootModelBase implements ModifiableRootModel
myOrderEntries.add(new ModuleSourceOrderEntryImpl(this));
}
myWritable = true;
myWritable = writable;
RootModelImpl originalRootModel = moduleRootManager.getRootModel();
for (ModuleExtension extension : originalRootModel.myExtensions) {