From 793f76563d4bb3f58fa62ff53985e20561c6e330 Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Wed, 01 Jun 2011 21:01:51 -0400
Subject: [PATCH] Refactored some unit tests and utils.

---
 src/com/gitblit/utils/TimeUtils.java             |   14 -
 src/com/gitblit/wicket/pages/TicketPage.java     |    4 
 tests/com/gitblit/tests/JGitUtilsTest.java       |   35 -----
 src/com/gitblit/utils/TicgitUtils.java           |  138 +++++++++++++++++++
 tests/com/gitblit/tests/MetricUtilsTest.java     |   35 +++++
 tests/com/gitblit/tests/TicgitUtilsTest.java     |   54 +++++++
 tests/com/gitblit/tests/GitBlitSuite.java        |    2 
 src/com/gitblit/wicket/pages/RepositoryPage.java |    3 
 src/com/gitblit/utils/JGitUtils.java             |  101 --------------
 src/com/gitblit/wicket/pages/TicketsPage.java    |    4 
 10 files changed, 241 insertions(+), 149 deletions(-)

diff --git a/src/com/gitblit/utils/JGitUtils.java b/src/com/gitblit/utils/JGitUtils.java
index 5f72c9a..32602b9 100644
--- a/src/com/gitblit/utils/JGitUtils.java
+++ b/src/com/gitblit/utils/JGitUtils.java
@@ -776,105 +776,4 @@
 		}
 		return false;
 	}
-
-	public static RefModel getTicketsBranch(Repository r) {
-		RefModel ticgitBranch = null;
-		try {
-			// search for ticgit branch in local heads
-			for (RefModel ref : getLocalBranches(r, -1)) {
-				if (ref.displayName.endsWith("ticgit")) {
-					ticgitBranch = ref;
-					break;
-				}
-			}
-
-			// search for ticgit branch in remote heads
-			if (ticgitBranch == null) {
-				for (RefModel ref : getRemoteBranches(r, -1)) {
-					if (ref.displayName.endsWith("ticgit")) {
-						ticgitBranch = ref;
-						break;
-					}
-				}
-			}
-		} catch (Throwable t) {
-			LOGGER.error("Failed to find ticgit branch!", t);
-		}
-		return ticgitBranch;
-	}
-
-	public static List<TicketModel> getTickets(Repository r) {
-		RefModel ticgitBranch = getTicketsBranch(r);
-		List<PathModel> paths = getFilesInPath(r, null, ticgitBranch.commit);
-		List<TicketModel> tickets = new ArrayList<TicketModel>();
-		for (PathModel ticketFolder : paths) {
-			if (ticketFolder.isTree()) {
-				try {
-					TicketModel t = new TicketModel(ticketFolder.name);
-					readTicketContents(r, ticgitBranch, t);
-					tickets.add(t);
-				} catch (Throwable t) {
-					LOGGER.error("Failed to get a ticket!", t);
-				}
-			}
-		}
-		Collections.sort(tickets);
-		Collections.reverse(tickets);
-		return tickets;
-	}
-
-	public static TicketModel getTicket(Repository r, String ticketFolder) {
-		RefModel ticketsBranch = getTicketsBranch(r);
-		if (ticketsBranch != null) {
-			try {
-				TicketModel ticket = new TicketModel(ticketFolder);
-				readTicketContents(r, ticketsBranch, ticket);
-				return ticket;
-			} catch (Throwable t) {
-				LOGGER.error("Failed to get ticket " + ticketFolder, t);
-			}
-		}
-		return null;
-	}
-
-	private static void readTicketContents(Repository r, RefModel ticketsBranch, TicketModel ticket) {
-		List<PathModel> ticketFiles = getFilesInPath(r, ticket.name, ticketsBranch.commit);
-		for (PathModel file : ticketFiles) {
-			String content = getRawContentAsString(r, ticketsBranch.commit, file.path).trim();
-			if (file.name.equals("TICKET_ID")) {
-				ticket.id = content;
-			} else if (file.name.equals("TITLE")) {
-				ticket.title = content;
-			} else {
-				String[] chunks = file.name.split("_");
-				if (chunks[0].equals("ASSIGNED")) {
-					ticket.handler = content;
-				} else if (chunks[0].equals("COMMENT")) {
-					try {
-						Comment c = new Comment(file.name, content);
-						ticket.comments.add(c);
-					} catch (ParseException e) {
-						e.printStackTrace();
-					}
-				} else if (chunks[0].equals("TAG")) {
-					if (content.startsWith("TAG_")) {
-						ticket.tags.add(content.substring(4));
-					} else {
-						ticket.tags.add(content);
-					}
-				} else if (chunks[0].equals("STATE")) {
-					ticket.state = content;
-				}
-			}
-		}
-		Collections.sort(ticket.comments);
-	}
-
-	public static String getTicketContent(Repository r, String filePath) {
-		RefModel ticketsBranch = getTicketsBranch(r);
-		if (ticketsBranch != null) {
-			return getRawContentAsString(r, ticketsBranch.commit, filePath);
-		}
-		return "";
-	}
 }
