James Moger
2011-05-29 ec97f716023c0bbd6a9e11cbe7144973cf1c103d
Unit testing.
2 files added
7 files modified
306 ■■■■ changed files
src/com/gitblit/utils/ByteFormat.java 45 ●●●● patch | view | raw | blame | history
src/com/gitblit/utils/DiffUtils.java 15 ●●●●● patch | view | raw | blame | history
src/com/gitblit/utils/StringUtils.java 2 ●●● patch | view | raw | blame | history
src/com/gitblit/utils/TimeUtils.java 4 ●●●● patch | view | raw | blame | history
tests/com/gitblit/tests/ByteFormatTest.java 33 ●●●●● patch | view | raw | blame | history
tests/com/gitblit/tests/GitBlitSuite.java 17 ●●●●● patch | view | raw | blame | history
tests/com/gitblit/tests/GitBlitTest.java 15 ●●●●● patch | view | raw | blame | history
tests/com/gitblit/tests/StringUtilsTest.java 78 ●●●●● patch | view | raw | blame | history
tests/com/gitblit/tests/TimeUtilsTest.java 97 ●●●●● patch | view | raw | blame | history
src/com/gitblit/utils/ByteFormat.java
@@ -20,46 +20,20 @@
import java.text.Format;
import java.text.ParsePosition;
/**
 * A formatter for formatting byte sizes. For example, formatting 12345 byes
 * results in "12.1 K" and 1234567 results in "1.18 MB".
 *
 */
