more ModuleRootModel modifications rewritten using util methods

This commit is contained in:
nik
2012-06-25 16:00:18 +04:00
parent b865966a15
commit 5a5abd8495
4 changed files with 32 additions and 102 deletions
@@ -11,7 +11,7 @@ import com.intellij.openapi.roots.*;
import com.intellij.openapi.util.Key;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.io.FileUtil;
import com.intellij.openapi.vfs.VfsUtil;
import com.intellij.openapi.vfs.VfsUtilCore;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileFilter;
import com.intellij.pom.java.LanguageLevel;
@@ -683,30 +683,8 @@ public class SrcRepositoryUseTest extends PsiTestCase{
psiFile.getText();
assertNotNull(aClass);
ApplicationManager.getApplication().runWriteAction(
new Runnable() {
@Override
public void run() {
VirtualFile newSourceRoot = psiFile.getVirtualFile().getParent();
final ModifiableRootModel rootModel = ModuleRootManager.getInstance(myModule).getModifiableModel();
final ContentEntry[] content = rootModel.getContentEntries();
boolean contentToChangeFound = false;
for (ContentEntry contentEntry : content) {
final SourceFolder[] sourceFolders = contentEntry.getSourceFolders();
for (SourceFolder sourceFolder : sourceFolders) {
contentEntry.removeSourceFolder(sourceFolder);
}
final VirtualFile contentRoot = contentEntry.getFile();
if (contentRoot != null && VfsUtil.isAncestor(contentRoot, newSourceRoot, false)) {
contentEntry.addSourceFolder(newSourceRoot, false);
contentToChangeFound = true;
}
}
assertTrue(contentToChangeFound);
rootModel.commit();
}
}
);
final VirtualFile newSourceRoot = psiFile.getVirtualFile().getParent();
replaceSourceRoot(newSourceRoot);
assertEquals("MyInterface1", aClass.getName());
}
@@ -715,11 +693,17 @@ public class SrcRepositoryUseTest extends PsiTestCase{
final PsiClass aClass = myJavaFacade.findClass("pack.MyInterface1", GlobalSearchScope.allScope(myProject));
assertNotNull(aClass);
final VirtualFile newSourceRoot = aClass.getContainingFile().getVirtualFile().getParent();
replaceSourceRoot(newSourceRoot);
assertEquals("MyInterface1", aClass.getName());
}
private void replaceSourceRoot(final VirtualFile newSourceRoot) {
ApplicationManager.getApplication().runWriteAction(
new Runnable() {
@Override
public void run() {
VirtualFile newSourceRoot = aClass.getContainingFile().getVirtualFile().getParent();
final ModifiableRootModel rootModel = ModuleRootManager.getInstance(myModule).getModifiableModel();
final ContentEntry[] content = rootModel.getContentEntries();
boolean contentToChangeFound = false;
@@ -729,7 +713,7 @@ public class SrcRepositoryUseTest extends PsiTestCase{
contentEntry.removeSourceFolder(sourceFolder);
}
final VirtualFile contentRoot = contentEntry.getFile();
if (contentRoot != null && VfsUtil.isAncestor(contentRoot, newSourceRoot, false)) {
if (contentRoot != null && VfsUtilCore.isAncestor(contentRoot, newSourceRoot, false)) {
contentEntry.addSourceFolder(newSourceRoot, false);
contentToChangeFound = true;
}
@@ -739,8 +723,6 @@ public class SrcRepositoryUseTest extends PsiTestCase{
}
}
);
assertEquals("MyInterface1", aClass.getName());
}
public void testParentIdAssert() throws Exception {
@@ -54,15 +54,7 @@ public class ManagingContentRootsTest extends IdeaTestCase {
final String url = root.getUrl();
root.delete(null);
ApplicationManager.getApplication().runWriteAction(new Runnable() {
@Override
public void run() {
ModifiableRootModel m = getRootManager().getModifiableModel();
m.addContentEntry(url);
m.commit();
}
});
addContentRoot(url);
assertNotNull(findContentEntry(url));
@@ -70,10 +62,7 @@ public class ManagingContentRootsTest extends IdeaTestCase {
assertSame(root, findContentEntry(url).getFile());
}
public void testCreationOfContentRootWithUrlWhenFileExists() throws IOException {
VirtualFile root = dir.createChildDirectory(null, "root");
final String url = root.getUrl();
private void addContentRoot(final String url) {
ApplicationManager.getApplication().runWriteAction(new Runnable() {
@Override
public void run() {
@@ -82,6 +71,13 @@ public class ManagingContentRootsTest extends IdeaTestCase {
m.commit();
}
});
}
public void testCreationOfContentRootWithUrlWhenFileExists() throws IOException {
VirtualFile root = dir.createChildDirectory(null, "root");
final String url = root.getUrl();
addContentRoot(url);
assertSame(root, findContentEntry(url).getFile());
@@ -31,10 +31,7 @@ import com.intellij.openapi.extensions.ExtensionPoint;
import com.intellij.openapi.extensions.Extensions;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.openapi.projectRoots.impl.JavaSdkImpl;
import com.intellij.openapi.roots.ContentEntry;
import com.intellij.openapi.roots.LanguageLevelProjectExtension;
import com.intellij.openapi.roots.ModifiableRootModel;
import com.intellij.openapi.roots.ModuleRootManager;
import com.intellij.openapi.vfs.LocalFileSystem;
import com.intellij.openapi.vfs.VfsUtilCore;
import com.intellij.openapi.vfs.VirtualFile;
@@ -153,23 +150,15 @@ public abstract class InspectionTestCase extends PsiTestCase {
if (sourceDir[0] == null) {
sourceDir[0] = projectDir;
}
final ModuleRootManager rootManager = ModuleRootManager.getInstance(myModule);
final ModifiableRootModel rootModel = rootManager.getModifiableModel();
rootModel.clear();
// configure source and output path
final ContentEntry contentEntry = rootModel.addContentEntry(projectDir);
contentEntry.addSourceFolder(sourceDir[0], false);
ext_src = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(new File(testDir + "/ext_src"));
if (ext_src != null) {
contentEntry.addSourceFolder(ext_src, false);
}
// IMPORTANT! The jdk must be obtained in a way it is obtained in the normal program!
//ProjectJdkEx jdk = ProjectJdkTable.getInstance().getInternalJdk();
rootModel.setSdk(getTestProjectSdk());
rootModel.commit();
PsiTestUtil.removeAllRoots(myModule, getTestProjectSdk());
PsiTestUtil.addContentRoot(myModule, projectDir);
PsiTestUtil.addSourceRoot(myModule, sourceDir[0]);
ext_src = LocalFileSystem.getInstance().refreshAndFindFileByIoFile(new File(testDir + "/ext_src"));
if (ext_src != null) {
PsiTestUtil.addSourceRoot(myModule, ext_src);
}
}
@Override
@@ -160,12 +160,12 @@ public abstract class IntegrationTestCase extends PlatformTestCase {
addContentRoot(myModule, path);
}
protected void addContentRoot(final Module module, final String path) {
protected static void addContentRoot(final Module module, final String path) {
ApplicationManager.getApplication().runWriteAction(new Runnable() {
public void run() {
ModuleRootManager rm = ModuleRootManager.getInstance(module);
ModifiableRootModel m = rm.getModifiableModel();
m.addContentEntry(VfsUtil.pathToUrl(FileUtil.toSystemIndependentName(path)));
m.addContentEntry(VfsUtilCore.pathToUrl(FileUtil.toSystemIndependentName(path)));
m.commit();
}
});
@@ -178,14 +178,14 @@ public abstract class IntegrationTestCase extends PlatformTestCase {
ModifiableRootModel m = rm.getModifiableModel();
for (ContentEntry e : m.getContentEntries()) {
if (e.getFile() != myRoot) continue;
e.addExcludeFolder(VfsUtil.pathToUrl(FileUtil.toSystemIndependentName(path)));
e.addExcludeFolder(VfsUtilCore.pathToUrl(FileUtil.toSystemIndependentName(path)));
}
m.commit();
}
});
}
protected void addFileListenerDuring(VirtualFileListener l, Runnable r) throws Exception {
protected static void addFileListenerDuring(VirtualFileListener l, Runnable r) throws Exception {
VirtualFileManager.getInstance().addVirtualFileListener(l);
try {
r.run();
@@ -196,44 +196,7 @@ public abstract class IntegrationTestCase extends PlatformTestCase {
}
protected void assertContent(String expected, Entry e) {
protected static void assertContent(String expected, Entry e) {
assertEquals(expected, new String(e.getContent().getBytes()));
}
protected class ContentChangesListener extends VirtualFileAdapter {
private final VirtualFile myFile;
private final String[] myContents = new String[2];
public ContentChangesListener(VirtualFile f) {
myFile = f;
}
public String getContentBefore() {
return myContents[0];
}
public String getContentAfter() {
return myContents[1];
}
@Override
public void beforeContentsChange(VirtualFileEvent e) {
logContent(e, 0);
}
@Override
public void contentsChanged(VirtualFileEvent e) {
logContent(e, 1);
}
private void logContent(VirtualFileEvent e, int i) {
try {
if (!e.getFile().equals(myFile)) return;
myContents[i] = new String(myFile.contentsToByteArray());
}
catch (IOException ex) {
throw new RuntimeException(ex);
}
}
}
}