package hg4idea.test.repo;

import com.intellij.dvcs.test.TestRepositoryUtil;
import com.intellij.openapi.application.PluginPathManager;
import com.intellij.openapi.util.io.FileUtil;
import hg4idea.test.HgPlatformTest;
import org.jetbrains.annotations.NotNull;
import org.zmlx.hg4idea.repo.HgRepositoryReader;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;

/**
 * @author Nadya Zabrodina
 */
public class HgRepositoryReaderTest extends HgPlatformTest {


  @NotNull private HgRepositoryReader myRepositoryReader;
  @NotNull private File myHgDir;
  @NotNull private Collection<String> myBranches;

  public void setUp() throws Exception {
    super.setUp();
    myHgDir = new File(myRepository.getPath(), ".hg");
    assertTrue(myHgDir.exists());
    File pluginRoot = new File(PluginPathManager.getPluginHomePath("hg4idea"));

    String pathToHg = "testData/repo/dot_hg";
    File testHgDir = new File(pluginRoot, FileUtil.toSystemDependentName(pathToHg));

    File cacheDir = new File(testHgDir, "cache");
    File testBranchFile = new File(testHgDir, "branch");
    FileUtil.copyDir(cacheDir, new File(myHgDir, "cache"));
    FileUtil.copy(testBranchFile, new File(myHgDir, "branch"));

    myRepositoryReader = new HgRepositoryReader(myHgDir);
    myBranches = readBranches();
  }


  public void testHEAD() {
    assertEquals("25e44c95b2612e3cdf29a704dabf82c77066cb67", myRepositoryReader.readCurrentRevision());
  }

  public void testCurrentBranch() {
    String currentBranch = myRepositoryReader.readCurrentBranch();
    assertEquals(currentBranch, "firstBranch");
  }

  public void testBranches() {
    Collection<String> branches = myRepositoryReader.readBranches();
    TestRepositoryUtil.assertEqualCollections(branches, myBranches);
  }

  @NotNull
  private Collection<String> readBranches() throws IOException {
    final Collection<String> branches = new ArrayList<String>();
    String branchesName = "branchheads-served";
    final File branchHeads = new File(new File(myHgDir, "cache"), branchesName);
    String[] branchesWithHashes = FileUtil.loadFile(branchHeads).split("\n");
    for (int i = 1; i < branchesWithHashes.length; ++i) {
      String ref = branchesWithHashes[i];
      String[] refAndName = ref.split(" ");
      String name = refAndName[1];

      if (!branches.contains(name)) {
        branches.add(name);
      }
    }
    return branches;
  }
}

@@ -1,8 +1,609 @@
+/*%0A * Copyright 2000-2013 JetBrains s.r.o.%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A * http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0A
 package 
@@ -958,36 +958,8 @@
 on;%0A
-import java.util.ArrayList;%0A
 impo
@@ -982,16 +982,42 @@
 lection;
+%0Aimport java.util.HashSet;
 %0A%0A/**%0A *
@@ -1107,17 +1107,16 @@
 Test %7B%0A%0A
-%0A
   @NotNu
@@ -1249,16 +1249,28 @@
 nches;%0A%0A
+  @Override%0A
   public
@@ -1951,17 +1951,16 @@
 );%0A  %7D%0A%0A
-%0A
   public
@@ -2497,22 +2497,16 @@
 n %7B%0A    
-final 
 Collecti
@@ -2535,16 +2535,14 @@
 new 
-ArrayLis
+HashSe
 t%3CSt
@@ -2558,62 +2558,8 @@
     
-String branchesName = %22branchheads-served%22;%0A    final 
 File
@@ -2614,20 +2614,28 @@
 %22), 
+%22
 branch
-esName
+heads-served%22
 );%0A 
@@ -2781,20 +2781,29 @@
   String
+%5B%5D
  ref
+AndName
  = branc
@@ -2822,130 +2822,70 @@
 s%5Bi%5D
-;%0A      String%5B%5D refAndName = ref.split(%22 %22);%0A      String name = refAndName%5B1%5D;%0A%0A      if (!branches.contains(name)) %7B%0A  
+.trim().split(%22 %22);%0A      assertEquals(2, refAndName.length);%0A
     
@@ -2903,22 +2903,23 @@
 add(
-name);%0A      %7D
+refAndName%5B1%5D);
 %0A   

Test fixed