/*
 * Copyright 2000-2015 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jetbrains.plugins.gradle.importing;

import com.intellij.compiler.CompilerConfiguration;
import com.intellij.openapi.roots.*;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.util.PathUtil;
import org.gradle.util.GradleVersion;
import org.junit.Test;
import org.junit.runners.Parameterized;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

/**
 * @author Vladislav.Soroka
 * @since 10/27/2015
 */
@SuppressWarnings("JUnit4AnnotatedMethodInJUnit3TestCase")
public class GradleMiscImportingTest extends GradleImportingTestCase {

  /**
   * It's sufficient to run the test against one gradle version
   */
  @SuppressWarnings("MethodOverridesStaticMethodOfSuperclass")
  @Parameterized.Parameters(name = "with Gradle-{0}")
  public static Collection<Object[]> data() throws Throwable {
    return Arrays.asList(new Object[][]{{BASE_GRADLE_VERSION}});
  }


  @Test
  public void testInheritProjectJdkForModules() throws Exception {
    importProject(
      "apply plugin: 'java'"
    );

    assertModules("project", "project_main", "project_test");
    assertTrue(ModuleRootManager.getInstance(getModule("project")).isSdkInherited());
    assertTrue(ModuleRootManager.getInstance(getModule("project_main")).isSdkInherited());
    assertTrue(ModuleRootManager.getInstance(getModule("project_test")).isSdkInherited());
  }

  @Test
  public void testLanguageLevel() throws Exception {
    importProject(
      "apply plugin: 'java'\n" +
      "sourceCompatibility = 1.5\n" +
      "apply plugin: 'java'\n" +
      "compileTestJava {\n" +
      "  sourceCompatibility = 1.8\n" +
      "}\n"
    );

    assertModules("project", "project_main", "project_test");
    assertEquals(LanguageLevel.JDK_1_5, getLanguageLevelForModule("project_main"));
    assertEquals(LanguageLevel.JDK_1_8, getLanguageLevelForModule("project_test"));
  }

  @Test
  public void testTargetLevel() throws Exception {
    importProject(
      "apply plugin: 'java'\n" +
      "targetCompatibility = 1.8\n" +
      "compileJava {\n" +
      "  targetCompatibility = 1.5\n" +
      "}\n"
    );

    assertModules("project", "project_main", "project_test");
    assertEquals("1.5", getBytecodeTargetLevel("project_main"));
    assertEquals("1.8", getBytecodeTargetLevel("project_test"));

  }

  private LanguageLevel getLanguageLevelForModule(final String moduleName) {
    return LanguageLevelModuleExtensionImpl.getInstance(getModule(moduleName)).getLanguageLevel();
  }

  private String getBytecodeTargetLevel(String moduleName) {
    return CompilerConfiguration.getInstance(myProject).getBytecodeTargetLevel(getModule(moduleName));
  }
}

@@ -727,15 +727,21 @@
 api.
-roots.*
+module.Module
 ;%0Aim
@@ -770,140 +770,199 @@
 api.
-vfs.VirtualFile;%0Aimport com.intellij.pom.java.LanguageLevel;%0Aimport com.intellij.util.PathUtil;%0Aimport org.gradle.util.GradleVersion
+roots.LanguageLevelModuleExtensionImpl;%0Aimport com.intellij.openapi.roots.ModuleRootManager;%0Aimport com.intellij.openapi.roots.TestModuleProperties;%0Aimport com.intellij.pom.java.LanguageLevel
 ;%0Aim
@@ -1080,31 +1080,8 @@
 ion;
-%0Aimport java.util.List;
 %0A%0A/*
@@ -1592,16 +1592,582 @@
 );%0A  %7D%0A%0A
+  @Test%0A  public void testTestModuleProperties() throws Exception %7B%0A    importProject(%0A      %22apply plugin: 'java'%22%0A    );%0A%0A    assertModules(%22project%22, %22project_main%22, %22project_test%22);%0A%0A    final Module testModule = getModule(%22project_test%22);%0A    TestModuleProperties testModuleProperties = TestModuleProperties.getInstance(testModule);%0A    assertEquals(%22project_main%22, testModuleProperties.getProductionModuleName());%0A%0A    final Module productionModule = getModule(%22project_main%22);%0A    assertSame(productionModule, testModuleProperties.getProductionModule());%0A  %7D%0A
 %0A  @Test

test added for TestModuleProperties importing