James Moger
2011-05-25 f13c4c5a35a18d8478b276cc44570bbc3398aa73
src/com/gitblit/tests/JGitUtilsTest.java
@@ -1,6 +1,22 @@
/*
 * Copyright 2011 gitblit.com.
 *
 * 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.gitblit.tests;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Date;
import java.util.List;
@@ -15,9 +31,10 @@
import org.eclipse.jgit.storage.file.FileRepository;
import com.gitblit.utils.JGitUtils;
import com.gitblit.wicket.models.PathModel;
import com.gitblit.utils.JGitUtils.DiffOutputType;
import com.gitblit.wicket.models.PathModel.PathChangeModel;
import com.gitblit.wicket.models.RefModel;
import com.gitblit.wicket.models.TicGitTicket;
import com.gitblit.wicket.models.TicketModel;
public class JGitUtilsTest extends TestCase {
@@ -50,6 +67,14 @@
      r.close();
      assertTrue("Could not get last repository change date!", date != null);
   }
   public void testFirstCommit() throws Exception {
      Repository r = getRepository();
      RevCommit commit = JGitUtils.getFirstCommit(r, null);
      r.close();
      assertTrue("Could not get first commit!", commit != null);
      System.out.println(commit.getName() + " " + commit.getShortMessage());
   }
   public void testRetrieveRevObject() throws Exception {
      Repository r = getRepository();
@@ -72,9 +97,9 @@
   public void testTicGit() throws Exception {
      Repository r = new FileRepository(new File(repositoriesFolder, "ticgit") + "/" + Constants.DOT_GIT);
      RefModel ticgit = JGitUtils.getTicGitBranch(r);
      RefModel ticgit = JGitUtils.getTicketsBranch(r);
      assertTrue("Ticgit branch does not exist!", ticgit != null);
      List<TicGitTicket> tickets = JGitUtils.getTicGitTickets(r);
      List<TicketModel> tickets = JGitUtils.getTickets(r);
      assertTrue("No tickets found!", tickets.size() > 0);
      r.close();
   }
@@ -82,7 +107,7 @@
   public void testFilesInCommit() throws Exception {
      Repository r = getRepository();
      RevCommit commit = JGitUtils.getCommit(r, Constants.HEAD);
      List<PathModel> paths = JGitUtils.getFilesInCommit(r, commit);
      List<PathChangeModel> paths = JGitUtils.getFilesInCommit(r, commit);
      r.close();
      assertTrue("No changed paths found!", paths.size() > 0);
   }
@@ -90,9 +115,31 @@
   public void testCommitDiff() throws Exception {
      Repository r = getRepository();
      RevCommit commit = JGitUtils.getCommit(r, Constants.HEAD);
      String diff = JGitUtils.getCommitDiff(r, commit, false);
      String diff = JGitUtils.getCommitDiff(r, commit, DiffOutputType.PLAIN);
      r.close();
      System.out.println(diff);
   }
   public void testZip() throws Exception {
      Repository r = new FileRepository(new File(repositoriesFolder, "gitblit.git/" + Constants.DOT_GIT));
      FileOutputStream fos = null;
      try {
         File zipFile = new File("c:/output.zip");
         zipFile.delete();
         fos = new FileOutputStream(zipFile);
         if (JGitUtils.zip(r, "src", Constants.HEAD, fos)) {
            System.out.println("zip = " + zipFile.length() + " bytes");
         } else {
            System.err.println("failed to generate zip file?!");
         }
      } finally {
         if (fos != null) {
            try {
               fos.close();
            } catch (Throwable t) {
            }
         }
      }
   }
}