]> source.dussan.org Git - gitblit.git/commitdiff
Refactored some unit tests and utils.
authorJames Moger <james.moger@gitblit.com>
Thu, 2 Jun 2011 01:01:51 +0000 (21:01 -0400)
committerJames Moger <james.moger@gitblit.com>
Thu, 2 Jun 2011 01:01:51 +0000 (21:01 -0400)
src/com/gitblit/utils/JGitUtils.java
src/com/gitblit/utils/TicgitUtils.java [new file with mode: 0644]
src/com/gitblit/utils/TimeUtils.java
src/com/gitblit/wicket/pages/RepositoryPage.java
src/com/gitblit/wicket/pages/TicketPage.java
src/com/gitblit/wicket/pages/TicketsPage.java
tests/com/gitblit/tests/GitBlitSuite.java
tests/com/gitblit/tests/JGitUtilsTest.java
tests/com/gitblit/tests/MetricUtilsTest.java [new file with mode: 0644]
tests/com/gitblit/tests/TicgitUtilsTest.java [new file with mode: 0644]

index 5f72c9a250a9e3f4f1118852ebd7055d3f5cc0d7..32602b94bb35b7f20bfb217ff94ed7265525b28a 100644 (file)
@@ -776,105 +776,4 @@ public class JGitUtils {
                }\r
                return false;\r
        }\r
-\r
-       public static RefModel getTicketsBranch(Repository r) {\r
-               RefModel ticgitBranch = null;\r
-               try {\r
-                       // search for ticgit branch in local heads\r
-                       for (RefModel ref : getLocalBranches(r, -1)) {\r
-                               if (ref.displayName.endsWith("ticgit")) {\r
-                                       ticgitBranch = ref;\r
-                                       break;\r
-                               }\r
-                       }\r
-\r
-                       // search for ticgit branch in remote heads\r
-                       if (ticgitBranch == null) {\r
-                               for (RefModel ref : getRemoteBranches(r, -1)) {\r
-                                       if (ref.displayName.endsWith("ticgit")) {\r
-                                               ticgitBranch = ref;\r
-                                               break;\r
-                                       }\r
-                               }\r
-                       }\r
-               } catch (Throwable t) {\r
-                       LOGGER.error("Failed to find ticgit branch!", t);\r
-               }\r
-               return ticgitBranch;\r
-       }\r
-\r
-       public static List<TicketModel> getTickets(Repository r) {\r
-               RefModel ticgitBranch = getTicketsBranch(r);\r
-               List<PathModel> paths = getFilesInPath(r, null, ticgitBranch.commit);\r
-               List<TicketModel> tickets = new ArrayList<TicketModel>();\r
-               for (PathModel ticketFolder : paths) {\r
-                       if (ticketFolder.isTree()) {\r
-                               try {\r
-                                       TicketModel t = new TicketModel(ticketFolder.name);\r
-                                       readTicketContents(r, ticgitBranch, t);\r
-                                       tickets.add(t);\r
-                               } catch (Throwable t) {\r
-                                       LOGGER.error("Failed to get a ticket!", t);\r
-                               }\r
-                       }\r
-               }\r
-               Collections.sort(tickets);\r
-               Collections.reverse(tickets);\r
-               return tickets;\r
-       }\r
-\r
-       public static TicketModel getTicket(Repository r, String ticketFolder) {\r
-               RefModel ticketsBranch = getTicketsBranch(r);\r
-               if (ticketsBranch != null) {\r
-                       try {\r
-                               TicketModel ticket = new TicketModel(ticketFolder);\r
-                               readTicketContents(r, ticketsBranch, ticket);\r
-                               return ticket;\r
-                       } catch (Throwable t) {\r
-                               LOGGER.error("Failed to get ticket " + ticketFolder, t);\r
-                       }\r
-               }\r
-               return null;\r
-       }\r
-\r
-       private static void readTicketContents(Repository r, RefModel ticketsBranch, TicketModel ticket) {\r
-               List<PathModel> ticketFiles = getFilesInPath(r, ticket.name, ticketsBranch.commit);\r
-               for (PathModel file : ticketFiles) {\r
-                       String content = getRawContentAsString(r, ticketsBranch.commit, file.path).trim();\r
-                       if (file.name.equals("TICKET_ID")) {\r
-                               ticket.id = content;\r
-                       } else if (file.name.equals("TITLE")) {\r
-                               ticket.title = content;\r
-                       } else {\r
-                               String[] chunks = file.name.split("_");\r
-                               if (chunks[0].equals("ASSIGNED")) {\r
-                                       ticket.handler = content;\r
-                               } else if (chunks[0].equals("COMMENT")) {\r
-                                       try {\r
-                                               Comment c = new Comment(file.name, content);\r
-                                               ticket.comments.add(c);\r
-                                       } catch (ParseException e) {\r
-                                               e.printStackTrace();\r
-                                       }\r
-                               } else if (chunks[0].equals("TAG")) {\r
-                                       if (content.startsWith("TAG_")) {\r
-                                               ticket.tags.add(content.substring(4));\r
-                                       } else {\r
-                                               ticket.tags.add(content);\r
-                                       }\r
-                               } else if (chunks[0].equals("STATE")) {\r
-                                       ticket.state = content;\r
-                               }\r
-                       }\r
-               }\r
-               Collections.sort(ticket.comments);\r
-       }\r
-\r
-       public static String getTicketContent(Repository r, String filePath) {\r
-               RefModel ticketsBranch = getTicketsBranch(r);\r
-               if (ticketsBranch != null) {\r
-                       return getRawContentAsString(r, ticketsBranch.commit, filePath);\r
-               }\r
-               return "";\r
-       }\r
 }\r
