From 29692a574b10a01e070c0454868a3c234039ab7b Mon Sep 17 00:00:00 2001
From: James Moger <james.moger@gitblit.com>
Date: Wed, 02 Nov 2011 17:27:04 -0400
Subject: [PATCH] Fixed bug in time utils. Updated some unit tests.

---
 src/com/gitblit/utils/TimeUtils.java        |    8 ++-
 tests/com/gitblit/tests/RpcTests.java       |   64 +++++++++++++++++++++++++++----
 tests/com/gitblit/tests/GitServletTest.java |   24 +++++++-----
 3 files changed, 74 insertions(+), 22 deletions(-)

diff --git a/src/com/gitblit/utils/TimeUtils.java b/src/com/gitblit/utils/TimeUtils.java
index 3f0f412..7ac1e79 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.text.SimpleDateFormat;
 import java.util.Calendar;
 import java.util.Date;
 
@@ -53,9 +54,10 @@
 	 */
 	public static boolean isYesterday(Date date) {
 		Calendar cal = Calendar.getInstance();
-		cal.setTime(date);
-		cal.add(Calendar.DATE, 1);
-		return (System.currentTimeMillis() - cal.getTimeInMillis()) < ONEDAY;
+		cal.setTime(new Date());
+		cal.add(Calendar.DATE, -1);
+		SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
+		return df.format(cal.getTime()).equals(df.format(date));
 	}
 
 	/**
diff --git a/tests/com/gitblit/tests/GitServletTest.java b/tests/com/gitblit/tests/GitServletTest.java
index 6a83974..89466d7 100644
--- a/tests/com/gitblit/tests/GitServletTest.java
+++ b/tests/com/gitblit/tests/GitServletTest.java
@@ -8,25 +8,26 @@
 import java.util.Date;
 import java.util.concurrent.Executors;
 
-import junit.framework.TestCase;
-
 import org.eclipse.jgit.api.CloneCommand;
 import org.eclipse.jgit.api.Git;
 import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
 import org.eclipse.jgit.util.FileUtils;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
 
 import com.gitblit.GitBlitServer;
 
-public class GitServletTest extends TestCase {
+public class GitServletTest {
 
 	File folder = new File(GitBlitSuite.REPOSITORIES, "working/ticgit");
 
-	int port = 8180;
+	static int port = 8180;
 
-	int shutdownPort = 8181;
+	static int shutdownPort = 8181;
 
-	@Override
-	protected void setUp() throws Exception {
+	@BeforeClass
+	public static void startGitblit() throws Exception {
 		// Start a Gitblit instance
 		Executors.newSingleThreadExecutor().execute(new Runnable() {
 			public void run() {
@@ -41,8 +42,8 @@
 		Thread.sleep(2500);
 	}
 
-	@Override
-	protected void tearDown() throws Exception {
+	@AfterClass
+	public static void stopGitblit() throws Exception {
 		// Stop Gitblit
 		GitBlitServer.main("--stop", "--shutdownPort", "" + shutdownPort);
 
@@ -50,6 +51,7 @@
 		Thread.sleep(2500);
 	}
 
+	@Test
 	public void testClone() throws Exception {
 		if (folder.exists()) {
 			FileUtils.delete(folder, FileUtils.RECURSIVE);
@@ -62,6 +64,7 @@
 		clone.call();
 	}
 
+	@Test
 	public void testAnonymousCommit() throws Exception {
 		Git git = Git.open(folder);
 		File file = new File(folder, "TODO");
@@ -74,7 +77,8 @@
 		git.push().setPushAll().call();
 		git.getRepository().close();
 	}
-		
+
+	@Test
 	public void testBogusLoginClone() throws Exception {
 		File folder = new File(GitBlitSuite.REPOSITORIES, "working/gitblit");
 		if (folder.exists()) {
diff --git a/tests/com/gitblit/tests/RpcTests.java b/tests/com/gitblit/tests/RpcTests.java
index 37a4955..a64d1b8 100644
--- a/tests/com/gitblit/tests/RpcTests.java
+++ b/tests/com/gitblit/tests/RpcTests.java
@@ -15,16 +15,23 @@
  */
 package com.gitblit.tests;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
 import java.io.IOException;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.concurrent.Executors;
 
-import junit.framework.TestCase;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
 
 import com.gitblit.Constants.AccessRestrictionType;
 import com.gitblit.GitBlitException.UnauthorizedException;
+import com.gitblit.GitBlitServer;
 import com.gitblit.Keys;
 import com.gitblit.models.FederationModel;
 import com.gitblit.models.FederationProposal;
@@ -41,18 +48,48 @@
  * @author James Moger
  * 
  */
