int mins = 60;\r
if (!StringUtils.isEmpty(frequency)) {\r
try {\r
- String str;\r
+ String str = frequency.trim();\r
if (frequency.indexOf(' ') > -1) {\r
- str = frequency.substring(0, frequency.indexOf(' ')).trim();\r
- } else {\r
- str = frequency.trim();\r
+ str = str.substring(0, str.indexOf(' ')).trim();\r
}\r
mins = (int) Float.parseFloat(str);\r
} catch (NumberFormatException e) {\r
}\r
if (frequency.indexOf("day") > -1) {\r
// convert to minutes\r
- mins *= 24 * 60;\r
+ mins *= 1440;\r
} else if (frequency.indexOf("hour") > -1) {\r
// convert to minutes\r
mins *= 60;\r
--- /dev/null
+/*\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 junit.framework.TestCase;\r
+\r
+import com.gitblit.utils.Base64;\r
+\r
+public class Base64Test extends TestCase {\r
+\r
+ public void testBase64() {\r
+ String source = "this is a test"; \r
+ String base64 = Base64.encodeBytes(source.getBytes());\r
+ assertEquals("dGhpcyBpcyBhIHRlc3Q=", base64);\r
+ String decoded = new String(Base64.decode(base64));\r
+ assertEquals(source, decoded);\r
+ }\r
+}
\ No newline at end of file
String rawContent = FileUtils.readContent(new File(dir, "LICENSE"), "\n");\r
assertTrue(rawContent.trim().startsWith("Apache License"));\r
}\r
+ \r
+ public void testWriteContent() throws Exception {\r
+ String contentA = "this is a test";\r
+ File tmp = File.createTempFile("gitblit-", ".test");\r
+ FileUtils.writeContent(tmp, contentA);\r
+ String contentB = FileUtils.readContent(tmp, "\n").trim();\r
+ assertEquals(contentA, contentB);\r
+ }\r
\r
public void testFolderSize() throws Exception {\r
assertEquals(-1, FileUtils.folderSize(null));\r
package com.gitblit.tests;\r
\r
import java.io.File;\r
+import java.util.concurrent.Executors;\r
\r
import junit.extensions.TestSetup;\r
import junit.framework.Test;\r
import com.gitblit.FileUserService;\r
import com.gitblit.GitBlit;\r
import com.gitblit.GitBlitException;\r
+import com.gitblit.GitBlitServer;\r
import com.gitblit.models.RepositoryModel;\r
import com.gitblit.utils.JGitUtils;\r
\r
public class GitBlitSuite extends TestSetup {\r
+\r
public static final File REPOSITORIES = new File("git");\r
\r
+ static int port = 8280;\r
+ static int shutdownPort = 8281;\r
+\r
+ public static String url = "http://localhost:" + port;\r
+ public static String account = "admin";\r
+ public static String password = "admin";\r
+\r
private GitBlitSuite(TestSuite suite) {\r
super(suite);\r
}\r
suite.addTestSuite(FileUtilsTest.class);\r
suite.addTestSuite(TimeUtilsTest.class);\r
suite.addTestSuite(StringUtilsTest.class);\r
+ suite.addTestSuite(Base64Test.class);\r
+ suite.addTestSuite(JsonUtilsTest.class);\r
suite.addTestSuite(ByteFormatTest.class);\r
+ suite.addTestSuite(ObjectCacheTest.class);\r
suite.addTestSuite(MarkdownUtilsTest.class);\r
suite.addTestSuite(JGitUtilsTest.class);\r
suite.addTestSuite(SyndicationUtilsTest.class);\r
suite.addTestSuite(MetricUtilsTest.class);\r
suite.addTestSuite(TicgitUtilsTest.class);\r
suite.addTestSuite(GitBlitTest.class);\r
+ suite.addTestSuite(RpcTests.class);\r
return new GitBlitSuite(suite);\r
}\r
\r
return new FileRepository(new File(REPOSITORIES, "test/bluez-gnome.git"));\r
}\r
\r
+ public static void startGitblit() throws Exception {\r
+ // Start a Gitblit instance\r
+ Executors.newSingleThreadExecutor().execute(new Runnable() {\r
+ public void run() {\r
+ GitBlitServer.main("--httpPort", "" + port, "--httpsPort", "0", "--shutdownPort",\r
+ "" + shutdownPort, "--repositoriesFolder",\r
+ "\"" + GitBlitSuite.REPOSITORIES.getAbsolutePath() + "\"", "--userService",\r
+ "distrib/users.properties");\r
+ }\r
+ });\r
+\r
+ // Wait a few seconds for it to be running\r
+ Thread.sleep(2500);\r
+ }\r
+\r
+ public static void stopGitblit() throws Exception {\r
+ // Stop Gitblit\r
+ GitBlitServer.main("--stop", "--shutdownPort", "" + shutdownPort);\r
+\r
+ // Wait a few seconds for it to be running\r
+ Thread.sleep(2500);\r
+ }\r
+\r
@Override\r
protected void setUp() throws Exception {\r
FileSettings settings = new FileSettings("distrib/gitblit.properties");\r
showRemoteBranches("ticgit.git");\r
showRemoteBranches("test/jgit.git");\r
}\r
+\r
+ startGitblit();\r
+ }\r
+\r
+ @Override\r
+ protected void tearDown() throws Exception {\r
+ stopGitblit();\r
}\r
\r
private void cloneOrFetch(String name, String fromUrl) throws Exception {\r
public void testGitblitSettings() throws Exception {\r
// These are already tested by above test method.\r
assertTrue(GitBlit.getBoolean("missing", true));\r
- assertTrue(GitBlit.getString("missing", "default").equals("default"));\r
- assertTrue(GitBlit.getInteger("missing", 10) == 10);\r
- assertTrue(GitBlit.getInteger("realm.userService", 5) == 5);\r
+ assertEquals("default", GitBlit.getString("missing", "default"));\r
+ assertEquals(10, GitBlit.getInteger("missing", 10));\r
+ assertEquals(5, GitBlit.getInteger("realm.userService", 5));\r
\r
assertTrue(GitBlit.getBoolean("git.enableGitServlet", false));\r
- assertTrue(GitBlit.getString("realm.userService", null).equals("users.properties"));\r
- assertTrue(GitBlit.getInteger("realm.minPasswordLength", 0) == 5);\r
+ assertEquals("distrib/users.properties", GitBlit.getString("realm.userService", null));\r
+ assertEquals(5, GitBlit.getInteger("realm.minPasswordLength", 0));\r
List<String> mdExtensions = GitBlit.getStrings("web.markdownExtensions");\r
assertTrue(mdExtensions.size() > 0);\r
assertTrue(mdExtensions.contains("md"));\r
--- /dev/null
+/*\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.text.SimpleDateFormat;\r
+import java.util.Date;\r
+import java.util.HashMap;\r
+import java.util.Map;\r
+\r
+import junit.framework.TestCase;\r
+\r
+import com.gitblit.utils.JsonUtils;\r
+import com.google.gson.reflect.TypeToken;\r
+\r
+public class JsonUtilsTest extends TestCase {\r
+\r
+ public void testSerialization() {\r
+ Map<String, String> map = new HashMap<String, String>();\r
+ map.put("a", "alligator");\r
+ map.put("b", "bear");\r
+ map.put("c", "caterpillar");\r
+ map.put("d", "dingo");\r
+ map.put("e", "eagle");\r
+ String json = JsonUtils.toJsonString(map);\r
+ assertEquals(\r
+ "{\n \"d\": \"dingo\",\n \"e\": \"eagle\",\n \"b\": \"bear\",\n \"c\": \"caterpillar\",\n \"a\": \"alligator\"\n}",\r
+ json);\r
+ Map<String, String> map2 = JsonUtils.fromJsonString(json,\r
+ new TypeToken<Map<String, String>>() {\r
+ }.getType());\r
+ assertEquals(map, map2);\r
+\r
+ SomeJsonObject someJson = new SomeJsonObject();\r
+ json = JsonUtils.toJsonString(someJson);\r
+ SomeJsonObject someJson2 = JsonUtils.fromJsonString(json, SomeJsonObject.class);\r
+ assertEquals(someJson.name, someJson2.name);\r
+ SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd HHmmss");\r
+ assertEquals(df.format(someJson.date), df.format(someJson2.date));\r
+ }\r
+\r
+ private class SomeJsonObject {\r
+ Date date = new Date();\r
+ String name = "myJson";\r
+ }\r
+}
\ No newline at end of file
--- /dev/null
+/*\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.Date;\r
+\r
+import junit.framework.TestCase;\r
+\r
+import com.gitblit.utils.ObjectCache;\r
+\r
+public class ObjectCacheTest extends TestCase {\r
+\r
+ public void testCache() throws Exception {\r
+ ObjectCache<String> cache = new ObjectCache<String>();\r
+ cache.updateObject("test", "alpha");\r
+ Date date = cache.getDate("test");\r
+ assertTrue("cache date is not working!", cache.hasCurrent("test", date));\r
+ // The cache is time-based (msecs) so we insert this artificial sleep to\r
+ // ensure that time (msecs) advances. The ObjectCache class is suitable\r
+ // for Gitblit's needs but may not be suitable for other needs.\r
+ Thread.sleep(10);\r
+ cache.updateObject("test", "beta");\r
+ assertFalse("update cache date is not working!", cache.hasCurrent("test", date));\r
+ assertEquals("unexpected cache object", cache.getObject("test"), "beta");\r
+ assertEquals("beta", cache.remove("test"));\r
+ assertEquals(null, cache.getObject("test"));\r
+ assertEquals(null, cache.remove("test"));\r
+ }\r
+}\r
*/\r
package com.gitblit.tests;\r
\r
-import static org.junit.Assert.assertEquals;\r
-import static org.junit.Assert.assertTrue;\r
-\r
import java.io.IOException;\r
import java.util.Collection;\r
import java.util.HashMap;\r
import java.util.List;\r
import java.util.Map;\r
-import java.util.concurrent.Executors;\r
+\r
+import junit.framework.TestCase;\r
\r
import org.junit.AfterClass;\r
import org.junit.BeforeClass;\r
\r
import com.gitblit.Constants.AccessRestrictionType;\r
import com.gitblit.GitBlitException.UnauthorizedException;\r
-import com.gitblit.GitBlitServer;\r
import com.gitblit.Keys;\r
import com.gitblit.models.FederationModel;\r
import com.gitblit.models.FederationProposal;\r
* @author James Moger\r
* \r
*/\r
-public class RpcTests {\r
-\r
- static int port = 8180;\r
- static int shutdownPort = 8181;\r
-\r
- String url = "http://localhost:" + port;\r
- String account = "admin";\r
- String password = "admin";\r
+public class RpcTests extends TestCase {\r
+ \r
+ String url = GitBlitSuite.url;\r
+ String account = GitBlitSuite.account;\r
+ String password = GitBlitSuite.password;\r
+ \r
\r
@BeforeClass\r
public static void startGitblit() throws Exception {\r
- // Start a Gitblit instance\r
- Executors.newSingleThreadExecutor().execute(new Runnable() {\r
- public void run() {\r
- GitBlitServer.main("--httpPort", "" + port, "--httpsPort", "0", "--shutdownPort",\r
- "" + shutdownPort, "--repositoriesFolder",\r
- "\"" + GitBlitSuite.REPOSITORIES.getAbsolutePath() + "\"", "--userService",\r
- "distrib/users.properties");\r
- }\r
- });\r
-\r
- // Wait a few seconds for it to be running\r
- Thread.sleep(2500);\r
+ GitBlitSuite.startGitblit();\r
}\r
\r
@AfterClass\r
public static void stopGitblit() throws Exception {\r
- // Stop Gitblit\r
- GitBlitServer.main("--stop", "--shutdownPort", "" + shutdownPort);\r
-\r
- // Wait a few seconds for it to be running\r
- Thread.sleep(2500);\r
+ GitBlitSuite.stopGitblit();\r
}\r
\r
@Test\r
\r
import junit.framework.TestCase;\r
\r
+import com.gitblit.Constants.SearchType;\r
import com.gitblit.models.SyndicatedEntryModel;\r
import com.gitblit.utils.SyndicationUtils;\r
\r
entry.content = "Content " + i;\r
entry.repository = "Repository " + i;\r
entry.branch = "Branch " + i;\r
+ List<String> tags = new ArrayList<String>();\r
+ for (int j = 0; j < 5; j++) {\r
+ tags.add("Tag " + j);\r
+ }\r
+ entry.tags = tags;\r
entries.add(entry);\r
}\r
ByteArrayOutputStream os = new ByteArrayOutputStream();\r
public void testFeedRead() throws Exception {\r
Set<String> links = new HashSet<String>();\r
for (int i = 0; i < 2; i++) {\r
- List<SyndicatedEntryModel> feed = SyndicationUtils.readFeed("https://localhost:8443",\r
- "ticgit.git", "master", 5, i, "admin", "admin".toCharArray());\r
+ List<SyndicatedEntryModel> feed = SyndicationUtils.readFeed(GitBlitSuite.url,\r
+ "ticgit.git", "master", 5, i, GitBlitSuite.account,\r
+ GitBlitSuite.password.toCharArray());\r
assertTrue(feed != null);\r
assertTrue(feed.size() > 0);\r
assertEquals(5, feed.size());\r
}\r
\r
public void testSearchFeedRead() throws Exception {\r
- List<SyndicatedEntryModel> feed = SyndicationUtils.readSearchFeed("https://localhost:8443",\r
- "ticgit.git", null, "documentation", null, 5, 0, "admin", "admin".toCharArray());\r
+ List<SyndicatedEntryModel> feed = SyndicationUtils.readSearchFeed(GitBlitSuite.url,\r
+ "ticgit.git", null, "test", null, 5, 0, GitBlitSuite.account,\r
+ GitBlitSuite.password.toCharArray());\r
+ assertTrue(feed != null);\r
+ assertTrue(feed.size() > 0);\r
+ assertEquals(5, feed.size());\r
+ feed = SyndicationUtils.readSearchFeed(GitBlitSuite.url, "ticgit.git", "master", "test",\r
+ SearchType.COMMIT, 5, 1, GitBlitSuite.account, GitBlitSuite.password.toCharArray());\r
assertTrue(feed != null);\r
assertTrue(feed.size() > 0);\r
- assertEquals(2, feed.size());\r
+ assertEquals(5, feed.size());\r
}\r
}
\ No newline at end of file
}\r
\r
public void testDurations() throws Exception {\r
- assertTrue(TimeUtils.duration(1).equals("1 day"));\r
- assertTrue(TimeUtils.duration(5).equals("5 days"));\r
- assertTrue(TimeUtils.duration(75).equals("3 months"));\r
- assertTrue(TimeUtils.duration(364).equals("12 months"));\r
- assertTrue(TimeUtils.duration(365 + 0).equals("1 year"));\r
- assertTrue(TimeUtils.duration(365 + 10).equals("1 year"));\r
- assertTrue(TimeUtils.duration(365 + 15).equals("1 year, 1 month"));\r
- assertTrue(TimeUtils.duration(365 + 30).equals("1 year, 1 month"));\r
- assertTrue(TimeUtils.duration(365 + 44).equals("1 year, 1 month"));\r
- assertTrue(TimeUtils.duration(365 + 45).equals("1 year, 2 months"));\r
- assertTrue(TimeUtils.duration(365 + 60).equals("1 year, 2 months"));\r
+ assertEquals("1 day", TimeUtils.duration(1));\r
+ assertEquals("5 days", TimeUtils.duration(5));\r
+ assertEquals("3 months", TimeUtils.duration(75));\r
+ assertEquals("12 months", TimeUtils.duration(364));\r
+ assertEquals("1 year", TimeUtils.duration(365 + 0));\r
+ assertEquals("1 year", TimeUtils.duration(365 + 10));\r
+ assertEquals("1 year, 1 month", TimeUtils.duration(365 + 15));\r
+ assertEquals("1 year, 1 month", TimeUtils.duration(365 + 30));\r
+ assertEquals("1 year, 1 month", TimeUtils.duration(365 + 44));\r
+ assertEquals("1 year, 2 months", TimeUtils.duration(365 + 45));\r
+ assertEquals("1 year, 2 months", TimeUtils.duration(365 + 60));\r
\r
- assertTrue(TimeUtils.duration(2 * 365 + 0).equals("2 years"));\r
- assertTrue(TimeUtils.duration(2 * 365 + 10).equals("2 years"));\r
- assertTrue(TimeUtils.duration(2 * 365 + 15).equals("2 years, 1 month"));\r
- assertTrue(TimeUtils.duration(2 * 365 + 30).equals("2 years, 1 month"));\r
- assertTrue(TimeUtils.duration(2 * 365 + 44).equals("2 years, 1 month"));\r
- assertTrue(TimeUtils.duration(2 * 365 + 45).equals("2 years, 2 months"));\r
- assertTrue(TimeUtils.duration(2 * 365 + 60).equals("2 years, 2 months"));\r
+ assertEquals("2 years", TimeUtils.duration(2 * 365 + 0));\r
+ assertEquals("2 years", TimeUtils.duration(2 * 365 + 10));\r
+ assertEquals("2 years, 1 month", TimeUtils.duration(2 * 365 + 15));\r
+ assertEquals("2 years, 1 month", TimeUtils.duration(2 * 365 + 30));\r
+ assertEquals("2 years, 1 month", TimeUtils.duration(2 * 365 + 44));\r
+ assertEquals("2 years, 2 months", TimeUtils.duration(2 * 365 + 45));\r
+ assertEquals("2 years, 2 months", TimeUtils.duration(2 * 365 + 60));\r
}\r
\r
public void testTimeAgo() throws Exception {\r
// standard time ago tests\r
- assertTrue(TimeUtils.timeAgo(offset(1 * TimeUtils.MIN)).equals("1 min ago"));\r
- assertTrue(TimeUtils.timeAgo(offset(60 * TimeUtils.MIN)).equals("60 mins ago"));\r
- assertTrue(TimeUtils.timeAgo(offset(120 * TimeUtils.MIN)).equals("2 hours ago"));\r
- assertTrue(TimeUtils.timeAgo(offset(15 * TimeUtils.ONEHOUR)).equals("15 hours ago"));\r
- assertTrue(TimeUtils.timeAgo(offset(24 * TimeUtils.ONEHOUR)).equals("yesterday"));\r
- assertTrue(TimeUtils.timeAgo(offset(2 * TimeUtils.ONEDAY)).equals("2 days ago"));\r
- assertTrue(TimeUtils.timeAgo(offset(35 * TimeUtils.ONEDAY)).equals("5 weeks ago"));\r
- assertTrue(TimeUtils.timeAgo(offset(84 * TimeUtils.ONEDAY)).equals("3 months ago"));\r
- assertTrue(TimeUtils.timeAgo(offset(95 * TimeUtils.ONEDAY)).equals("3 months ago"));\r
- assertTrue(TimeUtils.timeAgo(offset(104 * TimeUtils.ONEDAY)).equals("4 months ago"));\r
- assertTrue(TimeUtils.timeAgo(offset(365 * TimeUtils.ONEDAY)).equals("1 year ago"));\r
- assertTrue(TimeUtils.timeAgo(offset(395 * TimeUtils.ONEDAY)).equals("13 months ago"));\r
- assertTrue(TimeUtils.timeAgo(offset((2 * 365 + 30) * TimeUtils.ONEDAY)).equals(\r
- "2 years ago"));\r
+ assertEquals("1 min ago", TimeUtils.timeAgo(offset(1 * TimeUtils.MIN)));\r
+ assertEquals("60 mins ago", TimeUtils.timeAgo(offset(60 * TimeUtils.MIN)));\r
+ assertEquals("2 hours ago", TimeUtils.timeAgo(offset(120 * TimeUtils.MIN)));\r
+ assertEquals("15 hours ago", TimeUtils.timeAgo(offset(15 * TimeUtils.ONEHOUR)));\r
+ assertEquals("yesterday", TimeUtils.timeAgo(offset(24 * TimeUtils.ONEHOUR)));\r
+ assertEquals("2 days ago", TimeUtils.timeAgo(offset(2 * TimeUtils.ONEDAY)));\r
+ assertEquals("5 weeks ago", TimeUtils.timeAgo(offset(35 * TimeUtils.ONEDAY)));\r
+ assertEquals("3 months ago", TimeUtils.timeAgo(offset(84 * TimeUtils.ONEDAY)));\r
+ assertEquals("3 months ago", TimeUtils.timeAgo(offset(95 * TimeUtils.ONEDAY)));\r
+ assertEquals("4 months ago", TimeUtils.timeAgo(offset(104 * TimeUtils.ONEDAY)));\r
+ assertEquals("1 year ago", TimeUtils.timeAgo(offset(365 * TimeUtils.ONEDAY)));\r
+ assertEquals("13 months ago", TimeUtils.timeAgo(offset(395 * TimeUtils.ONEDAY)));\r
+ assertEquals("2 years ago", TimeUtils.timeAgo(offset((2 * 365 + 30) * TimeUtils.ONEDAY)));\r
\r
// css class tests\r
- assertTrue(TimeUtils.timeAgoCss(offset(1 * TimeUtils.MIN)).equals("age0"));\r
- assertTrue(TimeUtils.timeAgoCss(offset(60 * TimeUtils.MIN)).equals("age0"));\r
- assertTrue(TimeUtils.timeAgoCss(offset(120 * TimeUtils.MIN)).equals("age1"));\r
- assertTrue(TimeUtils.timeAgoCss(offset(24 * TimeUtils.ONEHOUR)).equals("age1"));\r
- assertTrue(TimeUtils.timeAgoCss(offset(2 * TimeUtils.ONEDAY)).equals("age2"));\r
+ assertEquals("age0", TimeUtils.timeAgoCss(offset(1 * TimeUtils.MIN)));\r
+ assertEquals("age0", TimeUtils.timeAgoCss(offset(60 * TimeUtils.MIN)));\r
+ assertEquals("age1", TimeUtils.timeAgoCss(offset(120 * TimeUtils.MIN)));\r
+ assertEquals("age1", TimeUtils.timeAgoCss(offset(24 * TimeUtils.ONEHOUR)));\r
+ assertEquals("age2", TimeUtils.timeAgoCss(offset(2 * TimeUtils.ONEDAY)));\r
+ }\r
+\r
+ public void testFrequency() {\r
+ assertEquals(5, TimeUtils.convertFrequencyToMinutes("2 mins"));\r
+ assertEquals(10, TimeUtils.convertFrequencyToMinutes("10 mins"));\r
+ assertEquals(600, TimeUtils.convertFrequencyToMinutes("10 hours"));\r
+ assertEquals(14400, TimeUtils.convertFrequencyToMinutes(" 10 days "));\r
}\r
}\r