diff --git a/src/com/gitblit/utils/TicgitUtils.java b/src/com/gitblit/utils/TicgitUtils.java
new file mode 100644 (file)
index 0000000..914b813
--- /dev/null
@@ -0,0 +1,138 @@
+/*\r
+ * Copyright 2011 gitblit.com.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *     http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+package com.gitblit.utils;\r
+\r
+import java.text.ParseException;\r
+import java.util.ArrayList;\r
+import java.util.Collections;\r
+import java.util.List;\r
+\r
+import org.eclipse.jgit.lib.Repository;\r
+import org.slf4j.Logger;\r
+import org.slf4j.LoggerFactory;\r
+\r
+import com.gitblit.models.PathModel;\r
+import com.gitblit.models.RefModel;\r
+import com.gitblit.models.TicketModel;\r
+import com.gitblit.models.TicketModel.Comment;\r
+\r
+public class TicgitUtils {\r
+\r
+       static final Logger LOGGER = LoggerFactory.getLogger(TicgitUtils.class);\r
+\r
+       public static RefModel getTicketsBranch(Repository r) {\r
+               RefModel ticgitBranch = null;\r
+               try {\r
+                       // search for ticgit branch in local heads\r
+                       for (RefModel ref : JGitUtils.getLocalBranches(r, -1)) {\r
+                               if (ref.displayName.endsWith("ticgit")) {\r
+                                       ticgitBranch = ref;\r
+                                       break;\r
+                               }\r
+                       }\r
+\r
+                       // search for ticgit branch in remote heads\r
+                       if (ticgitBranch == null) {\r
+                               for (RefModel ref : JGitUtils.getRemoteBranches(r, -1)) {\r
+                                       if (ref.displayName.endsWith("ticgit")) {\r
+                                               ticgitBranch = ref;\r
+                                               break;\r
+                                       }\r
+                               }\r
+                       }\r
+               } catch (Throwable t) {\r
+                       LOGGER.error("Failed to find ticgit branch!", t);\r
+               }\r
+               return ticgitBranch;\r
+       }\r
+\r
+       public static List<TicketModel> getTickets(Repository r) {\r
+               RefModel ticgitBranch = getTicketsBranch(r);\r
+               List<PathModel> paths = JGitUtils.getFilesInPath(r, null, ticgitBranch.commit);\r
+               List<TicketModel> tickets = new ArrayList<TicketModel>();\r
+               for (PathModel ticketFolder : paths) {\r
+                       if (ticketFolder.isTree()) {\r
+                               try {\r
+                                       TicketModel t = new TicketModel(ticketFolder.name);\r
+                                       readTicketContents(r, ticgitBranch, t);\r
+                                       tickets.add(t);\r
+                               } catch (Throwable t) {\r
+                                       LOGGER.error("Failed to get a ticket!", t);\r
+                               }\r
+                       }\r
+               }\r
+               Collections.sort(tickets);\r
+               Collections.reverse(tickets);\r
+               return tickets;\r
+       }\r
+\r
+       public static TicketModel getTicket(Repository r, String ticketFolder) {\r
+               RefModel ticketsBranch = getTicketsBranch(r);\r
+               if (ticketsBranch != null) {\r
+                       try {\r
+                               TicketModel ticket = new TicketModel(ticketFolder);\r
+                               readTicketContents(r, ticketsBranch, ticket);\r
+                               return ticket;\r
+                       } catch (Throwable t) {\r
+                               LOGGER.error("Failed to get ticket " + ticketFolder, t);\r
+                       }\r
+               }\r
+               return null;\r
+       }\r
+\r
+       private static void readTicketContents(Repository r, RefModel ticketsBranch, TicketModel ticket) {\r
+               List<PathModel> ticketFiles = JGitUtils\r
+                               .getFilesInPath(r, ticket.name, ticketsBranch.commit);\r
+               for (PathModel file : ticketFiles) {\r
+                       String content = JGitUtils.getRawContentAsString(r, ticketsBranch.commit, file.path)\r
+                                       .trim();\r
+                       if (file.name.equals("TICKET_ID")) {\r
+                               ticket.id = content;\r
+                       } else if (file.name.equals("TITLE")) {\r
+                               ticket.title = content;\r
+                       } else {\r
+                               String[] chunks = file.name.split("_");\r
+                               if (chunks[0].equals("ASSIGNED")) {\r
+                                       ticket.handler = content;\r
+                               } else if (chunks[0].equals("COMMENT")) {\r
+                                       try {\r
+                                               Comment c = new Comment(file.name, content);\r
+                                               ticket.comments.add(c);\r
+                                       } catch (ParseException e) {\r
+                                               e.printStackTrace();\r
+                                       }\r
+                               } else if (chunks[0].equals("TAG")) {\r
+                                       if (content.startsWith("TAG_")) {\r
+                                               ticket.tags.add(content.substring(4));\r
+                                       } else {\r
+                                               ticket.tags.add(content);\r
+                                       }\r
+                               } else if (chunks[0].equals("STATE")) {\r
+                                       ticket.state = content;\r
+                               }\r
+                       }\r
+               }\r
+               Collections.sort(ticket.comments);\r
+       }\r
+\r
+       public static String getTicketContent(Repository r, String filePath) {\r
+               RefModel ticketsBranch = getTicketsBranch(r);\r
+               if (ticketsBranch != null) {\r
+                       return JGitUtils.getRawContentAsString(r, ticketsBranch.commit, filePath);\r
+               }\r
+               return "";\r
+       }\r
+}\r
index ece87dd82b5981c8daf32460900496dbc1804068..44f51a5535040f03d382babc4fffaea6d00058b3 100644 (file)
@@ -15,6 +15,7 @@
  */\r
 package com.gitblit.utils;\r
 \r
