Merge branch 'idea90' of git@git.labs.intellij.net:idea/community into 90
[fedora-idea.git] / platform / lang-impl / testSrc / com / intellij / module / ModulePointerTest.java
bloba05e17a2d2a2655c3b1076e82383436fb90a14f0
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 assertNull(pointer.getModule());
31 assertEquals("m", pointer.getModuleName());
33 final Module module = addModule("m");
35 assertSame(module, pointer.getModule());
36 assertEquals("m", pointer.getModuleName());
39 public void testCreateByModule() throws Exception {
40 final Module module = addModule("x");
41 final ModulePointer pointer = getPointerManager().create(module);
42 assertSame(module, pointer.getModule());
43 assertEquals("x", pointer.getModuleName());
45 ModifiableModuleModel model = getModuleManager().getModifiableModel();
46 model.disposeModule(module);
47 commitModel(model);
49 assertNull(pointer.getModule());
50 assertEquals("x", pointer.getModuleName());
53 public void testRenameModule() throws Exception {
54 final ModulePointer pointer = getPointerManager().create("abc");
55 final Module module = addModule("abc");
56 ModifiableModuleModel model = getModuleManager().getModifiableModel();
57 model.renameModule(module, "xyz");
58 commitModel(model);
59 assertSame(module, pointer.getModule());
60 assertEquals("xyz", pointer.getModuleName());
63 public void testDisposePointerFromUncommitedModifiableModel() throws Exception {
64 final ModifiableModuleModel modifiableModel = getModuleManager().getModifiableModel();
65 final Module module = modifiableModel.newModule(myProject.getBaseDir().getPath() + "/xxx.iml", EmptyModuleType.getInstance());
66 final ModulePointer pointer = getPointerManager().create(module);
68 assertSame(module, pointer.getModule());
69 assertEquals("xxx", pointer.getModuleName());
71 modifiableModel.dispose();
73 assertNull(pointer.getModule());
74 assertEquals("xxx", pointer.getModuleName());
77 private ModuleManager getModuleManager() {
78 return ModuleManager.getInstance(myProject);
81 private Module addModule(final String name) {
82 final ModifiableModuleModel model = getModuleManager().getModifiableModel();
83 final Module module = model.newModule(myProject.getBaseDir().getPath() + "/" + name + ".iml", EmptyModuleType.getInstance());
84 commitModel(model);
85 disposeOnTearDown(new Disposable() {
86 public void dispose() {
87 if (!module.isDisposed()) {
88 getModuleManager().disposeModule(module);
91 });
92 return module;
95 private static void commitModel(final ModifiableModuleModel model) {
96 new WriteAction() {
97 protected void run(final Result result) {
98 model.commit();
100 }.execute();
103 private ModulePointerManager getPointerManager() {
104 return ModulePointerManager.getInstance(myProject);