summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJames Moger <james.moger@gitblit.com>2011-06-01 21:01:51 -0400
committerJames Moger <james.moger@gitblit.com>2011-06-01 21:01:51 -0400
commit793f76563d4bb3f58fa62ff53985e20561c6e330 (patch)
tree15ce25c40c0a4198f1a47f890250430ed9b6e941
parenta3bde69e9b3f3cc6212b0cd5fd7310b6a3301d8c (diff)
downloadgitblit-793f76563d4bb3f58fa62ff53985e20561c6e330.tar.gz
gitblit-793f76563d4bb3f58fa62ff53985e20561c6e330.zip
Refactored some unit tests and utils.
-rw-r--r--src/com/gitblit/utils/JGitUtils.java101
-rw-r--r--src/com/gitblit/utils/TicgitUtils.java138
-rw-r--r--src/com/gitblit/utils/TimeUtils.java14
-rw-r--r--src/com/gitblit/wicket/pages/RepositoryPage.java3
-rw-r--r--src/com/gitblit/wicket/pages/TicketPage.java4
-rw-r--r--src/com/gitblit/wicket/pages/TicketsPage.java4
-rw-r--r--tests/com/gitblit/tests/GitBlitSuite.java2
-rw-r--r--tests/com/gitblit/tests/JGitUtilsTest.java35
-rw-r--r--tests/com/gitblit/tests/MetricUtilsTest.java35
-rw-r--r--tests/com/gitblit/tests/TicgitUtilsTest.java54
10 files changed, 241 insertions, 149 deletions
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<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 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<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 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<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 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<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 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<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 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<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 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<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