Browse Source

Refactored some unit tests and utils.

tags/v0.5.0
James Moger 13 years ago
parent
commit
793f76563d

+ 0
- 101
src/com/gitblit/utils/JGitUtils.java View File

@@ -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 "";
}
}

+ 138
- 0
src/com/gitblit/utils/TicgitUtils.java View File

@@ -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 "";
}
}

+ 6
- 8
src/com/gitblit/utils/TimeUtils.java View File

@@ -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) {

+ 2
- 1
src/com/gitblit/wicket/pages/RepositoryPage.java View File

@@ -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");
}

+ 2
- 2
src/com/gitblit/wicket/pages/TicketPage.java View File

@@ -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));

+ 2
- 2
src/com/gitblit/wicket/pages/TicketsPage.java View File

@@ -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,

+ 2
- 0
tests/com/gitblit/tests/GitBlitSuite.java View File

@@ -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);
}

+ 0
- 35
tests/com/gitblit/tests/JGitUtilsTest.java View File

@@ -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());
}
}
}
}

+ 35
- 0
tests/com/gitblit/tests/MetricUtilsTest.java View File

@@ -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);
}
}

+ 54
- 0
tests/com/gitblit/tests/TicgitUtilsTest.java View File

@@ -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());
}
}
}
}

Loading…
Cancel
Save