diff --git a/src/com/gitblit/utils/TicgitUtils.java b/src/com/gitblit/utils/TicgitUtils.java
new file mode 100644
index 0000000..914b813
--- /dev/null
+++ b/src/com/gitblit/utils/TicgitUtils.java
@@ -0,0 +1,138 @@
+/*
+ * 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.utils;
+
+import java.text.ParseException;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import org.eclipse.jgit.lib.Repository;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.gitblit.models.PathModel;
+import com.gitblit.models.RefModel;
+import com.gitblit.models.TicketModel;
+import com.gitblit.models.TicketModel.Comment;
+
+public class TicgitUtils {
+
+	static final Logger LOGGER = LoggerFactory.getLogger(TicgitUtils.class);
+
+	public static RefModel getTicketsBranch(Repository r) {
+		RefModel ticgitBranch = null;
+		try {
+			// search for ticgit branch in local heads
+			for (RefModel ref : JGitUtils.getLocalBranches(r, -1)) {
+				if (ref.displayName.endsWith("ticgit")) {
+					ticgitBranch = ref;
+					break;
+				}
+			}
+
+			// search for ticgit branch in remote heads
+			if (ticgitBranch == null) {
+				for (RefModel ref : JGitUtils.getRemoteBranches(r, -1)) {
+					if (ref.displayName.endsWith("ticgit")) {
+						ticgitBranch = ref;
+						break;
+					}
+				}
+			}
+		} catch (Throwable t) {
+			LOGGER.error("Failed to find ticgit branch!", t);
+		}
+		return ticgitBranch;
+	}
+
+	public static List<TicketModel> getTickets(Repository r) {
+		RefModel ticgitBranch = getTicketsBranch(r);
+		List<PathModel> paths = JGitUtils.getFilesInPath(r, null, ticgitBranch.commit);
+		List<TicketModel> tickets = new ArrayList<TicketModel>();
+		for (PathModel ticketFolder : paths) {
+			if (ticketFolder.isTree()) {
+				try {
+					TicketModel t = new TicketModel(ticketFolder.name);
+					readTicketContents(r, ticgitBranch, t);
+					tickets.add(t);
+				} catch (Throwable t) {
+					LOGGER.error("Failed to get a ticket!", t);
+				}
+			}
+		}
+		Collections.sort(tickets);
+		Collections.reverse(tickets);
+		return tickets;
+	}
+
+	public static TicketModel getTicket(Repository r, String ticketFolder) {
+		RefModel ticketsBranch = getTicketsBranch(r);
+		if (ticketsBranch != null) {
+			try {
+				TicketModel ticket = new TicketModel(ticketFolder);
+				readTicketContents(r, ticketsBranch, ticket);
+				return ticket;
+			} catch (Throwable t) {
+				LOGGER.error("Failed to get ticket " + ticketFolder, t);
+			}
+		}
+		return null;
+	}
+
+	private static void readTicketContents(Repository r, RefModel ticketsBranch, TicketModel ticket) {
+		List<PathModel> ticketFiles = JGitUtils
+				.getFilesInPath(r, ticket.name, ticketsBranch.commit);
+		for (PathModel file : ticketFiles) {
+			String content = JGitUtils.getRawContentAsString(r, ticketsBranch.commit, file.path)
+					.trim();
+			if (file.name.equals("TICKET_ID")) {
+				ticket.id = content;
+			} else if (file.name.equals("TITLE")) {
+				ticket.title = content;
+			} else {
+				String[] chunks = file.name.split("_");
+				if (chunks[0].equals("ASSIGNED")) {
+					ticket.handler = content;
+				} else if (chunks[0].equals("COMMENT")) {
+					try {
+						Comment c = new Comment(file.name, content);
+						ticket.comments.add(c);
+					} catch (ParseException e) {
+						e.printStackTrace();
+					}
+				} else if (chunks[0].equals("TAG")) {
+					if (content.startsWith("TAG_")) {
+						ticket.tags.add(content.substring(4));
+					} else {
+						ticket.tags.add(content);
+					}
+				} else if (chunks[0].equals("STATE")) {
+					ticket.state = content;
+				}
+			}
+		}
+		Collections.sort(ticket.comments);
+	}
+
+	public static String getTicketContent(Repository r, String filePath) {
+		RefModel ticketsBranch = getTicketsBranch(r);
+		if (ticketsBranch != null) {
+			return JGitUtils.getRawContentAsString(r, ticketsBranch.commit, filePath);
+		}
+		return "";
+	}
+}
diff --git a/src/com/gitblit/utils/TimeUtils.java b/src/com/gitblit/utils/TimeUtils.java
index ece87dd..44f51a5 100644
--- a/src/com/gitblit/utils/TimeUtils.java
+++ b/src/com/gitblit/utils/TimeUtils.java
@@ -15,6 +15,7 @@
  */
 package com.gitblit.utils;
 
