Added tests to GitInterface.

Change-Id: I2498a634fd4618be81d384b40b89313a696cb212
diff --git a/pom.xml b/pom.xml
index fe6fc0d..881a047 100644
--- a/pom.xml
+++ b/pom.xml
@@ -33,6 +33,11 @@
       <artifactId>commons-exec</artifactId>
       <version>1.2</version>
     </dependency>
+    <dependency>
+      <groupId>net.lingala.zip4j</groupId>
+      <artifactId>zip4j</artifactId>
+      <version>1.3.2</version>
+    </dependency>
   </dependencies>
   <build>
     <pluginManagement>
diff --git a/src/main/java/com/google/gwt/benchmark/git/GitInterface.java b/src/main/java/com/google/gwt/benchmark/git/GitInterface.java
index caea987..3c5f78d 100644
--- a/src/main/java/com/google/gwt/benchmark/git/GitInterface.java
+++ b/src/main/java/com/google/gwt/benchmark/git/GitInterface.java
@@ -18,6 +18,8 @@
 import com.google.common.base.Preconditions;
 import com.google.common.collect.Lists;
 
+import org.eclipse.jgit.api.CheckoutCommand;
+import org.eclipse.jgit.api.CheckoutResult;
 import org.eclipse.jgit.api.Git;
 import org.eclipse.jgit.api.LogCommand;
 import org.eclipse.jgit.api.errors.GitAPIException;
@@ -61,14 +63,41 @@
   /**
    * Checkout revision {@code revision} in repository {@code repo}.
    */
-  public static void checkout(String repo, String revision) throws GitException {
+  public static RevisionInfo checkout(String repo, String revision) throws GitException {
     Git git = null;
     System.out.println("Checking out " + revision);
     try {
       git = Git.open(new File(repo));
-      git.checkout().setName(revision).call();
+      CheckoutCommand checkoutCommand = git.checkout().setName(revision);
+      checkoutCommand.call();
+      if (checkoutCommand.getResult().getStatus() != CheckoutResult.Status.OK) {
+        return null;
+      }
+      return createFromCommit(git.log().setMaxCount(1).call().iterator().next());
+
     } catch (IOException | GitAPIException e) {
       throw new GitException("Could not checkout " + revision, e);
+    } finally {
+      if (git != null) {
+        git.close();
+      }
+    }
+  }
+
+  /**
+   * Returns the RevisionInfo for HEAD.
+   */
+  public static RevisionInfo getCurrentCommit(String repo) throws GitException {
+    Git git = null;
+    try {
+      git = Git.open(new File(repo));
+      return createFromCommit(git.log().setMaxCount(1).call().iterator().next());
+    } catch (IOException | GitAPIException e) {
+      throw new GitException("Could not retrieve HEAD information ", e);
+    } finally {
+      if (git != null) {
+        git.close();
+      }
     }
   }
 
@@ -87,7 +116,7 @@
       for (RevCommit commit : log.all().call()) {
         RevisionInfo revInfo = maybeGetRevisionInfo(git.getRepository(), commit, prefixes,
             suffixes);
-        if (revInfo != null){
+        if (revInfo == null){
           continue;
         }
         revisionsToTest.add(revInfo);
@@ -95,6 +124,10 @@
       System.out.println("Revisions to test : " + revisionsToTest.size());
     } catch (IOException | GitAPIException e) {
       throw new GitException("Could not get revision list" , e);
+    } finally {
+      if (git != null) {
+        git.close();
+      }
     }
     return revisionsToTest;
   }
@@ -134,8 +167,7 @@
       if (treeWalk.next()) {
         // If there is at least one modified file that satisfied the query, this revision is
         // of interest.
-        return new RevisionInfo(commit.getName(), commit.getShortMessage(),
-            commit.getCommitterIdent().getName(), commit.getCommitterIdent().getWhen());
+        return createFromCommit(commit);
       }
     } finally {
       treeWalk.release();
@@ -143,6 +175,14 @@
     return null;
   }
 
