/*
 * Copyright 2000-2015 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 git4idea.log;

import com.intellij.openapi.util.Pair;
import com.intellij.util.Function;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.vcs.log.RefGroup;
import com.intellij.vcs.log.VcsRef;
import com.intellij.vcs.log.impl.SingletonRefGroup;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

public class GitRefGroupsTest extends GitRefManagerTest {
  public void test_single_tracked_branch() throws IOException {
    check(given("HEAD", "master", "origin/master"), ContainerUtil.list("HEAD"), Pair.create("Local", ContainerUtil.list("master")),
          Pair.create("Tracked", ContainerUtil.list("origin/master")));
  }

  public void test_single_local_branch() throws IOException {
    check(given("HEAD", "master"), ContainerUtil.list("HEAD"), Pair.create("Local", ContainerUtil.list("master")));
  }

  public void test_local_tracked_and_remote_branch() throws IOException {
    check(given("HEAD", "master", "origin/master", "origin/remote_branch", "local_branch"), ContainerUtil.list("HEAD"),
          Pair.create("Local", ContainerUtil.list("master", "local_branch")), Pair.create("Tracked", ContainerUtil.list("origin/master")),
          Pair.create("origin/...", ContainerUtil.list("origin/remote_branch")));
  }

  private void check(@NotNull Collection<VcsRef> actual,
                     @NotNull List<String> expectedSingleGroups,
                     Pair<String, List<String>>... expectedOtherGroups) {

    List<RefGroup> actualGroups = new GitRefManager(myGitRepositoryManager).groupForBranchFilter(actual);

    List<SingletonRefGroup> singleGroups = ContainerUtil.findAll(actualGroups, SingletonRefGroup.class);
    assertEquals(expectedSingleGroups, ContainerUtil.map(singleGroups, new Function<RefGroup, String>() {
      @Override
      public String fun(RefGroup singletonRefGroup) {
        return singletonRefGroup.getName();
      }
    }));

    actualGroups.removeAll(singleGroups);

    assertEquals(Arrays.asList(expectedOtherGroups), ContainerUtil.map(actualGroups, new Function<RefGroup, Pair<String, List<String>>>() {
      @Override
      public Pair<String, List<String>> fun(RefGroup refGroup) {
        return Pair.create(refGroup.getName(), ContainerUtil.map(refGroup.getRefs(), new Function<VcsRef, String>() {
          @Override
          public String fun(VcsRef vcsRef) {
            return vcsRef.getName();
          }
        }));
      }
    }));
  }
}

@@ -1295,32 +1295,106 @@
 reate(%22Tracked%22,
+ ContainerUtil.list(%22origin/master%22)),%0A          Pair.create(%22origin/...%22,
  ContainerUtil.l
@@ -1985,32 +1985,49 @@
 tainerUtil.list(
+%22origin/master%22, 
 %22origin/remote_b

fix ref groups test