From 793f76563d4bb3f58fa62ff53985e20561c6e330 Mon Sep 17 00:00:00 2001 From: James Moger Date: Wed, 1 Jun 2011 21:01:51 -0400 Subject: [PATCH] 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 +- .../gitblit/wicket/pages/RepositoryPage.java | 3 +- src/com/gitblit/wicket/pages/TicketPage.java | 4 +- src/com/gitblit/wicket/pages/TicketsPage.java | 4 +- tests/com/gitblit/tests/GitBlitSuite.java | 2 + tests/com/gitblit/tests/JGitUtilsTest.java | 35 ----- tests/com/gitblit/tests/MetricUtilsTest.java | 35 +++++ tests/com/gitblit/tests/TicgitUtilsTest.java | 54 +++++++ 10 files changed, 241 insertions(+), 149 deletions(-) create mode 100644 src/com/gitblit/utils/TicgitUtils.java create mode 100644 tests/com/gitblit/tests/MetricUtilsTest.java create mode 100644 tests/com/gitblit/tests/TicgitUtilsTest.java 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, diff --git a/tests/com/gitblit/tests/GitBlitSuite.java b/tests/com/gitblit/tests/GitBlitSuite.java index d9963646..fcb57239 100644 --- a/tests/com/gitblit/tests/GitBlitSuite.java +++ b/tests/com/gitblit/tests/GitBlitSuite.java @@ -44,6 +44,8 @@ public class GitBlitSuite extends TestSetup { 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 14d318f3..f1dcaac4 100644 --- a/tests/com/gitblit/tests/JGitUtilsTest.java +++ b/tests/com/gitblit/tests/JGitUtilsTest.java @@ -30,13 +30,9 @@ import org.eclipse.jgit.revwalk.RevObject; 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 { @@ -182,35 +178,4 @@ public class JGitUtilsTest extends TestCase { zipFile.delete(); repository.close(); } - - public void testMetrics() throws Exception { - Repository repository = GitBlitSuite.getHelloworldRepository(); - List 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 ticketsA = JGitUtils.getTickets(repository); - List 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 00000000..77f43a16 --- /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 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 00000000..25dba2c6 --- /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 ticketsA = TicgitUtils.getTickets(repository); + List 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 -- 2.39.5