James Moger
2011-11-07 143fc9d357c174a56340052618d481617686fc72
Unit testing
3 files added
7 files modified
344 ■■■■ changed files
src/com/gitblit/utils/TimeUtils.java 8 ●●●●● patch | view | raw | blame | history
tests/com/gitblit/tests/Base64Test.java 31 ●●●●● patch | view | raw | blame | history
tests/com/gitblit/tests/FileUtilsTest.java 8 ●●●●● patch | view | raw | blame | history
tests/com/gitblit/tests/GitBlitSuite.java 44 ●●●●● patch | view | raw | blame | history
tests/com/gitblit/tests/GitBlitTest.java 10 ●●●● patch | view | raw | blame | history
tests/com/gitblit/tests/JsonUtilsTest.java 58 ●●●●● patch | view | raw | blame | history
tests/com/gitblit/tests/ObjectCacheTest.java 42 ●●●●● patch | view | raw | blame | history
tests/com/gitblit/tests/RpcTests.java 40 ●●●● patch | view | raw | blame | history
tests/com/gitblit/tests/SyndicationUtilsTest.java 23 ●●●● patch | view | raw | blame | history
tests/com/gitblit/tests/TimeUtilsTest.java 80 ●●●● patch | view | raw | blame | history
src/com/gitblit/utils/TimeUtils.java
@@ -253,11 +253,9 @@
        int mins = 60;
        if (!StringUtils.isEmpty(frequency)) {
            try {
                String str;
                String str = frequency.trim();
                if (frequency.indexOf(' ') > -1) {
                    str = frequency.substring(0, frequency.indexOf(' ')).trim();
                } else {
                    str = frequency.trim();
                    str = str.substring(0, str.indexOf(' ')).trim();
                }
                mins = (int) Float.parseFloat(str);
            } catch (NumberFormatException e) {
@@ -268,7 +266,7 @@
        }
        if (frequency.indexOf("day") > -1) {
            // convert to minutes
            mins *= 24 * 60;
            mins *= 1440;
        } else if (frequency.indexOf("hour") > -1) {
            // convert to minutes
            mins *= 60;
tests/com/gitblit/tests/Base64Test.java
New file
@@ -0,0 +1,31 @@
/*
 * 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 junit.framework.TestCase;
import com.gitblit.utils.Base64;
public class Base64Test extends TestCase {
    public void testBase64() {
        String source = "this is a test";
        String base64 = Base64.encodeBytes(source.getBytes());
        assertEquals("dGhpcyBpcyBhIHRlc3Q=", base64);
        String decoded = new String(Base64.decode(base64));
        assertEquals(source, decoded);
    }
}
tests/com/gitblit/tests/FileUtilsTest.java
@@ -28,6 +28,14 @@
        String rawContent = FileUtils.readContent(new File(dir, "LICENSE"), "\n");
        assertTrue(rawContent.trim().startsWith("Apache License"));
    }
    public void testWriteContent() throws Exception {
        String contentA = "this is a test";
        File tmp = File.createTempFile("gitblit-", ".test");
        FileUtils.writeContent(tmp, contentA);
        String contentB = FileUtils.readContent(tmp, "\n").trim();
        assertEquals(contentA, contentB);
    }
    public void testFolderSize() throws Exception {
        assertEquals(-1, FileUtils.folderSize(null));
tests/com/gitblit/tests/GitBlitSuite.java
@@ -16,6 +16,7 @@
package com.gitblit.tests;
import java.io.File;
import java.util.concurrent.Executors;
import junit.extensions.TestSetup;
import junit.framework.Test;
@@ -28,11 +29,20 @@
import com.gitblit.FileUserService;
import com.gitblit.GitBlit;
import com.gitblit.GitBlitException;
import com.gitblit.GitBlitServer;
import com.gitblit.models.RepositoryModel;
import com.gitblit.utils.JGitUtils;
public class GitBlitSuite extends TestSetup {
    public static final File REPOSITORIES = new File("git");
    static int port = 8280;
    static int shutdownPort = 8281;
    public static String url = "http://localhost:" + port;
    public static String account = "admin";
    public static String password = "admin";
    private GitBlitSuite(TestSuite suite) {
        super(suite);
@@ -43,7 +53,10 @@
        suite.addTestSuite(FileUtilsTest.class);
        suite.addTestSuite(TimeUtilsTest.class);
        suite.addTestSuite(StringUtilsTest.class);
        suite.addTestSuite(Base64Test.class);
        suite.addTestSuite(JsonUtilsTest.class);
        suite.addTestSuite(ByteFormatTest.class);
        suite.addTestSuite(ObjectCacheTest.class);
        suite.addTestSuite(MarkdownUtilsTest.class);
        suite.addTestSuite(JGitUtilsTest.class);
        suite.addTestSuite(SyndicationUtilsTest.class);
@@ -51,6 +64,7 @@
        suite.addTestSuite(MetricUtilsTest.class);
        suite.addTestSuite(TicgitUtilsTest.class);
        suite.addTestSuite(GitBlitTest.class);
        suite.addTestSuite(RpcTests.class);
        return new GitBlitSuite(suite);
    }
@@ -68,6 +82,29 @@
    public static Repository getBluezGnomeRepository() throws Exception {
        return new FileRepository(new File(REPOSITORIES, "test/bluez-gnome.git"));
    }
    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);
    }
    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);
    }
    @Override
@@ -90,6 +127,13 @@
            showRemoteBranches("ticgit.git");
            showRemoteBranches("test/jgit.git");
        }
        startGitblit();
    }
    @Override
    protected void tearDown() throws Exception {
        stopGitblit();
    }
    private void cloneOrFetch(String name, String fromUrl) throws Exception {
tests/com/gitblit/tests/GitBlitTest.java
@@ -111,13 +111,13 @@
    public void testGitblitSettings() throws Exception {
        // These are already tested by above test method.
        assertTrue(GitBlit.getBoolean("missing", true));
        assertTrue(GitBlit.getString("missing", "default").equals("default"));
        assertTrue(GitBlit.getInteger("missing", 10) == 10);
        assertTrue(GitBlit.getInteger("realm.userService", 5) == 5);
        assertEquals("default", GitBlit.getString("missing", "default"));
        assertEquals(10, GitBlit.getInteger("missing", 10));
        assertEquals(5, GitBlit.getInteger("realm.userService", 5));
        assertTrue(GitBlit.getBoolean("git.enableGitServlet", false));
        assertTrue(GitBlit.getString("realm.userService", null).equals("users.properties"));
        assertTrue(GitBlit.getInteger("realm.minPasswordLength", 0) == 5);
        assertEquals("distrib/users.properties", GitBlit.getString("realm.userService", null));
        assertEquals(5, GitBlit.getInteger("realm.minPasswordLength", 0));
        List<String> mdExtensions = GitBlit.getStrings("web.markdownExtensions");
        assertTrue(mdExtensions.size() > 0);
        assertTrue(mdExtensions.contains("md"));
tests/com/gitblit/tests/JsonUtilsTest.java
New file
@@ -0,0 +1,58 @@
/*
 * 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.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import junit.framework.TestCase;
import com.gitblit.utils.JsonUtils;
import com.google.gson.reflect.TypeToken;
public class JsonUtilsTest extends TestCase {
    public void testSerialization() {
        Map<String, String> map = new HashMap<String, String>();
        map.put("a", "alligator");
        map.put("b", "bear");
        map.put("c", "caterpillar");
        map.put("d", "dingo");
        map.put("e", "eagle");
        String json = JsonUtils.toJsonString(map);
        assertEquals(
                "{\n  \"d\": \"dingo\",\n  \"e\": \"eagle\",\n  \"b\": \"bear\",\n  \"c\": \"caterpillar\",\n  \"a\": \"alligator\"\n}",
                json);
        Map<String, String> map2 = JsonUtils.fromJsonString(json,
                new TypeToken<Map<String, String>>() {
                }.getType());
        assertEquals(map, map2);
        SomeJsonObject someJson = new SomeJsonObject();
        json = JsonUtils.toJsonString(someJson);
        SomeJsonObject someJson2 = JsonUtils.fromJsonString(json, SomeJsonObject.class);
        assertEquals(someJson.name, someJson2.name);
        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd HHmmss");
        assertEquals(df.format(someJson.date), df.format(someJson2.date));
    }
    private class SomeJsonObject {
        Date date = new Date();
        String name = "myJson";
    }
}
tests/com/gitblit/tests/ObjectCacheTest.java
New file
@@ -0,0 +1,42 @@
/*
 * 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.Date;
import junit.framework.TestCase;
import com.gitblit.utils.ObjectCache;
public class ObjectCacheTest extends TestCase {
    public void testCache() throws Exception {
        ObjectCache<String> cache = new ObjectCache<String>();
        cache.updateObject("test", "alpha");
        Date date = cache.getDate("test");
        assertTrue("cache date is not working!", cache.hasCurrent("test", date));
        // The cache is time-based (msecs) so we insert this artificial sleep to
        // ensure that time (msecs) advances. The ObjectCache class is suitable
        // for Gitblit's needs but may not be suitable for other needs.
        Thread.sleep(10);
        cache.updateObject("test", "beta");
        assertFalse("update cache date is not working!", cache.hasCurrent("test", date));
        assertEquals("unexpected cache object", cache.getObject("test"), "beta");
        assertEquals("beta", cache.remove("test"));
        assertEquals(null, cache.getObject("test"));
        assertEquals(null, cache.remove("test"));
    }
}
tests/com/gitblit/tests/RpcTests.java
@@ -15,15 +15,13 @@
 */
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;
@@ -31,7 +29,6 @@
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;
@@ -48,38 +45,21 @@
 * @author James Moger
 * 
 */
public class RpcTests {
    static int port = 8180;
    static int shutdownPort = 8181;
    String url = "http://localhost:" + port;
    String account = "admin";
    String password = "admin";
public class RpcTests extends TestCase {
    String url = GitBlitSuite.url;
    String account = GitBlitSuite.account;
    String password = GitBlitSuite.password;
    @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);
        GitBlitSuite.startGitblit();
    }
    @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);
        GitBlitSuite.stopGitblit();
    }
    @Test
