]> source.dussan.org Git - gitblit.git/commitdiff
Unit testing.
authorJames Moger <james.moger@gitblit.com>
Sun, 29 May 2011 15:19:30 +0000 (11:19 -0400)
committerJames Moger <james.moger@gitblit.com>
Sun, 29 May 2011 15:19:30 +0000 (11:19 -0400)
src/com/gitblit/utils/ByteFormat.java
src/com/gitblit/utils/DiffUtils.java
src/com/gitblit/utils/StringUtils.java
src/com/gitblit/utils/TimeUtils.java
tests/com/gitblit/tests/ByteFormatTest.java [new file with mode: 0644]
tests/com/gitblit/tests/GitBlitSuite.java
tests/com/gitblit/tests/GitBlitTest.java
tests/com/gitblit/tests/StringUtilsTest.java [new file with mode: 0644]
tests/com/gitblit/tests/TimeUtilsTest.java

index 92a8e46dfaa3e645aee007dfee56b65d1df7cb13..fee645c78184ebe83b0b2f8538de01d945d8f3a7 100644 (file)
@@ -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;
        }
index d7a4a632b75e0d5c375887028baf2656fc0b87c0..c9d0fc369b8cc471323d7d59ec9b8413e7226042 100644 (file)
@@ -1,3 +1,18 @@
+/*\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.utils;\r
 \r
 import java.io.ByteArrayOutputStream;\r
index 4a9c88d5a3e3e89930b297b33832461e96ca27a5..fd6ca98b425bd3c1b4a54d8e51ca8a366ed7a40e 100644 (file)
@@ -130,7 +130,7 @@ public class StringUtils {
 \r
        public static String getRootPath(String path) {\r
                if (path.indexOf('/') > -1) {\r
-                       return path.substring(0, path.indexOf('/'));\r
+                       return path.substring(0, path.lastIndexOf('/'));\r
                }\r
                return "";\r
        }\r
index ac8e2098c5ccb2dbd0aad57247aa93340c86cd67..805b44f5e98a19615650bb92035f99e820f3da07 100644 (file)
@@ -137,7 +137,7 @@ public class TimeUtils {
                        int days = daysAgo(date, true);\r
                        if (days < 365) {\r
                                if (days <= 30) {\r
-                                       ago = days + " day" + (days > 1 ? "s" : "") + " ago";\r
+                                       ago = days + " days ago";\r
                                } else if (days <= 90) {\r
                                        int weeks = days / 7;\r
                                        if (weeks == 12) {\r
@@ -151,7 +151,7 @@ public class TimeUtils {
                                        if (weeks >= 2) {\r
                                                months++;\r
                                        }\r
-                                       ago = months + " month" + (months > 1 ? "s" : "") + " ago";\r
+                                       ago = months + " months ago";\r
                                }\r
                        } else if (days == 365) {\r
                                ago = "1 year ago";\r
diff --git a/tests/com/gitblit/tests/ByteFormatTest.java b/tests/com/gitblit/tests/ByteFormatTest.java
new file mode 100644 (file)
index 0000000..e969c4d
--- /dev/null
@@ -0,0 +1,33 @@
+/*\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.ByteFormat;\r
+\r
+public class ByteFormatTest extends TestCase {\r
+\r
+       public void testByteFormat() throws Exception {\r
+               ByteFormat format = new ByteFormat();\r
+               assertTrue(format.format(10).equals("10 b"));\r
+               assertTrue(format.format(1024*10).equals("10.0 KB"));\r
+               assertTrue(format.format(1024*1000).equals("1,000.0 KB"));\r
+               assertTrue(format.format(2*1024*1000).equals("2.0 MB"));\r
+               assertTrue(format.format(1024*1024*1000).equals("1,000.0 MB"));\r
+               assertTrue(format.format(2*1024*1024*1000).equals("2.0 GB"));\r
+       }\r
+}\r
index 63c26464de35f6c75b0405c063ba5e4cbc39045b..d6064b104f21c6c62891bb883669a177b5137741 100644 (file)
@@ -1,3 +1,18 @@
+/*\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.io.File;\r
@@ -25,6 +40,8 @@ public class GitBlitSuite extends TestSetup {
        public static Test suite() {\r
                TestSuite suite = new TestSuite();\r
                suite.addTestSuite(TimeUtilsTest.class);\r
+               suite.addTestSuite(StringUtilsTest.class);\r
+               suite.addTestSuite(ByteFormatTest.class);\r
                suite.addTestSuite(JGitUtilsTest.class);\r
                suite.addTestSuite(GitBlitTest.class);\r
                return new GitBlitSuite(suite);\r
index 50d360642962ba912876314e14c1cb461832412d..69880d92bd2291e642935cda098179ac58b58067 100644 (file)
@@ -1,3 +1,18 @@
+/*\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.List;\r
diff --git a/tests/com/gitblit/tests/StringUtilsTest.java b/tests/com/gitblit/tests/StringUtilsTest.java
new file mode 100644 (file)
index 0000000..24033b2
--- /dev/null
@@ -0,0 +1,78 @@
+/*\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.Arrays;\r
+\r
+import junit.framework.TestCase;\r
+\r
+import com.gitblit.utils.StringUtils;\r
+\r
+public class StringUtilsTest extends TestCase {\r
+\r
+       public void testIsEmpty() throws Exception {\r
+               assertTrue(StringUtils.isEmpty(null));\r
+               assertTrue(StringUtils.isEmpty(""));\r
+               assertTrue(StringUtils.isEmpty("  "));\r
+               assertFalse(StringUtils.isEmpty("A"));\r
+       }\r
+\r
+       public void testBreakLinesForHtml() throws Exception {\r
+               String input = "this\nis\r\na\rtest\r\n\r\nof\n\nline\r\rbreaking";\r
+               String output = "this<br/>is<br/>a<br/>test<br/><br/>of<br/><br/>line<br/><br/>breaking";\r
+               assertTrue(StringUtils.breakLinesForHtml(input).equals(output));\r
+       }\r
+       \r
+       public void testEscapeForHtml() throws Exception {\r
+               String input = "& < > \" \t";\r
+               String output_nochange = "&amp; &lt; &gt; &quot; \t";\r
+               String output_change = "&amp;&nbsp;&lt;&nbsp;&gt;&nbsp;&quot;&nbsp; &nbsp; &nbsp;";\r
+               assertTrue(StringUtils.escapeForHtml(input, false).equals(output_nochange));\r
+               assertTrue(StringUtils.escapeForHtml(input, true).equals(output_change));\r
+       }\r
+\r
+       public void testFlattenStrings() throws Exception {\r
+               String[] strings = { "A", "B", "C", "D" };\r
+               assertTrue(StringUtils.flattenStrings(Arrays.asList(strings)).equals("A B C D"));\r
+       }\r
+\r
+       public void testTrim() throws Exception {\r
+               String input = "123456789 123456789 123456789 123456789 123456789 123456789 123456789 ";\r
+               String output = "123456789 123456789 123456789 123456789 123456789 1234567...";\r
+               assertTrue(StringUtils.trimShortLog(input).equals(output));\r
+               assertTrue(StringUtils.trimString(input, input.length()).equals(input));\r
+       }\r
+\r
+       public void testPadding() throws Exception {\r
+               String input = "test";\r
+               assertTrue(StringUtils.leftPad(input, 6 + input.length(), ' ').equals("      test"));\r
+               assertTrue(StringUtils.rightPad(input, 6 + input.length(), ' ').equals("test      "));\r
+\r
+               assertTrue(StringUtils.leftPad(input, input.length(), ' ').equals(input));\r
+               assertTrue(StringUtils.rightPad(input, input.length(), ' ').equals(input));\r
+       }\r
+       \r
+       public void testSHA1() throws Exception {\r
+               assertTrue(StringUtils.getSHA1("blob 16\000what is up, doc?").equals("bd9dbf5aae1a3862dd1526723246b20206e5fc37"));\r
+       }\r
+       \r
+       public void testRootPath() throws Exception {\r
+               String input = "/nested/path/to/repository";\r
+               String output = "/nested/path/to";\r
+               assertTrue(StringUtils.getRootPath(input).equals(output));\r
+               assertTrue(StringUtils.getRootPath("repository").equals(""));\r
+       }\r
+}\r
index 8227fcf6514c7bfc658233e032fb8e8def6fe3ec..b2c819ff4f184338616a06f3bb381784dc01ad5b 100644 (file)
@@ -1,3 +1,18 @@
+/*\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
@@ -8,16 +23,33 @@ import com.gitblit.utils.TimeUtils;
 \r
 public class TimeUtilsTest extends TestCase {\r
 \r
+       private Date offset(long subtract) {\r
+               return new Date(System.currentTimeMillis() - subtract);\r
+       }\r
+\r
+       public void testBasicTimeFunctions() throws Exception {\r
+               assertTrue(TimeUtils.minutesAgo(offset(2 * TimeUtils.MIN), false) == 2);\r
+               assertTrue(TimeUtils.minutesAgo(offset((2 * TimeUtils.MIN) + (35 * 1000L)), true) == 3);\r
+\r
+               assertTrue(TimeUtils.hoursAgo(offset(2 * TimeUtils.ONEHOUR), false) == 2);\r
+               assertTrue(TimeUtils.hoursAgo(offset(5 * TimeUtils.HALFHOUR), true) == 3);\r
+\r
+               assertTrue(TimeUtils.daysAgo(offset(4 * TimeUtils.ONEDAY), false) == 4);\r
+               assertTrue(TimeUtils.daysAgo(offset(4 * TimeUtils.ONEDAY + 12 * TimeUtils.ONEHOUR), true) == 5);\r
+       }\r
+\r
        public void testToday() throws Exception {\r
-               assertTrue("Is today failed!", TimeUtils.isToday(new Date()));\r
+               assertTrue(TimeUtils.isToday(new Date()));\r
        }\r
 \r
        public void testYesterday() throws Exception {\r
-               assertTrue("Is yesterday failed!", TimeUtils.isYesterday(new Date(System.currentTimeMillis() - TimeUtils.ONEDAY)));\r
+               assertTrue(TimeUtils.isYesterday(offset(TimeUtils.ONEDAY)));\r
        }\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
@@ -26,35 +58,38 @@ public class TimeUtilsTest extends TestCase {
                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
-               \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
+\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
        }\r
-       \r
+\r
        public void testTimeAgo() throws Exception {\r
-               long time = System.currentTimeMillis();\r
-               assertTrue(TimeUtils.timeAgo(new Date(time - 1*TimeUtils.MIN)).equals("1 min ago"));\r
-               assertTrue(TimeUtils.timeAgo(new Date(time - 60*TimeUtils.MIN)).equals("60 mins ago"));\r
-               assertTrue(TimeUtils.timeAgo(new Date(time - 120*TimeUtils.MIN)).equals("2 hours ago"));\r
-               assertTrue(TimeUtils.timeAgo(new Date(time - 15*TimeUtils.ONEHOUR)).equals("15 hours ago"));\r
-               assertTrue(TimeUtils.timeAgo(new Date(time - 24*TimeUtils.ONEHOUR)).equals("yesterday"));\r
-               assertTrue(TimeUtils.timeAgo(new Date(time - 2*TimeUtils.ONEDAY)).equals("2 days ago"));\r
-               assertTrue(TimeUtils.timeAgo(new Date(time - 35*TimeUtils.ONEDAY)).equals("5 weeks ago"));\r
-               assertTrue(TimeUtils.timeAgo(new Date(time - 84*TimeUtils.ONEDAY)).equals("3 months ago"));\r
-               assertTrue(TimeUtils.timeAgo(new Date(time - 95*TimeUtils.ONEDAY)).equals("3 months ago"));\r
-               assertTrue(TimeUtils.timeAgo(new Date(time - 104*TimeUtils.ONEDAY)).equals("4 months ago"));\r
-               assertTrue(TimeUtils.timeAgo(new Date(time - 365*TimeUtils.ONEDAY)).equals("1 year ago"));\r
-               assertTrue(TimeUtils.timeAgo(new Date(time - 395*TimeUtils.ONEDAY)).equals("13 months ago"));\r
-               assertTrue(TimeUtils.timeAgo(new Date(time - (2*365 + 30)*TimeUtils.ONEDAY)).equals("2 years ago"));\r
-               \r
-               assertTrue(TimeUtils.timeAgoCss(new Date(time - 1*TimeUtils.MIN)).equals("age0"));\r
-               assertTrue(TimeUtils.timeAgoCss(new Date(time - 60*TimeUtils.MIN)).equals("age0"));\r
-               assertTrue(TimeUtils.timeAgoCss(new Date(time - 120*TimeUtils.MIN)).equals("age1"));\r
-               assertTrue(TimeUtils.timeAgoCss(new Date(time - 24*TimeUtils.ONEHOUR)).equals("age1"));\r
-               assertTrue(TimeUtils.timeAgoCss(new Date(time - 2*TimeUtils.ONEDAY)).equals("age2"));   }\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
+\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
+       }\r
 }\r