// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package com.intellij.roots.libraries;

import com.intellij.jarRepository.RepositoryLibraryType;
import com.intellij.openapi.application.ex.PathManagerEx;
import com.intellij.openapi.roots.impl.libraries.LibraryEx;
import com.intellij.openapi.roots.impl.libraries.LibraryTableBase;
import com.intellij.openapi.roots.libraries.LibraryTablesRegistrar;
import com.intellij.openapi.util.JDOMUtil;
import com.intellij.roots.ModuleRootManagerTestCase;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.idea.maven.utils.library.RepositoryLibraryProperties;

import java.io.IOException;

public class RepositoryLibrarySerializationTest extends ModuleRootManagerTestCase {
  public void testPlain() throws JDOMException, IOException {
    RepositoryLibraryProperties properties = loadLibrary("plain");
    assertEquals("junit", properties.getGroupId());
    assertEquals("junit", properties.getArtifactId());
    assertEquals("3.8.1", properties.getVersion());
    assertTrue(properties.isIncludeTransitiveDependencies());
    assertEmpty(properties.getExcludedDependencies());
  }

  public void testWithoutTransitiveDependencies() throws JDOMException, IOException {
    RepositoryLibraryProperties properties = loadLibrary("without-transitive-dependencies");
    assertFalse(properties.isIncludeTransitiveDependencies());
    assertEmpty(properties.getExcludedDependencies());
  }

  public void testWithExcludedDependencies() throws JDOMException, IOException {
    RepositoryLibraryProperties properties = loadLibrary("with-excluded-dependencies");
    assertTrue(properties.isIncludeTransitiveDependencies());
    assertSameElements(properties.getExcludedDependencies(), "org.apache.httpcomponents:httpclient");
  }

  @NotNull
  private RepositoryLibraryProperties loadLibrary(String name) throws JDOMException, IOException {
    String libraryPath = "jps/model-serialization/testData/repositoryLibraries/.idea/libraries/" + name + ".xml";
    Element element = JDOMUtil.load(PathManagerEx.findFileUnderCommunityHome(libraryPath));
    LibraryTableBase libraryTable = (LibraryTableBase)LibraryTablesRegistrar.getInstance().getLibraryTable(myProject);
    libraryTable.loadState(element);
    LibraryEx library = (LibraryEx)libraryTable.getLibraryByName(name);
    assertNotNull(library);
    assertSame(RepositoryLibraryType.REPOSITORY_LIBRARY_KIND, library.getKind());
    RepositoryLibraryProperties properties = (RepositoryLibraryProperties)library.getProperties();
    assertNotNull(properties);
    return properties;
  }
}

@@ -284,24 +284,69 @@
 hManagerEx;%0A
+import com.intellij.openapi.project.Project;%0A
 import com.i
@@ -419,37 +419,32 @@
 j.openapi.roots.
-impl.
 libraries.Librar
@@ -449,20 +449,16 @@
 aryTable
-Base
 ;%0Aimport
@@ -552,24 +552,115 @@
 pi.util.
-JDOMUtil
+io.FileUtil;%0Aimport com.intellij.openapi.vfs.LocalFileSystem;%0Aimport com.intellij.project.ProjectKt
 ;%0Aimport
@@ -710,33 +710,8 @@
 se;%0A
-import org.jdom.Element;%0A
 impo
@@ -851,24 +851,45 @@
 roperties;%0A%0A
+import java.io.File;%0A
 import java.
@@ -903,16 +903,43 @@
 ception;
+%0Aimport java.nio.file.Path;
 %0A%0Apublic
@@ -1016,16 +1016,124 @@
 tCase %7B%0A
+%0A  @NotNull%0A  @Override%0A  protected Path getProjectDirOrFile() %7B%0A    return getProjectDirOrFile(true);%0A  %7D%0A%0A
   public
@@ -2406,40 +2406,29 @@
     
-Element element = JDOMUtil.load(
+File librarySource = 
 Path
@@ -2480,65 +2480,265 @@
 ath)
-)
 ;%0A    
-LibraryTableBase libraryTable = (LibraryTableBase)
+File libraryFile = new File(myProject.getBasePath(), %22.idea/libraries/%22 + name + %22.xml%22);%0A    FileUtil.copy(librarySource, libraryFile);%0A    LocalFileSystem.getInstance().refreshAndFindFileByIoFile(libraryFile);%0A    LibraryTable projectLibraryTable = 
 Libr
@@ -2802,45 +2802,8 @@
 t);%0A
-    libraryTable.loadState(element);%0A
     
@@ -2833,17 +2833,24 @@
 braryEx)
-l
+projectL
 ibraryTa
@@ -3143,11 +3143,310 @@
 ies;%0A  %7D
+%0A%0A  @NotNull%0A  @Override%0A  protected Project doCreateProject(@NotNull Path projectFile) throws Exception %7B%0A    Project project = super.doCreateProject(projectFile);%0A    // Force loading libraries from disk%0A    ProjectKt.getStateStore(project).setOptimiseTestLoadSpeed(false);%0A    return project;%0A  %7D
 %0A%7D%0A

Rewrite RepositoryLibrarySerializationTest without using project module implementation details