// 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 org.jetbrains.plugins.gradle.importing;

import com.intellij.compiler.CompilerConfiguration;
import com.intellij.compiler.CompilerConfigurationImpl;
import com.intellij.compiler.CompilerWorkspaceConfiguration;
import com.intellij.compiler.impl.javaCompiler.javac.JavacConfiguration;
import com.intellij.packaging.artifacts.Artifact;
import com.intellij.packaging.artifacts.ArtifactManager;
import org.jetbrains.jps.model.java.compiler.JpsJavaCompilerOptions;
import org.junit.Test;

/**
 * @author Vladislav.Soroka
 */
public class GradleJavaSettingsImportingTest extends GradleSettingsImportingTest {
  @Test
  public void testCompilerConfigurationSettingsImport() throws Exception {

    importProject(
      withGradleIdeaExtPlugin(
        "idea {\n" +
        "  project.settings {\n" +
        "    compiler {\n" +
        "      resourcePatterns '!*.java;!*.class'\n" +
        "      clearOutputDirectory false\n" +
        "      addNotNullAssertions false\n" +
        "      autoShowFirstErrorInEditor false\n" +
        "      displayNotificationPopup false\n" +
        "      enableAutomake false\n" +
        "      parallelCompilation true\n" +
        "      rebuildModuleOnDependencyChange false\n" +
        "      javac {\n" +
        "        preferTargetJDKCompiler false\n" +
        "        javacAdditionalOptions '-Dkey=val'\n" +
        "        generateNoWarnings true\n" +
        "      }\n" +
        "    }\n" +
        "  }\n" +
        "}")
    );

    final CompilerConfigurationImpl compilerConfiguration = (CompilerConfigurationImpl)CompilerConfiguration.getInstance(myProject);
    final CompilerWorkspaceConfiguration workspaceConfiguration = CompilerWorkspaceConfiguration.getInstance(myProject);

    assertSameElements(compilerConfiguration.getResourceFilePatterns(), "!*.class", "!*.java");
    assertFalse(workspaceConfiguration.CLEAR_OUTPUT_DIRECTORY);
    assertFalse(compilerConfiguration.isAddNotNullAssertions());
    assertFalse(workspaceConfiguration.AUTO_SHOW_ERRORS_IN_EDITOR);
    assertFalse(workspaceConfiguration.DISPLAY_NOTIFICATION_POPUP);
    assertFalse(workspaceConfiguration.MAKE_PROJECT_ON_SAVE);
    assertTrue(workspaceConfiguration.PARALLEL_COMPILATION);
    assertFalse(workspaceConfiguration.REBUILD_ON_DEPENDENCY_CHANGE);

    final JpsJavaCompilerOptions javacOpts = JavacConfiguration.getOptions(myProject, JavacConfiguration.class);
    assertFalse(javacOpts.PREFER_TARGET_JDK_COMPILER);
    assertEquals("-Dkey=val", javacOpts.ADDITIONAL_OPTIONS_STRING);
    assertTrue(javacOpts.GENERATE_NO_WARNINGS);
  }

  @Test
  public void testArtifactsSettingsImport() throws Exception {
    importProject(
      withGradleIdeaExtPlugin(
        "import org.jetbrains.gradle.ext.*\n" +
        "idea {\n" +
        "  project.settings {\n" +
        "    ideArtifacts {\n" +
        "      ideArtifact(\"myArt\") {\n" +
        "         file(\"build.gradle\")\n" +
        "      }\n" +
        "    }\n" +
        "  }\n" +
        "}")
    );
    final ArtifactManager artifactManager = ArtifactManager.getInstance(myProject);

    final Artifact artifact = artifactManager.getArtifacts()[0];
    assertEquals("myArt", artifact.getName());
  }
}

@@ -2799,37 +2799,43 @@
 n %7B%0A    
-import
+create
 Project
+Config
 (%0A      
 withGrad
@@ -2830,32 +2830,113 @@
     
+ 
-withGradleIdeaExtPlugin(
+ %22plugins %7B%5Cn%22 +%0A        %22  id 'org.jetbrains.gradle.plugin.idea-ext' version '0.4'%5Cn%22 +%0A        %22%7D%5Cn%22 +
 %0A   
@@ -3088,30 +3088,13 @@
     
-ideArtifact(%5C%22
 myArt
-%5C%22)
  %7B%5Cn
@@ -3214,22 +3214,42 @@
      %22%7D%22
-)
 %0A    
+);%0A    importProject(
 );%0A    f

switch test data to remote repository content