tests/com/gitblit/tests/SyndicationUtilsTest.java
@@ -24,6 +24,7 @@
import junit.framework.TestCase;
import com.gitblit.Constants.SearchType;
import com.gitblit.models.SyndicatedEntryModel;
import com.gitblit.utils.SyndicationUtils;
@@ -41,6 +42,11 @@
            entry.content = "Content " + i;
            entry.repository = "Repository " + i;
            entry.branch = "Branch " + i;
            List<String> tags = new ArrayList<String>();
            for (int j = 0; j < 5; j++) {
                tags.add("Tag " + j);
            }
            entry.tags = tags;
            entries.add(entry);
        }
        ByteArrayOutputStream os = new ByteArrayOutputStream();
@@ -55,8 +61,9 @@
    public void testFeedRead() throws Exception {
        Set<String> links = new HashSet<String>();
        for (int i = 0; i < 2; i++) {
            List<SyndicatedEntryModel> feed = SyndicationUtils.readFeed("https://localhost:8443",
                    "ticgit.git", "master", 5, i, "admin", "admin".toCharArray());
            List<SyndicatedEntryModel> feed = SyndicationUtils.readFeed(GitBlitSuite.url,
                    "ticgit.git", "master", 5, i, GitBlitSuite.account,
                    GitBlitSuite.password.toCharArray());
            assertTrue(feed != null);
            assertTrue(feed.size() > 0);
            assertEquals(5, feed.size());
@@ -69,10 +76,16 @@
    }
    public void testSearchFeedRead() throws Exception {
        List<SyndicatedEntryModel> feed = SyndicationUtils.readSearchFeed("https://localhost:8443",
                "ticgit.git", null, "documentation", null, 5, 0, "admin", "admin".toCharArray());
        List<SyndicatedEntryModel> feed = SyndicationUtils.readSearchFeed(GitBlitSuite.url,
                "ticgit.git", null, "test", null, 5, 0, GitBlitSuite.account,
                GitBlitSuite.password.toCharArray());
        assertTrue(feed != null);
        assertTrue(feed.size() > 0);
        assertEquals(2, feed.size());
        assertEquals(5, feed.size());
        feed = SyndicationUtils.readSearchFeed(GitBlitSuite.url, "ticgit.git", "master", "test",
                SearchType.COMMIT, 5, 1, GitBlitSuite.account, GitBlitSuite.password.toCharArray());
        assertTrue(feed != null);
        assertTrue(feed.size() > 0);
        assertEquals(5, feed.size());
    }
}
tests/com/gitblit/tests/TimeUtilsTest.java
@@ -47,49 +47,55 @@
    }
    public void testDurations() throws Exception {
        assertTrue(TimeUtils.duration(1).equals("1 day"));
        assertTrue(TimeUtils.duration(5).equals("5 days"));
        assertTrue(TimeUtils.duration(75).equals("3 months"));
        assertTrue(TimeUtils.duration(364).equals("12 months"));
        assertTrue(TimeUtils.duration(365 + 0).equals("1 year"));
        assertTrue(TimeUtils.duration(365 + 10).equals("1 year"));
        assertTrue(TimeUtils.duration(365 + 15).equals("1 year, 1 month"));
        assertTrue(TimeUtils.duration(365 + 30).equals("1 year, 1 month"));
        assertTrue(TimeUtils.duration(365 + 44).equals("1 year, 1 month"));
        assertTrue(TimeUtils.duration(365 + 45).equals("1 year, 2 months"));
        assertTrue(TimeUtils.duration(365 + 60).equals("1 year, 2 months"));
        assertEquals("1 day", TimeUtils.duration(1));
        assertEquals("5 days", TimeUtils.duration(5));
        assertEquals("3 months", TimeUtils.duration(75));
        assertEquals("12 months", TimeUtils.duration(364));
        assertEquals("1 year", TimeUtils.duration(365 + 0));
        assertEquals("1 year", TimeUtils.duration(365 + 10));
        assertEquals("1 year, 1 month", TimeUtils.duration(365 + 15));
        assertEquals("1 year, 1 month", TimeUtils.duration(365 + 30));
        assertEquals("1 year, 1 month", TimeUtils.duration(365 + 44));
        assertEquals("1 year, 2 months", TimeUtils.duration(365 + 45));
        assertEquals("1 year, 2 months", TimeUtils.duration(365 + 60));
        assertTrue(TimeUtils.duration(2 * 365 + 0).equals("2 years"));
        assertTrue(TimeUtils.duration(2 * 365 + 10).equals("2 years"));
        assertTrue(TimeUtils.duration(2 * 365 + 15).equals("2 years, 1 month"));
        assertTrue(TimeUtils.duration(2 * 365 + 30).equals("2 years, 1 month"));
        assertTrue(TimeUtils.duration(2 * 365 + 44).equals("2 years, 1 month"));
        assertTrue(TimeUtils.duration(2 * 365 + 45).equals("2 years, 2 months"));
        assertTrue(TimeUtils.duration(2 * 365 + 60).equals("2 years, 2 months"));
        assertEquals("2 years", TimeUtils.duration(2 * 365 + 0));
        assertEquals("2 years", TimeUtils.duration(2 * 365 + 10));
        assertEquals("2 years, 1 month", TimeUtils.duration(2 * 365 + 15));
        assertEquals("2 years, 1 month", TimeUtils.duration(2 * 365 + 30));
        assertEquals("2 years, 1 month", TimeUtils.duration(2 * 365 + 44));
        assertEquals("2 years, 2 months", TimeUtils.duration(2 * 365 + 45));
        assertEquals("2 years, 2 months", TimeUtils.duration(2 * 365 + 60));
    }
    public void testTimeAgo() throws Exception {
        // standard time ago tests
        assertTrue(TimeUtils.timeAgo(offset(1 * TimeUtils.MIN)).equals("1 min ago"));
        assertTrue(TimeUtils.timeAgo(offset(60 * TimeUtils.MIN)).equals("60 mins ago"));
        assertTrue(TimeUtils.timeAgo(offset(120 * TimeUtils.MIN)).equals("2 hours ago"));
        assertTrue(TimeUtils.timeAgo(offset(15 * TimeUtils.ONEHOUR)).equals("15 hours ago"));
        assertTrue(TimeUtils.timeAgo(offset(24 * TimeUtils.ONEHOUR)).equals("yesterday"));
        assertTrue(TimeUtils.timeAgo(offset(2 * TimeUtils.ONEDAY)).equals("2 days ago"));
        assertTrue(TimeUtils.timeAgo(offset(35 * TimeUtils.ONEDAY)).equals("5 weeks ago"));
        assertTrue(TimeUtils.timeAgo(offset(84 * TimeUtils.ONEDAY)).equals("3 months ago"));
        assertTrue(TimeUtils.timeAgo(offset(95 * TimeUtils.ONEDAY)).equals("3 months ago"));
        assertTrue(TimeUtils.timeAgo(offset(104 * TimeUtils.ONEDAY)).equals("4 months ago"));
        assertTrue(TimeUtils.timeAgo(offset(365 * TimeUtils.ONEDAY)).equals("1 year ago"));
        assertTrue(TimeUtils.timeAgo(offset(395 * TimeUtils.ONEDAY)).equals("13 months ago"));
        assertTrue(TimeUtils.timeAgo(offset((2 * 365 + 30) * TimeUtils.ONEDAY)).equals(
                "2 years ago"));
        assertEquals("1 min ago", TimeUtils.timeAgo(offset(1 * TimeUtils.MIN)));
        assertEquals("60 mins ago", TimeUtils.timeAgo(offset(60 * TimeUtils.MIN)));
        assertEquals("2 hours ago", TimeUtils.timeAgo(offset(120 * TimeUtils.MIN)));
        assertEquals("15 hours ago", TimeUtils.timeAgo(offset(15 * TimeUtils.ONEHOUR)));
        assertEquals("yesterday", TimeUtils.timeAgo(offset(24 * TimeUtils.ONEHOUR)));
        assertEquals("2 days ago", TimeUtils.timeAgo(offset(2 * TimeUtils.ONEDAY)));
        assertEquals("5 weeks ago", TimeUtils.timeAgo(offset(35 * TimeUtils.ONEDAY)));
        assertEquals("3 months ago", TimeUtils.timeAgo(offset(84 * TimeUtils.ONEDAY)));
        assertEquals("3 months ago", TimeUtils.timeAgo(offset(95 * TimeUtils.ONEDAY)));
        assertEquals("4 months ago", TimeUtils.timeAgo(offset(104 * TimeUtils.ONEDAY)));
        assertEquals("1 year ago", TimeUtils.timeAgo(offset(365 * TimeUtils.ONEDAY)));
        assertEquals("13 months ago", TimeUtils.timeAgo(offset(395 * TimeUtils.ONEDAY)));
        assertEquals("2 years ago", TimeUtils.timeAgo(offset((2 * 365 + 30) * TimeUtils.ONEDAY)));
        // css class tests
        assertTrue(TimeUtils.timeAgoCss(offset(1 * TimeUtils.MIN)).equals("age0"));
        assertTrue(TimeUtils.timeAgoCss(offset(60 * TimeUtils.MIN)).equals("age0"));
        assertTrue(TimeUtils.timeAgoCss(offset(120 * TimeUtils.MIN)).equals("age1"));
        assertTrue(TimeUtils.timeAgoCss(offset(24 * TimeUtils.ONEHOUR)).equals("age1"));
        assertTrue(TimeUtils.timeAgoCss(offset(2 * TimeUtils.ONEDAY)).equals("age2"));
        assertEquals("age0", TimeUtils.timeAgoCss(offset(1 * TimeUtils.MIN)));
        assertEquals("age0", TimeUtils.timeAgoCss(offset(60 * TimeUtils.MIN)));
        assertEquals("age1", TimeUtils.timeAgoCss(offset(120 * TimeUtils.MIN)));
        assertEquals("age1", TimeUtils.timeAgoCss(offset(24 * TimeUtils.ONEHOUR)));
        assertEquals("age2", TimeUtils.timeAgoCss(offset(2 * TimeUtils.ONEDAY)));
    }
    public void testFrequency() {
        assertEquals(5, TimeUtils.convertFrequencyToMinutes("2 mins"));
        assertEquals(10, TimeUtils.convertFrequencyToMinutes("10 mins"));
        assertEquals(600, TimeUtils.convertFrequencyToMinutes("10 hours"));
        assertEquals(14400, TimeUtils.convertFrequencyToMinutes(" 10 days "));
    }
}