/*
 * Copyright 2000-2010 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 jetbrains.buildServer.eclipse.tests;

import java.io.File;

import jetbrains.buildServer.RunBuildException;
import jetbrains.buildServer.agent.BuildRunnerContext;
import jetbrains.buildServer.eclipse.java.PlatformDescriptor;
import jetbrains.buildServer.eclipse.tests.helper.EclipseBuilderFactoryTestAdapter;

import org.junit.Assert;
import org.junit.Test;

public class EclipsePlatformDescriptorTest extends AbstractEclipseBuilderTest {

  @Test(expected = RunBuildException.class)
  public void getPlatform_nothing_set() throws Exception {
    final EclipseBuilderFactoryTestAdapter testAdapter = new EclipseBuilderFactoryTestAdapter();
    final PlatformDescriptor platform = testAdapter.getPlatform(createRunnerContext(null, null, null));
    Assert.assertNotNull(platform);
  }

  @Test(expected = RunBuildException.class)
  public void getPlatform_runner_parameter_wrong_set() throws Exception {
    final File eclipseHome = new File("test-data/eclipse-PDE-build");
    BuildRunnerContext context = createRunnerContext(eclipseHome.getAbsolutePath(), null, null);
    final EclipseBuilderFactoryTestAdapter testAdapter = new EclipseBuilderFactoryTestAdapter();
    testAdapter.getPlatform(context);
  }

  @Test
  public void getPlatform_runner_parameter_set() throws Exception {
    final File eclipseHome = getEclipseHome();
    Assert.assertTrue(String.format("Eclipse home directory '%s' does not exist", eclipseHome), eclipseHome.exists());
    BuildRunnerContext context = createRunnerContext(eclipseHome.getAbsolutePath(), null, null);
    final EclipseBuilderFactoryTestAdapter testAdapter = new EclipseBuilderFactoryTestAdapter();
    final PlatformDescriptor platform = testAdapter.getPlatform(context);
    Assert.assertNotNull(platform);
  }

  @Test
  public void getPlatform_system_prop_set() throws Exception {
    final File eclipseHome = getEclipseHome();
    Assert.assertTrue(String.format("Eclipse home directory '%s' does not exist", eclipseHome), eclipseHome.exists());
    BuildRunnerContext context = createRunnerContext(null, eclipseHome.getAbsolutePath(), null);
    final EclipseBuilderFactoryTestAdapter testAdapter = new EclipseBuilderFactoryTestAdapter();
    final PlatformDescriptor platform = testAdapter.getPlatform(context);
    Assert.assertNotNull(platform);
  }

  @Test
  public void getPlatform_environment_set() throws Exception {
    final File eclipseHome = getEclipseHome();
    Assert.assertTrue(String.format("Eclipse home directory '%s' does not exist", eclipseHome), eclipseHome.exists());
    BuildRunnerContext context = createRunnerContext(null, null, eclipseHome.getAbsolutePath());
    final EclipseBuilderFactoryTestAdapter testAdapter = new EclipseBuilderFactoryTestAdapter();
    final PlatformDescriptor platform = testAdapter.getPlatform(context);
    Assert.assertNotNull(platform);
  }

  @Test
  public void check_platform_attributes() throws Exception {
    final File eclipseHome = getEclipseHome();
    Assert.assertTrue(String.format("Eclipse home directory '%s' does not exist", eclipseHome), eclipseHome.exists());
    BuildRunnerContext context = createRunnerContext(null, null, eclipseHome.getAbsolutePath());
    final EclipseBuilderFactoryTestAdapter testAdapter = new EclipseBuilderFactoryTestAdapter();
    final PlatformDescriptor platform = testAdapter.getPlatform(context);
    Assert.assertNotNull(platform);
    Assert.assertEquals(platform.getName(), "Eclipse Platform");
    Assert.assertEquals(platform.getId(), "org.eclipse.platform");
    Assert.assertEquals(platform.getVersion(), "3.6.1");
  }

}