+  /**
+   * Creates a RevisionInfo from a RevCommit.
+   */
+  private static RevisionInfo createFromCommit(RevCommit commit) {
+    return new RevisionInfo(commit.getName(), commit.getShortMessage(),
+        commit.getCommitterIdent().getName(), commit.getCommitterIdent().getWhen());
+  }
+
   private static TreeFilter or(List<TreeFilter> treeFilterList) {
     Preconditions.checkArgument(treeFilterList.size() > 0);
     if (treeFilterList.size() == 1) {
diff --git a/src/test/java/com/google/gwt/benchmark/git/GitInterfaceTest.java b/src/test/java/com/google/gwt/benchmark/git/GitInterfaceTest.java
new file mode 100644
index 0000000..58b3c13
--- /dev/null
+++ b/src/test/java/com/google/gwt/benchmark/git/GitInterfaceTest.java
@@ -0,0 +1,93 @@
+/*
+ * Copyright 2014 Google Inc.
+ *
+ * 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 com.google.gwt.benchmark.git;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.fail;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.io.Files;
+
+import net.lingala.zip4j.core.ZipFile;
+import net.lingala.zip4j.exception.ZipException;
+
+import org.junit.Test;
+
+import java.io.File;
+import java.util.List;
+
+/**
+ * Tests for {@link GitInterface}.
+ */
+public class GitInterfaceTest {
+
+  private static final String INITIAL_REVISION_TEST_REPO =
+      "5136b807e93d93800139ea43ae88fb5ddee7c226";
+
+  private static final String HEAD_TEST_REPO =
+      "559ed7a0f92ac7da8b072e6e67f09589793f78fe";
+
+  @Test
+  public void testCheckout_initial() throws GitException, ZipException {
+    File repodir = Files.createTempDir();
+    unzip("src/test/resources/testrepos/test.repo.zip", repodir.toString());
+    RevisionInfo revInfo =
+        GitInterface.checkout(repodir.toString() + "/test.repo", INITIAL_REVISION_TEST_REPO);
+    assertNotNull(revInfo);
+    assertEquals(INITIAL_REVISION_TEST_REPO, revInfo.getCommitId());
+  }
+
+  @Test
+  public void testCheckout_bad() throws ZipException {
+    File repodir = Files.createTempDir();
+    unzip("src/test/resources/testrepos/test.repo.zip", repodir.toString());
+    try {
+      GitInterface.checkout(repodir.toString() + "/test.repo", "_BAD_");
+      fail("Should have thrown an Exception");
+    } catch (GitException e) {
+      // expected.
+    }
+    try {
+      GitInterface.checkout(repodir.toString(), INITIAL_REVISION_TEST_REPO);
+      fail("Should have thrown an Exception");
+    } catch (GitException e) {
+      // expected.
+    }
+  }
+
+  @Test
+  public void testGetCurrentCommit() throws GitException, ZipException {
+    File repodir = Files.createTempDir();
+    unzip("src/test/resources/testrepos/test.repo.zip", repodir.toString());
+    RevisionInfo revInfo = GitInterface.getCurrentCommit(repodir.toString() + "/test.repo");
+    assertNotNull(revInfo);
+    assertEquals(HEAD_TEST_REPO, revInfo.getCommitId());
+  }
+
+  @Test
+  public void testGetRevisionsAffectingTargets() throws GitException, ZipException {
+    File repodir = Files.createTempDir();
+    unzip("src/test/resources/testrepos/test.repo.zip", repodir.toString());
+    List<RevisionInfo> revisions =
+        GitInterface.getRevisionsAffectingTargets(repodir.toString()  + "/test.repo",
+            ImmutableList.of("src"), ImmutableList.of(".java"));
+    assertEquals(1, revisions.size());
+    assertEquals(INITIAL_REVISION_TEST_REPO, revisions.get(0).getCommitId());
+  }
+
+  private void unzip(String zipFilePath, String destinationPath) throws ZipException {
+    new ZipFile(zipFilePath).extractAll(destinationPath);
+  }
+}
diff --git a/src/test/resources/testrepos/test.repo.zip b/src/test/resources/testrepos/test.repo.zip
new file mode 100644
index 0000000..e4b467f
--- /dev/null
+++ b/src/test/resources/testrepos/test.repo.zip
Binary files differ