/*
 * 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 jetbrains.buildServer.agent.android;

import jetbrains.buildServer.util.FileUtil;
import org.testng.Assert;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;

import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;

/**
 * @author Vladislav.Rassokhin
 */
public class AndroidSDKUtilTest {
  private final List<File> myFilesToDelete = new LinkedList<File>();

  @Test
  public void testLatestTargetDetection() throws Exception {
    doTestLatestTargetDetection("android-14", "android-10", "android-11", "android-12", "android-13", "android-14");
    doTestLatestTargetDetection("android-14", "android-9", "AndRoid-11", "android-12", "android-13", "android-14");
    doTestLatestTargetDetection("android-100", "android-9", "android-11", "android-12", "android-13", "android-14", "android-100");
  }

  private void doTestLatestTargetDetection(String expected, String... targets) throws IOException {
    final File sdk = prepareTargetsDir(targets);
    final File target = AndroidSDKUtil.getLatestTarget(sdk.getAbsolutePath());
    Assert.assertNotNull(target);
    Assert.assertEquals(target.getName(), expected);
  }

  private File prepareTargetsDir(String... targetNames) throws IOException {
    final File root = FileUtil.createTempDirectory(AndroidSDKUtilTest.class.getName(), null, null);
    final File platforms = new File(root, "platforms");
    platforms.mkdirs();
    for (String name : targetNames) {
      new File(platforms, name).mkdir();
    }
    myFilesToDelete.add(root);
    return root;
  }

  @Test
  public void testGetTargets() throws Exception {
    final File sdk = prepareTargetsDir("android-10", "android-11", "android-12", "android-13", "android-14");
    final Collection<File> targets = AndroidSDKUtil.getTargets(sdk.getAbsolutePath());
    Assert.assertEquals(targets.size(), 5);
  }

  @AfterMethod
  public void tearDown() throws Exception {
    for (File file : myFilesToDelete) {
      if (file != null) {
        FileUtil.delete(file);
      }
    }
  }
}
