From 793f76563d4bb3f58fa62ff53985e20561c6e330 Mon Sep 17 00:00:00 2001 From: James Moger Date: Wed, 1 Jun 2011 21:01:51 -0400 Subject: Refactored some unit tests and utils. --- src/com/gitblit/utils/JGitUtils.java | 101 ----------------- src/com/gitblit/utils/TicgitUtils.java | 138 +++++++++++++++++++++++ src/com/gitblit/utils/TimeUtils.java | 14 +-- src/com/gitblit/wicket/pages/RepositoryPage.java | 3 +- src/com/gitblit/wicket/pages/TicketPage.java | 4 +- src/com/gitblit/wicket/pages/TicketsPage.java | 4 +- 6 files changed, 150 insertions(+), 114 deletions(-) create mode 100644 src/com/gitblit/utils/TicgitUtils.java (limited to 'src') diff --git a/src/com/gitblit/utils/JGitUtils.java b/src/com/gitblit/utils/JGitUtils.java index 5f72c9a2..32602b94 100644 --- a/src/com/gitblit/utils/JGitUtils.java +++ b/src/com/gitblit/utils/JGitUtils.java @@ -776,105 +776,4 @@ public class JGitUtils { } 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 getTickets(Repository r) { - RefModel ticgitBranch = getTicketsBranch(r); - List paths = getFilesInPath(r, null, ticgitBranch.commit); - List tickets = new ArrayList(); - 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 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 00000000..914b8139 --- /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 getTickets(Repository r) { + RefModel ticgitBranch = getTicketsBranch(r); + List paths = JGitUtils.getFilesInPath(r, null, ticgitBranch.commit); + List tickets = new ArrayList(); + 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 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 ece87dd8..44f51a55 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 class TimeUtils { 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 143d885a..eceda998 100644 --- a/src/com/gitblit/wicket/pages/RepositoryPage.java +++ b/src/com/gitblit/wicket/pages/RepositoryPage.java @@ -49,6 +49,7 @@ import com.gitblit.models.RepositoryModel; 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 @@ public abstract class RepositoryPage extends BasePage { List extraPageLinks = new ArrayList(); // 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 353c543e..48db1cea 100644 --- a/src/com/gitblit/wicket/pages/TicketPage.java +++ b/src/com/gitblit/wicket/pages/TicketPage.java @@ -24,8 +24,8 @@ import org.eclipse.jgit.lib.Repository; 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 @@ public class TicketPage extends RepositoryPage { 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 8da4e942..f8473bd2 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.DataView; 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 class TicketsPage extends RepositoryPage { public TicketsPage(PageParameters params) { super(params); - List tickets = JGitUtils.getTickets(getRepository()); + List tickets = TicgitUtils.getTickets(getRepository()); // header add(new LinkPanel("header", "title", repositoryName, SummaryPage.class, -- cgit v1.2.3