/*******************************************************************************
 * Copyright 2011 Google Inc. All Rights Reserved.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * 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 com.google.gdt.eclipse.suite.launch.processors;

import com.google.gdt.eclipse.core.ClasspathUtilities.ClassFinder;
import com.google.gdt.eclipse.core.launch.LaunchConfigurationProcessorTestingHelper;
import com.google.gdt.eclipse.core.launch.LaunchConfigurationProcessorUtilities;
import com.google.gdt.eclipse.core.properties.WebAppProjectProperties;
import com.google.gwt.eclipse.core.nature.GWTNature;
import com.google.gwt.eclipse.core.projects.GwtEnablingProjectCreationParticipant;
import com.google.gwt.eclipse.testing.GwtRuntimeTestUtilities;
import com.google.gwt.eclipse.testing.GwtTestUtilities;

import junit.framework.TestCase;

import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.launching.IJavaLaunchConfigurationConstants;

/**
 * Tests the {@link MainTypeProcessor}.
 */
public class MainTypeProcessorTest extends TestCase {

  private final LaunchConfigurationProcessorTestingHelper helper = new LaunchConfigurationProcessorTestingHelper();

  @Override
  public void setUp() throws Exception {
    GwtTestUtilities.setUp();

    GwtRuntimeTestUtilities.addDefaultRuntime();

    helper.setUp(MainTypeProcessorTest.class.getSimpleName(),
        new GwtEnablingProjectCreationParticipant());
  }

  public void testGaeOnly() throws Exception {
    GWTNature.removeNatureFromProject(helper.getProject());
    new MainTypeProcessor(new ClassFinder()).update(helper.getLaunchConfig(),
        JavaCore.create(helper.getProject()), null, null);
    assertEquals(
        LaunchConfigurationProcessorUtilities.getMainTypeName(helper.getLaunchConfig()),
        MainTypeProcessor.MainType.GAE_APP_SERVER.mainTypeName);
  }

  public void testGwtCapabilityDetection() throws Exception {
    // Ensure it chooses dev mode if the class is around
    new MainTypeProcessor(new ClassFinder() {
      @Override
      public boolean exists(ClassLoader classLoader, String className) {
        // GWT 2.0+ include all classes
        return true;
      }
    }).update(helper.getLaunchConfig(), JavaCore.create(helper.getProject()), null, null);
    assertEquals(
        LaunchConfigurationProcessorUtilities.getMainTypeName(helper.getLaunchConfig()),
        MainTypeProcessor.MainType.GWT_DEV_MODE.mainTypeName);

    // Ensure it picks hosted mode when dev mode is not around
    new MainTypeProcessor(new ClassFinder() {
      @Override
      public boolean exists(ClassLoader classLoader, String className) {
        return !className.equals(MainTypeProcessor.MainType.GWT_DEV_MODE.mainTypeName);
      }
    }).update(helper.getLaunchConfig(), JavaCore.create(helper.getProject()), null, null);
    assertEquals(
        LaunchConfigurationProcessorUtilities.getMainTypeName(helper.getLaunchConfig()),
        MainTypeProcessor.MainType.GWT_HOSTED_MODE.mainTypeName);

    // Ensure it falls back to shell
    new MainTypeProcessor(new ClassFinder() {
      @Override
      public boolean exists(ClassLoader classLoader, String className) {
        return !className.equals(MainTypeProcessor.MainType.GWT_DEV_MODE.mainTypeName)
            && !className.equals(MainTypeProcessor.MainType.GWT_HOSTED_MODE.mainTypeName);
      }
    }).update(helper.getLaunchConfig(), JavaCore.create(helper.getProject()), null, null);
    assertEquals(
        LaunchConfigurationProcessorUtilities.getMainTypeName(helper.getLaunchConfig()),
        MainTypeProcessor.MainType.GWT_SHELL.mainTypeName);
  }

  public void testManuallyChanged() throws Exception {
    // Ensure the processor does not overwrite manual changes
    String otherMainType = "example.OtherMainType";
    helper.getLaunchConfig().setAttribute(
        IJavaLaunchConfigurationConstants.ATTR_MAIN_TYPE_NAME, otherMainType);
    assertEquals(
        LaunchConfigurationProcessorUtilities.getMainTypeName(helper.getLaunchConfig()),
        otherMainType);
    new MainTypeProcessor(new ClassFinder()).update(helper.getLaunchConfig(),
        JavaCore.create(helper.getProject()), null, null);
    assertEquals(
        LaunchConfigurationProcessorUtilities.getMainTypeName(helper.getLaunchConfig()),
        otherMainType);
  }

