/*
 * Copyright 2000-2012 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.idea.eclipse;

import com.intellij.ide.projectWizard.ProjectWizardTestCase;
import com.intellij.ide.util.projectWizard.ImportFromSourcesProvider;
import com.intellij.openapi.application.PluginPathManager;
import com.intellij.openapi.module.Module;
import com.intellij.openapi.util.io.FileUtil;
import org.jetbrains.idea.eclipse.importWizard.EclipseImportBuilder;
import org.jetbrains.idea.eclipse.importWizard.EclipseProjectImportProvider;

import java.io.File;
import java.io.IOException;

/**
 * @author Dmitry Avdeev
 *         Date: 11/7/12
 */
public class EclipseImportWizardTest extends ProjectWizardTestCase {

  public void testImportProject() throws Exception {
    Module module = doTest(".project");
    assertEquals("root", module.getName());
  }

  public void testImportClasspath() throws Exception {
    Module module = doTest(".classpath");
    assertEquals("root", module.getName());
  }

  public void testImportFromDirectory() throws Exception {
    Module module = doTest("");
    assertEquals("root", module.getName());
  }

  private Module doTest(final String fileName) throws IOException {
    copyTestData();
    return importProjectFrom(getProject().getBaseDir().getPath() + "/" + fileName, null,
                                      new EclipseProjectImportProvider(new EclipseImportBuilder()));
  }

  private void copyTestData() throws IOException {
    final File testRoot = new File(PluginPathManager.getPluginHomePath("eclipse") + "/testData", "import");
    FileUtil.copyDir(testRoot, new File(getProject().getBaseDir().getPath()));
  }

  public void testNothingToImport() throws Exception {
    try {
      File file = createTempFile("Foo.java", "class Foo {}");
      importProjectFrom(file.getParent(), null, new ImportFromSourcesProvider(), new EclipseProjectImportProvider(new EclipseImportBuilder()));
      fail("Exception should be frown");
    }
    catch (RuntimeException e) {
      assertEquals("Nothing found to import", e.getMessage());
    }
  }
}

@@ -896,24 +896,171 @@
 api.
-util.io.FileUtil
+roots.ModuleRootManager;%0Aimport com.intellij.openapi.util.io.FileUtil;%0Aimport com.intellij.openapi.vfs.LocalFileSystem;%0Aimport com.intellij.openapi.vfs.VirtualFile
 ;%0Aim
@@ -2358,23 +2358,33 @@
 test
-NothingToImport
+ImportingFromTwoProviders
 () t
@@ -2405,20 +2405,8 @@
 n %7B%0A
-    try %7B%0A  
     
@@ -2465,17 +2465,31 @@
 %22);%0A    
- 
+Module module =
  importP
@@ -2555,16 +2555,54 @@
 vider(),
+%0A                                     
  new Ecl
@@ -2665,152 +2665,236 @@
     
-  fail(%22Exception should be frown%22);%0A    %7D%0A    catch (RuntimeException e) %7B%0A      assertEquals(%22Nothing found to import%22, e.getMessage());%0A    %7D
+VirtualFile%5B%5D sourceRoots = ModuleRootManager.getInstance(module).getSourceRoots();%0A    assertEquals(1, sourceRoots.length);%0A    assertEquals(LocalFileSystem.getInstance().findFileByIoFile(file.getParentFile()), sourceRoots%5B0%5D);
 %0A  %7D

test fixed (cherry picked from commit a8fa72083db11a268179c331bda1e03ac0789512)