public class ByteFormat extends Format {
    private static final long serialVersionUID = 1L;
    public ByteFormat() {
    }
    /**
     * Formats a long which represent a number of bytes.
     */
    public String format(long bytes) {
        return format(Long.valueOf(bytes));
    public String format(long value) {
        return format(new Long(value));
    }
    /**
     * Formats a long which represent a number of kilobytes.
     */
    public String formatKB(long kilobytes) {
        return format(Long.valueOf(kilobytes * 1024));
    }
    /**
     * Format the given object (must be a Long).
     *
     * @param obj
     *            assumed to be the number of bytes as a Long.
     * @param buf
     *            the StringBuffer to append to.
     * @param pos
     * @return A formatted string representing the given bytes in more
     *         human-readable form.
     */
    public StringBuffer format(Object obj, StringBuffer buf, FieldPosition pos) {
        if (obj instanceof Long) {
            long numBytes = ((Long) obj).longValue();
        if (obj instanceof Number) {
            long numBytes = ((Number) obj).longValue();
            if (numBytes < 1024) {
                DecimalFormat formatter = new DecimalFormat("#,##0");
                buf.append(formatter.format((double) numBytes)).append(" b");
@@ -77,14 +51,7 @@
        }
        return buf;
    }
    /**
     * In this implementation, returns null always.
     *
     * @param source
     * @param pos
     * @return returns null in this implementation.
     */
    public Object parseObject(String source, ParsePosition pos) {
        return null;
    }
src/com/gitblit/utils/DiffUtils.java
@@ -1,3 +1,18 @@
/*
 * 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.io.ByteArrayOutputStream;
src/com/gitblit/utils/StringUtils.java
@@ -130,7 +130,7 @@
    public static String getRootPath(String path) {
        if (path.indexOf('/') > -1) {
            return path.substring(0, path.indexOf('/'));
            return path.substring(0, path.lastIndexOf('/'));
        }
        return "";
    }
src/com/gitblit/utils/TimeUtils.java
@@ -137,7 +137,7 @@
            int days = daysAgo(date, true);
            if (days < 365) {
                if (days <= 30) {
                    ago = days + " day" + (days > 1 ? "s" : "") + " ago";
                    ago = days + " days ago";
                } else if (days <= 90) {
                    int weeks = days / 7;
                    if (weeks == 12) {
@@ -151,7 +151,7 @@
                    if (weeks >= 2) {
                        months++;
                    }
                    ago = months + " month" + (months > 1 ? "s" : "") + " ago";
                    ago = months + " months ago";
                }
            } else if (days == 365) {
                ago = "1 year ago";
tests/com/gitblit/tests/ByteFormatTest.java
New file
@@ -0,0 +1,33 @@
/*
 * 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.ByteFormat;
public class ByteFormatTest extends TestCase {
    public void testByteFormat() throws Exception {
        ByteFormat format = new ByteFormat();
        assertTrue(format.format(10).equals("10 b"));
        assertTrue(format.format(1024*10).equals("10.0 KB"));
        assertTrue(format.format(1024*1000).equals("1,000.0 KB"));
        assertTrue(format.format(2*1024*1000).equals("2.0 MB"));
        assertTrue(format.format(1024*1024*1000).equals("1,000.0 MB"));
        assertTrue(format.format(2*1024*1024*1000).equals("2.0 GB"));
    }
}
tests/com/gitblit/tests/GitBlitSuite.java
@@ -1,3 +1,18 @@
/*
 * 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.io.File;
@@ -25,6 +40,8 @@
    public static Test suite() {
        TestSuite suite = new TestSuite();
        suite.addTestSuite(TimeUtilsTest.class);
        suite.addTestSuite(StringUtilsTest.class);
        suite.addTestSuite(ByteFormatTest.class);
        suite.addTestSuite(JGitUtilsTest.class);
        suite.addTestSuite(GitBlitTest.class);
        return new GitBlitSuite(suite);
tests/com/gitblit/tests/GitBlitTest.java
@@ -1,3 +1,18 @@
/*
 * 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;
tests/com/gitblit/tests/StringUtilsTest.java
New file
@@ -0,0 +1,78 @@
/*
 * 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.Arrays;
import junit.framework.TestCase;
import com.gitblit.utils.StringUtils;
public class StringUtilsTest extends TestCase {
    public void testIsEmpty() throws Exception {
        assertTrue(StringUtils.isEmpty(null));
        assertTrue(StringUtils.isEmpty(""));
        assertTrue(StringUtils.isEmpty("  "));
        assertFalse(StringUtils.isEmpty("A"));
    }
    public void testBreakLinesForHtml() throws Exception {
        String input = "this\nis\r\na\rtest\r\n\r\nof\n\nline\r\rbreaking";
        String output = "this<br/>is<br/>a<br/>test<br/><br/>of<br/><br/>line<br/><br/>breaking";
        assertTrue(StringUtils.breakLinesForHtml(input).equals(output));
    }
    public void testEscapeForHtml() throws Exception {
        String input = "& < > \" \t";
        String output_nochange = "&amp; &lt; &gt; &quot; \t";
        String output_change = "&amp;&nbsp;&lt;&nbsp;&gt;&nbsp;&quot;&nbsp; &nbsp; &nbsp;";
        assertTrue(StringUtils.escapeForHtml(input, false).equals(output_nochange));
        assertTrue(StringUtils.escapeForHtml(input, true).equals(output_change));
    }
    public void testFlattenStrings() throws Exception {
        String[] strings = { "A", "B", "C", "D" };
        assertTrue(StringUtils.flattenStrings(Arrays.asList(strings)).equals("A B C D"));
    }
    public void testTrim() throws Exception {
        String input = "123456789 123456789 123456789 123456789 123456789 123456789 123456789 ";
        String output = "123456789 123456789 123456789 123456789 123456789 1234567...";
        assertTrue(StringUtils.trimShortLog(input).equals(output));
        assertTrue(StringUtils.trimString(input, input.length()).equals(input));
    }
    public void testPadding() throws Exception {
        String input = "test";
        assertTrue(StringUtils.leftPad(input, 6 + input.length(), ' ').equals("      test"));
        assertTrue(StringUtils.rightPad(input, 6 + input.length(), ' ').equals("test      "));
        assertTrue(StringUtils.leftPad(input, input.length(), ' ').equals(input));
        assertTrue(StringUtils.rightPad(input, input.length(), ' ').equals(input));
    }
    public void testSHA1() throws Exception {
        assertTrue(StringUtils.getSHA1("blob 16\000what is up, doc?").equals("bd9dbf5aae1a3862dd1526723246b20206e5fc37"));
    }
    public void testRootPath() throws Exception {
        String input = "/nested/path/to/repository";
        String output = "/nested/path/to";
        assertTrue(StringUtils.getRootPath(input).equals(output));
        assertTrue(StringUtils.getRootPath("repository").equals(""));
    }
}
tests/com/gitblit/tests/TimeUtilsTest.java
@@ -1,3 +1,18 @@
/*
 * 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;
@@ -8,16 +23,33 @@
public class TimeUtilsTest extends TestCase {
    private Date offset(long subtract) {
        return new Date(System.currentTimeMillis() - subtract);
    }
    public void testBasicTimeFunctions() throws Exception {
        assertTrue(TimeUtils.minutesAgo(offset(2 * TimeUtils.MIN), false) == 2);
        assertTrue(TimeUtils.minutesAgo(offset((2 * TimeUtils.MIN) + (35 * 1000L)), true) == 3);
        assertTrue(TimeUtils.hoursAgo(offset(2 * TimeUtils.ONEHOUR), false) == 2);
        assertTrue(TimeUtils.hoursAgo(offset(5 * TimeUtils.HALFHOUR), true) == 3);
        assertTrue(TimeUtils.daysAgo(offset(4 * TimeUtils.ONEDAY), false) == 4);
        assertTrue(TimeUtils.daysAgo(offset(4 * TimeUtils.ONEDAY + 12 * TimeUtils.ONEHOUR), true) == 5);
    }
    public void testToday() throws Exception {
        assertTrue("Is today failed!", TimeUtils.isToday(new Date()));
        assertTrue(TimeUtils.isToday(new Date()));
    }
    public void testYesterday() throws Exception {
        assertTrue("Is yesterday failed!", TimeUtils.isYesterday(new Date(System.currentTimeMillis() - TimeUtils.ONEDAY)));
        assertTrue(TimeUtils.isYesterday(offset(TimeUtils.ONEDAY)));
    }
    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"));
@@ -26,35 +58,38 @@
        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"));
        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"));
        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"));
    }
    public void testTimeAgo() throws Exception {
        long time = System.currentTimeMillis();
        assertTrue(TimeUtils.timeAgo(new Date(time - 1*TimeUtils.MIN)).equals("1 min ago"));
        assertTrue(TimeUtils.timeAgo(new Date(time - 60*TimeUtils.MIN)).equals("60 mins ago"));
        assertTrue(TimeUtils.timeAgo(new Date(time - 120*TimeUtils.MIN)).equals("2 hours ago"));
        assertTrue(TimeUtils.timeAgo(new Date(time - 15*TimeUtils.ONEHOUR)).equals("15 hours ago"));
        assertTrue(TimeUtils.timeAgo(new Date(time - 24*TimeUtils.ONEHOUR)).equals("yesterday"));
        assertTrue(TimeUtils.timeAgo(new Date(time - 2*TimeUtils.ONEDAY)).equals("2 days ago"));
        assertTrue(TimeUtils.timeAgo(new Date(time - 35*TimeUtils.ONEDAY)).equals("5 weeks ago"));
        assertTrue(TimeUtils.timeAgo(new Date(time - 84*TimeUtils.ONEDAY)).equals("3 months ago"));
        assertTrue(TimeUtils.timeAgo(new Date(time - 95*TimeUtils.ONEDAY)).equals("3 months ago"));
        assertTrue(TimeUtils.timeAgo(new Date(time - 104*TimeUtils.ONEDAY)).equals("4 months ago"));
        assertTrue(TimeUtils.timeAgo(new Date(time - 365*TimeUtils.ONEDAY)).equals("1 year ago"));
        assertTrue(TimeUtils.timeAgo(new Date(time - 395*TimeUtils.ONEDAY)).equals("13 months ago"));
        assertTrue(TimeUtils.timeAgo(new Date(time - (2*365 + 30)*TimeUtils.ONEDAY)).equals("2 years ago"));
        assertTrue(TimeUtils.timeAgoCss(new Date(time - 1*TimeUtils.MIN)).equals("age0"));
        assertTrue(TimeUtils.timeAgoCss(new Date(time - 60*TimeUtils.MIN)).equals("age0"));
        assertTrue(TimeUtils.timeAgoCss(new Date(time - 120*TimeUtils.MIN)).equals("age1"));
        assertTrue(TimeUtils.timeAgoCss(new Date(time - 24*TimeUtils.ONEHOUR)).equals("age1"));
        assertTrue(TimeUtils.timeAgoCss(new Date(time - 2*TimeUtils.ONEDAY)).equals("age2"));    }
        // 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"));
        // 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"));
    }
}