public void testCompilerOutputInheritance() throws Exception {
   File moduleFile = new File(getTestRoot(), "test.iml");
   Module module = createModule(moduleFile);
   final ModuleRootManagerImpl moduleRootManager =
       (ModuleRootManagerImpl) ModuleRootManager.getInstance(module);
   final ModifiableRootModel rootModel = moduleRootManager.getModifiableModel();
   rootModel.getModuleExtension(CompilerModuleExtension.class).inheritCompilerOutputPath(true);
   ApplicationManager.getApplication()
       .runWriteAction(
           new Runnable() {
             @Override
             public void run() {
               rootModel.commit();
             }
           });
   Element element = new Element("root");
   moduleRootManager.getState().writeExternal(element);
   assertElementEquals(
       element,
       "<root inherit-compiler-output=\"true\">"
           + "<exclude-output />"
           + "<orderEntry type=\"sourceFolder\" forTests=\"false\" />"
           + "</root>",
       module);
 }
 public void testEmptyModuleWrite() throws Exception {
   try {
     ModuleRootManagerImpl moduleRootManager = createTempModuleRootManager();
     Element root = new Element("root");
     moduleRootManager.getState().writeExternal(root);
     assertEquals(root.getText(), "");
   } catch (IOException e) {
     LOG.error(e);
   }
 }
  public void testContentWrite() throws Exception {
    File content = getTestRoot();
    File source = new File(content, "source");
    File testSource = new File(content, "testSource");
    File exclude = new File(content, "exclude");
    File classes = new File(content, "classes");
    File testClasses = new File(content, "testClasses");
    final VirtualFile contentFile = LocalFileSystem.getInstance().findFileByIoFile(content);
    assertNotNull(contentFile);
    final VirtualFile sourceFile = LocalFileSystem.getInstance().findFileByIoFile(source);
    assertNotNull(sourceFile);
    final VirtualFile testSourceFile = LocalFileSystem.getInstance().findFileByIoFile(testSource);
    assertNotNull(testSourceFile);
    final VirtualFile excludeFile = LocalFileSystem.getInstance().findFileByIoFile(exclude);

    assertNotNull(excludeFile);
    final VirtualFile classesFile = LocalFileSystem.getInstance().findFileByIoFile(classes);

    assertNotNull(classesFile);
    final VirtualFile testClassesFile = LocalFileSystem.getInstance().findFileByIoFile(testClasses);

    assertNotNull(testClassesFile);

    final File moduleFile = new File(content, "test.iml");
    final Module module = createModule(moduleFile);
    final ModuleRootManagerImpl moduleRootManager =
        (ModuleRootManagerImpl) ModuleRootManager.getInstance(module);

    PsiTestUtil.addContentRoot(module, contentFile);
    PsiTestUtil.addSourceRoot(module, sourceFile);
    PsiTestUtil.addSourceRoot(module, testSourceFile, true);
    ModuleRootModificationUtil.setModuleSdk(module, JavaSdkImpl.getMockJdk17());
    PsiTestUtil.addExcludedRoot(module, excludeFile);
    PsiTestUtil.setCompilerOutputPath(module, classesFile.getUrl(), false);
    PsiTestUtil.setCompilerOutputPath(module, testClassesFile.getUrl(), true);

    final Element element = new Element("root");
    moduleRootManager.getState().writeExternal(element);
    assertElementEquals(
        element,
        "<root inherit-compiler-output=\"false\">"
            + "<output url=\"file://$MODULE_DIR$/classes\" />"
            + "<output-test url=\"file://$MODULE_DIR$/testClasses\" />"
            + "<exclude-output />"
            + "<content url=\"file://$MODULE_DIR$\">"
            + "<sourceFolder url=\"file://$MODULE_DIR$/source\" isTestSource=\"false\" />"
            + "<sourceFolder url=\"file://$MODULE_DIR$/testSource\" isTestSource=\"true\" />"
            + "<excludeFolder url=\"file://$MODULE_DIR$/exclude\" />"
            + "</content>"
            + "<orderEntry type=\"jdk\" jdkName=\"java 1.7\" jdkType=\"JavaSDK\" />"
            + "<orderEntry type=\"sourceFolder\" forTests=\"false\" />"
            + "</root>",
        module);
  }
  public void testModuleLibraries() throws Exception {
    File moduleFile = new File(getTestRoot(), "test.iml");
    Module module = createModule(moduleFile);
    final ModuleRootManagerImpl moduleRootManager =
        (ModuleRootManagerImpl) ModuleRootManager.getInstance(module);
    final ModifiableRootModel rootModel = moduleRootManager.getModifiableModel();
    final LibraryTable moduleLibraryTable = rootModel.getModuleLibraryTable();

    final Library unnamedLibrary = moduleLibraryTable.createLibrary();
    final File unnamedLibClasses = new File(getTestRoot(), "unnamedLibClasses");
    final VirtualFile unnamedLibClassesRoot =
        LocalFileSystem.getInstance().findFileByIoFile(unnamedLibClasses);
    final Library.ModifiableModel libraryModifyableModel = unnamedLibrary.getModifiableModel();
    libraryModifyableModel.addRoot(unnamedLibClassesRoot.getUrl(), OrderRootType.CLASSES);

    final Library namedLibrary = moduleLibraryTable.createLibrary("namedLibrary");
    final File namedLibClasses = new File(getTestRoot(), "namedLibClasses");
    final VirtualFile namedLibClassesRoot =
        LocalFileSystem.getInstance().findFileByIoFile(namedLibClasses);
    final Library.ModifiableModel namedLibraryModel = namedLibrary.getModifiableModel();
    namedLibraryModel.addRoot(namedLibClassesRoot.getUrl(), OrderRootType.CLASSES);

    ApplicationManager.getApplication()
        .runWriteAction(
            new Runnable() {
              @Override
              public void run() {
                libraryModifyableModel.commit();
                namedLibraryModel.commit();
              }
            });

    final Iterator libraryIterator = moduleLibraryTable.getLibraryIterator();
    assertEquals(libraryIterator.next(), unnamedLibrary);
    assertEquals(libraryIterator.next(), namedLibrary);

    ApplicationManager.getApplication()
        .runWriteAction(
            new Runnable() {
              @Override
              public void run() {
                rootModel.commit();
              }
            });
    final Element element = new Element("root");
    moduleRootManager.getState().writeExternal(element);
    assertElementEquals(
        element,
        "<root inherit-compiler-output=\"true\">"
            + "<exclude-output />"
            + "<orderEntry type=\"sourceFolder\" forTests=\"false\" />"
            + "<orderEntry type=\"module-library\">"
            + "<library>"
            + "<CLASSES><root url=\"file://$MODULE_DIR$/unnamedLibClasses\" /></CLASSES>"
            + "<JAVADOC />"
            + "<SOURCES />"
            + "</library>"
            + "</orderEntry>"
            + "<orderEntry type=\"module-library\">"
            + "<library name=\"namedLibrary\">"
            + "<CLASSES><root url=\"file://$MODULE_DIR$/namedLibClasses\" /></CLASSES>"
            + "<JAVADOC />"
            + "<SOURCES />"
            + "</library>"
            + "</orderEntry>"
            + "</root>",
        module);
  }