+import java.util.Calendar;\r
 import java.util.Date;\r
 \r
 public class TimeUtils {\r
@@ -28,18 +29,15 @@ public class TimeUtils {
 \r
        public static final long ONEYEAR = ONEDAY * 365L;\r
 \r
-       @SuppressWarnings("deprecation")\r
        public static boolean isToday(Date date) {\r
-               Date now = new Date();\r
-               return now.getDate() == date.getDate() && now.getMonth() == date.getMonth()\r
-                               && now.getYear() == date.getYear();\r
+               return (System.currentTimeMillis() - date.getTime()) < ONEDAY; \r
        }\r
 \r
-       @SuppressWarnings("deprecation")\r
        public static boolean isYesterday(Date date) {\r
-               Date now = new Date();\r
-               return now.getDate() == (date.getDate() + 1) && now.getMonth() == date.getMonth()\r
-                               && now.getYear() == date.getYear();\r
+               Calendar cal = Calendar.getInstance();\r
+               cal.setTime(date);\r
+               cal.add(Calendar.DATE, 1);\r
+               return (System.currentTimeMillis() - cal.getTimeInMillis()) < ONEDAY; \r
        }\r
 \r
        public static String duration(int days) {\r
index 143d885ac3edbbd8efea5de2935b9a8cf9c12244..eceda99873f97eecd7847b1bc02dfc9a818726b0 100644 (file)
@@ -49,6 +49,7 @@ import com.gitblit.models.RepositoryModel;
 import com.gitblit.utils.JGitUtils;\r
 import com.gitblit.utils.JGitUtils.SearchType;\r
 import com.gitblit.utils.StringUtils;\r
+import com.gitblit.utils.TicgitUtils;\r
 import com.gitblit.wicket.GitBlitWebSession;\r
 import com.gitblit.wicket.WicketUtils;\r
 import com.gitblit.wicket.panels.LinkPanel;\r
@@ -111,7 +112,7 @@ public abstract class RepositoryPage extends BasePage {
                List<String> extraPageLinks = new ArrayList<String>();\r
 \r
                // Conditionally add tickets link\r
-               if (model.useTickets && JGitUtils.getTicketsBranch(r) != null) {\r
+               if (model.useTickets && TicgitUtils.getTicketsBranch(r) != null) {\r
                        extraPageLinks.add("tickets");\r
                }\r
 \r
index 353c543e82ddb11ab6968c84474c512b2f47e704..48db1cea2cfb1aeacc49d83dd95c607c0c8c7fa8 100644 (file)
@@ -24,8 +24,8 @@ import org.eclipse.jgit.lib.Repository;
 \r
 import com.gitblit.models.TicketModel;\r
 import com.gitblit.models.TicketModel.Comment;\r
-import com.gitblit.utils.JGitUtils;\r
 import com.gitblit.utils.StringUtils;\r
+import com.gitblit.utils.TicgitUtils;\r
 import com.gitblit.wicket.GitBlitWebSession;\r
 import com.gitblit.wicket.WicketUtils;\r
 \r
@@ -37,7 +37,7 @@ public class TicketPage extends RepositoryPage {
                final String ticketFolder = WicketUtils.getPath(params);\r
 \r
                Repository r = getRepository();\r
-               TicketModel t = JGitUtils.getTicket(r, ticketFolder);\r
+               TicketModel t = TicgitUtils.getTicket(r, ticketFolder);\r
 \r
                add(new Label("ticketTitle", t.title));\r
                add(new Label("ticketId", t.id));\r
index 8da4e942b983e554c99f14415c4bdfb420527e4c..f8473bd2cbe2417d56a163a72f4a364e4c4703de 100644 (file)
@@ -24,8 +24,8 @@ import org.apache.wicket.markup.repeater.data.DataView;
 import org.apache.wicket.markup.repeater.data.ListDataProvider;\r
 \r
 import com.gitblit.models.TicketModel;\r
-import com.gitblit.utils.JGitUtils;\r
 import com.gitblit.utils.StringUtils;\r
+import com.gitblit.utils.TicgitUtils;\r
 import com.gitblit.wicket.GitBlitWebSession;\r
 import com.gitblit.wicket.WicketUtils;\r
 import com.gitblit.wicket.panels.LinkPanel;\r
@@ -35,7 +35,7 @@ public class TicketsPage extends RepositoryPage {
        public TicketsPage(PageParameters params) {\r
                super(params);\r
 \r
-               List<TicketModel> tickets = JGitUtils.getTickets(getRepository());\r
+               List<TicketModel> tickets = TicgitUtils.getTickets(getRepository());\r
 \r
                // header\r
                add(new LinkPanel("header", "title", repositoryName, SummaryPage.class,\r
index d9963646b8b89d9daf37d279c77f946e500ed613..fcb57239e23b13a3cf3d6baf112fc2c4bedc1d82 100644 (file)
@@ -44,6 +44,8 @@ public class GitBlitSuite extends TestSetup {
                suite.addTestSuite(ByteFormatTest.class);\r
                suite.addTestSuite(JGitUtilsTest.class);\r
                suite.addTestSuite(DiffUtilsTest.class);\r
+               suite.addTestSuite(MetricUtilsTest.class);\r
+               suite.addTestSuite(TicgitUtilsTest.class);\r
                suite.addTestSuite(GitBlitTest.class);\r
                return new GitBlitSuite(suite);\r
        }\r
index 14d318f3cfe22f8ee65cfb6d8d2f83209d1ca285..f1dcaac4e53947c38ddf0de4dfe16533a13c7d9b 100644 (file)
@@ -30,13 +30,9 @@ import org.eclipse.jgit.revwalk.RevObject;
 import org.eclipse.jgit.revwalk.RevTree;\r
 \r
 import com.gitblit.GitBlit;\r
-import com.gitblit.models.Metric;\r
 import com.gitblit.models.PathModel.PathChangeModel;\r
 import com.gitblit.models.RefModel;\r
-import com.gitblit.models.TicketModel;\r
-import com.gitblit.models.TicketModel.Comment;\r
 import com.gitblit.utils.JGitUtils;\r
-import com.gitblit.utils.MetricUtils;\r
 \r
 public class JGitUtilsTest extends TestCase {\r
 \r
@@ -182,35 +178,4 @@ public class JGitUtilsTest extends TestCase {
                zipFile.delete();\r
                repository.close();\r
        }\r
-\r
-       public void testMetrics() throws Exception {\r
-               Repository repository = GitBlitSuite.getHelloworldRepository();\r
-               List<Metric> metrics = MetricUtils.getDateMetrics(repository, true);\r
-               repository.close();\r
-               assertTrue("No metrics found!", metrics.size() > 0);\r
-       }\r
-\r
-       public void testTicGit() throws Exception {\r
-               Repository repository = GitBlitSuite.getTicgitRepository();\r
-               RefModel branch = JGitUtils.getTicketsBranch(repository);\r
-               assertTrue("Ticgit branch does not exist!", branch != null);\r
-               List<TicketModel> ticketsA = JGitUtils.getTickets(repository);\r
-               List<TicketModel> ticketsB = JGitUtils.getTickets(repository);\r
-               repository.close();\r
-               assertTrue("No tickets found!", ticketsA.size() > 0);\r
-               for (int i = 0; i < ticketsA.size(); i++) {\r
-                       TicketModel ticketA = ticketsA.get(i);\r
-                       TicketModel ticketB = ticketsB.get(i);\r
-                       assertTrue("Tickets are not equal!", ticketA.equals(ticketB));\r
-                       assertFalse(ticketA.equals(""));\r
-                       assertTrue(ticketA.hashCode() == ticketA.id.hashCode());\r
-                       for (int j = 0; j < ticketA.comments.size(); j++) {\r
-                               Comment commentA = ticketA.comments.get(j);\r
-                               Comment commentB = ticketB.comments.get(j);\r
-                               assertTrue("Comments are not equal!", commentA.equals(commentB));\r
-                               assertFalse(commentA.equals(""));\r
-                               assertTrue(commentA.hashCode() == commentA.text.hashCode());\r
-                       }\r
-               }\r
-       }\r
 }
\ 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 (file)
index 0000000..77f43a1
--- /dev/null
@@ -0,0 +1,35 @@
+/*\r
+ * Copyright 2011 gitblit.com.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *     http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+package com.gitblit.tests;\r
+\r
+import java.util.List;\r
+\r
+import junit.framework.TestCase;\r
+\r
+import org.eclipse.jgit.lib.Repository;\r
+\r
+import com.gitblit.models.Metric;\r
+import com.gitblit.utils.MetricUtils;\r
+\r
+public class MetricUtilsTest extends TestCase {\r
+\r
+       public void testMetrics() throws Exception {\r
+               Repository repository = GitBlitSuite.getHelloworldRepository();\r
+               List<Metric> metrics = MetricUtils.getDateMetrics(repository, true);\r
+               repository.close();\r
+               assertTrue("No metrics found!", metrics.size() > 0);\r
+       }\r
+}
\ 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 (file)
index 0000000..25dba2c
--- /dev/null
@@ -0,0 +1,54 @@
+/*\r
+ * Copyright 2011 gitblit.com.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *     http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+package com.gitblit.tests;\r
+\r
+import java.util.List;\r
+\r
+import junit.framework.TestCase;\r
+\r
+import org.eclipse.jgit.lib.Repository;\r
+\r
+import com.gitblit.models.RefModel;\r
+import com.gitblit.models.TicketModel;\r
+import com.gitblit.models.TicketModel.Comment;\r
+import com.gitblit.utils.TicgitUtils;\r
+\r
+public class TicgitUtilsTest extends TestCase {\r
+\r
+       public void testTicGit() throws Exception {\r
+               Repository repository = GitBlitSuite.getTicgitRepository();\r
+               RefModel branch = TicgitUtils.getTicketsBranch(repository);\r
+               assertTrue("Ticgit branch does not exist!", branch != null);\r
+               List<TicketModel> ticketsA = TicgitUtils.getTickets(repository);\r
+               List<TicketModel> ticketsB = TicgitUtils.getTickets(repository);\r
+               repository.close();\r
+               assertTrue("No tickets found!", ticketsA.size() > 0);\r
+               for (int i = 0; i < ticketsA.size(); i++) {\r
+                       TicketModel ticketA = ticketsA.get(i);\r
+                       TicketModel ticketB = ticketsB.get(i);\r
+                       assertTrue("Tickets are not equal!", ticketA.equals(ticketB));\r
+                       assertFalse(ticketA.equals(""));\r
+                       assertTrue(ticketA.hashCode() == ticketA.id.hashCode());\r
+                       for (int j = 0; j < ticketA.comments.size(); j++) {\r
+                               Comment commentA = ticketA.comments.get(j);\r
+                               Comment commentB = ticketB.comments.get(j);\r
+                               assertTrue("Comments are not equal!", commentA.equals(commentB));\r
+                               assertFalse(commentA.equals(""));\r
+                               assertTrue(commentA.hashCode() == commentA.text.hashCode());\r
+                       }\r
+               }\r
+       }\r
+}
\ No newline at end of file