IDEA-51749 "Could not save project" error message may occur after deleting module...
[fedora-idea.git] / java / java-tests / testSrc / com / intellij / module / ModulePointerTest.java
blob32a944d3f4df3e27d135bd94fb32e6692a0c160c
1 /*
2 * Copyright 2000-2009 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.module;
18 import com.intellij.openapi.Disposable;
19 import com.intellij.openapi.application.Result;
20 import com.intellij.openapi.application.WriteAction;
21 import com.intellij.openapi.module.*;
22 import com.intellij.testFramework.PlatformTestCase;
24 /**
25 * @author nik
27 public class ModulePointerTest extends PlatformTestCase {
28 public void testCreateByName() throws Exception {
29 final ModulePointer pointer = getPointerManager().create("m");
30 assertSame(pointer, getPointerManager().create("m"));
31 assertNull(pointer.getModule());
32 assertEquals("m", pointer.getModuleName());
34 final Module module = addModule("m");
36 assertSame(module, pointer.getModule());
37 assertEquals("m", pointer.getModuleName());
40 public void testCreateByModule() throws Exception {
41 final Module module = addModule("x");
42 final ModulePointer pointer = getPointerManager().create(module);
43 assertSame(pointer, getPointerManager().create(module));
44 assertSame(pointer, getPointerManager().create("x"));
45 assertSame(module, pointer.getModule());
46 assertEquals("x", pointer.getModuleName());
48 ModifiableModuleModel model = getModuleManager().getModifiableModel();
49 model.disposeModule(module);
50 commitModel(model);
52 assertNull(pointer.getModule());
53 assertEquals("x", pointer.getModuleName());
55 final Module newModule = addModule("x");
56 assertSame(pointer, getPointerManager().create(newModule));
59 public void testRenameModule() throws Exception {
60 final ModulePointer pointer = getPointerManager().create("abc");
61 final Module module = addModule("abc");
62 ModifiableModuleModel model = getModuleManager().getModifiableModel();
63 model.renameModule(module, "xyz");
64 commitModel(model);
65 assertSame(module, pointer.getModule());
66 assertEquals("xyz", pointer.getModuleName());
69 public void testDisposePointerFromUncommitedModifiableModel() throws Exception {
70 final ModulePointer pointer = getPointerManager().create("xxx");
72 final ModifiableModuleModel modifiableModel = getModuleManager().getModifiableModel();
73 final Module module = modifiableModel.newModule(myProject.getBaseDir().getPath() + "/xxx.iml", EmptyModuleType.getInstance());
74 assertSame(pointer, getPointerManager().create(module));
75 assertSame(pointer, getPointerManager().create("xxx"));
77 assertSame(module, pointer.getModule());
78 assertEquals("xxx", pointer.getModuleName());
80 modifiableModel.dispose();
82 assertNull(pointer.getModule());
83 assertEquals("xxx", pointer.getModuleName());
86 private ModuleManager getModuleManager() {
87 return ModuleManager.getInstance(myProject);
90 private Module addModule(final String name) {
91 final ModifiableModuleModel model = getModuleManager().getModifiableModel();
92 final Module module = model.newModule(myProject.getBaseDir().getPath() + "/" + name + ".iml", EmptyModuleType.getInstance());
93 commitModel(model);
94 disposeOnTearDown(new Disposable() {
95 public void dispose() {
96 if (!module.isDisposed()) {
97 getModuleManager().disposeModule(module);
101 return module;
104 private static void commitModel(final ModifiableModuleModel model) {
105 new WriteAction() {
106 protected void run(final Result result) {
107 model.commit();
109 }.execute();
112 private ModulePointerManager getPointerManager() {
113 return ModulePointerManager.getInstance(myProject);