diff options
author | James Moger <james.moger@gitblit.com> | 2011-05-29 11:19:30 -0400 |
---|---|---|
committer | James Moger <james.moger@gitblit.com> | 2011-05-29 11:19:30 -0400 |
commit | ec97f716023c0bbd6a9e11cbe7144973cf1c103d (patch) | |
tree | 134278a3d32bb6418ed2f413f8ad500580f384e8 | |
parent | 28d6b2a860740557bf93dd0f9a48d059379ed696 (diff) | |
download | gitblit-ec97f716023c0bbd6a9e11cbe7144973cf1c103d.tar.gz gitblit-ec97f716023c0bbd6a9e11cbe7144973cf1c103d.zip |
Unit testing.
-rw-r--r-- | src/com/gitblit/utils/ByteFormat.java | 45 | ||||
-rw-r--r-- | src/com/gitblit/utils/DiffUtils.java | 15 | ||||
-rw-r--r-- | src/com/gitblit/utils/StringUtils.java | 2 | ||||
-rw-r--r-- | src/com/gitblit/utils/TimeUtils.java | 4 | ||||
-rw-r--r-- | tests/com/gitblit/tests/ByteFormatTest.java | 33 | ||||
-rw-r--r-- | tests/com/gitblit/tests/GitBlitSuite.java | 17 | ||||
-rw-r--r-- | tests/com/gitblit/tests/GitBlitTest.java | 15 | ||||
-rw-r--r-- | tests/com/gitblit/tests/StringUtilsTest.java | 78 | ||||
-rw-r--r-- | tests/com/gitblit/tests/TimeUtilsTest.java | 97 |
9 files changed, 233 insertions, 73 deletions
diff --git a/src/com/gitblit/utils/ByteFormat.java b/src/com/gitblit/utils/ByteFormat.java index 92a8e46d..fee645c7 100644 --- a/src/com/gitblit/utils/ByteFormat.java +++ b/src/com/gitblit/utils/ByteFormat.java @@ -20,46 +20,20 @@ import java.text.FieldPosition; 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 @@ public class ByteFormat extends Format { } 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; } diff --git a/src/com/gitblit/utils/DiffUtils.java b/src/com/gitblit/utils/DiffUtils.java index d7a4a632..c9d0fc36 100644 --- a/src/com/gitblit/utils/DiffUtils.java +++ b/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;
diff --git a/src/com/gitblit/utils/StringUtils.java b/src/com/gitblit/utils/StringUtils.java index 4a9c88d5..fd6ca98b 100644 --- a/src/com/gitblit/utils/StringUtils.java +++ b/src/com/gitblit/utils/StringUtils.java @@ -130,7 +130,7 @@ public class StringUtils { public static String getRootPath(String path) {
if (path.indexOf('/') > -1) {
- return path.substring(0, path.indexOf('/'));
+ return path.substring(0, path.lastIndexOf('/'));
}
return "";
}
diff --git a/src/com/gitblit/utils/TimeUtils.java b/src/com/gitblit/utils/TimeUtils.java index ac8e2098..805b44f5 100644 --- a/src/com/gitblit/utils/TimeUtils.java +++ b/src/com/gitblit/utils/TimeUtils.java @@ -137,7 +137,7 @@ public class TimeUtils { 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 @@ public class TimeUtils { if (weeks >= 2) {
months++;
}
- ago = months + " month" + (months > 1 ? "s" : "") + " ago";
+ ago = months + " months ago";
}
} else if (days == 365) {
ago = "1 year ago";
diff --git a/tests/com/gitblit/tests/ByteFormatTest.java b/tests/com/gitblit/tests/ByteFormatTest.java new file mode 100644 index 00000000..e969c4dc --- /dev/null +++ b/tests/com/gitblit/tests/ByteFormatTest.java @@ -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"));
+ }
+}
diff --git a/tests/com/gitblit/tests/GitBlitSuite.java b/tests/com/gitblit/tests/GitBlitSuite.java index 63c26464..d6064b10 100644 --- a/tests/com/gitblit/tests/GitBlitSuite.java +++ b/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 class GitBlitSuite extends TestSetup { 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);
diff --git a/tests/com/gitblit/tests/GitBlitTest.java b/tests/com/gitblit/tests/GitBlitTest.java index 50d36064..69880d92 100644 --- a/tests/com/gitblit/tests/GitBlitTest.java +++ b/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;
diff --git a/tests/com/gitblit/tests/StringUtilsTest.java b/tests/com/gitblit/tests/StringUtilsTest.java new file mode 100644 index 00000000..24033b22 --- /dev/null +++ b/tests/com/gitblit/tests/StringUtilsTest.java @@ -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 = "& < > " \t";
+ String output_change = "& < > " ";
+ 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(""));
+ }
+}
diff --git a/tests/com/gitblit/tests/TimeUtilsTest.java b/tests/com/gitblit/tests/TimeUtilsTest.java index 8227fcf6..b2c819ff 100644 --- a/tests/com/gitblit/tests/TimeUtilsTest.java +++ b/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 @@ import com.gitblit.utils.TimeUtils; 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 @@ public class TimeUtilsTest extends TestCase { 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"));
+ }
}
|