-public class RpcTests extends TestCase {
+public class RpcTests {
 
-	String url = "https://localhost:8443";
+	static int port = 8180;
+	static int shutdownPort = 8181;
+
+	String url = "http://localhost:" + port;
 	String account = "admin";
 	String password = "admin";
 
+	@BeforeClass
+	public static void startGitblit() throws Exception {
+		// Start a Gitblit instance
+		Executors.newSingleThreadExecutor().execute(new Runnable() {
+			public void run() {
+				GitBlitServer.main("--httpPort", "" + port, "--httpsPort", "0", "--shutdownPort",
+						"" + shutdownPort, "--repositoriesFolder",
+						"\"" + GitBlitSuite.REPOSITORIES.getAbsolutePath() + "\"", "--userService",
+						"distrib/users.properties");
+			}
+		});
+
+		// Wait a few seconds for it to be running
+		Thread.sleep(2500);
+	}
+
+	@AfterClass
+	public static void stopGitblit() throws Exception {
+		// Stop Gitblit
+		GitBlitServer.main("--stop", "--shutdownPort", "" + shutdownPort);
+
+		// Wait a few seconds for it to be running
+		Thread.sleep(2500);
+	}
+
+	@Test
 	public void testListRepositories() throws IOException {
 		Map<String, RepositoryModel> map = RpcUtils.getRepositories(url, null, null);
 		assertTrue("Repository list is null!", map != null);
 		assertTrue("Repository list is empty!", map.size() > 0);
 	}
 
+	@Test
 	public void testListUsers() throws IOException {
 		List<UserModel> list = null;
 		try {
@@ -65,6 +102,7 @@
 		assertTrue("User list is empty!", list.size() > 0);
 	}
 
+	@Test
 	public void testUserAdministration() throws IOException {
 		UserModel user = new UserModel("garbage");
 		user.canAdmin = true;
@@ -109,6 +147,7 @@
 		return retrievedUser;
 	}
 
+	@Test
 	public void testRepositoryAdministration() throws IOException {
 		RepositoryModel model = new RepositoryModel();
 		model.name = "garbagerepo.git";
@@ -186,39 +225,46 @@
 		return retrievedRepository;
 	}
 
+	@Test
 	public void testFederationRegistrations() throws Exception {
 		List<FederationModel> registrations = RpcUtils.getFederationRegistrations(url, account,
 				password.toCharArray());
-		assertTrue("No federation registrations wre retrieved!", registrations.size() > 0);
+		assertTrue("No federation registrations were retrieved!", registrations.size() >= 0);
 	}
 
+	@Test
 	public void testFederationResultRegistrations() throws Exception {
 		List<FederationModel> registrations = RpcUtils.getFederationResultRegistrations(url,
 				account, password.toCharArray());
-		assertTrue("No federation result registrations were retrieved!", registrations.size() > 0);
+		assertTrue("No federation result registrations were retrieved!", registrations.size() >= 0);
 	}
 
+	@Test
 	public void testFederationProposals() throws Exception {
 		List<FederationProposal> proposals = RpcUtils.getFederationProposals(url, account,
 				password.toCharArray());
-		assertTrue("No federation proposals were retrieved!", proposals.size() > 0);
+		assertTrue("No federation proposals were retrieved!", proposals.size() >= 0);
 	}
 
+	@Test
 	public void testFederationSets() throws Exception {
 		List<FederationSet> sets = RpcUtils.getFederationSets(url, account, password.toCharArray());
-		assertTrue("No federation sets were retrieved!", sets.size() > 0);
+		assertTrue("No federation sets were retrieved!", sets.size() >= 0);
 	}
 
+	@Test
 	public void testSettings() throws Exception {
 		ServerSettings settings = RpcUtils.getSettings(url, account, password.toCharArray());
 		assertTrue("No settings were retrieved!", settings != null);
 	}
 
+	@Test
 	public void testServerStatus() throws Exception {
 		ServerStatus status = RpcUtils.getStatus(url, account, password.toCharArray());
 		assertTrue("No status was retrieved!", status != null);
 	}
 
+	@Test
 	public void testUpdateSettings() throws Exception {
 		Map<String, String> updated = new HashMap<String, String>();
 
@@ -229,8 +275,7 @@
 
 		// update setting
 		updated.put(Keys.web.showRepositorySizes, String.valueOf(showSizes));
-		boolean success = RpcUtils.updateSettings(updated, "http://localhost:8080/gb", account,
-				password.toCharArray());
+		boolean success = RpcUtils.updateSettings(updated, url, account, password.toCharArray());
 		assertTrue("Failed to update server settings", success);
 
 		// confirm setting change
@@ -243,6 +288,7 @@
 		updated.put(Keys.web.showRepositorySizes, String.valueOf(newValue));
 	}
 
+	@Test
 	public void testBranches() throws Exception {
 		Map<String, Collection<String>> branches = RpcUtils.getBranches(url, account,
 				password.toCharArray());

--
Gitblit v1.9.1