  public void testNoWar() throws Exception {
    // Ensure shell is used when there isn't a WAR dir
    WebAppProjectProperties.setWarSrcDir(helper.getProject(), new Path(""));
    new MainTypeProcessor(new ClassFinder()).update(helper.getLaunchConfig(),
        JavaCore.create(helper.getProject()), null, null);
    assertEquals(
        LaunchConfigurationProcessorUtilities.getMainTypeName(helper.getLaunchConfig()),
        MainTypeProcessor.MainType.GWT_SHELL.mainTypeName);
  }

  @Override
  protected void tearDown() throws Exception {
    helper.tearDown();
  }

}

@@ -1074,132 +1074,8 @@
 es;%0A
-import com.google.gdt.eclipse.core.properties.WebAppProjectProperties;%0Aimport com.google.gwt.eclipse.core.nature.GWTNature;%0A
 impo
@@ -1153,16 +1153,16 @@
 cipant;%0A
+
 import c
@@ -1311,46 +1311,8 @@
 e;%0A%0A
-import org.eclipse.core.runtime.Path;%0A
 impo
@@ -1893,2194 +1893,8 @@
  %7D%0A%0A
-  public void testGaeOnly() throws Exception %7B%0A    GWTNature.removeNatureFromProject(helper.getProject());%0A    new MainTypeProcessor(new ClassFinder()).update(helper.getLaunchConfig(),%0A        JavaCore.create(helper.getProject()), null, null);%0A    assertEquals(%0A        LaunchConfigurationProcessorUtilities.getMainTypeName(helper.getLaunchConfig()),%0A        MainTypeProcessor.MainType.GAE_APP_SERVER.mainTypeName);%0A  %7D%0A%0A  public void testGwtCapabilityDetection() throws Exception %7B%0A    // Ensure it chooses dev mode if the class is around%0A    new MainTypeProcessor(new ClassFinder() %7B%0A      @Override%0A      public boolean exists(ClassLoader classLoader, String className) %7B%0A        // GWT 2.0+ include all classes%0A        return true;%0A      %7D%0A    %7D).update(helper.getLaunchConfig(), JavaCore.create(helper.getProject()), null, null);%0A    assertEquals(%0A        LaunchConfigurationProcessorUtilities.getMainTypeName(helper.getLaunchConfig()),%0A        MainTypeProcessor.MainType.GWT_DEV_MODE.mainTypeName);%0A%0A    // Ensure it picks hosted mode when dev mode is not around%0A    new MainTypeProcessor(new ClassFinder() %7B%0A      @Override%0A      public boolean exists(ClassLoader classLoader, String className) %7B%0A        return !className.equals(MainTypeProcessor.MainType.GWT_DEV_MODE.mainTypeName);%0A      %7D%0A    %7D).update(helper.getLaunchConfig(), JavaCore.create(helper.getProject()), null, null);%0A    assertEquals(%0A        LaunchConfigurationProcessorUtilities.getMainTypeName(helper.getLaunchConfig()),%0A        MainTypeProcessor.MainType.GWT_HOSTED_MODE.mainTypeName);%0A%0A    // Ensure it falls back to shell%0A    new MainTypeProcessor(new ClassFinder() %7B%0A      @Override%0A      public boolean exists(ClassLoader classLoader, String className) %7B%0A        return !className.equals(MainTypeProcessor.MainType.GWT_DEV_MODE.mainTypeName)%0A            && !className.equals(MainTypeProcessor.MainType.GWT_HOSTED_MODE.mainTypeName);%0A      %7D%0A    %7D).update(helper.getLaunchConfig(), JavaCore.create(helper.getProject()), null, null);%0A    assertEquals(%0A        LaunchConfigurationProcessorUtilities.getMainTypeName(helper.getLaunchConfig()),%0A        MainTypeProcessor.MainType.GWT_SHELL.mainTypeName);%0A  %7D%0A%0A
   pu
@@ -2547,32 +2547,32 @@
 aunchConfig()),%0A
+
         otherMai
@@ -2588,494 +2588,8 @@
  %7D%0A%0A
-  public void testNoWar() throws Exception %7B%0A    // Ensure shell is used when there isn't a WAR dir%0A    WebAppProjectProperties.setWarSrcDir(helper.getProject(), new Path(%22%22));%0A    new MainTypeProcessor(new ClassFinder()).update(helper.getLaunchConfig(),%0A        JavaCore.create(helper.getProject()), null, null);%0A    assertEquals(%0A        LaunchConfigurationProcessorUtilities.getMainTypeName(helper.getLaunchConfig()),%0A        MainTypeProcessor.MainType.GWT_SHELL.mainTypeName);%0A  %7D%0A%0A
   @O

Fix a broken test.