+import java.util.Calendar;
 import java.util.Date;
 
 public class TimeUtils {
@@ -28,18 +29,15 @@
 
 	public static final long ONEYEAR = ONEDAY * 365L;
 
-	@SuppressWarnings("deprecation")
 	public static boolean isToday(Date date) {
-		Date now = new Date();
-		return now.getDate() == date.getDate() && now.getMonth() == date.getMonth()
-				&& now.getYear() == date.getYear();
+		return (System.currentTimeMillis() - date.getTime()) < ONEDAY; 
 	}
 
-	@SuppressWarnings("deprecation")
 	public static boolean isYesterday(Date date) {
-		Date now = new Date();
-		return now.getDate() == (date.getDate() + 1) && now.getMonth() == date.getMonth()
-				&& now.getYear() == date.getYear();
+		Calendar cal = Calendar.getInstance();
+		cal.setTime(date);
+		cal.add(Calendar.DATE, 1);
+		return (System.currentTimeMillis() - cal.getTimeInMillis()) < ONEDAY; 
 	}
 
 	public static String duration(int days) {
diff --git a/src/com/gitblit/wicket/pages/RepositoryPage.java b/src/com/gitblit/wicket/pages/RepositoryPage.java
index 143d885..eceda99 100644
--- a/src/com/gitblit/wicket/pages/RepositoryPage.java
+++ b/src/com/gitblit/wicket/pages/RepositoryPage.java
@@ -49,6 +49,7 @@
 import com.gitblit.utils.JGitUtils;
 import com.gitblit.utils.JGitUtils.SearchType;
 import com.gitblit.utils.StringUtils;
+import com.gitblit.utils.TicgitUtils;
 import com.gitblit.wicket.GitBlitWebSession;
 import com.gitblit.wicket.WicketUtils;
 import com.gitblit.wicket.panels.LinkPanel;
@@ -111,7 +112,7 @@
 		List<String> extraPageLinks = new ArrayList<String>();
 
 		// Conditionally add tickets link
-		if (model.useTickets && JGitUtils.getTicketsBranch(r) != null) {
+		if (model.useTickets && TicgitUtils.getTicketsBranch(r) != null) {
 			extraPageLinks.add("tickets");
 		}
 
diff --git a/src/com/gitblit/wicket/pages/TicketPage.java b/src/com/gitblit/wicket/pages/TicketPage.java
index 353c543..48db1ce 100644
--- a/src/com/gitblit/wicket/pages/TicketPage.java
+++ b/src/com/gitblit/wicket/pages/TicketPage.java
@@ -24,8 +24,8 @@
 
 import com.gitblit.models.TicketModel;
 import com.gitblit.models.TicketModel.Comment;
-import com.gitblit.utils.JGitUtils;
 import com.gitblit.utils.StringUtils;
+import com.gitblit.utils.TicgitUtils;
 import com.gitblit.wicket.GitBlitWebSession;
 import com.gitblit.wicket.WicketUtils;
 
@@ -37,7 +37,7 @@
 		final String ticketFolder = WicketUtils.getPath(params);
 
 		Repository r = getRepository();
-		TicketModel t = JGitUtils.getTicket(r, ticketFolder);
+		TicketModel t = TicgitUtils.getTicket(r, ticketFolder);
 
 		add(new Label("ticketTitle", t.title));
 		add(new Label("ticketId", t.id));
diff --git a/src/com/gitblit/wicket/pages/TicketsPage.java b/src/com/gitblit/wicket/pages/TicketsPage.java
index 8da4e94..f8473bd 100644
--- a/src/com/gitblit/wicket/pages/TicketsPage.java
+++ b/src/com/gitblit/wicket/pages/TicketsPage.java
@@ -24,8 +24,8 @@
 import org.apache.wicket.markup.repeater.data.ListDataProvider;
 
 import com.gitblit.models.TicketModel;
-import com.gitblit.utils.JGitUtils;
 import com.gitblit.utils.StringUtils;
+import com.gitblit.utils.TicgitUtils;
 import com.gitblit.wicket.GitBlitWebSession;
 import com.gitblit.wicket.WicketUtils;
 import com.gitblit.wicket.panels.LinkPanel;
@@ -35,7 +35,7 @@
 	public TicketsPage(PageParameters params) {
 		super(params);
 
-		List<TicketModel> tickets = JGitUtils.getTickets(getRepository());
+		List<TicketModel> tickets = TicgitUtils.getTickets(getRepository());
 
 		// header
 		add(new LinkPanel("header", "title", repositoryName, SummaryPage.class,
diff --git a/tests/com/gitblit/tests/GitBlitSuite.java b/tests/com/gitblit/tests/GitBlitSuite.java
index d996364..fcb5723 100644
--- a/tests/com/gitblit/tests/GitBlitSuite.java
+++ b/tests/com/gitblit/tests/GitBlitSuite.java
@@ -44,6 +44,8 @@
 		suite.addTestSuite(ByteFormatTest.class);
 		suite.addTestSuite(JGitUtilsTest.class);
 		suite.addTestSuite(DiffUtilsTest.class);
+		suite.addTestSuite(MetricUtilsTest.class);
+		suite.addTestSuite(TicgitUtilsTest.class);
 		suite.addTestSuite(GitBlitTest.class);
 		return new GitBlitSuite(suite);
 	}
diff --git a/tests/com/gitblit/tests/JGitUtilsTest.java b/tests/com/gitblit/tests/JGitUtilsTest.java
index 14d318f..f1dcaac 100644
--- a/tests/com/gitblit/tests/JGitUtilsTest.java
+++ b/tests/com/gitblit/tests/JGitUtilsTest.java
@@ -30,13 +30,9 @@
 import org.eclipse.jgit.revwalk.RevTree;
 
 import com.gitblit.GitBlit;
-import com.gitblit.models.Metric;
 import com.gitblit.models.PathModel.PathChangeModel;
 import com.gitblit.models.RefModel;
-import com.gitblit.models.TicketModel;
-import com.gitblit.models.TicketModel.Comment;
 import com.gitblit.utils.JGitUtils;
-import com.gitblit.utils.MetricUtils;
 
 public class JGitUtilsTest extends TestCase {
 
@@ -181,36 +177,5 @@
 		fos.close();
 		zipFile.delete();
 		repository.close();
-	}
-
-	public void testMetrics() throws Exception {
-		Repository repository = GitBlitSuite.getHelloworldRepository();
-		List<Metric> metrics = MetricUtils.getDateMetrics(repository, true);
-		repository.close();
-		assertTrue("No metrics found!", metrics.size() > 0);
-	}
-
-	public void testTicGit() throws Exception {
-		Repository repository = GitBlitSuite.getTicgitRepository();
-		RefModel branch = JGitUtils.getTicketsBranch(repository);
-		assertTrue("Ticgit branch does not exist!", branch != null);
-		List<TicketModel> ticketsA = JGitUtils.getTickets(repository);
-		List<TicketModel> ticketsB = JGitUtils.getTickets(repository);
-		repository.close();
-		assertTrue("No tickets found!", ticketsA.size() > 0);
-		for (int i = 0; i < ticketsA.size(); i++) {
-			TicketModel ticketA = ticketsA.get(i);
-			TicketModel ticketB = ticketsB.get(i);
-			assertTrue("Tickets are not equal!", ticketA.equals(ticketB));
-			assertFalse(ticketA.equals(""));
-			assertTrue(ticketA.hashCode() == ticketA.id.hashCode());
-			for (int j = 0; j < ticketA.comments.size(); j++) {
-				Comment commentA = ticketA.comments.get(j);
-				Comment commentB = ticketB.comments.get(j);
-				assertTrue("Comments are not equal!", commentA.equals(commentB));
-				assertFalse(commentA.equals(""));
-				assertTrue(commentA.hashCode() == commentA.text.hashCode());
-			}
-		}
 	}
 }
\ No newline at end of file
diff --git a/tests/com/gitblit/tests/MetricUtilsTest.java b/tests/com/gitblit/tests/MetricUtilsTest.java
new file mode 100644
index 0000000..77f43a1
--- /dev/null
+++ b/tests/com/gitblit/tests/MetricUtilsTest.java
@@ -0,0 +1,35 @@
+/*
+ * 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.util.List;
+
+import junit.framework.TestCase;
+
+import org.eclipse.jgit.lib.Repository;
+
+import com.gitblit.models.Metric;
+import com.gitblit.utils.MetricUtils;
+
+public class MetricUtilsTest extends TestCase {
+
+	public void testMetrics() throws Exception {
+		Repository repository = GitBlitSuite.getHelloworldRepository();
+		List<Metric> metrics = MetricUtils.getDateMetrics(repository, true);
+		repository.close();
+		assertTrue("No metrics found!", metrics.size() > 0);
+	}
+}
\ No newline at end of file
diff --git a/tests/com/gitblit/tests/TicgitUtilsTest.java b/tests/com/gitblit/tests/TicgitUtilsTest.java
new file mode 100644
index 0000000..25dba2c
--- /dev/null
+++ b/tests/com/gitblit/tests/TicgitUtilsTest.java
@@ -0,0 +1,54 @@
+/*
+ * 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.util.List;
+
+import junit.framework.TestCase;
+
+import org.eclipse.jgit.lib.Repository;
+
+import com.gitblit.models.RefModel;
+import com.gitblit.models.TicketModel;
+import com.gitblit.models.TicketModel.Comment;
+import com.gitblit.utils.TicgitUtils;
+
+public class TicgitUtilsTest extends TestCase {
+
+	public void testTicGit() throws Exception {
+		Repository repository = GitBlitSuite.getTicgitRepository();
+		RefModel branch = TicgitUtils.getTicketsBranch(repository);
+		assertTrue("Ticgit branch does not exist!", branch != null);
+		List<TicketModel> ticketsA = TicgitUtils.getTickets(repository);
+		List<TicketModel> ticketsB = TicgitUtils.getTickets(repository);
+		repository.close();
+		assertTrue("No tickets found!", ticketsA.size() > 0);
+		for (int i = 0; i < ticketsA.size(); i++) {
+			TicketModel ticketA = ticketsA.get(i);
+			TicketModel ticketB = ticketsB.get(i);
+			assertTrue("Tickets are not equal!", ticketA.equals(ticketB));
+			assertFalse(ticketA.equals(""));
+			assertTrue(ticketA.hashCode() == ticketA.id.hashCode());
+			for (int j = 0; j < ticketA.comments.size(); j++) {
+				Comment commentA = ticketA.comments.get(j);
+				Comment commentB = ticketB.comments.get(j);
+				assertTrue("Comments are not equal!", commentA.equals(commentB));
+				assertFalse(commentA.equals(""));
+				assertTrue(commentA.hashCode() == commentA.text.hashCode());
+			}
+		}
+	}
+}
\ No newline at end of file

--
Gitblit v1.9.1