/*
 * Copyright 2000-2011 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.nuget.tests.server.tools;

import jetbrains.buildServer.BaseTestCase;
import jetbrains.buildServer.nuget.server.ToolPaths;
import jetbrains.buildServer.nuget.server.toolRegistry.NuGetInstalledTool;
import jetbrains.buildServer.nuget.server.toolRegistry.impl.PluginNaming;
import jetbrains.buildServer.nuget.server.toolRegistry.impl.ToolsRegistry;
import jetbrains.buildServer.nuget.server.toolRegistry.impl.ToolsWatcher;
import jetbrains.buildServer.util.FileUtil;
import org.jetbrains.annotations.NotNull;
import org.jmock.Expectations;
import org.jmock.Mockery;
import org.testng.Assert;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.io.File;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;

/**
 * @author Eugene Petrenko (eugene.petrenko@gmail.com)
 *         Date: 30.09.11 17:07
 */
public class ToolsRegistryTest extends BaseTestCase {
  private Mockery m;
  private File myToolsHome;
  private File myPackagesHome;
  private File myAgentHome;
  private ToolsRegistry myRegistry;
  private ToolsWatcher myWatcher;

  @BeforeMethod
  @Override
  protected void setUp() throws Exception {
    super.setUp();

    m = new Mockery();
    myToolsHome = createTempDir();
    myPackagesHome = createTempDir();
    myAgentHome = createTempDir();

    final ToolPaths path = m.mock(ToolPaths.class);
    myWatcher = m.mock(ToolsWatcher.class);

    m.checking(new Expectations(){{
      allowing(path).getNuGetToolsPath(); will(returnValue(myToolsHome));
      allowing(path).getNuGetToolsPackages(); will(returnValue(myPackagesHome));
      allowing(path).getNuGetToolsAgentPluginsPath(); will(returnValue(myAgentHome));
    }});

    myRegistry = new ToolsRegistry(path, new PluginNaming(path), myWatcher);
  }


  @Test
  public void test_empty() {
    Assert.assertTrue(myRegistry.getTools().isEmpty());
  }


  private void file(@NotNull File root, @NotNull String path) {
    final File dest = new File(root, path);
    FileUtil.createParentDirs(dest);
    FileUtil.writeFile(dest, "2qwerwer" + path);
  }

  @Test
  public void test_one_package() {
    nupkg("NuGet.CommandLine.1.2.3.4");
    Assert.assertEquals(myRegistry.getTools().size(), 1);
  }

  private void nupkg(String name) {
    file(myPackagesHome, name + ".nupkg");
    file(myAgentHome, name + ".nupkg.zip");
    file(myToolsHome, name + ".nupkg/tools/nuget.exe");
  }

  @Test
  public void test_two_packages() {
    nupkg("NuGet.CommandLine.1.2.3.4");
    nupkg("NuGet.CommandLine.1.3.3.4");
    nupkg("NuGet.CommandLine.1.2.6.4");
    nupkg("NuGet.CommandLine.2.2.3.4.z");
    nupkg("NuGet.CommandLine.2.2.3.14.y");
    nupkg("NuGet.aaaamandLine.4.4.4");

    final Collection<? extends NuGetInstalledTool> tools = myRegistry.getTools();
    final Iterator<String> versions = Arrays.asList("1.2.3.4", "1.2.6.4", "1.3.3.4", "2.2.3.4", "2.2.3.14", "4.4.4").iterator();

    for (NuGetInstalledTool tool : tools) {
      System.out.println("tool.getVersion() = " + tool.getVersion());
      final String v = versions.next();
      Assert.assertEquals(tool.getVersion(), v);
    }
  }


}

@@ -1371,72 +1371,9 @@
 til.
-Arrays;%0D%0Aimport java.util.Collection;%0D%0Aimport java.util.Iterator
+*
 ;%0D%0A%0D
@@ -3474,16 +3474,12 @@
 nal 
-Iterator
+List
 %3CStr
@@ -3487,113 +3487,42 @@
 ng%3E 
-versions = Arrays.asList(%221.2.3.4%22, %221.2.6.4%22, %221.3.3.4%22, %222.2.3.4%22, %222.2.3.14%22, %224.4.4%22).iterator
+names = new ArrayList%3CString%3E
 ();%0D%0A
-%0D%0A
     
@@ -3643,45 +3643,48 @@
     
-final String v = versions.next
+names.add(tool.getVersion
 ()
+)
 ;%0D%0A  
+  %7D%0D%0A%0D%0A
     
@@ -3707,37 +3707,118 @@
 als(
-tool.getVersion(), v);%0D%0A    %7D
+names, Arrays.asList(%22NuGet.aaaamandLine.4.4.4%22, %221.2.3.4%22, %221.2.6.4%22, %221.3.3.4%22, %222.2.3.4.z%22, %222.2.3.14.y%22));
 %0D%0A  

fix test